-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit CPU time given to the server to 60FPS
Also, lay the groundwork for frame lenghtening.
- Loading branch information
Showing
4 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters