Skip to content

Instantly share code, notes, and snippets.

@oxycoder
Created May 30, 2019 07:01
Show Gist options
  • Save oxycoder/5dc7be254b02e18e674e8b7f6733702a to your computer and use it in GitHub Desktop.
Save oxycoder/5dc7be254b02e18e674e8b7f6733702a to your computer and use it in GitHub Desktop.
C++ game loop
// Basic c++ game loop
// idea: Render without affected to game logic and input
int main()
{
bool hasFocus = true; // check if windows is focus and active
bool isMinimize = false; // check if windows is minized
DWORD currentTime = timeGetTime();
DWORD lastFrameTime = 0;
int fpsLimitWithFocused = 60;
int fpsLimitWithoutFocus = 15;
while (!quit) {
currentTime = timeGetTime();
if (hasFocus) {
ProccessInput();
}
ProcessLogic();
if (hasFocus) {
if ((currentTime - lastFrameTime) > (1000 / fpsLimitWithFocused)) {
Render();
lastFrameTime = currentTime;
}
}
else {
if (!isMinimize && (currentTime - lastFrameTime) > (1000 / fpsLimitWithoutFocus))
Render();
lastFrameTime = currentTime;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment