Author: davedes (posted 2012-05-21 19:59:23, viewed 146 times)
| 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
| package slim;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import slim.texture.Texture2D;
import slim.util.FastTrig;
public class SpriteBatchImage {
public static final int ALIGN_LEFT = 0;
public static final int ALIGN_CENTER = 1;
public static final int ALIGN_RIGHT = 2;
public static void setUseTriangles(boolean b) {
mode = b ? GL11.GL_TRIANGLES : GL11.GL_QUADS;
}
public static boolean isUseTriangles() {
return mode==GL11.GL_TRIANGLES;
}
private static int mode = GL11.GL_TRIANGLES;
private final int TOLERANCE = 48;
private int idx = 0;
private Texture2D texture;
public int renderCalls = 0;
private FloatBuffer vertices, colors, texcoords;
private int maxVerts;
private Color currentColor = new Color(Color.white);
private Color sharedColor = new Color(Color.white);
private float translateX, translateY;
public SpriteBatchImage() {
this(1000);
}
public SpriteBatchImage(int size) {
if (size<=0)
throw new IllegalArgumentException("batch size must be larger than 0");
this.maxVerts = size;
vertices = BufferUtils.createFloatBuffer(size * 2);
colors = BufferUtils.createFloatBuffer(size * 4);
texcoords = BufferUtils.createFloatBuffer(size * 2);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
}
public int getSize() {
return maxVerts;
}
public void setColor(Color color) {
this.currentColor = color;
}
public void setColor(float r, float g, float b, float a) {
sharedColor.r = r;
sharedColor.g = g;
sharedColor.b = b;
sharedColor.a = a;
setColor(sharedColor);
}
public Color getColor() {
return currentColor;
}
public void resetTranslation() {
translateX = translateY = 0;
}
public void translate(float x, float y) {
translateX += x;
translateY += y;
}
public void flush() {
if (idx>0)
render();
idx = 0;
texture = null;
vertices.clear();
texcoords.clear();
colors.clear();
}
protected void render() {
if (idx==0)
return;
renderCalls++;
if (texture!=null) {
Texture2D.enable(texture.getTarget());
texture.bind();
}
vertices.flip();
colors.flip();
texcoords.flip();
GL11.glVertexPointer(2, 0, vertices);
GL11.glColorPointer(4, 0, colors);
GL11.glTexCoordPointer(2, 0, texcoords);
GL11.glDrawArrays(mode, 0, idx);
vertices.clear();
colors.clear();
texcoords.clear();
idx = 0;
}
public void drawImageScaled(Image image, float x, float y, float scale) {
drawImage(image, x, y, image.getWidth()*scale, image.getHeight()*scale);
}
public void drawImage(Image image) {
drawImage(image, 0, 0);
}
public void drawImage(Image image, float x, float y) {
drawImage(image, x, y, image.getWidth(), image.getHeight());
}
public void drawImage(Image image, float x, float y, float w, float h) {
drawImage(image, x, y, w, h, null);
}
public void drawImage(Image image, float x, float y, float rotation) {
drawImage(image, x, y, rotation, image.getWidth(), image.getHeight(), null);
}
public void drawImage(Image image, float x, float y, float rotation, float w, float h, Color[] corners) {
if (rotation==0) {
drawImage(image, x, y, w, h, corners);
return;
}
checkRender(image);
float scaleX = w/image.getWidth();
float scaleY = h/image.getHeight();
float cx = image.getCenterX()*scaleX;
float cy = image.getCenterY()*scaleY;
float p1x = -cx;
float p1y = -cy;
float p2x = w - cx;
float p2y = -cy;
float p3x = w - cx;
float p3y = h - cy;
float p4x = -cx;
float p4y = h - cy;
double rad = Math.toRadians(rotation);
final float cos = (float) FastTrig.cos(rad);
final float sin = (float) FastTrig.sin(rad);
float tx = image.getNormalizedXOffset();
float ty = image.getNormalizedYOffset();
float tw = image.getNormalizedWidth();
float th = image.getNormalizedHeight();
float x1 = (cos * p1x - sin * p1y) + cx; float y1 = (sin * p1x + cos * p1y) + cy;
float x2 = (cos * p2x - sin * p2y) + cx; float y2 = (sin * p2x + cos * p2y) + cy;
float x3 = (cos * p3x - sin * p3y) + cx; float y3 = (sin * p3x + cos * p3y) + cy;
float x4 = (cos * p4x - sin * p4y) + cx; float y4 = (sin * p4x + cos * p4y) + cy;
drawQuadElement(x+x1, y+y1, tx, ty, corners!=null ? corners[0] : null,
x+x2, y+y2, tx+tw, ty, corners!=null ? corners[1] : null,
x+x3, y+y3, tx+tw, ty+th, corners!=null ? corners[2] : null,
x+x4, y+y4, tx, ty+th, corners!=null ? corners[3] : null);
}
public void drawImage(Image image, float x, float y, float w, float h, Color[] corners) {
checkRender(image);
float tx = image.getNormalizedXOffset();
float ty = image.getNormalizedYOffset();
float tw = image.getNormalizedWidth();
float th = image.getNormalizedHeight();
drawImage(image, x, y, w, h, tx, ty, tw, th, corners);
}
public void drawSubImage(Image image, float srcx, float srcy,
float srcwidth, float srcheight, float x, float y) {
drawSubImage(image, srcx, srcy, srcwidth, srcheight, x, y, srcwidth, srcheight);
}
public void drawSubImage(Image image, float srcx, float srcy,
float srcwidth, float srcheight, float x, float y, float w, float h) {
drawSubImage(image, srcx, srcy, srcwidth, srcheight, x, y, w, h, null);
}
public void drawSubImage(Image image, float srcx, float srcy,
float srcwidth, float srcheight, float x, float y, float w,
float h, Color[] corners) {
checkRender(image);
float iw = image.getWidth();
float ih = image.getHeight();
float tx = (srcx / iw * image.getNormalizedWidth()) + image.getNormalizedXOffset();
float ty = (srcy / ih * image.getNormalizedHeight()) + image.getNormalizedYOffset();
float tw = w / iw * image.getNormalizedWidth();
float th = h / ih * image.getNormalizedHeight();
drawQuadElement(x, y, tx, ty, corners != null ? corners[0] : null, x
+ w, y, tx + tw, ty, corners != null ? corners[1] : null,
x + w, y + h, tx + tw, ty + th, corners != null ? corners[2]
: null, x, y + h, tx, ty + th,
corners != null ? corners[3] : null);
}
public void drawImage(Image image, float x, float y, float width, float height,
float u, float v, float uWidth, float vHeight, Color[] corners) {
checkRender(image);
drawQuadElement(x, y, u, v, corners!=null ? corners[0] : null,
x+width, y, u+uWidth, v, corners!=null ? corners[1] : null,
x+width, y+height, u+uWidth, v+vHeight, corners!=null ? corners[2] : null,
x, y+height, u, v+vHeight, corners!=null ? corners[3] : null);
}
public void drawImage(Image image, float x, float y, float[] points,
float[] texcoords, int offset, int texcoordsOffset, Color[] corners) {
checkRender(image);
float x1 = points[offset++];
float y1 = points[offset++];
float x2 = points[offset++];
float y2 = points[offset++];
float x3 = points[offset++];
float y3 = points[offset++];
float x4 = points[offset++];
float y4 = points[offset++];
float u1 = texcoords[texcoordsOffset++];
float v1 = texcoords[texcoordsOffset++];
float u2 = texcoords[texcoordsOffset++];
float v2 = texcoords[texcoordsOffset++];
float u3 = texcoords[texcoordsOffset++];
float v3 = texcoords[texcoordsOffset++];
float u4 = texcoords[texcoordsOffset++];
float v4 = texcoords[texcoordsOffset++];
drawQuadElement(x+x1, y+y1, u1, v1, corners!=null ? corners[0] : null,
x+x2, y+y2, u2, v2, corners!=null ? corners[1] : null,
x+x3, y+y3, u3, v3, corners!=null ? corners[2] : null,
x+x4, y+y4, u4, v4, corners!=null ? corners[3] : null);
}
private void checkRender(Image image) {
if (image==null || image.getTexture()==null)
throw new NullPointerException("null texture");
if (image.getTexture()!=texture) {
render();
texture = image.getTexture();
} else if (idx >= maxVerts - 4)
render();
}
protected void vertex(float x, float y, float u, float v, Color color) {
vertices.put(x);
vertices.put(y);
texcoords.put(u);
texcoords.put(v);
Color c = color!=null ? color : currentColor;
colors.put(c.r);
colors.put(c.g);
colors.put(c.b);
colors.put(c.a);
idx++;
}
protected void drawQuadElement(
float x1, float y1, float u1, float v1, Color c1, float x2, float y2, float u2, float v2, Color c2, float x3, float y3, float u3, float v3, Color c3, float x4, float y4, float u4, float v4, Color c4) { x1 += translateX;
y1 += translateY;
x2 += translateX;
y2 += translateY;
x3 += translateX;
y3 += translateY;
x4 += translateX;
y4 += translateY;
if (mode == GL11.GL_TRIANGLES) {
vertex(x1, y1, u1, v1, c1);
vertex(x2, y2, u2, v2, c2);
vertex(x4, y4, u4, v4, c4);
vertex(x2, y2, u2, v2, c2);
vertex(x3, y3, u3, v3, c3);
vertex(x4, y4, u4, v4, c4);
} else {
vertex(x1, y1, u1, v1, c1);
vertex(x2, y2, u2, v2, c2);
vertex(x3, y3, u3, v3, c3);
vertex(x4, y4, u4, v4, c4);
}
}
} |
Special syntax:
- To highlight a line (yellow background), prefix it with '@@'
- To indicate that a line should be removed (red background), prefix it with '-'
- To indicate that a line should be added (green background), prefix it with '+'
- To post multiple snippets, seperate them by '~~~~'
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|