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<Body *> other_bodies;
-void nocheckin_init_other_bodies()
-{
- other_bodies.ensure_capacity(1024);
-}
-
void
body_accumulate_move(Body* body, QuadTree<Body>* qt_bodies, f64 dt)
{
V2f force = { 0.0f, 0.0f };
+ thread_local persist Array<Body *> other_bodies;
other_bodies.clear();
qt_bodies->query(AABB { body->pos.x - 300, body->pos.y - 300, 600, 600 }, &other_bodies); // :ArbitraryConstant
#include "settings.h"
+#include <cstring>
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:
+ <file> = <line> '\n' <file>
+ = <line> '\n'?
+
+ <line> = '#' .*
+ | <setting>
+ | e
+
+ <setting> = ...
+ */
+
+ 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); };
fprintf(file, "interaction %d %d %f %f\n", i, j, br.distance_range, br.max_force);
}
}
+
+ return true;
}
void
};
}
}
+
+ save_settings_to_file(settings, "generated.settings");
}