Skip to content

Commit

Permalink
main: add frame timing
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
deltabeard committed Feb 12, 2024
1 parent 20a3f90 commit 8d8de02
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion deobfuscated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ uint16_t PC = 256, *reg16 = (uint16_t *)reg8, &HL = reg16[2], SP = 65534,
*reg16_group1[] = {reg16, reg16 + 1, &HL, &SP},
*reg16_group2[] = {reg16, reg16 + 1, &HL, &HL}, prev_cycles, cycles;

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

void tick() { cycles += 4; }

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

ot = SDL_GetTicks64();
while (1) {
prev_cycles = cycles;
if (IME & IF & io[511]) {
Expand Down Expand Up @@ -420,6 +423,13 @@ int main(int, char**) {
SDL_RWclose(sav);
return 0;
}
nt = SDL_GetTicks64();
speed_compensation += 16.7427 - (nt - ot);
delay = (int)(speed_compensation);
speed_compensation -= delay;
ot = nt;
if(delay > 0)
SDL_Delay(delay);
}

LY = (LY + 1) % 154;
Expand Down

0 comments on commit 8d8de02

Please sign in to comment.