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
//
// Atlas Demo
// a cocos2d example
// http://code.google.com/p/cocos2d-iphone
//

// cocos import
#import "cocos2d.h"

// local import
#import "AtlasSpriteTest.h"

static int sceneIdx=-1;
static NSString *transitions[] = {
@"Atlas1",
@"Atlas2",
@"Atlas3",
@"Atlas4",
@"Atlas5",
@"Atlas6",
};

enum {
kTagTileMap = 1,
kTagSpriteManager = 1,
kTagAnimation1 = 1,
};

enum {
kTagSprite1,
kTagSprite2,
kTagSprite3,
kTagSprite4,
kTagSprite5,
kTagSprite6,
kTagSprite7,
kTagSprite8,
};

Class nextAction()
{

sceneIdx++;
sceneIdx = sceneIdx % ( sizeof(transitions) / sizeof(transitions[0]) );
NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}

Class backAction()
{
sceneIdx--;
int total = ( sizeof(transitions) / sizeof(transitions[0]) );
if( sceneIdx < 0 )
sceneIdx += total;

NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}

Class restartAction()
{
NSString *r = transitions[sceneIdx];
Class c = NSClassFromString(r);
return c;
}


@implementation AtlasDemo
-(id) init
{
[super init];


CGSize s = [[Director sharedDirector] winSize];

Label* label = [Label labelWithString:[self title] fontName:@"Arial" fontSize:32];
[self addChild: label z:1];
[label setPosition: ccp(s.width/2, s.height-50)];

MenuItemImage *item1 = [MenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
MenuItemImage *item2 = [MenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)];
MenuItemImage *item3 = [MenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];

Menu *menu = [Menu menuWithItems:item1, item2, item3, nil];

menu.position = CGPointZero;
item1.position = ccp( s.width/2 - 100,30);
item2.position = ccp( s.width/2, 30);
item3.position = ccp( s.width/2 + 100,30);
[self addChild: menu z:1];

return self;
}

-(void) dealloc
{
[super dealloc];
}

-(void) restartCallback: (id) sender
{
Scene *s = [Scene node];
[s addChild: [restartAction() node]];
[[Director sharedDirector] replaceScene: s];
}

-(void) nextCallback: (id) sender
{
Scene *s = [Scene node];
[s addChild: [nextAction() node]];
[[Director sharedDirector] replaceScene: s];
}

-(void) backCallback: (id) sender
{
Scene *s = [Scene node];
[s addChild: [backAction() node]];
[[Director sharedDirector] replaceScene: s];
}

-(NSString*) title
{
return @"No title";
}
@end

#pragma mark Example Atlas 1

@implementation Atlas1

-(id) init
{
if( (self=[super init]) ) {

isTouchEnabled = YES;

AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
[self addChild:mgr z:0 tag:kTagSpriteManager];

[self addNewSpriteWithCoords:ccp(480/2, 320/2)];

}
return self;
}

-(void) addNewSpriteWithCoords:(CGPoint)p
{
AtlasSpriteManager *mgr = (AtlasSpriteManager*) [self getChildByTag:kTagSpriteManager];

int idx = CCRANDOM_0_1() * 1400 / 100;
int x = (idx%5) * 85;
int y = (idx/5) * 121;


AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(x,y,85,121) spriteManager:mgr];
[mgr addChild:sprite];

sprite.position = ccp( p.x, p.y);

id action;
float r = CCRANDOM_0_1();

if( r < 0.33 )
action = [ScaleBy actionWithDuration:3 scale:2];
else if(r < 0.66)
action = [RotateBy actionWithDuration:3 angle:360];
else
action = [Blink actionWithDuration:1 blinks:3];
id action_back = [action reverse];
id seq = [Sequence actions:action, action_back, nil];

[sprite runAction: [RepeatForever actionWithAction:seq]];
}

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];

location = [[Director sharedDirector] convertCoordinate: location];

[self addNewSpriteWithCoords: location];
}
return kEventHandled;
}

-(NSString *) title
{
return @"AtlasSprite (tap screen)";
}
@end

#pragma mark Example Atlas 2

@implementation Atlas2

-(id) init
{
if( (self=[super init]) ) {

[Texture2D saveTexParameters];
[Texture2D setAliasTexParameters];
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
[self addChild:mgr z:0 tag:kTagSpriteManager];

[Texture2D restoreTexParameters];

AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
AtlasSprite *sprite2 = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
AtlasSprite *sprite3 = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];

AtlasAnimation *animation = [AtlasAnimation animationWithName:@"dance" delay:0.2f];
for(int i=0;i<14;i++) {
int x= i % 5;
int y= i / 5;
[animation addFrameWithRect: CGRectMake(x*85, y*121, 85, 121) ];

}

[mgr addChild:sprite];
[mgr addChild:sprite2];
[mgr addChild:sprite3];

CGSize s = [[Director sharedDirector] winSize];
sprite.position = ccp( s.width /2, s.height/2);
sprite2.position = ccp( s.width /2 - 100, s.height/2);
sprite3.position = ccp( s.width /2 + 100, s.height/2);

id action = [Animate actionWithAnimation: animation];
id action2 = [[action copy] autorelease];
id action3 = [[action copy] autorelease];

sprite.scale = 0.5f;
sprite2.scale = 1.0f;
sprite3.scale = 1.5f;

[sprite runAction:action];
[sprite2 runAction:action2];
[sprite3 runAction:action3];


}
return self;
}

-(NSString *) title
{
return @"AtlasSprite: Animation";
}
@end

#pragma mark Example Atlas 3

@implementation Atlas3

-(id) init
{
if( (self=[super init]) ) {

// small capacity. Testing resizing.
// Don't use capacity=1 in your real game. It is expensive to resize the capacity
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
[self addChild:mgr z:0 tag:kTagSpriteManager];

AtlasSprite *sprite1 = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite2 = [AtlasSprite spriteWithRect:CGRectMake(85*1, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite3 = [AtlasSprite spriteWithRect:CGRectMake(85*2, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite4 = [AtlasSprite spriteWithRect:CGRectMake(85*3, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite5 = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite6 = [AtlasSprite spriteWithRect:CGRectMake(85*1, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite7 = [AtlasSprite spriteWithRect:CGRectMake(85*2, 121*1, 85, 121) spriteManager: mgr];
AtlasSprite *sprite8 = [AtlasSprite spriteWithRect:CGRectMake(85*3, 121*1, 85, 121) spriteManager: mgr];

CGSize s = [[Director sharedDirector] winSize];
sprite1.position = ccp( (s.width/5)*1, (s.height/3)*1);
sprite2.position = ccp( (s.width/5)*2, (s.height/3)*1);
sprite3.position = ccp( (s.width/5)*3, (s.height/3)*1);
sprite4.position = ccp( (s.width/5)*4, (s.height/3)*1);
sprite5.position = ccp( (s.width/5)*1, (s.height/3)*2);
sprite6.position = ccp( (s.width/5)*2, (s.height/3)*2);
sprite7.position = ccp( (s.width/5)*3, (s.height/3)*2);
sprite8.position = ccp( (s.width/5)*4, (s.height/3)*2);

id action = [FadeIn actionWithDuration:2];
id action_back = [action reverse];
id fade = [RepeatForever actionWithAction: [Sequence actions: action, action_back, nil]];

id tintred = [TintBy actionWithDuration:2 red:0 green:-255 blue:-255];
id tintred_back = [tintred reverse];
id red = [RepeatForever actionWithAction: [Sequence actions: tintred, tintred_back, nil]];

id tintgreen = [TintBy actionWithDuration:2 red:-255 green:0 blue:-255];
id tintgreen_back = [tintgreen reverse];
id green = [RepeatForever actionWithAction: [Sequence actions: tintgreen, tintgreen_back, nil]];

id tintblue = [TintBy actionWithDuration:2 red:-255 green:-255 blue:0];
id tintblue_back = [tintblue reverse];
id blue = [RepeatForever actionWithAction: [Sequence actions: tintblue, tintblue_back, nil]];


[sprite5 runAction:red];
[sprite6 runAction:green];
[sprite7 runAction:blue];
[sprite8 runAction:fade];

// late add: test dirtyColor and dirtyPosition
[mgr addChild:sprite1 z:0 tag:kTagSprite1];
[mgr addChild:sprite2 z:0 tag:kTagSprite2];
[mgr addChild:sprite3 z:0 tag:kTagSprite3];
[mgr addChild:sprite4 z:0 tag:kTagSprite4];
[mgr addChild:sprite5 z:0 tag:kTagSprite5];
[mgr addChild:sprite6 z:0 tag:kTagSprite6];
[mgr addChild:sprite7 z:0 tag:kTagSprite7];
[mgr addChild:sprite8 z:0 tag:kTagSprite8];


[self schedule:@selector(removeAndAddSprite:) interval:2];

}
return self;
}

// this function test if remove and add works as expected:
// color array and vertex array should be reindexed
-(void) removeAndAddSprite:(ccTime) dt
{
id mgr = [self getChildByTag:kTagSpriteManager];
id sprite = [mgr getChildByTag:kTagSprite5];

[sprite retain];

[mgr removeChild:sprite cleanup:NO];
[mgr addChild:sprite z:0 tag:kTagSprite5];

[sprite release];
}

-(NSString *) title
{
return @"AtlasSprite: Color & Opacity";
}
@end

#pragma mark Example Atlas 4

@implementation Atlas4

-(id) init
{
if( (self=[super init]) ) {

dir = 1;

// small capacity. Testing resizing.
// Don't use capacity=1 in your real game. It is expensive to resize the capacity
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
[self addChild:mgr z:0 tag:kTagSpriteManager];

CGSize s = [[Director sharedDirector] winSize];

for(int i=0;i<5;i++) {
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*0, 121*1, 85, 121) spriteManager: mgr];
sprite.position = ccp( 50 + i*40, s.height/2);
[mgr addChild:sprite z:i];
}

for(int i=5;i<10;i++) {
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*1, 121*0, 85, 121) spriteManager: mgr];
sprite.position = ccp( 50 + i*40, s.height/2);
[mgr addChild:sprite z:14-i];
}

AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*3, 121*0, 85, 121) spriteManager: mgr];
[mgr addChild:sprite z:-1 tag:kTagSprite1];
sprite.position = ccp(s.width/2, s.height/2 - 20);
sprite.scaleX = 6;
[sprite setRGB:255:0:0];

[self schedule:@selector(reorderSprite:) interval:1];
}
return self;
}

-(void) reorderSprite:(ccTime) dt
{
id mgr = [self getChildByTag:kTagSpriteManager];
id sprite = [mgr getChildByTag:kTagSprite1];

int z = [sprite zOrder];

if( z < -1 )
dir = 1;
if( z > 10 )
dir = -1;

z += dir * 3;

[mgr reorderChild:sprite z:z];

}

-(NSString *) title
{
return @"AtlasSprite: Z order";
}
@end

#pragma mark Example Atlas 5

@implementation Atlas5

-(id) init
{
if( (self=[super init]) ) {

// small capacity. Testing resizing.
// Don't use capacity=1 in your real game. It is expensive to resize the capacity
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
[self addChild:mgr z:0 tag:kTagSpriteManager];

CGSize s = [[Director sharedDirector] winSize];


id rotate = [RotateBy actionWithDuration:10 angle:360];
id action = [RepeatForever actionWithAction:rotate];
for(int i=0;i<3;i++) {
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*i, 121*1, 85, 121) spriteManager: mgr];
sprite.position = ccp( 90 + i*150, s.height/2);


Sprite *point = [Sprite spriteWithFile:@"r1.png"];
point.scale = 0.25f;
point.position = sprite.position;
[self addChild:point z:1];

switch(i) {
case 0:
sprite.transformAnchor = CGPointZero;
break;
case 1:
break;
case 2:
sprite.transformAnchor = ccp(85,121);
break;
}

point.position = sprite.position;

id copy = [[action copy] autorelease];
[sprite runAction:copy];
[mgr addChild:sprite z:i];
}
}
return self;
}

-(NSString *) title
{
return @"AtlasSprite: anchor point";
}
@end

#pragma mark Example Atlas 6

@implementation Atlas6
-(id) init
{
if( (self=[super init]) ) {

// small capacity. Testing resizing
// Don't use capacity=1 in your real game. It is expensive to resize the capacity
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:1];
[self addChild:mgr z:0 tag:kTagSpriteManager];

CGSize s = [[Director sharedDirector] winSize];

mgr.relativeTransformAnchor = NO;
mgr.transformAnchor = ccp( s.width / 2.0f, s.height / 2.0f );


// AtlasSprite actions
id rotate = [RotateBy actionWithDuration:5 angle:360];
id action = [RepeatForever actionWithAction:rotate];

// AtlasSpriteManager actions
id rotate_back = [rotate reverse];
id rotate_seq = [Sequence actions:rotate, rotate_back, nil];
id rotate_forever = [RepeatForever actionWithAction:rotate_seq];

id scale = [ScaleBy actionWithDuration:5 scale:1.5f];
id scale_back = [scale reverse];
id scale_seq = [Sequence actions: scale, scale_back, nil];
id scale_forever = [RepeatForever actionWithAction:scale_seq];


for(int i=0;i<3;i++) {
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(85*i, 121*1, 85, 121) spriteManager: mgr];
sprite.position = ccp( 90 + i*150, s.height/2);

[sprite runAction: [[action copy] autorelease]];
[mgr addChild:sprite z:i];
}

[mgr runAction: scale_forever];
[mgr runAction: rotate_forever];
}
return self;
}
-(NSString*) title
{
return @"AtlasSpriteManager transformation";
}
@end


#pragma mark AppDelegate

// CLASS IMPLEMENTATIONS
@implementation AppController

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// cocos2d will inherit these values
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:NO];

// must be called before any othe call to the director
// [Director useFastDirector];

// before creating any layer, set the landscape mode
[[Director sharedDirector] setLandscape: YES];
[[Director sharedDirector] setAnimationInterval:1.0/60];
[[Director sharedDirector] setDisplayFPS:YES];

// create an openGL view inside a window
[[Director sharedDirector] attachInView:window];
[window makeKeyAndVisible];

Scene *scene = [Scene node];
[scene addChild: [nextAction() node]];

[[Director sharedDirector] runWithScene: scene];
}

// getting a call, pause the game
-(void) applicationWillResignActive:(UIApplication *)application
{
[[Director sharedDirector] pause];
}

// call got rejected
-(void) applicationDidBecomeActive:(UIApplication *)application
{
[[Director sharedDirector] resume];
}

// purge memroy
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[TextureMgr sharedTextureMgr] removeAllTextures];
}

// next delta time will be zero
-(void) applicationSignificantTimeChange:(UIApplication *)application
{
[[Director sharedDirector] setNextDeltaTimeZero:YES];
}

- (void) dealloc
{
[window release];
[super dealloc];
}
@end

Change log

r812 by ricardoquesada on Apr 17, 2009   Diff
real branch of v0.7.x
Go to: 
Project members, sign in to write a code review

Older revisions

r799 by ricardoquesada on Apr 15, 2009   Diff
improved  issue #290 
r785 by ricardoquesada on Apr 12, 2009   Diff
fixed  issue #305 
r784 by ricardoquesada on Apr 12, 2009   Diff
fixed  issue #308 .
Added test in AtlasSpriteTest.m

Since there are a lot of tests in
AtlasTest.m I splitted it in 2:
...
All revisions of this file

File info

Size: 15262 bytes, 579 lines
Powered by Google Project Hosting