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