From 2f883d39b0d3a8ef7dfdbc4f456b207b9617b087 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Thu, 11 Apr 2024 17:01:22 +0800 Subject: [PATCH] WIP file-based level loading --- app_file.c | 47 +++++++++++++++++++++++++++++++++++++++++++- particle.c | 12 +++++++++++ particle.h | 3 +++ render_bundle_file.c | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/app_file.c b/app_file.c index c18e57a..3a73a22 100644 --- a/app_file.c +++ b/app_file.c @@ -1,6 +1,7 @@ #include #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; } diff --git a/particle.c b/particle.c index d739118..14b8415 100644 --- a/particle.c +++ b/particle.c @@ -3,6 +3,7 @@ #include "types.h" #include "util/tree.h" #include "util/vector.h" +#include "util/assert.h" #include @@ -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); +} diff --git a/particle.h b/particle.h index 3ce6813..9ad2234 100644 --- a/particle.h +++ b/particle.h @@ -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 } diff --git a/render_bundle_file.c b/render_bundle_file.c index 8a9734f..3ef075e 100644 --- a/render_bundle_file.c +++ b/render_bundle_file.c @@ -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)