WIP file-based level loading

This commit is contained in:
Edgaru089 2024-04-11 17:01:22 +08:00
parent 9f60aa339b
commit 2f883d39b0
4 changed files with 62 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include "app.h"
#include "particle.h"
#include "types.h"
@ -12,5 +13,49 @@ void app_QueueLoadLevel(App *app, const char *level_name) {
app->switch_level = copy_malloc(level_name);
}
void _app_SwitchLevel(App *app) {
#define CMD1(str) if (strcmp(cmd, str) == 0)
#define CMD(str) else if (strcmd(cmd, str) == 0)
#define TOKEN (strtok(NULL, " "))
#define TOKEN_INT (atoi(TOKEN))
#define TOKEN_DOUBLE (strtod(TOKEN))
// Subsequent tokens can be read by strtok(NULL, " ")
static void _app_LevelCommand(char *cmd) {
}
// Defined in render_bundle_file.c
extern char linebuf[512];
void _app_SwitchLevel(App *app) {
if (app->switch_level == NULL) {
WARN("called when switch_level is NULL", 0);
return;
}
FILE *f = fopen(app->switch_level, "r");
if (!f) {
WARN("failed to open file\"%s\"", app->switch_level);
return;
}
// Clear the current level
entity_Clear(app->entity);
particle_Clear(app->particle);
// Read every line
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(cmd);
}
free(app->switch_level);
app->switch_level = NULL;
}

View File

@ -3,6 +3,7 @@
#include "types.h"
#include "util/tree.h"
#include "util/vector.h"
#include "util/assert.h"
#include <stdio.h>
@ -78,3 +79,14 @@ void particle_Emit(System_Particle *sys, Vec2 pos, Vec2 vec, double vec_friction
p->sizedec = sizedec;
p->mode = fill;
}
void particle_Clear(System_Particle *sys) {
vector_Clear(sys->deleted_ids);
// Delete every first node
int count = tree_Count(sys->parts);
while (count--)
tree_Delete(sys->parts, tree_FirstNode(sys->parts));
ASSERT(tree_Count(sys->parts) == 0);
}

View File

@ -46,6 +46,9 @@ void particle_Render(System_Particle *sys);
void particle_Emit(System_Particle *sys, Vec2 pos, Vec2 vec, double vec_friction, double size, double sizedec, Duration tolive, const FillMode *fill);
void particle_Delete(System_Particle *sys, uintptr_t id);
// Clear all particles.
void particle_Clear(System_Particle *sys);
#ifdef __cplusplus
}

View File

@ -110,7 +110,7 @@ static void _render_BundleCommand(char *cmd) {
}
static char linebuf[512];
char linebuf[512];
void render_LoadBundle(const char *filename) {
if (!render_Bundles)