Created
May 18, 2020 02:29
-
-
Save jrjbertram/0ddbf09c47f4197a60eb08ededb9fef1 to your computer and use it in GitHub Desktop.
Example of breaking up the run_window_config so that render could be called by an external simulation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import moderngl_window as mglw | |
| def run_window_config(config_cls: mglw.WindowConfig, timer=None, args=None) -> None: | |
| """ | |
| Run an WindowConfig entering a blocking main loop | |
| Args: | |
| config_cls: The WindowConfig class to render | |
| args: Override sys.args | |
| """ | |
| mglw.setup_basic_logging(config_cls.log_level) | |
| values = mglw.parse_args(args) | |
| window_cls = mglw.get_local_window_cls(values.window) | |
| # Calculate window size | |
| size = values.size or config_cls.window_size | |
| size = int(size[0] * values.size_mult), int(size[1] * values.size_mult) | |
| # Resolve cursor | |
| show_cursor = values.cursor | |
| if show_cursor is None: | |
| show_cursor = config_cls.cursor | |
| window = window_cls( | |
| title=config_cls.title, | |
| size=size, | |
| fullscreen=config_cls.fullscreen or values.fullscreen, | |
| resizable=values.resizable if values.resizable is not None else config_cls.resizable, | |
| gl_version=config_cls.gl_version, | |
| aspect_ratio=config_cls.aspect_ratio, | |
| vsync=values.vsync if values.vsync is not None else config_cls.vsync, | |
| samples=values.samples if values.samples is not None else config_cls.samples, | |
| cursor=show_cursor if show_cursor is not None else True, | |
| ) | |
| window.print_context_info() | |
| mglw.activate_context(window=window) | |
| window.config = config_cls(ctx=window.ctx, wnd=window, timer=timer) | |
| return window | |
| #if duration > 0: | |
| # logger.info("Duration: {0:.2f}s @ {1:.2f} FPS".format(duration, window.frames / duration)) | |
| class Render(): | |
| def __init__(self): | |
| self.window = run_window_config( MultiTextireTerrain ) # or whatever your class name is | |
| # Enter a blocking loop | |
| def run(self): | |
| timer = mglw.Timer() | |
| timer.start() | |
| while not self.window.is_closing: | |
| current_time, delta = timer.next_frame() | |
| #window.clear(*window.config.clear_color) | |
| self.window.render(current_time, delta) | |
| self.window.swap_buffers() | |
| _, duration = timer.stop() | |
| self.window.destroy() | |
| # Just render one frame and return | |
| def render(self, | |
| some_args | |
| ): | |
| # I don't actually care about the time values passed into render for my case | |
| self.window.render(0, 0) | |
| self.window.swap_buffers() | |
| if __name__ == '__main__': | |
| render = Render() | |
| render.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment