From 16265b6bb7bfd992798d1e56db54831117ddbf69 Mon Sep 17 00:00:00 2001 From: Daivik Bhatia Date: Sat, 17 Aug 2024 19:21:18 +0530 Subject: [PATCH] Fixed issue #333: Moved frame limiter from open_gl and sdl2 window to PyBoyWindowPlugin --- pyboy/plugins/base_plugin.pxd | 3 ++- pyboy/plugins/base_plugin.py | 14 +++++++++++++- pyboy/plugins/window_open_gl.pxd | 1 - pyboy/plugins/window_open_gl.py | 12 +----------- pyboy/plugins/window_sdl2.pxd | 1 - pyboy/plugins/window_sdl2.py | 13 +------------ 6 files changed, 17 insertions(+), 27 deletions(-) diff --git a/pyboy/plugins/base_plugin.pxd b/pyboy/plugins/base_plugin.pxd index 6f555480c..cc65f0ca3 100644 --- a/pyboy/plugins/base_plugin.pxd +++ b/pyboy/plugins/base_plugin.pxd @@ -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 @@ -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 diff --git a/pyboy/plugins/base_plugin.py b/pyboy/plugins/base_plugin.py index 71d737bf1..391d9fd32 100644 --- a/pyboy/plugins/base_plugin.py +++ b/pyboy/plugins/base_plugin.py @@ -14,6 +14,7 @@ import io import random from array import array +import time import numpy as np @@ -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 @@ -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 diff --git a/pyboy/plugins/window_open_gl.pxd b/pyboy/plugins/window_open_gl.pxd index 3d5c9203f..e13dd9101 100644 --- a/pyboy/plugins/window_open_gl.pxd +++ b/pyboy/plugins/window_open_gl.pxd @@ -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 diff --git a/pyboy/plugins/window_open_gl.py b/pyboy/plugins/window_open_gl.py index 7374c3b40..fae5c887d 100644 --- a/pyboy/plugins/window_open_gl.py +++ b/pyboy/plugins/window_open_gl.py @@ -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): @@ -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: diff --git a/pyboy/plugins/window_sdl2.pxd b/pyboy/plugins/window_sdl2.pxd index 558d54c0d..4d6cb843b 100644 --- a/pyboy/plugins/window_sdl2.pxd +++ b/pyboy/plugins/window_sdl2.pxd @@ -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 diff --git a/pyboy/plugins/window_sdl2.py b/pyboy/plugins/window_sdl2.py index 7919a45af..faf175b18 100644 --- a/pyboy/plugins/window_sdl2.py +++ b/pyboy/plugins/window_sdl2.py @@ -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], @@ -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