From: Brendan Hansen Date: Mon, 23 Nov 2020 00:56:05 +0000 (-0600) Subject: loading complete settings file X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=refs%2Fheads%2Fparallel;p=csc718.git loading complete settings file --- diff --git a/src/settings.cpp b/src/settings.cpp index c731816..ef034c3 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -45,10 +45,12 @@ load_settings_from_file(SimSettings* settings, const char* filename) char *line = NULL; size_t linesize = 0; ssize_t linelen = 0; + i32 linenum = 0; while (!hiterror && ((linelen = getline(&rawline, &linesize, file)) != -1)) { line = rawline; + linenum++; // NOTE(Brendan): Empty line if (linelen == 1) continue; @@ -70,6 +72,10 @@ load_settings_from_file(SimSettings* settings, const char* filename) { settings->body_type_count = strtoll(line, &line, 10); logprint(LOG_LEVEL_INFO, "Loaded setting: body_type_count = %ld", settings->body_type_count); + + settings->body_colors.ensure_capacity(settings->body_type_count); + settings->body_mass_range.ensure_capacity(settings->body_type_count); + settings->body_relations.ensure_capacity(settings->body_type_count * settings->body_type_count); } else if (starts_with_and_advance("friction", &line)) { @@ -81,11 +87,72 @@ load_settings_from_file(SimSettings* settings, const char* filename) settings->universe_scale = strtof(line, &line); logprint(LOG_LEVEL_INFO, "Loaded setting: universe_scale = %f", settings->universe_scale); } + else if (starts_with_and_advance("body_color", &line)) + { + i32 body_type = strtoll(line, &line, 10); + f32 red = strtof(line, &line); + f32 green = strtof(line, &line); + f32 blue = strtof(line, &line); + + if (body_type < 0 || body_type >= settings->body_type_count) + { + logprint(LOG_LEVEL_ERROR, "Invalid body type number: %d", body_type); + logprint(LOG_LEVEL_ERROR, "Expected a number between 0 and %d (inclusive)", settings->body_type_count); + return false; + } + + settings->body_colors[body_type] = BodyColor { red, green, blue }; + logprint(LOG_LEVEL_INFO, "Loaded setting: body_color[%d] = { %f, %f, %f }", body_type, red, green, blue); + } + else if (starts_with_and_advance("body_mass_range", &line)) + { + i32 body_type = strtoll(line, &line, 10); + f32 min = strtof(line, &line); + f32 max = strtof(line, &line); + + if (body_type < 0 || body_type >= settings->body_type_count) + { + logprint(LOG_LEVEL_ERROR, "Invalid body type number: %d", body_type); + logprint(LOG_LEVEL_ERROR, "Expected a number between 0 and %d (inclusive)", settings->body_type_count); + return false; + } + + settings->body_mass_range[body_type] = BodyMassRange { min, max }; + logprint(LOG_LEVEL_INFO, "Loaded setting: body_color[%d] = { %f, %f }", body_type, min, max); + } + else if (starts_with_and_advance("interaction", &line)) + { + i32 body_type = strtoll(line, &line, 10); + i32 other_type = strtoll(line, &line, 10); + f32 dist = strtof(line, &line); + f32 force = strtof(line, &line); + + if (body_type < 0 || body_type >= settings->body_type_count) + { + logprint(LOG_LEVEL_ERROR, "Invalid body type number: %d", body_type); + logprint(LOG_LEVEL_ERROR, "Expected a number between 0 and %d (inclusive)", settings->body_type_count); + return false; + } + if (other_type < 0 || other_type >= settings->body_type_count) + { + logprint(LOG_LEVEL_ERROR, "Invalid body type number: %d", body_type); + logprint(LOG_LEVEL_ERROR, "Expected a number between 0 and %d (inclusive)", settings->body_type_count); + return false; + } + + settings->body_relations[body_type * settings->body_type_count + other_type] = BodyRelation { dist, force }; + logprint(LOG_LEVEL_INFO, "Loaded setting: interaction[%d][%d] = { %f, %f }", body_type, other_type, dist, force); + } + else + { + logprint(LOG_LEVEL_ERROR, "Invalid syntax on line %d\n", linenum); + hiterror = true; + } } if (rawline) free(rawline); - return true; + return !hiterror; } bool diff --git a/src/sim.cpp b/src/sim.cpp index c78e4af..638be06 100644 --- a/src/sim.cpp +++ b/src/sim.cpp @@ -316,8 +316,14 @@ main(i32 argc, char* argv[]) { srand(time(NULL)); - load_settings_from_file(&global_settings, "dummy.settings"); - generate_random_settings(&global_settings); + if (argc > 1) + { + load_settings_from_file(&global_settings, argv[1]); + } + else + { + generate_random_settings(&global_settings); + } init_glfw(); defer { deinit_glfw(); };