aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorArslaan Pathan <[email protected]>2025-06-29 11:53:21 +1200
committerArslaan Pathan <[email protected]>2025-06-29 11:53:21 +1200
commitde6ecfdf287f2dcc625bbd32f65a99511e985f7a (patch)
treefc51e677277f871da2115b24b9a5660add5fb1db /src/main.cpp
parentf502d0f5b7c1f1edd876bcabbc56fbefdd902a8e (diff)
downloadshowdownofthesticks-de6ecfdf287f2dcc625bbd32f65a99511e985f7a.tar.xz
showdownofthesticks-de6ecfdf287f2dcc625bbd32f65a99511e985f7a.zip
Fix a lot of shit, deltaTime, collisions, and more.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 24a3147..eeed38f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -363,6 +363,9 @@ int main()
SDL_RenderSetLogicalSize(renderer, WIDTH, HEIGHT);
SDL_RenderSetIntegerScale(renderer, SDL_FALSE);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
+ #ifdef __APPLE__
+ SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal");
+ #endif
SDL_SetWindowResizable(window, SDL_TRUE);
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
@@ -393,15 +396,13 @@ int main()
while (running)
{
+ Uint32 frameStart = SDL_GetTicks();
+
push_keys_to_lua(L, keys);
lua_setglobal(L, "keys");
call_lua_function(L, "Update");
- Uint32 currentFrameTime = SDL_GetTicks();
- deltaTime = (currentFrameTime - lastFrameTime) / 1000.0f;
- lastFrameTime = currentFrameTime;
-
lua_pushnumber(L, deltaTime);
lua_setglobal(L, "deltaTime");
@@ -548,7 +549,21 @@ int main()
textList.clear();
SDL_RenderPresent(renderer);
- SDL_Delay(1);
+
+ // Frame limiting
+ Uint32 frameTime = SDL_GetTicks() - frameStart;
+ const int targetFrameTimeMs = 1000 / 60; // 60 FPS
+
+ if (frameTime < targetFrameTimeMs)
+ {
+ SDL_Delay(targetFrameTimeMs - frameTime);
+ frameTime = targetFrameTimeMs; // We waited full frame time
+ }
+
+ // Update deltaTime for Lua with the actual frame duration including delay
+ deltaTime = frameTime / 1000.0f;
+ lua_pushnumber(L, deltaTime);
+ lua_setglobal(L, "deltaTime");
}
lua_close(L);