palpic2png.c: improve, make usable with ppic binary files
[rofl0r-openDOW.git] / gameobj.c
blob2c8cfd0d0a6102403322b77e0ab381b6382df1c9
1 #include "gameobj.h"
2 #include <stddef.h>
3 #include <assert.h>
5 gameobj objs[OBJ_MAX];
6 uint8_t obj_slot_used[OBJ_MAX];
7 uint8_t obj_count;
8 // return -1 on error or gameobj slot id.
9 int gameobj_alloc(void) {
10 if(obj_count >= OBJ_MAX) return -1;
11 // fast path: try next slot
12 size_t i = obj_count;
13 if(!obj_slot_used[i]) {
14 return_slot:
15 obj_slot_used[i] = 1;
16 obj_count++;
17 return i;
20 for (i = 0; i < OBJ_MAX; i++) {
21 if(!obj_slot_used[i]) goto return_slot;
23 return -1;
26 void gameobj_free(int id) {
27 assert(obj_slot_used[id] == 1);
28 obj_slot_used[id] = 0;
29 obj_count--;
32 extern uint32_t tickcounter;
33 void gameobj_start_anim(int obj_id, enum animation_id aid) {
34 if(obj_id == -1) return;
35 objs[obj_id].animid = aid;
36 objs[obj_id].anim_curr = ANIM_STEP_INIT;
37 size_t tick_frames = objs[obj_id].objtype == OBJ_BIG_EXPLOSION ? 8 : 4;
38 objs[obj_id].anim_frame = tickcounter % tick_frames;
41 void gameobj_init(int id, vec2f *pos, vec2f* vel,
42 enum sprite_index spritemap_id,
43 enum animation_id animid, enum objtype objtype) {
44 if(id == -1) return;
45 struct gameobj *o = &objs[id];
46 o->pos = *pos;
47 o->vel = *vel;
48 o->spritemap_id = spritemap_id;
49 o->objtype = objtype;
50 gameobj_start_anim(id, animid);
53 void gameobj_init_bulletdata(int id, int steps) {
54 if(id == -1) return;
55 struct gameobj *o = &objs[id];
56 o->objspecific.bullet.step_curr = 0;
57 o->objspecific.bullet.step_max = steps;