My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.graphics.g2d;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.NumberUtils;

import static com.badlogic.gdx.graphics.g2d.SpriteBatch.*;

/** Holds the geometry, color, and texture information for drawing 2D sprites using {@link SpriteBatch}. A Sprite has a position
* and a size given as width and height. The position is relative to the origin of the coordinate system specified via
* {@link SpriteBatch#begin()} and the respective matrices. A Sprite is always rectangular and its position (x, y) are located in
* the bottom left corner of that rectangle. A Sprite also has an origin around which rotations and scaling are performed (that
* is, the origin is not modified by rotation and scaling). The origin is given relative to the bottom left corner of the Sprite,
* its position.
* @author mzechner
* @author Nathan Sweet */
public class Sprite extends TextureRegion {
static final int VERTEX_SIZE = 2 + 1 + 2;
static final int SPRITE_SIZE = 4 * VERTEX_SIZE;

final float[] vertices = new float[SPRITE_SIZE];
private final Color color = new Color(1, 1, 1, 1);
private float x, y;
float width, height;
private float originX, originY;
private float rotation;
private float scaleX = 1, scaleY = 1;
private boolean dirty = true;
private Rectangle bounds = new Rectangle();

/** Creates an uninitialized sprite. The sprite will need a texture, texture region, bounds, and color set before it can be
* drawn. */
public Sprite () {
setColor(1, 1, 1, 1);
}

/** Creates a sprite with width, height, and texture region equal to the size of the texture. */
public Sprite (Texture texture) {
this(texture, 0, 0, texture.getWidth(), texture.getHeight());
}

/** Creates a sprite with width, height, and texture region equal to the specified size. The texture region's upper left corner
* will be 0,0. * @param srcWidth The width of the texture region. May be negative to flip the sprite when drawn.
* @param srcHeight The height of the texture region. May be negative to flip the sprite when drawn. */
public Sprite (Texture texture, int srcWidth, int srcHeight) {
this(texture, 0, 0, srcWidth, srcHeight);
}

/** Creates a sprite with width, height, and texture region equal to the specified size. * @param srcWidth The width of the
* texture region. May be negative to flip the sprite when drawn.
* @param srcHeight The height of the texture region. May be negative to flip the sprite when drawn. */
public Sprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) {
if (texture == null) throw new IllegalArgumentException("texture cannot be null.");
this.texture = texture;
setRegion(srcX, srcY, srcWidth, srcHeight);
setColor(1, 1, 1, 1);
setSize(Math.abs(srcWidth), Math.abs(srcHeight));
setOrigin(width / 2, height / 2);
}

// Note the region is copied.
public Sprite (TextureRegion region) {
setRegion(region);
setColor(1, 1, 1, 1);
setSize(Math.abs(region.getRegionWidth()), Math.abs(region.getRegionHeight()));
setOrigin(width / 2, height / 2);
}

/** Creates a sprite with width, height, and texture region equal to the specified size, relative to specified sprite's texture
* region.
* @param srcWidth The width of the texture region. May be negative to flip the sprite when drawn.
* @param srcHeight The height of the texture region. May be negative to flip the sprite when drawn. */
public Sprite (TextureRegion region, int srcX, int srcY, int srcWidth, int srcHeight) {
setRegion(region, srcX, srcY, srcWidth, srcHeight);
setColor(1, 1, 1, 1);
setSize(Math.abs(srcWidth), Math.abs(srcHeight));
setOrigin(width / 2, height / 2);
}

/** Creates a sprite that is a copy in every way of the specified sprite. */
public Sprite (Sprite sprite) {
set(sprite);
}

public void set (Sprite sprite) {
if (sprite == null) throw new IllegalArgumentException("sprite cannot be null.");
System.arraycopy(sprite.vertices, 0, vertices, 0, SPRITE_SIZE);
texture = sprite.texture;
u = sprite.u;
v = sprite.v;
u2 = sprite.u2;
v2 = sprite.v2;
x = sprite.x;
y = sprite.y;
width = sprite.width;
height = sprite.height;
originX = sprite.originX;
originY = sprite.originY;
rotation = sprite.rotation;
scaleX = sprite.scaleX;
scaleY = sprite.scaleY;
color.set(sprite.color);
dirty = sprite.dirty;
}

/** Sets the position and size of the sprite when drawn, before scaling and rotation are applied. If origin, rotation, or scale
* are changed, it is slightly more efficient to set the bounds after those operations. */
public void setBounds (float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;

if (dirty) return;

float x2 = x + width;
float y2 = y + height;
float[] vertices = this.vertices;
vertices[X1] = x;
vertices[Y1] = y;

vertices[X2] = x;
vertices[Y2] = y2;

vertices[X3] = x2;
vertices[Y3] = y2;

vertices[X4] = x2;
vertices[Y4] = y;

if (rotation != 0 || scaleX != 1 || scaleY != 1) dirty = true;
}

/** Sets the size of the sprite when drawn, before scaling and rotation are applied. If origin, rotation, or scale are changed,
* it is slightly more efficient to set the size after those operations. If both position and size are to be changed, it is
* better to use {@link #setBounds(float, float, float, float)}. */
public void setSize (float width, float height) {
this.width = width;
this.height = height;

if (dirty) return;

float x2 = x + width;
float y2 = y + height;
float[] vertices = this.vertices;
vertices[X1] = x;
vertices[Y1] = y;

vertices[X2] = x;
vertices[Y2] = y2;

vertices[X3] = x2;
vertices[Y3] = y2;

vertices[X4] = x2;
vertices[Y4] = y;

if (rotation != 0 || scaleX != 1 || scaleY != 1) dirty = true;
}

/** Sets the position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient
* to set the position after those operations. If both position and size are to be changed, it is better to use
* {@link #setBounds(float, float, float, float)}. */
public void setPosition (float x, float y) {
translate(x - this.x, y - this.y);
}

/** Sets the x position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient
* to set the position after those operations. If both position and size are to be changed, it is better to use
* {@link #setBounds(float, float, float, float)}. */
public void setX (float x) {
translateX(x - this.x);
}

/** Sets the y position where the sprite will be drawn. If origin, rotation, or scale are changed, it is slightly more efficient
* to set the position after those operations. If both position and size are to be changed, it is better to use
* {@link #setBounds(float, float, float, float)}. */
public void setY (float y) {
translateY(y - this.y);
}

/** Sets the x position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are
* changed, it is slightly more efficient to translate after those operations. */
public void translateX (float xAmount) {
this.x += xAmount;

if (dirty) return;

float[] vertices = this.vertices;
vertices[X1] += xAmount;
vertices[X2] += xAmount;
vertices[X3] += xAmount;
vertices[X4] += xAmount;
}

/** Sets the y position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are
* changed, it is slightly more efficient to translate after those operations. */
public void translateY (float yAmount) {
y += yAmount;

if (dirty) return;

float[] vertices = this.vertices;
vertices[Y1] += yAmount;
vertices[Y2] += yAmount;
vertices[Y3] += yAmount;
vertices[Y4] += yAmount;
}

/** Sets the position relative to the current position where the sprite will be drawn. If origin, rotation, or scale are
* changed, it is slightly more efficient to translate after those operations. */
public void translate (float xAmount, float yAmount) {
x += xAmount;
y += yAmount;

if (dirty) return;

float[] vertices = this.vertices;
vertices[X1] += xAmount;
vertices[Y1] += yAmount;

vertices[X2] += xAmount;
vertices[Y2] += yAmount;

vertices[X3] += xAmount;
vertices[Y3] += yAmount;

vertices[X4] += xAmount;
vertices[Y4] += yAmount;
}

public void setColor (Color tint) {
float color = tint.toFloatBits();
float[] vertices = this.vertices;
vertices[C1] = color;
vertices[C2] = color;
vertices[C3] = color;
vertices[C4] = color;
}

public void setColor (float r, float g, float b, float a) {
int intBits = ((int)(255 * a) << 24) | ((int)(255 * b) << 16) | ((int)(255 * g) << 8) | ((int)(255 * r));
float color = NumberUtils.intToFloatColor(intBits);
float[] vertices = this.vertices;
vertices[C1] = color;
vertices[C2] = color;
vertices[C3] = color;
vertices[C4] = color;
}

/** Sets the origin in relation to the sprite's position for scaling and rotation. */
public void setOrigin (float originX, float originY) {
this.originX = originX;
this.originY = originY;
dirty = true;
}

public void setRotation (float degrees) {
this.rotation = degrees;
dirty = true;
}

/** Sets the sprite's rotation relative to the current rotation. */
public void rotate (float degrees) {
rotation += degrees;
dirty = true;
}

/** Rotates this sprite 90 degrees in-place by rotating the texture coordinates. This rotation is unaffected by
* {@link #setRotation(float)} and {@link #rotate(float)}. */
public void rotate90 (boolean clockwise) {
float[] vertices = this.vertices;

if (clockwise) {
float temp = vertices[V1];
vertices[V1] = vertices[V4];
vertices[V4] = vertices[V3];
vertices[V3] = vertices[V2];
vertices[V2] = temp;

temp = vertices[U1];
vertices[U1] = vertices[U4];
vertices[U4] = vertices[U3];
vertices[U3] = vertices[U2];
vertices[U2] = temp;
} else {
float temp = vertices[V1];
vertices[V1] = vertices[V2];
vertices[V2] = vertices[V3];
vertices[V3] = vertices[V4];
vertices[V4] = temp;

temp = vertices[U1];
vertices[U1] = vertices[U2];
vertices[U2] = vertices[U3];
vertices[U3] = vertices[U4];
vertices[U4] = temp;
}
}

public void setScale (float scaleXY) {
this.scaleX = scaleXY;
this.scaleY = scaleXY;
dirty = true;
}

public void setScale (float scaleX, float scaleY) {
this.scaleX = scaleX;
this.scaleY = scaleY;
dirty = true;
}

/** Sets the sprite's scale relative to the current scale. */
public void scale (float amount) {
this.scaleX += amount;
this.scaleY += amount;
dirty = true;
}

/** Returns the packed vertices, colors, and texture coordinates for this sprite. */
public float[] getVertices () {
if (dirty) {
dirty = false;

float[] vertices = this.vertices;
float localX = -originX;
float localY = -originY;
float localX2 = localX + width;
float localY2 = localY + height;
float worldOriginX = this.x - localX;
float worldOriginY = this.y - localY;
if (scaleX != 1 || scaleY != 1) {
localX *= scaleX;
localY *= scaleY;
localX2 *= scaleX;
localY2 *= scaleY;
}
if (rotation != 0) {
final float cos = MathUtils.cosDeg(rotation);
final float sin = MathUtils.sinDeg(rotation);
final float localXCos = localX * cos;
final float localXSin = localX * sin;
final float localYCos = localY * cos;
final float localYSin = localY * sin;
final float localX2Cos = localX2 * cos;
final float localX2Sin = localX2 * sin;
final float localY2Cos = localY2 * cos;
final float localY2Sin = localY2 * sin;

final float x1 = localXCos - localYSin + worldOriginX;
final float y1 = localYCos + localXSin + worldOriginY;
vertices[X1] = x1;
vertices[Y1] = y1;

final float x2 = localXCos - localY2Sin + worldOriginX;
final float y2 = localY2Cos + localXSin + worldOriginY;
vertices[X2] = x2;
vertices[Y2] = y2;

final float x3 = localX2Cos - localY2Sin + worldOriginX;
final float y3 = localY2Cos + localX2Sin + worldOriginY;
vertices[X3] = x3;
vertices[Y3] = y3;

vertices[X4] = x1 + (x3 - x2);
vertices[Y4] = y3 - (y2 - y1);
} else {
final float x1 = localX + worldOriginX;
final float y1 = localY + worldOriginY;
final float x2 = localX2 + worldOriginX;
final float y2 = localY2 + worldOriginY;

vertices[X1] = x1;
vertices[Y1] = y1;

vertices[X2] = x1;
vertices[Y2] = y2;

vertices[X3] = x2;
vertices[Y3] = y2;

vertices[X4] = x2;
vertices[Y4] = y1;
}
}
return vertices;
}

/** Returns the bounding axis aligned {@link Rectangle} that bounds this sprite. The rectangles x and y coordinates describe its
* bottom left corner. If you change the position or size of the sprite, you have to fetch the triangle again for it to be recomputed.
*
* @return the bounding Rectangle */
public Rectangle getBoundingRectangle () {
final float[] vertices = getVertices();

float minx = vertices[X1];
float miny = vertices[Y1];
float maxx = vertices[X1];
float maxy = vertices[Y1];

minx = minx > vertices[X2] ? vertices[X2] : minx;
minx = minx > vertices[X3] ? vertices[X3] : minx;
minx = minx > vertices[X4] ? vertices[X4] : minx;

maxx = maxx < vertices[X2] ? vertices[X2] : maxx;
maxx = maxx < vertices[X3] ? vertices[X3] : maxx;
maxx = maxx < vertices[X4] ? vertices[X4] : maxx;

miny = miny > vertices[Y2] ? vertices[Y2] : miny;
miny = miny > vertices[Y3] ? vertices[Y3] : miny;
miny = miny > vertices[Y4] ? vertices[Y4] : miny;

maxy = maxy < vertices[Y2] ? vertices[Y2] : maxy;
maxy = maxy < vertices[Y3] ? vertices[Y3] : maxy;
maxy = maxy < vertices[Y4] ? vertices[Y4] : maxy;

bounds.x = minx;
bounds.y = miny;
bounds.width = maxx - minx;
bounds.height = maxy - miny;

return bounds;
}

public void draw (SpriteBatch spriteBatch) {
spriteBatch.draw(texture, getVertices(), 0, SPRITE_SIZE);
}

public void draw (SpriteBatch spriteBatch, float alphaModulation) {
Color color = getColor();
float oldAlpha = color.a;
color.a *= alphaModulation;
setColor(color);
draw(spriteBatch);
color.a = oldAlpha;
setColor(color);
}

public float getX () {
return x;
}

public float getY () {
return y;
}

public float getWidth () {
return width;
}

public float getHeight () {
return height;
}

public float getOriginX () {
return originX;
}

public float getOriginY () {
return originY;
}

public float getRotation () {
return rotation;
}

public float getScaleX () {
return scaleX;
}

public float getScaleY () {
return scaleY;
}

/** Returns the color of this sprite. Changing the returned color will have no affect, {@link #setColor(Color)} or
* {@link #setColor(float, float, float, float)} must be used. */
public Color getColor () {
float floatBits = vertices[C1];
int intBits = NumberUtils.floatToIntColor(vertices[C1]);
Color color = this.color;
color.r = (intBits & 0xff) / 255f;
color.g = ((intBits >>> 8) & 0xff) / 255f;
color.b = ((intBits >>> 16) & 0xff) / 255f;
color.a = ((intBits >>> 24) & 0xff) / 255f;
return color;
}

public void setRegion (float u, float v, float u2, float v2) {
this.u = u;
this.v = v;
this.u2 = u2;
this.v2 = v2;

float[] vertices = Sprite.this.vertices;
vertices[U1] = u;
vertices[V1] = v2;

vertices[U2] = u;
vertices[V2] = v;

vertices[U3] = u2;
vertices[V3] = v;

vertices[U4] = u2;
vertices[V4] = v2;
}

public void setU (float u) {
this.u = u;
vertices[U1] = u;
vertices[U2] = u;
}

public void setV (float v) {
this.v = v;
vertices[V2] = v;
vertices[V3] = v;
}

public void setU2 (float u2) {
this.u2 = u2;
vertices[U3] = u2;
vertices[U4] = u2;
}

public void setV2 (float v2) {
this.v2 = v2;
vertices[V1] = v2;
vertices[V4] = v2;
}

public void flip (boolean x, boolean y) {
super.flip(x, y);
float[] vertices = Sprite.this.vertices;
if (x) {
float temp = vertices[U1];
vertices[U1] = vertices[U3];
vertices[U3] = temp;
temp = vertices[U2];
vertices[U2] = vertices[U4];
vertices[U4] = temp;
}
if (y) {
float temp = vertices[V1];
vertices[V1] = vertices[V3];
vertices[V3] = temp;
temp = vertices[V2];
vertices[V2] = vertices[V4];
vertices[V4] = temp;
}
}

public void scroll (float xAmount, float yAmount) {
float[] vertices = Sprite.this.vertices;
if (xAmount != 0) {
float u = (vertices[U1] + xAmount) % 1;
float u2 = u + width / texture.getWidth();
this.u = u;
this.u2 = u2;
vertices[U1] = u;
vertices[U2] = u;
vertices[U3] = u2;
vertices[U4] = u2;
}
if (yAmount != 0) {
float v = (vertices[V2] + yAmount) % 1;
float v2 = v + height / texture.getHeight();
this.v = v;
this.v2 = v2;
vertices[V1] = v2;
vertices[V2] = v;
vertices[V3] = v;
vertices[V4] = v2;
}
}
}

Change log

r3659 by badlogicgames on Mar 24, 2012   Diff
[fixed] minor issue in docs of Sprite.
Go to: 
Project members, sign in to write a code review

Older revisions

r3444 by badlogicgames on Mar 3, 2012   Diff
[fixed]  issue 733 , copy constructor of
Sprite had a few issues.
r2955 by nathan.sweet on Dec 12, 2011   Diff
[No log message]
r2818 by badlogicgames on Oct 22, 2011   Diff
[fixed] alpha channel in colors used
to full accuracy now.
All revisions of this file

File info

Size: 18481 bytes, 594 lines
Powered by Google Project Hosting