Skip to content

Commit

Permalink
Fixed issue #333: Moved frame limiter from open_gl and sdl2 window to…
Browse files Browse the repository at this point in the history
… PyBoyWindowPlugin
  • Loading branch information
Daivik Bhatia authored and Daivik Bhatia committed Aug 17, 2024
1 parent cfef332 commit 16265b6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 27 deletions.
3 changes: 2 additions & 1 deletion pyboy/plugins/base_plugin.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
cimport cython
cimport numpy as cnp
from cpython.array cimport array
from libc.stdint cimport uint8_t, uint16_t, uint32_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, int64_t

from pyboy.core.lcd cimport Renderer
from pyboy.core.mb cimport Motherboard
Expand Down Expand Up @@ -38,6 +38,7 @@ cdef class PyBoyWindowPlugin(PyBoyPlugin):
cdef bint enable_title
cdef Renderer renderer

cdef int64_t _ftime
cdef bint frame_limiter(self, int) noexcept
cdef void set_title(self, str) noexcept

Expand Down
14 changes: 13 additions & 1 deletion pyboy/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io
import random
from array import array
import time

import numpy as np

Expand Down Expand Up @@ -65,6 +66,10 @@ class PyBoyWindowPlugin(PyBoyPlugin):
def __init__(self, pyboy, mb, pyboy_argv, *args, **kwargs):
super().__init__(pyboy, mb, pyboy_argv, *args, **kwargs)

print("TRYING!!")
self._ftime = time.perf_counter_ns()

print(f"PyBoyWindowPlugin initialized with _ftime = {self._ftime}")
if not self.enabled():
return

Expand All @@ -83,7 +88,14 @@ def __cinit__(self, *args, **kwargs):
self.renderer = self.mb.lcd.renderer

def frame_limiter(self, speed):
return False
self._ftime += int((1.0 / (60.0*speed)) * 1_000_000_000)
now = time.perf_counter_ns()
if (self._ftime > now):
delay = (self._ftime - now) // 1_000_000
time.sleep(delay / 1000)
else:
self._ftime = now
return True

def set_title(self, title):
pass
Expand Down
1 change: 0 additions & 1 deletion pyboy/plugins/window_open_gl.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ cdef int ROWS, COLS
cdef class WindowOpenGL(PyBoyWindowPlugin):
cdef list events

cdef int64_t _ftime
cdef void _glkeyboard(self, str, int, int, bint) noexcept
cdef void _glkeyboardspecial(self, char, int, int, bint) noexcept

Expand Down
12 changes: 1 addition & 11 deletions pyboy/plugins/window_open_gl.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, pyboy, mb, pyboy_argv):
glPixelZoom(self.scale, self.scale)
glutReshapeFunc(self._glreshape)
glutDisplayFunc(self._gldraw)
self._ftime = time.perf_counter_ns()


# Cython does not cooperate with lambdas
def _key(self, c, x, y):
Expand Down Expand Up @@ -139,16 +139,6 @@ def _gldraw(self):
glDrawPixels(COLS, ROWS, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, buf)
glFlush()

def frame_limiter(self, speed):
self._ftime += int((1.0 / (60.0*speed)) * 1_000_000_000)
now = time.perf_counter_ns()
if (self._ftime > now):
delay = (self._ftime - now) // 1_000_000
time.sleep(delay / 1000)
else:
self._ftime = now
return True

def enabled(self):
if self.pyboy_argv.get("window") == "OpenGL":
if opengl_enabled:
Expand Down
1 change: 0 additions & 1 deletion pyboy/plugins/window_sdl2.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ cpdef list sdl2_event_pump(list) noexcept

cdef class WindowSDL2(PyBoyWindowPlugin):

cdef int64_t _ftime
cdef dict _key_down
cdef dict _key_up
cdef bint fullscreen
Expand Down
13 changes: 1 addition & 12 deletions pyboy/plugins/window_sdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ def __init__(self, pyboy, mb, pyboy_argv):

if not self.enabled():
return

print(f"WindowSDL2 initialized with _ftime = {self._ftime}")
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER)
self._ftime = time.perf_counter_ns()

self._window = sdl2.SDL_CreateWindow(
b"PyBoy", sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED, self._scaledresolution[0],
Expand Down Expand Up @@ -205,16 +204,6 @@ def enabled(self):
else:
return False

def frame_limiter(self, speed):
self._ftime += int((1.0 / (60.0*speed)) * 1_000_000_000)
now = time.perf_counter_ns()
if (self._ftime > now):
delay = (self._ftime - now) // 1_000_000
sdl2.SDL_Delay(delay)
else:
self._ftime = now
return True

def stop(self):
sdl2.SDL_DestroyWindow(self._window)
for _ in range(10): # At least 2 to close
Expand Down

0 comments on commit 16265b6

Please sign in to comment.