diff --git a/src/Input.hpp b/src/Input.hpp index e4919bd..cb3b2f0 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -131,6 +131,15 @@ struct GameState } }; + struct InitialConfiguration + { + constexpr static std::string_view name{ "InitialConfiguration" }; + constexpr static auto elements = std::to_array({ "width", "height", "scale", "imgui" }); + long width; + long height; + long scale; + std::string imgui; + }; struct Joystick { @@ -206,7 +215,8 @@ struct GameState Pressed, Released, CloseWindow, - TimeElapsed>; + TimeElapsed, + InitialConfiguration>; static sf::Event::KeyEvent toSFMLEventInternal(const Key &key) @@ -280,6 +290,7 @@ struct GameState { return std::visit( overloaded{ [&](const auto &value) -> std::optional { return toSFMLEventInternal(value); }, + [&](const InitialConfiguration &) -> std::optional { return {}; }, [&](const TimeElapsed &) -> std::optional { return {}; }, [&](const std::monostate &) -> std::optional { return {}; } }, event); @@ -298,8 +309,10 @@ struct GameState pendingEvents.erase(pendingEvents.begin()); std::visit(overloaded{ - [](const TimeElapsed &te) { - std::this_thread::sleep_for(te.elapsed); + [&](const TimeElapsed &te) { + const auto timeElapsed = clock::now() - lastTick; + std::this_thread::sleep_for(te.elapsed - timeElapsed); + lastTick += te.elapsed; }, [&](const Moved &me) { sf::Mouse::setPosition({me.source.x, me.source.y}, window); diff --git a/src/main.cpp b/src/main.cpp index 0b3344b..211a4d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,9 +39,9 @@ int main(int argc, const char **argv) true,// show help if requested "Game 0.0");// version string - const auto width = args["--width"].asLong(); - const auto height = args["--height"].asLong(); - const auto scale = args["--scale"].asLong(); + auto width = args["--width"].asLong(); + auto height = args["--height"].asLong(); + auto scale = args["--scale"].asLong(); std::vector initialEvents; if (args["--replay"]) { @@ -49,6 +49,23 @@ int main(int argc, const char **argv) std::ifstream ifs(eventFile); const auto j = nlohmann::json::parse(ifs); initialEvents = j.get>(); + + auto event = initialEvents.front(); + + std::visit(Game::overloaded{ + [&](const Game::GameState::InitialConfiguration &ic) { + width = ic.width; + height = ic.height; + scale = ic.scale; + + std::ofstream ofs{ "imgui.ini" }; + ofs << ic.imgui; + + initialEvents.erase(initialEvents.begin()); + }, + [](const auto &) { } + }, + event); } @@ -103,17 +120,24 @@ int main(int argc, const char **argv) std::uint64_t eventsProcessed{ 0 }; - std::vector events{ Game::GameState::TimeElapsed{} }; - + std::string imgui; + { + std::ifstream f("imgui.ini", std::ios::binary); + std::vector v(std::istreambuf_iterator{f}, {}); + imgui= std::string(v.begin(), v.end()); + } + std::vector events{ Game::GameState::InitialConfiguration{width, height, scale, imgui}, Game::GameState::TimeElapsed{} }; + while (window.isOpen()) { const auto event = gs.nextEvent(window); - std::visit(Game::overloaded{ [](Game::GameState::TimeElapsed &prev, const Game::GameState::TimeElapsed &next) { + std::visit(Game::overloaded{ [](Game::GameState::TimeElapsed &/*prevPrev*/, Game::GameState::TimeElapsed &prev, const Game::GameState::TimeElapsed &next) { prev.elapsed += next.elapsed; }, - [&](const auto & /*prev*/, const std::monostate &) {}, - [&](const auto & /*prev*/, const auto &next) { events.push_back(next); } }, + [&](const auto & /*prev*/, const auto & /*prev*/, const std::monostate &) {}, + [&](const auto & /*prev*/, const auto & /*prev*/, const auto &next) { events.push_back(next); } }, + *std::prev(events.end(),2), events.back(), event);