package com xamgore particles import android content Context import an

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.xamgore.particles;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameSurfaceView extends SurfaceView
implements SurfaceHolder.Callback {
// TODO: Move to Graphics
private static final int BACKGROUND_COLOR = 0xff191919; // #222
private Paint paint;
private DrawingThread thread;
private final SurfaceHolder mSurfaceHolder;
private final ParticleSystem particleSystem;
public GameSurfaceView(Context context) {
super(context);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
particleSystem = new ParticleSystem();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
thread = new DrawingThread();
thread.keepRunning = true;
thread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int newW, int newH) {
particleSystem.makeOutburst(newW, newH);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.keepRunning = false;
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
paint = null;
thread = null;
} catch (InterruptedException e) {}
}
}
@Override
protected void onDraw(Canvas canvas) {
// Fill background
paint.setColor(BACKGROUND_COLOR);
canvas.drawPaint(paint);
// Draw particles
particleSystem.draw(canvas);
// Draw FPS
paint.setColor(Color.WHITE);
canvas.drawText(Fps.getFpsAsString(), 10, 10, paint);
canvas.drawText(String.valueOf(particleSystem.count()), 50, 10, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// ACTION_DOWN || ACTION_POINTER_DOWN || ACTION_MOVE
if (event.getAction() >= 0 && event.getAction() < 3)
synchronized (mSurfaceHolder) {
boolean multiTouch = event.getPointerCount() > 1;
// Создаём вспышки под каждым пальцем
for (int i = 0; i < event.getPointerCount(); i++)
particleSystem.makeFlush(
event.getX(i), event.getY(i), multiTouch
);
return true;
}
/*
* XXX: Или тут лучше сразу возвращать return true; ???
*/
return super.onTouchEvent(event);
}
private class DrawingThread extends Thread {
public boolean keepRunning = true;
@Override
public void run() {
Canvas c;
while (keepRunning) {
Fps.startMeasuringDelay();
synchronized (mSurfaceHolder) {
particleSystem.update();
}
c = null;
try {
c = mSurfaceHolder.lockCanvas();
if (c != null) {
synchronized (mSurfaceHolder) {
onDraw(c);
}
}
} finally {
if (c != null)
mSurfaceHolder.unlockCanvasAndPost(c);
}
try {
Thread.sleep(Fps.getDelay());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
}