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
18ANIM_LIB* create_anim_library(char* name, unsigned int size) {
19 if (size < 1) {
20 n_log(LOG_ERR, "Invalid size (<1) for anim lib creation");
21 return NULL;
22 }
23
24 ANIM_LIB* lib = NULL;
25 Malloc(lib, ANIM_LIB, 1);
26 __n_assert(lib, return NULL);
27
28 lib->gfxs = NULL;
29 Malloc(lib->gfxs, ANIM_GFX*, size);
30 if (!lib->gfxs) {
31 n_log(LOG_ERR, "Unable to allocate a gfx_lib of size %d", size);
32 return NULL;
33 }
34
35 if (name)
36 lib->name = strdup(name);
37 else
38 lib->name = strdup("generic-name");
39
40 lib->nb_max_gfxs = size;
41
42 return lib;
43} /* create_anim_lib */
44
51 __n_assert((*lib), return FALSE);
52
53 for (uint32_t it = 0; it < (*lib)->nb_max_gfxs; it++) {
54 FreeNoLog((*lib)->gfxs[it]);
55 }
56 Free((*lib));
57 return TRUE;
58} /* destroy_anim_lib */
59
66int delete_bmp_from_lib(ANIM_LIB* lib, unsigned int id) {
67 __n_assert(lib, return FALSE);
68 __n_assert(lib->gfxs, return FALSE);
69
70 __n_assert(id, return FALSE);
71 return TRUE;
72} /* delete anim from lib */
73
82int add_bmp_to_lib(ANIM_LIB* lib, unsigned int pos, char* file, char* resfile) {
83 __n_assert(lib, return FALSE);
84 __n_assert(file, return FALSE);
85 __n_assert(resfile, return FALSE);
86
87 if (pos >= lib->nb_max_gfxs) {
88 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);
89 return FALSE;
90 }
91 if (lib->gfxs[pos] != NULL) {
92 n_log(LOG_ERR, "there already is a gfx at pos %d in anim lib %s", pos, lib->name);
93 return FALSE;
94 }
95
96 FILE* data = fopen(resfile, "r");
97 if (!data) {
98 n_log(LOG_ERR, "Unable to open %s !", resfile);
99 return FALSE;
100 }
101
102 int check_id = 0;
103 int check_fscanf = 0;
104 ANIM_GFX* gfx = NULL;
105
106 check_fscanf = fscanf(data, "%d", &check_id);
107 if (check_fscanf == 1 && check_id == 211282) {
108 Malloc(gfx, ANIM_GFX, 1);
109 __n_assert(gfx, fclose(data); return FALSE);
110 } else {
111 n_log(LOG_ERR, "file %s: invalid check_id of %d, should be 211282", resfile, check_id);
112 fclose(data);
113 return FALSE;
114 }
115
116 if (fscanf(data, "%d %d %d", &gfx->w, &gfx->h, &gfx->nb_frames) != 3) {
117 n_log(LOG_ERR, "file %s: invalid &gfx -> w, &gfx -> h, &gfx -> nb_frames !", resfile);
118 Free(gfx);
119 fclose(data);
120 return FALSE;
121 }
122
123 if (gfx->nb_frames == 0 || gfx->nb_frames >= UINT_MAX) {
124 n_log(LOG_ERR, "file %s: invalid number of frames: %u", resfile, gfx->nb_frames);
125 Free(gfx);
126 fclose(data);
127 return FALSE;
128 }
129
130 Malloc(gfx->frames, ANIM_FRAME, gfx->nb_frames);
131 if (!gfx->frames) {
132 n_log(LOG_ERR, "file %s: could not allocate %d frames", resfile, gfx->nb_frames);
133 Free(gfx);
134 fclose(data);
135 return FALSE;
136 }
137
138 gfx->bmp = al_load_bitmap(file);
139 if (!gfx->bmp) {
140 n_log(LOG_ERR, "file %s: unable to load image", file);
141 Free(gfx);
142 fclose(data);
143 return FALSE;
144 }
145
146 for (unsigned int it = 0; it < gfx->nb_frames; it++) {
147 if (fscanf(data, "%d %d %d", &gfx->frames[it].x, &gfx->frames[it].y, &gfx->frames[it].duration) != 3) {
148 n_log(LOG_ERR, "file %s: invalid &gfx -> frames[ %d ].x, &gfx -> frames[ %d ].y, &gfx -> frames[ %d ] . duration !", resfile, it, it, it);
149 Free(gfx);
150 fclose(data);
151 return FALSE;
152 }
153 }
154 fclose(data);
155
156 lib->gfxs[pos] = gfx;
157
158 return TRUE;
159} /* add_bmp_to_lib( ... ) */
160
167int update_anim(ANIM_DATA* data, unsigned int delta_t) {
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 data->elapsed = 0;
173 data->frame++;
174 if (data->frame >= data->lib->gfxs[data->id]->nb_frames)
175 data->frame = 0;
176 }
177 return TRUE;
178} /* update_anim( ... ) */
179
187int draw_anim(ANIM_DATA* data, int x, int y) {
188 ALLEGRO_BITMAP* bmp = data->lib->gfxs[data->id]->bmp;
189
190 unsigned int tilew = data->lib->gfxs[data->id]->w;
191 unsigned int tileh = data->lib->gfxs[data->id]->h;
192
193 int px = data->lib->gfxs[data->id]->frames[data->frame].x;
194 int py = data->lib->gfxs[data->id]->frames[data->frame].y;
195
196 unsigned int framex = data->frame * tilew;
197
198 al_draw_bitmap_region(bmp, framex, 0, tilew, tileh, x - px, y - py, 0);
199
200 return TRUE;
201}
202/* draw_anim( ... ) */
unsigned int nb_frames
nb frames in anim
Definition n_anim.h:46
unsigned int w
width of a frame
Definition n_anim.h:42
unsigned int elapsed
elapsed time since last frame change
Definition n_anim.h:71
char * name
name of the anim library
Definition n_anim.h:58
ANIM_GFX ** gfxs
Stack of gfxs.
Definition n_anim.h:52
unsigned int h
height of a frame
Definition n_anim.h:44
ANIM_FRAME * frames
each frame properties
Definition n_anim.h:35
unsigned int frame
id of the current frame
Definition n_anim.h:69
ALLEGRO_BITMAP * bmp
bitmap with a list of lil' bitmap inside
Definition n_anim.h:37
ANIM_LIB * lib
pointer to an anim gfx library
Definition n_anim.h:74
unsigned int nb_max_gfxs
size of the stack
Definition n_anim.h:55
int delete_bmp_from_lib(ANIM_LIB *lib, unsigned int id)
Delete the frame at 'id' from 'lib'.
Definition n_anim.c:66
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:82
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:187
ANIM_LIB * create_anim_library(char *name, unsigned int size)
Allocate an animation library.
Definition n_anim.c:18
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:167
int destroy_anim_lib(ANIM_LIB **lib)
Destroy an animation library.
Definition n_anim.c:50
animation properties
Definition n_anim.h:62
struct of the properties of a frame in an animation
Definition n_anim.h:26
struct of an animation
Definition n_anim.h:33
structure of a library of gfxs
Definition n_anim.h:50
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:251
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:187
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:258
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:242
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:69
#define LOG_ERR
error conditions
Definition n_log.h:56
Animations graphics and animations parameters.