Browse Source

Add headless mode for terminal-based simulation

- Add --headless/-h flag to run without graphics
- Add --days N flag to specify simulation duration
- Print initial state, periodic updates, and final state
- Useful for testing, debugging, and headless environments

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
main
cinnaboot 6 months ago
parent
commit
14038dae62
  1. 120
      src/main.cpp

120
src/main.cpp

@ -4,16 +4,30 @@
#include "renderer.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
int main(int argc, char** argv) {
// Parse command line arguments
const char* config_file = "configs/solar_system.txt";
if (argc > 1) {
config_file = argv[1];
bool headless = false;
int sim_duration_days = 365; // Default: 1 year for headless mode
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--headless") == 0 || strcmp(argv[i], "-h") == 0) {
headless = true;
} else if (strcmp(argv[i], "--days") == 0 && i + 1 < argc) {
sim_duration_days = atoi(argv[++i]);
} else if (argv[i][0] != '-') {
config_file = argv[i];
}
}
printf("=== Orbital Mechanics Simulation ===\n");
printf("Loading configuration: %s\n", config_file);
if (headless) {
printf("Mode: Headless (terminal output only)\n");
printf("Duration: %d days\n", sim_duration_days);
}
// Create simulation with time step of 60 seconds
const int MAX_BODIES = 100;
@ -27,28 +41,75 @@ int main(int argc, char** argv) {
return 1;
}
// Initialize renderer
init_renderer(1280, 720, "Orbital Mechanics Simulation");
// Setup rendering state
RenderState render_state;
setup_camera(&render_state);
// If headless mode, run simulation without rendering
if (headless) {
const double SECONDS_PER_DAY = 86400.0;
const double total_time = sim_duration_days * SECONDS_PER_DAY;
const double output_interval = SECONDS_PER_DAY; // Print status once per day
double next_output_time = 0.0;
printf("\n=== Initial State ===\n");
for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i];
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
}
// Simulation control variables
bool paused = false;
double speed_multiplier = 1.0;
int physics_steps_per_frame = 100; // Multiple physics steps per frame for stability
printf("\n=== Running Simulation ===\n");
while (sim->time < total_time) {
update_simulation(sim);
// Print periodic updates
if (sim->time >= next_output_time) {
printf("Day %.1f: ", sim->time / SECONDS_PER_DAY);
for (int i = 0; i < sim->body_count && i < 3; i++) {
double dist = vec3_magnitude(sim->bodies[i].position);
printf("%s=%.3e m ", sim->bodies[i].name, dist);
}
printf("\n");
next_output_time += output_interval;
}
}
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(" I: Toggle info display\n");
printf(" ESC: Quit\n\n");
printf("\n=== Final State (Day %.1f) ===\n", sim->time / SECONDS_PER_DAY);
for (int i = 0; i < sim->body_count; i++) {
CelestialBody* body = &sim->bodies[i];
printf("%s:\n", body->name);
printf(" Position: (%.3e, %.3e, %.3e) m\n",
body->position.x, body->position.y, body->position.z);
printf(" Velocity: (%.3e, %.3e, %.3e) m/s\n",
body->velocity.x, body->velocity.y, body->velocity.z);
}
// Main loop
while (!WindowShouldClose()) {
destroy_simulation(sim);
return 0;
} else {
// Graphical mode
// Initialize renderer
init_renderer(1280, 720, "Orbital Mechanics Simulation");
// Setup rendering state
RenderState render_state;
setup_camera(&render_state);
// Simulation control variables
bool paused = false;
double speed_multiplier = 1.0;
int physics_steps_per_frame = 100; // Multiple physics steps per frame for stability
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(" I: Toggle info display\n");
printf(" ESC: Quit\n\n");
// Main loop
while (!WindowShouldClose()) {
// Handle input
if (IsKeyPressed(KEY_SPACE)) {
paused = !paused;
@ -77,14 +138,15 @@ int main(int argc, char** argv) {
}
}
// Render
render_simulation(sim, &render_state);
}
// Render
render_simulation(sim, &render_state);
}
// Cleanup
close_renderer();
destroy_simulation(sim);
// Cleanup
close_renderer();
destroy_simulation(sim);
printf("\nSimulation ended. Final time: %.2f days\n", sim->time / 86400.0);
return 0;
printf("\nSimulation ended. Final time: %.2f days\n", sim->time / 86400.0);
return 0;
}
}

Loading…
Cancel
Save