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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451 | package ebit.war.jbox2d;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.jbox2d.common.Color3f;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.DebugDraw;
import org.jbox2d.testbed.TestSettings;
import ebit.game.core.Util;
import ebit.war.game.combat.Combat;
/**
*
* @author hakan eryargi (r a f t)
*/
public class ReplayDebugDraw extends DebugDraw implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public static final String FILE_DESCRIPTION = "Debug draw replay file";
public static final String FILE_EXTENSION = "draw";
/** stores only one of this much frames */
private static final int SAVE_RATIO = 6;
private final DebugDraw delegate;
private DrawPack drawPack = new DrawPack();
private Queue<DrawTask> currentFrame = new LinkedList<DrawTask>();
private int frameNo = 0;
private boolean saveThisFrame = true;
Combat combat;
public ReplayDebugDraw() {
this(null,null);
}
public ReplayDebugDraw(DebugDraw delegate, Combat combat) {
this.delegate = delegate;
this.combat=combat;
applySettings(new TestSettings());
}
@Override
public void drawCircle(Vec2 center, float radius, Color3f color) {
if (delegate != null)
delegate.drawCircle(center, radius, color);
if (saveThisFrame)
currentFrame.add(new DrawCircle(center, radius, color));
}
@Override
public void drawPoint(Vec2 position, float f, Color3f color3f) {
if (delegate != null)
delegate.drawPoint(position, f, color3f);
if (saveThisFrame)
currentFrame.add(new DrawPoint(position, f, color3f));
}
@Override
public void drawPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
if (delegate != null)
delegate.drawPolygon(vertices, vertexCount, color);
if (saveThisFrame)
currentFrame.add(new DrawPolygon(vertices, vertexCount, color));
}
@Override
public void drawSegment(Vec2 p1, Vec2 p2, Color3f color) {
if (delegate != null)
delegate.drawSegment(p1, p2, color);
if (saveThisFrame)
currentFrame.add(new DrawSegment(p1, p2, color));
}
@Override
public void drawSolidCircle(Vec2 center, float radius, Vec2 axis, Color3f color) {
if (delegate != null)
delegate.drawSolidCircle(center, radius, axis, color);
if (saveThisFrame)
currentFrame.add(new DrawSolidCircle(center, radius, axis, color));
}
@Override
public void drawSolidPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
if (delegate != null)
delegate.drawSolidPolygon(vertices, vertexCount, color);
if (saveThisFrame)
currentFrame.add(new DrawSolidPolygon(vertices, vertexCount, color));
}
@Override
public void drawString(float x, float y, String s, Color3f color) {
if (delegate != null)
delegate.drawString(x, y, s, color);
//currentFrame.add(new DrawString(x, y, s, color));
}
@Override
public void drawStringToWorld(float x, float y, String s, Color3f color) {
if (delegate != null)
delegate.drawStringToWorld(x, y, s, color);
if (saveThisFrame)
currentFrame.add(new DrawStringToWorld(x, y, s, color));
}
@Override
public void drawXForm(XForm xf) {
if (delegate != null)
delegate.drawXForm(xf);
if (saveThisFrame)
currentFrame.add(new DrawXForm(xf));
}
@Override
public void setCamera(float x, float y, float scale) {
if (delegate != null)
delegate.setCamera(x, y, scale);
super.setCamera(x, y, scale);
//currentFrame.add(new SetCamera(x, y, scale));
}
@Override
public void setFlags(int flags) {
if (delegate != null)
delegate.setFlags(flags);
super.setFlags(flags);
}
@Override
public void appendFlags(int flags) {
if (delegate != null)
delegate.appendFlags(flags);
super.appendFlags(flags);
}
@Override
public Vec2 screenToWorld(Vec2 screenV) {
return (delegate != null)
? delegate.screenToWorld(screenV)
: super.screenToWorld(screenV);
}
@Override
public Vec2 screenToWorld(float screenx, float screeny) {
return (delegate != null)
? delegate.screenToWorld(screenx, screeny)
: super.screenToWorld(screenx, screeny);
}
@Override
public Vec2 worldToScreen(Vec2 worldV) {
return (delegate != null)
? delegate.worldToScreen(worldV)
: super.worldToScreen(worldV);
}
@Override
public Vec2 worldToScreen(float worldx, float worldy) {
return (delegate != null)
? delegate.worldToScreen(worldx, worldy)
: super.worldToScreen(worldx, worldy);
}
public void applySettings(TestSettings settings) {
if (settings.drawShapes) appendFlags(DebugDraw.e_shapeBit);
if (settings.drawJoints) appendFlags(DebugDraw.e_jointBit);
if (settings.drawCoreShapes) appendFlags(DebugDraw.e_coreShapeBit);
if (settings.drawAABBs) appendFlags(DebugDraw.e_aabbBit);
if (settings.drawOBBs) appendFlags(DebugDraw.e_obbBit);
if (settings.drawPairs) appendFlags(DebugDraw.e_pairBit);
if (settings.drawCOMs) appendFlags(DebugDraw.e_centerOfMassBit);
}
public void endFrame() {
drawPack.addFrame(currentFrame);
frameNo++;
saveThisFrame = ((frameNo % SAVE_RATIO) == 0);
if (saveThisFrame) {
currentFrame = new LinkedList<DrawTask>();
} // else use same
}
public DrawPack pack() {
if (!currentFrame.isEmpty())
endFrame();
return drawPack;
}
public void reset() {
this.currentFrame.clear();
this.drawPack = new DrawPack();
}
private static Vec2[] copyVertices(Vec2[] vertices, int count) {
if (vertices == null)
return null;
Vec2[] copy = new Vec2[vertices.length];
for (int i = 0; i < count; i++) {
Vec2 v = vertices[i];
if (v != null) {
copy[i] = v.clone();
}
}
return copy;
}
public static class DrawPack implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private final List<Queue<DrawTask>> frames = new LinkedList<Queue<DrawTask>>();
private transient Iterator<Queue<DrawTask>> frameIter = null;
private transient int frameNo = 0;
//private final int saveRatio = SAVE_RATIO;
private void addFrame(Queue<DrawTask> frame) {
frames.add(frame);
}
public int getFrameCount() {
return frames.size();
}
public int getFrameNo() {
return frameNo;
}
public void rewind() {
frameNo = 0;
frameIter = frames.iterator();
}
public boolean hasNextFrame() {
return (frameIter != null) ? frameIter.hasNext() : !frames.isEmpty();
}
public void drawNextFrame(DebugDraw debugDraw) {
if (frameIter == null)
frameIter = frames.iterator();
Queue<DrawTask> frame = frameIter.next();
drawFrame(debugDraw, frame);
frameNo++;
}
public void drawFrame(DebugDraw debugDraw, int frameNo) {
Queue<DrawTask> frame = frames.get(frameNo);
drawFrame(debugDraw, frame);
}
private void drawFrame(DebugDraw debugDraw, Queue<DrawTask> frame) {
for (DrawTask task : frame) {
task.drawOn(debugDraw);
}
}
public void saveToFile(File file) throws IOException {
Util.Resource.saveObjectToStream(new FileOutputStream(file),
this, Util.Resource.CLOSE_STREAM);
// Create output stream.
FileOutputStream fos = new FileOutputStream("foo.xml");
// Create XML encoder.
XMLEncoder xenc = new XMLEncoder(fos);
xenc.writeObject(this);
}
public static DrawPack loadFromFile(File file) throws IOException {
return Util.Resource.loadObjectFromStream(
new FileInputStream(file), Util.Resource.CLOSE_STREAM);
}
}
private static interface DrawTask {
public void drawOn(DebugDraw debugDraw);
}
private static class DrawCircle implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2 center;
final float radius;
final Color3f color;
private DrawCircle(Vec2 center, float radius, Color3f color) {
this.center = center.clone();
this.radius = radius;
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawCircle(center, radius, color);
}
}
private static class DrawPoint implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2 position;
final float f;
final Color3f color3f;
private DrawPoint(Vec2 position, float f, Color3f color3f) {
this.position = position.clone();
this.f = f;
this.color3f = color3f;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawPoint(position, f, color3f);
}
}
private static class DrawPolygon implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2[] vertices;
final int vertexCount;
final Color3f color;
private DrawPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
this.vertices = copyVertices(vertices, vertexCount);
this.vertexCount = vertexCount;
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawPolygon(vertices, vertexCount, color);
}
}
private static class DrawSegment implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2 p1;
final Vec2 p2;
final Color3f color;
private DrawSegment(Vec2 p1, Vec2 p2, Color3f color) {
this.p1 = p1.clone();
this.p2 = p2.clone();
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawSegment(p1, p2, color);
}
}
private static class DrawSolidCircle implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2 center;
final float radius;
final Vec2 axis;
final Color3f color;
private DrawSolidCircle(Vec2 center, float radius, Vec2 axis, Color3f color) {
this.center = center.clone();
this.radius = radius;
this.axis = axis.clone();
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawSolidCircle(center, radius, axis, color);
}
}
private static class DrawSolidPolygon implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final Vec2[] vertices;
final int vertexCount;
final Color3f color;
private DrawSolidPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
this.vertices = copyVertices(vertices, vertexCount);
this.vertexCount = vertexCount;
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawSolidPolygon(vertices, vertexCount, color);
}
}
private static class DrawStringToWorld implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final float x;
final float y;
final String s;
final Color3f color;
private DrawStringToWorld(float x, float y, String s, Color3f color) {
this.x = x;
this.y = y;
this.s = s;
this.color = color;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawStringToWorld(x, y, s, color);
}
}
private static class DrawXForm implements DrawTask, java.io.Serializable {
private static final long serialVersionUID = 1L;
final XForm xf;
private DrawXForm(XForm xf) {
this.xf = xf;
}
@Override
public void drawOn(DebugDraw debugDraw) {
debugDraw.drawXForm(xf);
}
}
}
|