Skip to content

Commit

Permalink
Split up run_window_config into smaller functions #159
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 30, 2024
1 parent ca0d477 commit 980daeb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
34 changes: 33 additions & 1 deletion moderngl_window/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -254,6 +285,7 @@ def run_window_config(
window.use()

window.render(current_time, delta)

if not window.is_closing:
window.swap_buffers()

Expand Down
15 changes: 5 additions & 10 deletions moderngl_window/context/base/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions moderngl_window/timers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .base import BaseTimer as BaseTimer
from .clock import Timer as Timer

__all__ = ["BaseTimer", "Timer"]

0 comments on commit 980daeb

Please sign in to comment.