From: Brendan Hansen Date: Sun, 22 Nov 2020 23:11:07 +0000 (-0600) Subject: added loading some settings X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=b56eb48723dc9bbf005bbaea7c170de17ebc98c5;p=csc718.git added loading some settings --- diff --git a/include/physics.h b/include/physics.h index d2ac939..6d76058 100644 --- a/include/physics.h +++ b/include/physics.h @@ -29,7 +29,6 @@ struct BodyRelation f32 max_force; }; -void nocheckin_init_other_bodies(); void body_accumulate_move(Body* body, QuadTree* qt_bodies, f64 dt); void body_apply_move(Body* body, f64 dt); diff --git a/include/settings.h b/include/settings.h index ec5b8d5..8fc46af 100644 --- a/include/settings.h +++ b/include/settings.h @@ -36,8 +36,8 @@ struct SimSettings extern SimSettings global_settings; -void load_settings_from_file(SimSettings* settings, const char* filename); -void save_settings_to_file(SimSettings* settings, const char* filename); +bool load_settings_from_file(SimSettings* settings, const char* filename); +bool save_settings_to_file(SimSettings* settings, const char* filename); void generate_random_settings(SimSettings* settings); #endif diff --git a/src/physics.cpp b/src/physics.cpp index 4270996..f7a6285 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -48,18 +48,12 @@ body_can_move(Body* body, const Array other_bodies, V2f d) return true; } -// TODO(Brendan): :Parallel This is used to allow for a statically allocated array for the quad tree results. This will need to be a thread local variable. -thread_local internal Array other_bodies; -void nocheckin_init_other_bodies() -{ - other_bodies.ensure_capacity(1024); -} - void body_accumulate_move(Body* body, QuadTree* qt_bodies, f64 dt) { V2f force = { 0.0f, 0.0f }; + thread_local persist Array other_bodies; other_bodies.clear(); qt_bodies->query(AABB { body->pos.x - 300, body->pos.y - 300, 600, 600 }, &other_bodies); // :ArbitraryConstant diff --git a/src/settings.cpp b/src/settings.cpp index 7833841..c731816 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1,24 +1,101 @@ #include "settings.h" +#include SimSettings global_settings; -void +internal bool +starts_with_and_advance(const char* pattern, char **str) +{ + if (!strncmp(pattern, *str, strlen(pattern))) + { + *str = *str + strlen(pattern); + return true; + } + else + { + return false; + } +} + +bool load_settings_from_file(SimSettings* settings, const char* filename) { FILE* file = fopen(filename, "r"); + if (file == NULL) + { + logprint(LOG_LEVEL_ERROR, "Failed to open file '%s' for loading settings.", filename); + return false; + } defer { fclose(file); }; + /* + BNF Grammar: + = '\n' + = '\n'? + + = '#' .* + | + | e + + = ... + */ + + bool hiterror = false; + char *rawline = NULL; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen = 0; + + while (!hiterror && ((linelen = getline(&rawline, &linesize, file)) != -1)) + { + line = rawline; + + // NOTE(Brendan): Empty line + if (linelen == 1) continue; + // NOTE(Brendan): Comment + if (line[0] == '#') continue; + + if (starts_with_and_advance("body_count", &line)) + { + settings->body_count = strtoll(line, &line, 10); + logprint(LOG_LEVEL_INFO, "Loaded setting: body_count = %ld", settings->body_count); + } + else if (starts_with_and_advance("thread_count", &line)) + { + settings->thread_count = strtoll(line, &line, 10); + logprint(LOG_LEVEL_INFO, "Loaded setting: thread_count = %ld", settings->thread_count); + } + else if (starts_with_and_advance("body_type_count", &line)) + { + settings->body_type_count = strtoll(line, &line, 10); + logprint(LOG_LEVEL_INFO, "Loaded setting: body_type_count = %ld", settings->body_type_count); + } + else if (starts_with_and_advance("friction", &line)) + { + settings->friction = strtof(line, &line); + logprint(LOG_LEVEL_INFO, "Loaded setting: friction = %f", settings->friction); + } + else if (starts_with_and_advance("universe_scale", &line)) + { + settings->universe_scale = strtof(line, &line); + logprint(LOG_LEVEL_INFO, "Loaded setting: universe_scale = %f", settings->universe_scale); + } + } + + if (rawline) free(rawline); + + return true; } -void +bool save_settings_to_file(SimSettings* settings, const char* filename) { FILE* file = fopen(filename, "w"); if (file == NULL) { logprint(LOG_LEVEL_ERROR, "Failed to open file '%s' for saving settings.", filename); - return; + return false; } defer { fclose(file); }; @@ -49,6 +126,8 @@ save_settings_to_file(SimSettings* settings, const char* filename) fprintf(file, "interaction %d %d %f %f\n", i, j, br.distance_range, br.max_force); } } + + return true; } void @@ -92,4 +171,6 @@ generate_random_settings(SimSettings* settings) }; } } + + save_settings_to_file(settings, "generated.settings"); } diff --git a/src/sim.cpp b/src/sim.cpp index fe49721..c78e4af 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -316,6 +316,7 @@ main(i32 argc, char* argv[]) { srand(time(NULL)); + load_settings_from_file(&global_settings, "dummy.settings"); generate_random_settings(&global_settings); init_glfw(); @@ -356,7 +357,6 @@ main(i32 argc, char* argv[]) glBindBuffer(GL_ARRAY_BUFFER, -1); } - nocheckin_init_other_bodies(); loop(state); return 0;