diff --git a/moderngl_window/__init__.py b/moderngl_window/__init__.py index dd34240..9e73853 100644 --- a/moderngl_window/__init__.py +++ b/moderngl_window/__init__.py @@ -197,6 +197,25 @@ def run_window_config( timer: A custom timer instance args: Override sys.args """ + config = create_window_config_instance(config_cls, timer=timer, args=args) + run_window_config_instance(config) + + +def create_window_config_instance( + config_cls: type[WindowConfig], timer: Optional[Timer] = None, args: Any = None +) -> WindowConfig: + """ + Create and initialize a instance of a WindowConfig class. + Quite a bit of boilerplate is required to create a WindowConfig instance + and this function aims to simplify that. + + Args: + window_config: The WindowConfig class to create an instance of + Keyword Args: + kwargs: Arguments to pass to the WindowConfig constructor + Returns: + An instance of the WindowConfig class + """ setup_basic_logging(config_cls.log_level) parser = create_parser() config_cls.add_arguments(parser) @@ -237,10 +256,22 @@ def run_window_config( window._config = weakref.ref(config) # Swap buffers once before staring the main loop. - # This can trigged additional resize events reporting + # This can trigger additional resize events reporting # a more accurate buffer size window.swap_buffers() window.set_default_viewport() + return config + + +def run_window_config_instance(config: WindowConfig) -> None: + """ + Run an WindowConfig instance entering a blocking main loop. + + Args: + window_config: The WindowConfig instance + """ + window = config.wnd + timer = config.timer timer.start() @@ -254,6 +285,7 @@ def run_window_config( window.use() window.render(current_time, delta) + if not window.is_closing: window.swap_buffers() diff --git a/moderngl_window/context/base/window.py b/moderngl_window/context/base/window.py index 40a9f00..9fed325 100644 --- a/moderngl_window/context/base/window.py +++ b/moderngl_window/context/base/window.py @@ -19,12 +19,7 @@ TextureDescription, ) from moderngl_window.scene import Scene -from moderngl_window.timers.base import BaseTimer - -try: - from pygame.event import Event -except ModuleNotFoundError: - Event = Any +from moderngl_window.timers import BaseTimer, Timer FuncAny = Callable[[Any], Any] @@ -157,7 +152,7 @@ def __init__( self._files_dropped_event_func: Callable[[int, int, list[Union[str, Path]]], None] = ( dummy_func ) - self._on_generic_event_func: Callable[[Event], None] = dummy_func + self._on_generic_event_func: Callable = dummy_func # Internal states self._ctx: moderngl.Context @@ -897,7 +892,7 @@ def _calc_mouse_delta(self, xpos: int, ypos: int) -> tuple[int, int]: @property def on_generic_event_func( self, - ) -> Union[Callable[[int, int, int, int], None], Callable[[Event], None]]: + ) -> Union[Callable[[int, int, int, int], None], None]: """ callable: Get or set the on_generic_event callable used to funnel all non-processed events @@ -906,7 +901,7 @@ def on_generic_event_func( @on_generic_event_func.setter @require_callable - def on_generic_event_func(self, func: Callable[[Event], None]) -> None: + def on_generic_event_func(self, func: Callable) -> None: self._on_generic_event_func = func @@ -1114,7 +1109,7 @@ def __init__( self.ctx = ctx self.wnd = wnd - self.timer = timer + self.timer: BaseTimer = timer or Timer() self.assign_event_callbacks() diff --git a/moderngl_window/timers/__init__.py b/moderngl_window/timers/__init__.py index e69de29..cf9c6b9 100644 --- a/moderngl_window/timers/__init__.py +++ b/moderngl_window/timers/__init__.py @@ -0,0 +1,4 @@ +from .base import BaseTimer as BaseTimer +from .clock import Timer as Timer + +__all__ = ["BaseTimer", "Timer"]