Nilorea Library
C utilities for networking, threading, graphics
n_anim.c
Go to the documentation of this file.
1
8#include "nilorea/n_anim.h"
9
10#include "math.h"
11
12
19ANIM_LIB *create_anim_library( char *name, unsigned int size )
20{
21 if( size < 1 )
22 {
23 n_log( LOG_ERR, "Invalid size (<1) for anim lib creation" );
24 return NULL ;
25 }
26
27 ANIM_LIB *lib = NULL ;
28 Malloc( lib, ANIM_LIB, 1 );
29 __n_assert( lib, return NULL );
30
31 lib -> gfxs = NULL ;
32 Malloc( lib -> gfxs, ANIM_GFX *, size );
33 if( !lib -> gfxs )
34 {
35 n_log( LOG_ERR, "Unable to allocate a gfx_lib of size %d", size );
36 return NULL ;
37 }
38
39 if( name )
40 lib -> name = strdup( name );
41 else
42 lib -> name = strdup( "generic-name" );
43
44 lib -> nb_max_gfxs = size ;
45
46 return lib ;
47} /* create_anim_lib */
48
49
50
57{
58 __n_assert( (*lib), return FALSE );
59
60 for( uint32_t it = 0 ; it < (*lib) -> nb_max_gfxs ; it ++ )
61 {
62 FreeNoLog( (*lib) -> gfxs[ it ] );
63 }
64 Free( (*lib) );
65 return TRUE ;
66} /* destroy_anim_lib */
67
68
75int delete_bmp_from_lib( ANIM_LIB *lib, unsigned int id )
76{
77 __n_assert( lib, return FALSE );
78 __n_assert( lib -> gfxs, return FALSE );
79
80 __n_assert( id, return FALSE );
81 return TRUE ;
82} /* delete anim from lib */
83
84
85
94int add_bmp_to_lib( ANIM_LIB *lib, unsigned int pos, char *file, char *resfile )
95{
96 __n_assert( lib, return FALSE );
97 __n_assert( file, return FALSE );
98 __n_assert( resfile, return FALSE );
99
100 if( pos >= lib -> nb_max_gfxs )
101 {
102 n_log( LOG_ERR, "invalid position %d, can only go from 0 to %d in anim lib %s", pos, lib -> nb_max_gfxs, lib -> name );
103 return FALSE ;
104 }
105 if( lib -> gfxs[ pos ] != NULL )
106 {
107 n_log( LOG_ERR, "there already is a gfx at pos %d in anim lib %s", pos, lib -> name );
108 return FALSE ;
109 }
110
111 FILE *data = fopen( resfile,"r");
112 if( !data )
113 {
114 n_log( LOG_ERR, "Unable to open %s !", resfile );
115 }
116 int check_id ;
117 fscanf( data, "%d", &check_id );
118 if( check_id !=211282 )
119 {
120 n_log( LOG_ERR, "file %s: invalid check_id of %d, should be 211282", resfile, check_id );
121 fclose( data );
122 return FALSE ;
123 }
124
125 ANIM_GFX *gfx = NULL ;
126 Malloc( gfx, ANIM_GFX, 1 );
127
128 fscanf( data, "%d %d %d", &gfx -> w, &gfx -> h, &gfx -> nb_frames );
129
130 Malloc( gfx -> frames, ANIM_FRAME, gfx -> nb_frames );
131
132 gfx -> bmp = al_load_bitmap( file );
133 if( !gfx -> bmp )
134 {
135 n_log( LOG_ERR, "file %s: unable to load image", file );
136 fclose( data );
137 return FALSE ;
138 }
139
140 for( unsigned int it = 0 ; it < gfx -> nb_frames ; it ++ )
141 {
142 fscanf( data, "%d %d %d", &gfx -> frames[ it ].x, &gfx -> frames[ it ].y, &gfx -> frames[ it ] . duration );
143 }
144 fclose( data );
145
146 lib -> gfxs[ pos ] = gfx ;
147
148 return TRUE ;
149} /* add_bmp_to_lib( ... ) */
150
151
158int update_anim( ANIM_DATA *data, unsigned int delta_t )
159{
160 __n_assert( data, return FALSE );
161
162 data -> elapsed += delta_t ;
163 if( data -> elapsed > data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ].duration )
164 {
165 data -> elapsed = 0 ;
166 data -> frame ++ ;
167 if( data -> frame >= data -> lib -> gfxs[ data -> id ] -> nb_frames )
168 data -> frame = 0 ;
169 }
170 return TRUE ;
171} /* update_anim( ... ) */
172
173
181int draw_anim( ANIM_DATA *data, int x, int y )
182{
183 ALLEGRO_BITMAP *bmp = data -> lib -> gfxs[ data -> id ] -> bmp ;
184
185
186 unsigned int tilew = data -> lib -> gfxs[ data -> id ] -> w ;
187 unsigned int tileh = data -> lib -> gfxs[ data -> id ] -> h ;
188
189 int px = data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ] . x ;
190 int py = data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ] . y ;
191
192 unsigned int framex = data -> frame * tilew ;
193
194 al_draw_bitmap_region( bmp, framex, 0, tilew, tileh, x - px, y - py, 0 );
195
196 return TRUE ;
197}
198/* draw_anim( ... ) */
int delete_bmp_from_lib(ANIM_LIB *lib, unsigned int id)
Delete the frame at 'id' from 'lib'.
Definition: n_anim.c:75
int add_bmp_to_lib(ANIM_LIB *lib, unsigned int pos, char *file, char *resfile)
add a bitmap to a ANIM_LIB *lib
Definition: n_anim.c:94
int draw_anim(ANIM_DATA *data, int x, int y)
blit an ANIM_DATA at position x,y ( current selected video buffer is used )
Definition: n_anim.c:181
ANIM_LIB * create_anim_library(char *name, unsigned int size)
Allocate an animation library.
Definition: n_anim.c:19
int update_anim(ANIM_DATA *data, unsigned int delta_t)
compute and update an ANIM_DATA context based on elapsed delta_t time in usecs
Definition: n_anim.c:158
int destroy_anim_lib(ANIM_LIB **lib)
Destroy an animation library.
Definition: n_anim.c:56
animation properties
Definition: n_anim.h:67
struct of the properties of a frame in an animation
Definition: n_anim.h:27
struct of an animation
Definition: n_anim.h:35
structure of a library of gfxs
Definition: n_anim.h:53
#define FreeNoLog(__ptr)
Free Handler without log.
Definition: n_common.h:268
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition: n_common.h:183
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define Free(__ptr)
Free Handler to get errors.
Definition: n_common.h:256
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_ERR
error conditions
Definition: n_log.h:58
Animations graphics and animations parameters.