-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_file.c
262 lines (218 loc) · 5.93 KB
/
app_file.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
#include <stdio.h>
#include "app.h"
#include "entity.h"
#include "mapper_misc.h"
#include "particle.h"
#include "physics.h"
#include "render_component.h"
#include "types.h"
#include "util/vector.h"
#include "util/assert.h"
void app_QueueLoadLevel(App *app, const char *level_name) {
if (app->switch_level != NULL) {
WARN("previous switch_level \"%s\" not processed; purged", app->switch_level);
free(app->switch_level);
}
app->switch_level = copy_malloc(level_name);
}
#define CMD1(str) if (strcmp(cmd, str) == 0)
#define CMD(str) else if (strcmp(cmd, str) == 0)
#define TOKEN (strtok(NULL, " "))
#define TOKEN_INT (atoi(TOKEN))
#define TOKEN_DOUBLE (strtod(TOKEN, NULL))
static vector_Vector *charbuf;
static void _app_UnescapeTextbox(char *t) {
ASSERT(t && "only can be called with t!=NULL");
if (!charbuf)
charbuf = vector_Create(sizeof(char));
else
vector_Clear(charbuf);
size_t len = strlen(t);
const char bs = '\\', space = ' ', newline = '\n', tab = '\t', zero = '\0';
for (int i = 0; i < len; i++) {
if (t[i] == '\\') {
if (i == len - 1)
vector_Push(charbuf, &bs); // Weird case
else {
if (t[i + 1] == 's')
vector_Push(charbuf, &space);
else if (t[i + 1] == 'n')
vector_Push(charbuf, &newline);
else if (t[i + 1] == 't')
vector_Push(charbuf, &tab);
i++;
}
} else
vector_Push(charbuf, &t[i]);
}
vector_Push(charbuf, &zero);
}
static inline Vec2 readvec2() {
double a, b;
a = TOKEN_DOUBLE;
b = TOKEN_DOUBLE;
Vec2 v = {.x = a, .y = b};
return v;
}
static inline Box2 readbox2() {
Vec2 a, b;
a = readvec2();
b = readvec2();
Box2 box = {.lefttop = a, .size = b};
return box;
}
static inline uint32_t readcolor() {
int r, g, b;
r = TOKEN_INT;
g = TOKEN_INT;
b = TOKEN_INT;
return (uint32_t)((r) | (g << 8) | (b << 16));
}
// Subsequent tokens can be read by strtok(NULL, " ")
static void _app_LevelCommand(App *app, char *cmd) {
CMD1("HITBOX") {
Box2 box = readbox2();
Entity *e = entity_Create(app->entity, cmd);
ADD_COMPONENT(e, hitbox);
e->hitbox->box = box;
e->hitbox->fixed = true;
entity_Commit(app->entity, e);
}
CMD("PLAYER") {
Vec2 vec = readvec2();
Entity *e = entity_Create(app->entity, cmd);
ADD_COMPONENT(e, player);
e->player->hazardRespawn = vec;
ADD_COMPONENT(e, position);
e->position->position = vec;
e->position->velocity = vec2(0, 0);
ADD_COMPONENT(e, hitbox);
e->hitbox->box.lefttop = vec2(-20, -50);
e->hitbox->box.size = vec2(40, 50);
entity_Commit(app->entity, e);
}
CMD("HAZARD_RESPAWN") {
Box2 box = readbox2();
Vec2 vec = readvec2();
Entity *e = entity_Create(app->entity, cmd);
misc_InstantiateHazardRespawn(app, e, box, vec);
entity_Commit(app->entity, e);
}
CMD("HAZARD") {
Box2 box = readbox2();
Entity *e = entity_Create(app->entity, cmd);
misc_InstantiateHazard(app, e, box);
entity_Commit(app->entity, e);
}
CMD("TEXTBOX") {
double a, b, c, d;
a = TOKEN_DOUBLE;
b = TOKEN_DOUBLE;
c = TOKEN_DOUBLE;
d = TOKEN_DOUBLE;
Entity *e = entity_Create(app->entity, cmd);
char *bundle = TOKEN;
if (bundle != NULL)
e->render = render_NewComponent(e, bundle);
// We need to compute a position element
Vec2 position = {
.x = a + c / 2.0,
.y = b + d};
ADD_COMPONENT(e, position);
e->position->position = position;
e->position->velocity = vec2(0.0, 0.0);
char *text_raw = TOKEN;
if (text_raw) {
_app_UnescapeTextbox(text_raw);
misc_InstantiateTextbox(app, e, vector_Data(charbuf), box2(-c / 2.0, -d, c, d), (-d - 40));
}
entity_Commit(app->entity, e);
}
CMD("LEVEL_TRANSITION") {
Box2 box = readbox2();
char *next_level = TOKEN;
if (next_level) {
Entity *e = entity_Create(app->entity, cmd);
misc_InstantiateChangeLevel(app, e, box, next_level);
entity_Commit(app->entity, e);
}
}
CMD("CAMERA_FOCUS") {
Box2 box = readbox2();
Entity *e = entity_Create(app->entity, cmd);
misc_InstantiateCameraFocus(app, e, box);
entity_Commit(app->entity, e);
}
CMD("PUT_CAMERA") {
Vec2 center = readvec2();
app->camera->cam = box2_FromCenter(center, app->camera->screen.size);
}
CMD("CUTOFF") {
app->player->cutoff = TOKEN_DOUBLE;
}
CMD("BACKGROUND") {
app->clear_color = readcolor();
}
CMD("FILL") {
uint32_t color = readcolor();
Box2 box = readbox2();
Entity *e = entity_Create(app->entity, cmd);
ADD_COMPONENT(e, render);
e->render->fillbox = box;
e->render->fillcolor = color;
}
CMD("FILLPOLY") {
uint32_t color = readcolor();
int n = TOKEN_INT;
// Allocate a Vector
vector_Vector *vec = vector_Create(sizeof(Vec2));
vector_Reserve(vec, n);
// Read N points
for (int i = 0; i < n; i++) {
Vec2 point = readvec2();
vector_Push(vec, &point);
}
Entity *e = entity_Create(app->entity, cmd);
ADD_COMPONENT(e, render);
e->render->fillpoly = vec; // Deallocated in render_DeleteComponent
e->render->fillcolor = color;
}
else {
WARN("unknown command \"%s\"", cmd);
}
}
void _app_SwitchLevel(App *app) {
if (app->switch_level == NULL) {
WARN("called when switch_level is NULL");
return;
}
INFO("Switching level to %s", app->switch_level);
FILE *f = fopen(app->switch_level, "r");
if (!f) {
WARN("failed to open file\"%s\"", app->switch_level);
free(app->switch_level);
app->switch_level = NULL;
return;
}
// Clear the current level
entity_Clear(app->entity);
particle_Clear(app->particle);
app->camera->target = NULL;
app->level_playtime.microseconds = 0;
// Read every line
char linebuf[512];
memset(linebuf, 0, sizeof(linebuf));
while (!feof(f) && fgets(linebuf, sizeof(linebuf), f)) {
while (linebuf[strlen(linebuf) - 1] == '\n')
linebuf[strlen(linebuf) - 1] = '\0';
char *cmd = strtok(linebuf, " ");
if (cmd == NULL)
continue;
_app_LevelCommand(app, cmd);
}
if (app->current_level)
free(app->current_level);
app->current_level = app->switch_level;
app->switch_level = NULL;
fclose(f);
}