-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentity.c
401 lines (359 loc) · 11.4 KB
/
entity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <SDL2/SDL.h>
#include "list.h"
#include "entity.h"
static lua_State *L; //there can be only one
struct entity {
char *name; //used for lookup
SDL_Texture *draw; //graphic to draw (in memory) TODO: read from db, not local
SDL_Rect pos; //x, y, w, h (dst rect)
SDL_Rect anim; //animation frame (src rect from draw)
const char *dialogue;
List *components; //list of components to check for, of type Component
};
typedef struct event Event;
struct event {
char *event_name;
char *input_type;
char *input_name;
};
typedef struct component Component;
struct component {
int luaref;
List *inputs; //list of Event* structs
Entity *parent;
};
//no global Entities list bc different levels, etc. may have different entity stores
//but there is a global list of components and it's a hard array
static Component **components = NULL; //init size of 2048, but will be realloced if needed
static int components_index = 0;
static int components_max = 2048; //keep track of storage space
Component *push_component(int ref, List *inputs, Entity *parent) {
if(!components) {
components = malloc(sizeof(Component*) * components_max);
}
if(components_index + 1 > components_max) {
components_max *= 2;
components = realloc(components, sizeof(Component*) * components_max);
}
if(!components) {
printf("Error: Allocation failed for components array!\n");
exit(1);
}
components[components_index] = malloc(sizeof(Component));
components[components_index]->luaref = ref;
components[components_index]->inputs = inputs;
components[components_index]->parent = parent;
components_index++;
return components[components_index-1];
}
static void stackDump (lua_State *L) {
int i;
int top = lua_gettop(L);
printf("Lua: Stack dump:");
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(L, i));
break;
default: /* other values */
printf("%s", lua_typename(L, t));
break;
}
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
}
void init_entities() {
L = lua_open();
luaL_openlibs(L);
//TODO: register 'locate_entity' function
}
static List *register_inputs(char *filename) {
printf("Registering from %s.\n", filename);
List *inputs = new_list();
FILE *fp = fopen(filename, "r");
char line[2048]; //stack hog
while(fgets(line, 2048, fp)) {
char *event_name = malloc(sizeof(char)*256); //heap hog
char *input_type = malloc(sizeof(char)*16);
char *input_name = malloc(sizeof(char)*16);
printf("Event: %s", line);
if(line[0] == '#') //skip comments
continue;
sscanf(line, "%s %s %s", event_name, input_type, input_name);
printf("Event confirmed: %s: %s %s\n", event_name, input_type, input_name);
Event *new_event = malloc(sizeof(Event));
new_event->event_name = event_name;
new_event->input_type = input_type;
new_event->input_name = input_name;
printf("Loaded event.\n");
add_item(inputs, new_event);
printf("Added to events list.\n");
}
return inputs;
}
//TODO: cache these somewhere
Component *load_component(char *name, Entity *parent) {
char script_name[256]; //way big filename but whatever
if(strlen(name) + 16 > 256) {
printf("Error: Component name exceeds buffer length!\n");
return NULL;
}
strcpy(script_name, "objects/");
strcat(script_name, name);
strcat(script_name, ".lua");
printf("Reading %s...\n", script_name);
if(luaL_loadfile(L, script_name) || lua_pcall(L, 0, 0, 0)) {
printf("Error: Cannot load script file for %s\n.", name);
}
printf("Lua: Setting metatable.\n");
//get the metatable
lua_getglobal(L, name); //get the component table, pushes it to -1
if(lua_isnil(L, -1)) {
printf("Error: %s table is nil!\n", name);
}
lua_pushvalue(L, -1); //get the component table again
lua_setfield(L, -2, "__index"); //component.__index = component; make into metatable
lua_newtable(L); //-1 is {}, pushes 'component' metatable to -2
lua_pushvalue(L, -2); //get the metatable to the top
lua_setmetatable(L, -2); //metatable's copy is subsequently popped
//-1 is now the new table, but metatable set properly
printf("Collecting reference to component.\n");
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
List *events = NULL;
lua_getfield(L, -1, "inputs");
if(!lua_isnil(L, -1)) {
printf("Lua: Script has inputs. Registering them...\n");
char inputs_filename[256];
strcpy(inputs_filename, "objects/");
strcat(inputs_filename, name);
strcat(inputs_filename, ".inputs");
events = register_inputs(inputs_filename);
}
printf("Pushing component...\n");
Component *comp = push_component(ref, events, parent);//store a reference so that we can keep the stack clean
lua_pop(L, 2); //pop the metatable, stack returns to 0
stackDump(L);
return comp;
}
static void set_parent(int luaref, Entity *parent) {
lua_rawgeti(L, LUA_REGISTRYINDEX, luaref); //grab the table
lua_newtable(L); //create 'parent' table
lua_newtable(L); //create 'pos' table
lua_pushnumber(L, parent->pos.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, parent->pos.y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, parent->pos.w);
lua_setfield(L, -2, "w");
lua_pushnumber(L, parent->pos.h);
lua_setfield(L, -2, "h");
lua_setfield(L, -2, "pos");
lua_newtable(L); //create 'anim' table
lua_pushnumber(L, parent->anim.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, parent->anim.y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, parent->anim.w);
lua_setfield(L, -2, "w");
lua_pushnumber(L, parent->anim.h);
lua_setfield(L, -2, "h");
lua_setfield(L, -2, "anim");
lua_setfield(L, -2, "parent"); //pops new table
lua_pop(L, 1); //pop the table
}
Entity *new_entity(char *name, SDL_Renderer *renderer, int x, int y) {
printf("Creating new entity in memory...\n");
Entity *new = malloc(sizeof(Entity));
new->pos.x = x;
new->pos.y = y;
new->components = new_list();
char entpath[256];
strcpy(entpath, "objects/");
strcat(entpath, name);
strcat(entpath, ".ent");
FILE *entfile = fopen(entpath, "r");
if(!entfile) {
printf("Could not read %s.\n", name);
}
char line[2048];
//get sprite
printf("Loading sprite...\n");
fgets(line, 2048, entfile);
line[strcspn(line, "\n")] = 0;
char spritepath[256];
strcpy(spritepath, "objects/");
strcat(spritepath, line);
SDL_Surface *s = SDL_LoadBMP(spritepath);
if(!s) {
printf("Loading %s fail! SDL_Error: %s\n", spritepath, SDL_GetError());
}
SDL_SetColorKey(s, SDL_TRUE, SDL_MapRGB(s->format, 0xFF, 0x00, 0xFF));
new->pos.w = s->w;
new->pos.h = s->h;
new->anim.w = s->w;
new->anim.h = s->h;
new->draw = SDL_CreateTextureFromSurface(renderer, s);
if(!new->draw) {
printf("Creating entity texture fail! SDL_Error: %s\n", SDL_GetError());
}
SDL_FreeSurface(s);
printf("Begin loading components...\n");
//now loop through and get components
while(fgets(line, 2048, entfile)) {
line[strcspn(line, "\n")] = 0; //remove trailing newline
char path[256];
strcpy(path, "objects/");
strcat(path, line);
printf("Loading component %s.\n", path);
Component *comp = load_component(line, new);
printf("Component loaded. Adding to current entity...\n");
//inject 'parent' table
set_parent(comp->luaref, new);
add_item(new->components, comp);
}
printf("Entity component list populated.\n");
return new;
}
void update_component(Component *component) {
//printf("Updating id %d.\n", ref);
//stackDump(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, component->luaref); //grab the table
//push parent vars
lua_getfield(L, -1, "parent");
lua_getfield(L, -1, "pos");
lua_getfield(L, -1, "x");
if(lua_isnumber(L, -1)) {
component->parent->pos.x = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "y");
if(lua_isnumber(L, -1)) {
component->parent->pos.y = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "w");
if(lua_isnumber(L, -1)) {
component->parent->pos.w = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "h");
if(lua_isnumber(L, -1)) {
component->parent->pos.h = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_pop(L, 1); //pop 'pos'
lua_getfield(L, -1, "anim");
lua_getfield(L, -1, "x");
if(lua_isnumber(L, -1)) {
component->parent->anim.x = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "y");
if(lua_isnumber(L, -1)) {
component->parent->anim.y = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "w");
if(lua_isnumber(L, -1)) {
component->parent->anim.w = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_getfield(L, -1, "h");
if(lua_isnumber(L, -1)) {
component->parent->anim.h = lua_tonumber(L, -1);
}
lua_pop(L, 1);
lua_pop(L, 1); //pop 'anim'
lua_pop(L, 1); //pop parent
//call update function
lua_getfield(L, -1, "update");
lua_pushvalue(L, -2); //put the needed table at the top of the stack for the 'self' reference
//State, argc, retc, errfunc
if(lua_pcall(L, 1, 0, 0) != 0) {
printf("error: %s\n", lua_tostring(L, -1));
}
//lua_pcall pops the 'update function', so no explicit lua_pop required
lua_pop(L, 1); //pop the table
}
//to be run every update cycle in main
void update_components() {
for(int i = 0; i < components_index; ++i) {
update_component(components[i]);
}
}
static const char *get_event_from_input(List *events, const char *input_name) {
Node *i = NULL;
while(i = next(events, i)) {
Event *ev = item(i);
if(strcmp(ev->input_name, input_name) == 0) {
return ev->event_name;
}
}
return "";
}
//to be run in input catching loop in main
void dispatch_events(SDL_Event e) {
for(int i = 0; i < components_index; ++i) {
lua_rawgeti(L, LUA_REGISTRYINDEX, components[i]->luaref); //pushes component table to stack
if(!components[i]->inputs) {
lua_pop(L, 1); //pop component table
continue; //NO INPUTS, SO SKIP IT
}
lua_getfield(L, -1, "inputs"); //pushes "inputs" table
if(!lua_isnil(L, -1)) { //double-checking, highly unnecessary
const char *ev = get_event_from_input(components[i]->inputs, SDL_GetKeyName(e.key.keysym.sym));
if(strcmp(ev, "") == 0) {
lua_pop(L, 2); //pop inputs table + component table
continue; //component doesn't respond to the current event, so skip it
}
if(e.type == SDL_KEYDOWN) {
lua_getfield(L, -1, ev); //get the appropriate event from inputs table
if(!lua_isnil(L, -1)) {
lua_pop(L, 1); //pop the value, if it's not nil we don't need it
lua_pushboolean(L, 1);
lua_setfield(L, -2, ev); //set it here
}
} else if(e.type == SDL_KEYUP) {
lua_getfield(L, -1, ev); //get the appropriate event from inputs table
if(!lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_pushboolean(L, 0);
lua_setfield(L, -2, ev);
}
}
}
lua_pop(L, 2); //pop the current component + inputs table
}
}
void draw_entity(Entity *ent, SDL_Renderer *renderer) {
SDL_RenderCopy(renderer, ent->draw, &(ent->anim), &(ent->pos));
}
void free_entities(List *entities) {
for(Node *ei = NULL; ei; ei = next(entities, ei)) {
Entity *e = item(ei);
for(Node *ci = NULL; ci; ci = next(e->components, ci)) {
Component *c = item(ci);
for(Node *ii = NULL; ii; ii = next(c->inputs, ii)) {
Event *i = item(ii);
free(i->event_name);
free(i->input_type);
free(i->input_name);
free(i);
}
free_list(c->inputs);
free(c);
}
free_list(e->components);
SDL_DestroyTexture(e->draw);
free(e);
}
free_list(entities);
}