Files
engine-sim-cpp/main.cpp
T
Avery Haas 1175b498b0 add to gui
2026-08-31 23:57:40 -04:00

57 lines
1.9 KiB
C++

#include <chrono>
#include "imgui.h"
#include "engine/simulation.h"
#include "gui/gui.h"
int main() {
Gui gui;
if (!gui.init()) return 1;
Simulation sim;
const double SIM_DT = 1.0 / 360.0;
double accumulator = 0.0;
auto last_time = std::chrono::steady_clock::now();
while (gui.begin_frame()) {
// 'r' resets the engine (rpm -> 0). false = don't auto-repeat while held.
if (ImGui::IsKeyPressed(ImGuiKey_R, false)) {
sim.engine().reset();
}
auto now = std::chrono::steady_clock::now();
double frame_time = std::chrono::duration<double>(now - last_time).count();
last_time = now;
if (frame_time > 0.25) frame_time = 0.25; // clamp huge stalls (e.g. breakpoint)
accumulator += frame_time;
while (accumulator >= SIM_DT) {
sim.step(SIM_DT);
accumulator -= SIM_DT;
}
// Build this frame's display data. Only rpm is real right now;
// the rest are placeholders until those models exist.
EngineStats stats;
stats.rpm = sim.engine().rpm();
stats.fuel_consumption_lph = 0.0; // TODO: derive from fuel model
stats.crank_power_kw = 0.0; // TODO: torque_nm * rpm -> kW
stats.output_heat_j = 0.0; // TODO: energy balance / exhaust heat model
stats.extra_stats.push_back({"Oil Temp", 0.0, "C"});
stats.extra_stats.push_back({"Coolant Temp", 0.0, "C"});
stats.extra_stats.push_back({"Throttle Position", sim.engine().get_throttle(), "%"});
gui.render(stats);
gui.end_frame();
// The lever now lives inline in render()'s window, so it's read
// after drawing — this frame's drag feeds next frame's step,
// same one-frame lag as gui.accessories().
sim.engine().set_throttle(gui.throttle());
}
gui.shutdown();
return 0;
}