Browse Source

Fix UI buffer caching: remove dead code, use count-based invalidation, fix update bug

main
cinnaboot 5 months ago
parent
commit
028b8a8fcc
  1. 2
      src/maneuver.cpp
  2. 52
      src/simulation.cpp
  3. 16
      src/simulation.h
  4. 26
      src/ui_renderer.cpp
  5. 4
      src/ui_renderer.h

2
src/maneuver.cpp

@ -252,7 +252,6 @@ int add_maneuver_to_simulation(SimulationState* sim, Maneuver* maneuver) {
sim->maneuvers[new_idx].executed = false; sim->maneuvers[new_idx].executed = false;
sim->maneuvers[new_idx].executed_time = 0.0; sim->maneuvers[new_idx].executed_time = 0.0;
sim->maneuver_count++; sim->maneuver_count++;
increment_sim_version(sim);
return new_idx; return new_idx;
} }
@ -269,7 +268,6 @@ bool remove_maneuver_by_index(SimulationState* sim, int index) {
} }
sim->maneuver_count--; sim->maneuver_count--;
increment_sim_version(sim);
return true; return true;
} }

52
src/simulation.cpp

@ -64,7 +64,6 @@ int add_spacecraft(SimulationState* sim, Spacecraft* craft) {
int new_idx = sim->craft_count; int new_idx = sim->craft_count;
sim->spacecraft[new_idx] = *craft; sim->spacecraft[new_idx] = *craft;
sim->craft_count++; sim->craft_count++;
increment_sim_version(sim);
return new_idx; return new_idx;
} }
@ -99,7 +98,6 @@ int add_body_to_simulation(SimulationState* sim, CelestialBody* body) {
sim->bodies[new_idx].global_position = body->global_position; sim->bodies[new_idx].global_position = body->global_position;
sim->bodies[new_idx].global_velocity = body->global_velocity; sim->bodies[new_idx].global_velocity = body->global_velocity;
increment_sim_version(sim);
return new_idx; return new_idx;
} }
@ -349,53 +347,3 @@ void compute_global_coordinates(SimulationState* sim) {
} }
} }
} }
OrbitalAnalysis calculate_orbital_elements(CelestialBody* body, CelestialBody* primary,
CelestialBody* optional_ref_body, double current_time) {
const double AU = 1.496e11;
const double SECONDS_PER_DAY = 86400.0;
const double M_sun = primary->mass;
OrbitalAnalysis elem;
elem.time_days = current_time / SECONDS_PER_DAY;
Vec3 r_vec = vec3_sub(body->global_position, primary->global_position);
double r = vec3_magnitude(r_vec);
double v = vec3_magnitude(body->global_velocity);
elem.distance_to_sun_au = r / AU;
elem.velocity_magnitude = v;
if (optional_ref_body) {
double dist_ref = vec3_distance(body->global_position, optional_ref_body->global_position);
elem.distance_to_ref_body_au = dist_ref / AU;
} else {
elem.distance_to_ref_body_au = -1.0;
}
elem.specific_energy = (v * v) / 2.0 - (G * M_sun) / r;
if (elem.specific_energy < 0) {
elem.semi_major_axis_au = -(G * M_sun) / (2.0 * elem.specific_energy) / AU;
double v_squared = v * v;
double r_dot_v = r_vec.x * body->global_velocity.x + r_vec.y * body->global_velocity.y + r_vec.z * body->global_velocity.z;
Vec3 e_vec;
e_vec.x = (v_squared - G * M_sun / r) * r_vec.x - r_dot_v * body->global_velocity.x;
e_vec.y = (v_squared - G * M_sun / r) * r_vec.y - r_dot_v * body->global_velocity.y;
e_vec.z = (v_squared - G * M_sun / r) * r_vec.z - r_dot_v * body->global_velocity.z;
double e_mag = vec3_magnitude(e_vec) / (G * M_sun);
elem.eccentricity = e_mag;
} else {
elem.semi_major_axis_au = 0.0;
elem.eccentricity = 1.0;
}
return elem;
}
void increment_sim_version(SimulationState* sim) {
sim->version++;
}

16
src/simulation.h

@ -46,13 +46,11 @@ struct SimulationState {
double time; double time;
double dt; double dt;
char config_name[256]; char config_name[256];
int version;
}; };
// Simulation management functions // Simulation management functions
SimulationState* create_simulation(int max_bodies, int max_craft, int max_maneuvers, double time_step); SimulationState* create_simulation(int max_bodies, int max_craft, int max_maneuvers, double time_step);
void destroy_simulation(SimulationState* sim); void destroy_simulation(SimulationState* sim);
void increment_sim_version(SimulationState* sim);
// Dynamic body management // Dynamic body management
int add_body_to_simulation(SimulationState* sim, CelestialBody* body); int add_body_to_simulation(SimulationState* sim, CelestialBody* body);
@ -77,18 +75,4 @@ void compute_spacecraft_globals(SimulationState* sim);
// Converts orbital elements to local position/velocity and computes global coordinates // Converts orbital elements to local position/velocity and computes global coordinates
void initialize_orbital_objects(SimulationState* sim); void initialize_orbital_objects(SimulationState* sim);
// Orbital elements calculation (for output/analysis)
struct OrbitalAnalysis {
double time_days;
double semi_major_axis_au;
double eccentricity;
double specific_energy;
double distance_to_sun_au;
double distance_to_ref_body_au;
double velocity_magnitude;
};
OrbitalAnalysis calculate_orbital_elements(CelestialBody* body, CelestialBody* primary,
CelestialBody* optional_ref_body, double current_time);
#endif #endif

26
src/ui_renderer.cpp

@ -96,7 +96,9 @@ void render_body_list_ui(SimulationState* sim, RenderState* render_state, UIStat
int total_items = sim->body_count + sim->craft_count; int total_items = sim->body_count + sim->craft_count;
// Check if buffer needs rebuilding // Check if buffer needs rebuilding
bool needs_rebuild = (ui_state->sim_version != sim->version) || (ui_state->body_list_buffer == NULL); bool needs_rebuild = (ui_state->cached_body_count != sim->body_count ||
ui_state->cached_craft_count != sim->craft_count ||
ui_state->body_list_buffer == NULL);
if (needs_rebuild) { if (needs_rebuild) {
int buffer_size = total_items * (64 + 3 + 1) + 1; int buffer_size = total_items * (64 + 3 + 1) + 1;
@ -130,7 +132,8 @@ void render_body_list_ui(SimulationState* sim, RenderState* render_state, UIStat
item_index++; item_index++;
} }
ui_state->sim_version = sim->version; ui_state->cached_body_count = sim->body_count;
ui_state->cached_craft_count = sim->craft_count;
} }
// Draw window (no close button - panels always shown) // Draw window (no close button - panels always shown)
@ -404,7 +407,8 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI
} }
// Check if buffer needs rebuilding // Check if buffer needs rebuilding
bool needs_rebuild = (ui_state->sim_version != sim->version) || (ui_state->maneuver_list_buffer == NULL); bool needs_rebuild = (ui_state->cached_maneuver_count != sim->maneuver_count ||
ui_state->maneuver_list_buffer == NULL);
if (needs_rebuild) { if (needs_rebuild) {
int buffer_size = craft_maneuver_count * (64 + 10 + 1) + 1; int buffer_size = craft_maneuver_count * (64 + 10 + 1) + 1;
@ -432,7 +436,7 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI
} }
} }
ui_state->sim_version = sim->version; ui_state->cached_maneuver_count = sim->maneuver_count;
} }
Rectangle list_bounds = { Rectangle list_bounds = {
@ -744,7 +748,12 @@ void render_maneuver_hohmann_tab(SimulationState* sim, UIState* ui_state, int x,
} }
} }
if (ui_state->sim_version != sim->version) { bool needs_rebuild = (ui_state->cached_body_count != sim->body_count ||
ui_state->cached_craft_count != sim->craft_count ||
ui_state->craft_list_buffer == NULL ||
ui_state->body_dropdown_buffer == NULL);
if (needs_rebuild) {
int craft_buffer_size = sim->craft_count * 64 + 1; int craft_buffer_size = sim->craft_count * 64 + 1;
if (ui_state->craft_list_buffer == NULL || ui_state->craft_list_buffer_size < craft_buffer_size) { if (ui_state->craft_list_buffer == NULL || ui_state->craft_list_buffer_size < craft_buffer_size) {
@ -785,7 +794,8 @@ void render_maneuver_hohmann_tab(SimulationState* sim, UIState* ui_state, int x,
offset += snprintf(ui_state->body_dropdown_buffer + offset, ui_state->body_dropdown_buffer_size - offset, "%s", sim->bodies[i].name); offset += snprintf(ui_state->body_dropdown_buffer + offset, ui_state->body_dropdown_buffer_size - offset, "%s", sim->bodies[i].name);
} }
ui_state->sim_version = sim->version; ui_state->cached_body_count = sim->body_count;
ui_state->cached_craft_count = sim->craft_count;
} }
GuiLabel({(float)x, (float)current_y, (float)width, 25}, "Calculate optimal Hohmann transfer between orbits"); GuiLabel({(float)x, (float)current_y, (float)width, 25}, "Calculate optimal Hohmann transfer between orbits");
@ -1430,7 +1440,9 @@ void delete_maneuver_from_dialog(SimulationState* sim, UIState* ui_state) {
} }
void ui_state_init_buffers(UIState* ui_state) { void ui_state_init_buffers(UIState* ui_state) {
ui_state->sim_version = 0; ui_state->cached_body_count = 0;
ui_state->cached_craft_count = 0;
ui_state->cached_maneuver_count = 0;
ui_state->body_list_buffer_size = 0; ui_state->body_list_buffer_size = 0;
ui_state->body_list_buffer = NULL; ui_state->body_list_buffer = NULL;
ui_state->craft_list_buffer_size = 0; ui_state->craft_list_buffer_size = 0;

4
src/ui_renderer.h

@ -51,7 +51,9 @@ struct UIState {
int maneuver_list_scroll; int maneuver_list_scroll;
int selected_maneuver_index; int selected_maneuver_index;
int sim_version; int cached_body_count;
int cached_craft_count;
int cached_maneuver_count;
int body_list_buffer_size; int body_list_buffer_size;
char* body_list_buffer; char* body_list_buffer;
int craft_list_buffer_size; int craft_list_buffer_size;

Loading…
Cancel
Save