loading complete settings file parallel
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 23 Nov 2020 00:56:05 +0000 (18:56 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 23 Nov 2020 00:56:05 +0000 (18:56 -0600)
src/settings.cpp
src/sim.cpp

index c7318160744edfefe04699b96569fa22de97ceee..ef034c303ba164e3ceeec9b37f08f362bade538a 100644 (file)
@@ -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
index c78e4afd80e16a616c4397961cf4043e212430c8..638be06eb4a8b43f271884c40ab5b49a75c006d7 100644 (file)
@@ -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(); };