diff --git a/src/game.cpp b/src/game.cpp index b72fd04..bc5bc1f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -52,6 +52,25 @@ disableGameOrbit(GameState* gs, GameOrbit* orbit) *orbit = {0}; } +double +getTimeStep(GameState* gs) +{ + u64 last_sdl_tick = gs->game_time_ms; + gs->game_time_ms = SDL_GetTicks64(); + u64 current_tick = gs->game_time_ms - last_sdl_tick; + double time_step = 0; + + // NOTE: update sim time base on time since last frame * sim_speed + if (gs->running) { + u64 last_game_tick = gs->sim_time_ms; + gs->sim_time_ms = gs->sim_time_ms + current_tick * gs->sim_speed; + u32 diff_ms = gs->sim_time_ms - last_game_tick; + time_step = double(diff_ms) / 1000; + } + + return time_step; +} + Ellipse3D ellipse3DInit(EllipseParameters ep, uint vert_count) { diff --git a/src/game.h b/src/game.h index c79fcc4..e5115c3 100644 --- a/src/game.h +++ b/src/game.h @@ -65,6 +65,8 @@ GameOrbit* getSelectedOrbit(GameState* gs); void disableGameOrbit(GameState* gs, GameOrbit* orbit); +double getTimeStep(GameState* gs); + // NOTE: create vertices for a 3d ellipse // all vertices are in the x/y plane with z = 0 Ellipse3D ellipse3DInit(EllipseParameters ep, uint vert_count); diff --git a/src/main.cpp b/src/main.cpp index 8248362..1ff3b36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -215,6 +215,23 @@ loadScene(GameState* gs, RenderState* rs) return true; } +void +processInput(RenderState* rs) +{ + static InputState is = {}; + SDL_Event e; + bool gooey_wants = false; + + while (SDL_PollEvent(&e)) { + // FIXME: we should check for escape key here first + gooey_wants = gooProcessEvent(e); + if (!gooey_wants) inputProcessEvent(&is, e); + } + + if (is.window_closed || is.escape) + rs->running = false; +} + void updateSatelliteModel(TwoBodySystem& sys, double time_step) { @@ -235,67 +252,35 @@ updateSatelliteEntity(Entity* e, const Satellite& sat) setEntityPosition(e, xform * vec4(v.x, v.y, v.z, 1)); } -// NOTE: use ellipseValidate(ep) before calling to avoid failing assertions void -updateOrbit(TwoBodySystem sys, Entity& ellipse_entity) +updateOrbit(GameOrbit* orbit, bool running, double time_step) { -#if 0 - ellipse3DUpdate(sys.ep, sys.e3d); - - for (uint i = 0; i < sys.e3d.vert_count; i++) - ellipse_entity.mesh->vertices[i] = sys.e3d.vertices[i]; - - entUpdateSimpleMesh(ellipse_entity, ellipse_entity.mesh, GL_LINE_LOOP); -#endif + assert(orbit); + + if (running && orbit->in_use) { + double previous_true_anom = orbit->system.sat.theta; + updateSatelliteModel(orbit->system, time_step); + updateSatelliteEntity(orbit->satellite_entity, orbit->system.sat); + + if (testManeuverStep(orbit->maneuver, + previous_true_anom, + orbit->system.sat.theta)) + { + applyManeuver(orbit); + } + } } -// FIXME: this needs some helpers void preFrameCallback(RenderState* rs, void* user_data = nullptr) { - static InputState is = {}; - SDL_Event e; - bool gooey_wants = false; - - while (SDL_PollEvent(&e)) { - // FIXME: we should check for escape key here first - gooey_wants = gooProcessEvent(e); - if (!gooey_wants) inputProcessEvent(&is, e); - } - - if (is.window_closed || is.escape) - rs->running = false; - - // TODO: sim time stuff should have dedicated functions, and maybe a test - // since it's pretty important that the simulation is accurate-ish - // TODO: verify time to apoapsis/periapsis are correct + processInput(rs); assert(user_data != nullptr); GameState* gs = (GameState*) user_data; - u64 last_sdl_tick = gs->game_time_ms; - gs->game_time_ms = SDL_GetTicks64(); - u64 current_tick = gs->game_time_ms - last_sdl_tick; - double time_step = 0; - - // NOTE: update sim time base on time since last frame * sim_speed - if (gs->running) { - u64 last_game_tick = gs->sim_time_ms; - gs->sim_time_ms = gs->sim_time_ms + current_tick * gs->sim_speed; - u32 diff_ms = gs->sim_time_ms - last_game_tick; - time_step = double(diff_ms) / 1000; - } - - for (u32 i = 0; i < gs->num_orbits; i++) { - GameOrbit& orbit = gs->orbits[i]; + double time_step = getTimeStep(gs); - if (orbit.in_use && orbit.satellite_entity) { - if (gs->running) { - updateSatelliteModel(orbit.system, time_step); - updateSatelliteEntity(orbit.satellite_entity, orbit.system.sat); - // TODO: update EllipseEntity per frame - //updateOrbit(gs->system, ellipse_entity); - } - } - } + for (u32 i = 0; i < gs->num_orbits; i++) + updateOrbit(&gs->orbits[i], gs->running, time_step); } void