Skip to content

Commit 2fc1475

Browse files
committed
Fix big bounces in OS X
Inconsistent SDL_Delay performance
1 parent 474856e commit 2fc1475

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

platform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <stdbool.h>
2121
#include <stdint.h>
2222

23+
#define FPS 60
24+
2325
extern void InitializePlatform(void);
2426

2527
/*

platform/general.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "platform.h"
2626

2727
static Uint32 LastTicks = 0;
28+
static Uint32 TicksElapsed = 0;
2829

2930
void InitializePlatform(void)
3031
{
@@ -33,11 +34,21 @@ void InitializePlatform(void)
3334

3435
Uint32 ToNextFrame(void)
3536
{
36-
SDL_Delay(8);
37-
Uint32 Ticks = SDL_GetTicks();
38-
Uint32 Duration = Ticks - LastTicks;
39-
LastTicks = Ticks;
40-
return Duration;
37+
const Uint32 duration = 1000 / FPS;
38+
for (;;)
39+
{
40+
Uint32 Ticks = SDL_GetTicks();
41+
TicksElapsed += Ticks - LastTicks;
42+
LastTicks = Ticks;
43+
if (TicksElapsed <= duration)
44+
{
45+
SDL_Delay(1);
46+
continue;
47+
}
48+
break;
49+
}
50+
TicksElapsed -= duration;
51+
return duration;
4152
}
4253

4354
bool IsEnterGamePressingEvent(const SDL_Event* event)

0 commit comments

Comments
 (0)