Skip to content

Commit 8d8de02

Browse files
committed
main: add frame timing
Fixes issue binji#10 whereby gameplay would run at the speed of the monitor refresh rate. A delay is automatically calculated on each frame to slow down the gameplay if required. Note that this does not speed up the gameplay if the monitor has a refresh rate lower than 59 Hz. In such cases, VSYNC should just be disabled in the source code. Signed-off-by: Mahyar Koshkouei <[email protected]>
1 parent 20a3f90 commit 8d8de02

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

deobfuscated.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ uint16_t PC = 256, *reg16 = (uint16_t *)reg8, &HL = reg16[2], SP = 65534,
3838
*reg16_group1[] = {reg16, reg16 + 1, &HL, &SP},
3939
*reg16_group2[] = {reg16, reg16 + 1, &HL, &HL}, prev_cycles, cycles;
4040

41-
int tmp, tmp2, F_mask[] = {128, 128, 16, 16}, frame_buffer[23040],
41+
int tmp, delay, F_mask[] = {128, 128, 16, 16}, frame_buffer[23040],
4242
palette[] = {-1, -23197, -65536, -1 << 24,
4343
-1, -8092417, -12961132, -1 << 24};
44+
double speed_compensation;
45+
Uint64 ot, nt;
4446

4547
void tick() { cycles += 4; }
4648

@@ -146,6 +148,7 @@ int main(int, char**) {
146148
renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, 160, 144);
147149
key_state = SDL_GetKeyboardState(0);
148150

151+
ot = SDL_GetTicks64();
149152
while (1) {
150153
prev_cycles = cycles;
151154
if (IME & IF & io[511]) {
@@ -420,6 +423,13 @@ int main(int, char**) {
420423
SDL_RWclose(sav);
421424
return 0;
422425
}
426+
nt = SDL_GetTicks64();
427+
speed_compensation += 16.7427 - (nt - ot);
428+
delay = (int)(speed_compensation);
429+
speed_compensation -= delay;
430+
ot = nt;
431+
if(delay > 0)
432+
SDL_Delay(delay);
423433
}
424434

425435
LY = (LY + 1) % 154;

0 commit comments

Comments
 (0)