You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.8 KiB
123 lines
3.8 KiB
#include "physics.h" |
|
#include "simulation.h" |
|
#include "config_loader.h" |
|
#include "renderer.h" |
|
#include <cstdio> |
|
#include <cstring> |
|
#include <cstdlib> |
|
#include <cmath> |
|
|
|
// Configuration defaults - edit to change default run mode |
|
//#define DEFAULT_CONFIG_FILE "configs/test_simple.toml" |
|
#define DEFAULT_CONFIG_FILE "tests/configs/solar_system.toml" |
|
|
|
struct ProgramArgs { |
|
const char* config_file; |
|
}; |
|
|
|
void parse_command_line_args(int argc, char** argv, ProgramArgs* args) { |
|
args->config_file = DEFAULT_CONFIG_FILE; |
|
|
|
for (int i = 1; i < argc; i++) { |
|
if (argv[i][0] != '-') { |
|
args->config_file = argv[i]; |
|
} |
|
} |
|
} |
|
|
|
void print_startup_info(const ProgramArgs* args) { |
|
printf("=== Orbital Mechanics Simulation ===\n"); |
|
printf("Loading configuration: %s\n", args->config_file); |
|
} |
|
|
|
void run_gui_simulation(SimulationState* sim) { |
|
init_renderer(1600, 900, "Orbital Mechanics Simulation"); |
|
|
|
RenderState render_state; |
|
setup_camera(&render_state); |
|
|
|
// Find root body (body with parent_index == -1) |
|
int root_body_index = -1; |
|
for (int i = 0; i < sim->body_count; i++) { |
|
if (sim->bodies[i].parent_index == -1) { |
|
root_body_index = i; |
|
break; |
|
} |
|
} |
|
|
|
// Initialize UI state |
|
render_state.selected_body_index = root_body_index >= 0 ? root_body_index : -1; |
|
render_state.previous_selected_body = -1; // Previous selected body index |
|
render_state.body_list_scroll = 0; // Initial scroll position |
|
render_state.body_list_active = root_body_index >= 0 ? root_body_index : -1; // Sync with selected body |
|
render_state.camera_follow_body = root_body_index >= 0; // Follow root body if found |
|
render_state.camera_offset = (Vector3){ 0, 50, 100 }; // Default offset |
|
render_state.was_following_body = false; |
|
|
|
bool paused = false; |
|
double speed_multiplier = 1.0; |
|
int physics_steps_per_frame = 100; |
|
|
|
printf("\nSimulation started!\n"); |
|
printf("Controls:\n"); |
|
printf(" Arrow keys: Rotate and zoom camera\n"); |
|
printf(" Space: Pause/Resume\n"); |
|
printf(" +/-: Speed up/slow down simulation\n"); |
|
printf(" Select body from list to follow it\n"); |
|
printf(" ESC: Quit\n\n"); |
|
|
|
while (!WindowShouldClose()) { |
|
if (IsKeyPressed(KEY_SPACE)) { |
|
paused = !paused; |
|
printf("Simulation %s\n", paused ? "paused" : "resumed"); |
|
} |
|
|
|
if (IsKeyPressed(KEY_EQUAL) || IsKeyPressed(KEY_KP_ADD)) { |
|
speed_multiplier *= 2.0; |
|
printf("Speed multiplier: %.1fx\n", speed_multiplier); |
|
} |
|
|
|
if (IsKeyPressed(KEY_MINUS) || IsKeyPressed(KEY_KP_SUBTRACT)) { |
|
speed_multiplier /= 2.0; |
|
if (speed_multiplier < 0.125) speed_multiplier = 0.125; |
|
printf("Speed multiplier: %.1fx\n", speed_multiplier); |
|
} |
|
|
|
if (!paused) { |
|
int steps = (int)(physics_steps_per_frame * speed_multiplier); |
|
for (int i = 0; i < steps; i++) { |
|
update_simulation(sim); |
|
} |
|
} |
|
|
|
update_camera(&render_state, sim); |
|
|
|
render_simulation(sim, &render_state); |
|
} |
|
|
|
close_renderer(); |
|
printf("\nSimulation ended. Final time: %.2f days\n", sim->time / 86400.0); |
|
} |
|
|
|
int main(int argc, char** argv) { |
|
ProgramArgs args; |
|
parse_command_line_args(argc, argv, &args); |
|
print_startup_info(&args); |
|
|
|
// Create simulation with time step of 60 seconds |
|
const int MAX_BODIES = 100; |
|
const double TIME_STEP = 60.0; // 60 seconds per step |
|
SimulationState* sim = create_simulation(MAX_BODIES, TIME_STEP); |
|
|
|
// Load system configuration |
|
if (!load_system_config(sim, args.config_file)) { |
|
printf("Failed to load configuration file\n"); |
|
destroy_simulation(sim); |
|
return 1; |
|
} |
|
|
|
run_gui_simulation(sim); |
|
|
|
destroy_simulation(sim); |
|
return 0; |
|
}
|
|
|