Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
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 if( fscanf( data, "%d", &check_id ) != 1 || check_id !=211282 )
118 {
119 n_log( LOG_ERR, "file %s: invalid check_id of %d, should be 211282", resfile, check_id );
120 fclose( data );
121 return FALSE ;
122 }
123
124 ANIM_GFX *gfx = NULL ;
125 Malloc( gfx, ANIM_GFX, 1 );
126
127 if( fscanf( data, "%d %d %d", &gfx -> w, &gfx -> h, &gfx -> nb_frames ) != 3 )
128 {
129 n_log( LOG_ERR, "file %s: invalid &gfx -> w, &gfx -> h, &gfx -> nb_frames !", resfile );
130 fclose( data );
131 return FALSE ;
132 }
133
134 Malloc( gfx -> frames, ANIM_FRAME, gfx -> nb_frames );
135 gfx -> bmp = al_load_bitmap( file );
136 if( !gfx -> bmp )
137 {
138 n_log( LOG_ERR, "file %s: unable to load image", file );
139 fclose( data );
140 return FALSE ;
141 }
142
143 for( unsigned int it = 0 ; it < gfx -> nb_frames ; it ++ )
144 {
145 if( fscanf( data, "%d %d %d", &gfx -> frames[ it ].x, &gfx -> frames[ it ].y, &gfx -> frames[ it ] . duration ) != 3 )
146 {
147 n_log( LOG_ERR, "file %s: invalid &gfx -> frames[ %d ].x, &gfx -> frames[ %d ].y, &gfx -> frames[ %d ] . duration !", resfile, it, it, it);
148 fclose( data );
149 return FALSE ;
150 }
151 }
152 fclose( data );
153
154 lib -> gfxs[ pos ] = gfx ;
155
156 return TRUE ;
157} /* add_bmp_to_lib( ... ) */
158
159
166int update_anim( ANIM_DATA *data, unsigned int delta_t )
167{
168 __n_assert( data, return FALSE );
169
170 data -> elapsed += delta_t ;
171 if( data -> elapsed > data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ].duration )
172 {
173 data -> elapsed = 0 ;
174 data -> frame ++ ;
175 if( data -> frame >= data -> lib -> gfxs[ data -> id ] -> nb_frames )
176 data -> frame = 0 ;
177 }
178 return TRUE ;
179} /* update_anim( ... ) */
180
181
189int draw_anim( ANIM_DATA *data, int x, int y )
190{
191 ALLEGRO_BITMAP *bmp = data -> lib -> gfxs[ data -> id ] -> bmp ;
192
193
194 unsigned int tilew = data -> lib -> gfxs[ data -> id ] -> w ;
195 unsigned int tileh = data -> lib -> gfxs[ data -> id ] -> h ;
196
197 int px = data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ] . x ;
198 int py = data -> lib -> gfxs[ data -> id ] -> frames[ data -> frame ] . y ;
199
200 unsigned int framex = data -> frame * tilew ;
201
202 al_draw_bitmap_region( bmp, framex, 0, tilew, tileh, x - px, y - py, 0 );
203
204 return TRUE ;
205}
206/* 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:189
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:166
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:276
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:191
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:284
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:264
#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.