Skip to content

Commit

Permalink
Limit CPU time given to the server to 60FPS
Browse files Browse the repository at this point in the history
Also, lay the groundwork for frame lenghtening.
  • Loading branch information
nerudaj committed May 24, 2024
1 parent 37ebb82 commit 87b117c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib-app/include/app/AppStateIngame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "engine/RenderingEngine.hpp"
#include "utils/DemoFileHandler.hpp"
#include "utils/DependencyContainer.hpp"
#include "utils/Framerate.hpp"
#include <DGM/dgm.hpp>
#include <GameLoop.hpp>
#include <LevelD.hpp>
Expand Down Expand Up @@ -90,4 +91,5 @@ class [[nodiscard]] AppStateIngame final : public dgm::AppState
mem::Box<dgm::Camera> camera;
bool ready = false;
size_t lastTick = {};
Framerate framerate = Framerate(FPS);
};
31 changes: 31 additions & 0 deletions src/lib-app/include/utils/Framerate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <chrono>
#include <thread>

class [[nodiscard]] Framerate final
{
public:
explicit Framerate(const unsigned fps)
: targetFrameTime(1'000'000 / fps), clock(), clockStart(clock.now())
{
}

public:
void ensureFramerate(const std::chrono::milliseconds extraDelay = {})
{
auto&& clockEnd = clock.now();
auto&& frameTime = clockEnd - clockStart;
if (targetFrameTime > frameTime)
{
std::this_thread::sleep_for(
targetFrameTime - frameTime + extraDelay);
}
clockStart = clock.now();
}

private:
std::chrono::microseconds targetFrameTime;
std::chrono::high_resolution_clock clock;
std::chrono::time_point<std::chrono::high_resolution_clock> clockStart;
};
6 changes: 4 additions & 2 deletions src/lib-app/src/app/AppStateIngame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "app/AppStateWinnerAnnounced.hpp"
#include "utils/AppMessage.hpp"
#include <builder/SceneBuilder.hpp>
#include <chrono>
#include <events/EventQueue.hpp>
#include <print>
#include <thread>

[[nodiscard]] static std::vector<mem::Rc<ControllerInterface>> createInputs(
mem::Rc<PhysicalController> physicalController,
Expand Down Expand Up @@ -54,7 +56,6 @@ AppStateIngame::AppStateIngame(
dic->settings->display.resolution.width,
dic->settings->display.resolution.height)))
{
app.window.getWindowContext().setFramerateLimit(60);
lockMouse();
createPlayers();
dic->jukebox->playIngameSong();
Expand Down Expand Up @@ -127,6 +128,8 @@ void AppStateIngame::update()
++lastTick;

evaluateWinCondition();

framerate.ensureFramerate();
}

void AppStateIngame::draw()
Expand All @@ -139,7 +142,6 @@ void AppStateIngame::restoreFocusImpl(const std::string& message)
{
handleAppMessage<decltype(this)>(app, message);

app.window.getWindowContext().setFramerateLimit(60);
propagateSettings();
lockMouse();
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib-app/src/app/AppStateServerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
#include "Dialogs/YesNoCancelDialog.hpp"
#include "Server.hpp"
#include "Shortcuts/ShortcutEngine.hpp"
#include "utils/Framerate.hpp"
#include <app/AppStateEditor.hpp>
#include <app/AppStateGameSetup.hpp>
#include <app/AppStateServerWrapper.hpp>
#include <core/Constants.hpp>

void serverLoop(Server server, std::atomic_bool& serverEnabled)
{
auto&& framerate = Framerate(FPS);
std::cout << "Server loop started" << std::endl;
while (serverEnabled)
{
server.update();
framerate.ensureFramerate();
}
}

Expand Down

0 comments on commit 87b117c

Please sign in to comment.