From 028b8a8fcce677280a371a645cfd16d784074865 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 6 Feb 2026 11:05:24 -0500 Subject: [PATCH] Fix UI buffer caching: remove dead code, use count-based invalidation, fix update bug --- src/maneuver.cpp | 2 -- src/simulation.cpp | 52 --------------------------------------------- src/simulation.h | 16 -------------- src/ui_renderer.cpp | 26 +++++++++++++++++------ src/ui_renderer.h | 4 +++- 5 files changed, 22 insertions(+), 78 deletions(-) diff --git a/src/maneuver.cpp b/src/maneuver.cpp index da8f08c..f2aafa1 100644 --- a/src/maneuver.cpp +++ b/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_time = 0.0; sim->maneuver_count++; - increment_sim_version(sim); return new_idx; } @@ -269,7 +268,6 @@ bool remove_maneuver_by_index(SimulationState* sim, int index) { } sim->maneuver_count--; - increment_sim_version(sim); return true; } diff --git a/src/simulation.cpp b/src/simulation.cpp index 061d62b..c0597d2 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -64,7 +64,6 @@ int add_spacecraft(SimulationState* sim, Spacecraft* craft) { int new_idx = sim->craft_count; sim->spacecraft[new_idx] = *craft; sim->craft_count++; - increment_sim_version(sim); 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_velocity = body->global_velocity; - increment_sim_version(sim); 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++; -} diff --git a/src/simulation.h b/src/simulation.h index ca54591..116a8a0 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -46,13 +46,11 @@ struct SimulationState { double time; double dt; char config_name[256]; - int version; }; // Simulation management functions SimulationState* create_simulation(int max_bodies, int max_craft, int max_maneuvers, double time_step); void destroy_simulation(SimulationState* sim); -void increment_sim_version(SimulationState* sim); // Dynamic body management 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 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 diff --git a/src/ui_renderer.cpp b/src/ui_renderer.cpp index cd1249d..3374515 100644 --- a/src/ui_renderer.cpp +++ b/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; // 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) { 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++; } - 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) @@ -404,7 +407,8 @@ void render_maneuver_list_ui(SimulationState* sim, RenderState* render_state, UI } // 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) { 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 = { @@ -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; 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); } - 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"); @@ -1430,7 +1440,9 @@ void delete_maneuver_from_dialog(SimulationState* sim, 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 = NULL; ui_state->craft_list_buffer_size = 0; diff --git a/src/ui_renderer.h b/src/ui_renderer.h index defb680..737667b 100644 --- a/src/ui_renderer.h +++ b/src/ui_renderer.h @@ -51,7 +51,9 @@ struct UIState { int maneuver_list_scroll; int selected_maneuver_index; - int sim_version; + int cached_body_count; + int cached_craft_count; + int cached_maneuver_count; int body_list_buffer_size; char* body_list_buffer; int craft_list_buffer_size;