@@ -43,16 +43,35 @@ typedef struct Aseprite {
43
43
ase_t * ase ;
44
44
} Aseprite ;
45
45
46
- Aseprite LoadAseprite (const char * fileName ); // Load an .aseprite file.
47
- Aseprite LoadAsepriteFromMemory (unsigned char * fileData , unsigned int size ); // Load an aseprite file from memory.
48
- void UnloadAseprite (Aseprite aseprite ); // Unloads the aseprite file.
49
- void TraceAseprite (Aseprite aseprite ); // Display all information associated with the aseprite.
50
- Texture GetAsepriteTexture (Aseprite aseprite ); // Retrieve the raylib texture associated with the aseprite.
46
+ typedef struct AsepriteTag {
47
+ ase_tag_t * tag ;
48
+ Aseprite aseprite ;
49
+ int currentFrame ;
50
+ float timer ;
51
+ int direction ;
52
+ float speed ;
53
+ } AsepriteTag ;
54
+
55
+ Aseprite LoadAseprite (const char * fileName ); // Load an .aseprite file
56
+ Aseprite LoadAsepriteFromMemory (unsigned char * fileData , unsigned int size ); // Load an aseprite file from memory
57
+ void UnloadAseprite (Aseprite aseprite ); // Unloads the aseprite file
58
+ void TraceAseprite (Aseprite aseprite ); // Display all information associated with the aseprite
59
+ Texture GetAsepriteTexture (Aseprite aseprite ); // Retrieve the raylib texture associated with the aseprite
51
60
void DrawAseprite (Aseprite aseprite , int frame , int posX , int posY , Color tint );
52
61
void DrawAsepriteV (Aseprite aseprite , int frame , Vector2 position , Color tint );
53
62
void DrawAsepriteEx (Aseprite aseprite , int frame , Vector2 position , float rotation , float scale , Color tint );
54
63
void DrawAsepritePro (Aseprite aseprite , int frame , Rectangle dest , Vector2 origin , float rotation , Color tint );
55
64
65
+ AsepriteTag LoadAsepriteTag (Aseprite aseprite , const char * name ); // Load a Aseprite tag animation sequence
66
+ AsepriteTag LoadAsepriteTagFromId (Aseprite aseprite , int id ); // Load a Aseprite tag animation sequence from its index
67
+ void UpdateAsepriteTag (AsepriteTag * tag ); // Update the tag animation frame if needed
68
+ void DrawAsepriteTag (AsepriteTag tag , int posX , int posY , Color tint );
69
+ void DrawAsepriteTagV (AsepriteTag tag , Vector2 position , Color tint );
70
+ void DrawAsepriteTagEx (AsepriteTag tag , Vector2 position , float rotation , float scale , Color tint );
71
+ void DrawAsepriteTagPro (AsepriteTag tag , Rectangle dest , Vector2 origin , float rotation , Color tint );
72
+ const char * GetAsepriteTagName (AsepriteTag tag ); // Retrieve the given tag's name
73
+ int GetAspriteTagCount (Aseprite aseprite ); // Get the total amount of available tags from the loaded Aseprite
74
+
56
75
#ifdef __cplusplus
57
76
}
58
77
#endif
@@ -122,9 +141,9 @@ extern "C" {
122
141
#endif
123
142
124
143
Aseprite LoadAsepriteFromMemory (unsigned char * fileData , unsigned int size ) {
144
+ struct Aseprite aseprite ;
125
145
ase_t * ase = cute_aseprite_load_from_memory (fileData , (int )size , 0 );
126
146
if (ase == 0 ) {
127
- struct Aseprite aseprite ;
128
147
aseprite .ase = 0 ;
129
148
return aseprite ;
130
149
}
@@ -174,7 +193,6 @@ Aseprite LoadAsepriteFromMemory(unsigned char* fileData, unsigned int size) {
174
193
texturePointer -> mipmaps = texture .mipmaps ;
175
194
texturePointer -> width = texture .width ;
176
195
texturePointer -> height = texture .height ;
177
- struct Aseprite aseprite ;
178
196
aseprite .ase = ase ;
179
197
return aseprite ;
180
198
}
@@ -221,19 +239,21 @@ void DrawAseprite(Aseprite aseprite, int frame, int posX, int posY, Color tint)
221
239
222
240
void DrawAsepriteV (Aseprite aseprite , int frame , Vector2 position , Color tint ) {
223
241
ase_t * ase = aseprite .ase ;
224
- if (frame < 0 || frame >= ase -> frame_count ) {
242
+ if (ase == 0 || frame < 0 || frame >= ase -> frame_count ) {
225
243
return ;
226
244
}
245
+
227
246
Rectangle source = {(float )(frame * ase -> w ), 0 , (float )ase -> w , (float )ase -> h };
228
247
Texture2D texture = GetAsepriteTexture (aseprite );
229
248
DrawTextureRec (texture , source , position , tint );
230
249
}
231
250
232
251
void DrawAsepriteEx (Aseprite aseprite , int frame , Vector2 position , float rotation , float scale , Color tint ) {
233
252
ase_t * ase = aseprite .ase ;
234
- if (frame < 0 || frame >= ase -> frame_count ) {
253
+ if (ase == 0 || frame < 0 || frame >= ase -> frame_count ) {
235
254
return ;
236
255
}
256
+
237
257
Rectangle source = {(float )(frame * ase -> w ), 0 , (float )ase -> w , (float )ase -> h };
238
258
Texture2D texture = GetAsepriteTexture (aseprite );
239
259
Rectangle dest = {(float )position .x , (float )position .y , (float )(ase -> w * scale ), (float )(ase -> h * scale )};
@@ -243,16 +263,22 @@ void DrawAsepriteEx(Aseprite aseprite, int frame, Vector2 position, float rotati
243
263
244
264
void DrawAsepritePro (Aseprite aseprite , int frame , Rectangle dest , Vector2 origin , float rotation , Color tint ) {
245
265
ase_t * ase = aseprite .ase ;
246
- if (frame < 0 || frame >= ase -> frame_count ) {
266
+ if (ase == 0 || frame < 0 || frame >= ase -> frame_count ) {
247
267
return ;
248
268
}
269
+
249
270
Rectangle source = {(float )(frame * ase -> w ), 0 , (float )ase -> w , (float )ase -> h };
250
271
Texture2D texture = GetAsepriteTexture (aseprite );
251
272
DrawTexturePro (texture , source , dest , origin , rotation , tint );
252
273
}
253
274
254
275
void TraceAseprite (Aseprite aseprite ) {
255
276
ase_t * ase = aseprite .ase ;
277
+ if (ase == 0 ) {
278
+ TraceLog (LOG_INFO , "ASEPRITE: Empty file information" );
279
+ return ;
280
+ }
281
+
256
282
TraceLog (LOG_INFO , "ASEPRITE: File information:" );
257
283
TraceLog (LOG_INFO , " > Size: %ix%i" , ase -> w , ase -> h );
258
284
TraceLog (LOG_INFO , " > Frames: %i" , ase -> frame_count );
@@ -264,13 +290,153 @@ void TraceAseprite(Aseprite aseprite) {
264
290
ase_layer_t * layer = ase -> layers + i ;
265
291
TraceLog (LOG_INFO , " - %s" , layer -> name );
266
292
}
293
+
267
294
TraceLog (LOG_INFO , " > Tags: %i" , ase -> tag_count );
268
295
for (int i = 0 ; i < ase -> tag_count ; i ++ ) {
269
296
ase_tag_t * tag = ase -> tags + i ;
270
297
TraceLog (LOG_INFO , " - %s" , tag -> name );
271
298
}
272
299
}
273
300
301
+ void UpdateAsepriteTag (AsepriteTag * tag ) {
302
+ if (tag == 0 || tag -> tag == 0 || tag -> aseprite .ase == 0 ) {
303
+ TraceLog (LOG_ERROR , "ASEPRITE: Cannot update empty tag" );
304
+ return ;
305
+ }
306
+
307
+ ase_t * ase = tag -> aseprite .ase ;
308
+ ase_tag_t * aseTag = tag -> tag ;
309
+
310
+ // Count down the timer and see if it's time to update the frame.
311
+ tag -> timer -= GetFrameTime () * tag -> speed ;
312
+ if (tag -> timer > 0 ) {
313
+ return ;
314
+ }
315
+
316
+ // Advance the frame and see if it's time to reset the position.
317
+ tag -> currentFrame += tag -> direction ;
318
+ switch (aseTag -> loop_animation_direction ) {
319
+ case ASE_ANIMATION_DIRECTION_BACKWORDS :
320
+ if (tag -> currentFrame < aseTag -> from_frame ) {
321
+ tag -> currentFrame = aseTag -> to_frame ;
322
+ }
323
+ break ;
324
+ case ASE_ANIMATION_DIRECTION_FORWARDS :
325
+ if (tag -> currentFrame > aseTag -> to_frame ) {
326
+ tag -> currentFrame = aseTag -> from_frame ;
327
+ }
328
+ break ;
329
+ case ASE_ANIMATION_DIRECTION_PINGPONG :
330
+ if (tag -> direction > 0 ) {
331
+ if (tag -> currentFrame > aseTag -> to_frame ) {
332
+ tag -> direction = -1 ;
333
+ tag -> currentFrame = aseTag -> to_frame - 1 ;
334
+ }
335
+ }
336
+ else {
337
+ if (tag -> currentFrame < aseTag -> from_frame ) {
338
+ tag -> direction = 1 ;
339
+ tag -> currentFrame = aseTag -> from_frame + 1 ;
340
+ }
341
+ }
342
+ break ;
343
+ }
344
+
345
+ // Reset the timer.
346
+ // TODO: Add the original tag->timer to make up the different in frame time?
347
+ tag -> timer = (float )(ase -> frames [tag -> currentFrame ].duration_milliseconds ) / 1000.0f /* + tag->timer; */ ;
348
+ }
349
+
350
+ void DrawAsepriteTag (AsepriteTag tag , int posX , int posY , Color tint ) {
351
+ DrawAseprite (tag .aseprite , tag .currentFrame , posX , posY , tint );
352
+ }
353
+
354
+ void DrawAsepriteTagV (AsepriteTag tag , Vector2 position , Color tint ) {
355
+ DrawAsepriteV (tag .aseprite , tag .currentFrame , position , tint );
356
+ }
357
+
358
+ void DrawAsepriteTagEx (AsepriteTag tag , Vector2 position , float rotation , float scale , Color tint ) {
359
+ DrawAsepriteEx (tag .aseprite , tag .currentFrame , position , rotation , scale , tint );
360
+ }
361
+
362
+ void DrawAsepriteTagPro (AsepriteTag tag , Rectangle dest , Vector2 origin , float rotation , Color tint ) {
363
+ DrawAsepritePro (tag .aseprite , tag .currentFrame , dest , origin , rotation , tint );
364
+ }
365
+
366
+ AsepriteTag LoadAsepriteTagFromId (Aseprite aseprite , int id ) {
367
+ struct AsepriteTag tag ;
368
+ tag .aseprite .ase = 0 ;
369
+ tag .currentFrame = 0 ;
370
+ tag .tag = 0 ;
371
+ tag .timer = 0 ;
372
+ tag .direction = 0 ;
373
+ tag .speed = 1.0f ;
374
+ ase_t * ase = aseprite .ase ;
375
+ if (ase == 0 ) {
376
+ TraceLog (LOG_ERROR , "ASEPRITE: Asprite not loaded when attempting to load tag #%i" , id );
377
+ return tag ;
378
+ }
379
+
380
+ if (id < 0 || id >= ase -> tag_count ) {
381
+ TraceLog (LOG_ERROR , "ASEPRITE: Tag index %i out of range for %i tags" , id , ase -> tag_count );
382
+ return tag ;
383
+ }
384
+
385
+ tag .aseprite .ase = aseprite .ase ;
386
+ tag .tag = & ase -> tags [id ];
387
+ tag .currentFrame = tag .tag -> from_frame ;
388
+ tag .direction = 1 ;
389
+ if (tag .tag -> loop_animation_direction == ASE_ANIMATION_DIRECTION_BACKWORDS ) {
390
+ tag .currentFrame = tag .tag -> to_frame ;
391
+ tag .direction = -1 ;
392
+ }
393
+ tag .timer = (float )(ase -> frames [tag .currentFrame ].duration_milliseconds ) / 1000.0f ; // Timer in seconds.
394
+ return tag ;
395
+ }
396
+
397
+ AsepriteTag LoadAsepriteTag (Aseprite aseprite , const char * name ) {
398
+ struct AsepriteTag tag ;
399
+ tag .aseprite .ase = 0 ;
400
+ tag .currentFrame = 0 ;
401
+ tag .tag = 0 ;
402
+ tag .timer = 0 ;
403
+ tag .direction = 0 ;
404
+ tag .speed = 1.0f ;
405
+ ase_t * ase = aseprite .ase ;
406
+ if (ase == 0 ) {
407
+ TraceLog (LOG_ERROR , "ASEPRITE: Asprite not loaded when attempting to load tag '%s'" , name );
408
+ return tag ;
409
+ }
410
+
411
+ // Loop through all tags to find the correct name.
412
+ for (int i = 0 ; i < ase -> tag_count ; i ++ ) {
413
+ if (TextIsEqual (name , ase -> tags [i ].name )) {
414
+ return LoadAsepriteTagFromId (aseprite , i );
415
+ }
416
+ }
417
+
418
+ TraceLog (LOG_WARNING , "ASEPRITE: Could not find tag %s" , name );
419
+ return tag ;
420
+ }
421
+
422
+ const char * GetAsepriteTagName (AsepriteTag tag ) {
423
+ if (tag .tag == 0 ) {
424
+ TraceLog (LOG_WARNING , "ASEPRITE: Cannot get name of missing tag" );
425
+ return 0 ;
426
+ }
427
+
428
+ return tag .tag -> name ;
429
+ }
430
+
431
+ int GetAspriteTagCount (Aseprite aseprite ) {
432
+ if (aseprite .ase == 0 ) {
433
+ TraceLog (LOG_WARNING , "ASEPRITE: Cannot get tag count when there is not loaded aseprite" );
434
+ return 0 ;
435
+ }
436
+
437
+ return aseprite .ase -> tag_count ;
438
+ }
439
+
274
440
#ifdef __cplusplus
275
441
}
276
442
#endif
0 commit comments