From 82702c3f8ff36b6fa4bb7393935161748a95d3f4 Mon Sep 17 00:00:00 2001 From: Einar Forselv Date: Fri, 29 Nov 2024 00:48:17 +0100 Subject: [PATCH] Example tweaks --- examples/advanced/animated_gif.py | 28 +++++++++++-------- .../advanced/compute_render_to_texture.py | 2 +- examples/advanced/shader_includes.py | 2 +- examples/geometry_quad_fs_mouse_scroll.py | 2 +- examples/window_config.py | 2 +- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/examples/advanced/animated_gif.py b/examples/advanced/animated_gif.py index b3cd507..9131014 100644 --- a/examples/advanced/animated_gif.py +++ b/examples/advanced/animated_gif.py @@ -1,3 +1,14 @@ +""" +Loads two gif files into texture arrays and renders them into an offscreen buffer +that is then displayed on the screen using nearest neighbor filtering. + +Possible improvements: +- Make the example configurable to load any gif file(s) +- Load the raw byte data for each frame and instead use a palette texture to + reduce the memory footprint +- Take the gif transparency key into account? +""" + from pathlib import Path import glm @@ -6,12 +17,8 @@ import moderngl_window as mglw from moderngl_window import geometry -# from moderngl_window.conf import settings -# settings.SCREENSHOT_PATH = 'screenshots' -# from moderngl_window import screenshot - -class Test(mglw.WindowConfig): +class AnimatedGif(mglw.WindowConfig): title = "Animated Sprite" resource_dir = (Path(__file__) / "../../resources").resolve() aspect_ratio = 320 / 256 @@ -25,7 +32,8 @@ def __init__(self, **kwargs): self.background_texture.repeat_x = False self.background_texture.repeat_y = False self.caveman_texture = self.load_texture_array( - "textures/animated_sprites/player_2.gif", layers=35 + "textures/animated_sprites/player_2.gif", + layers=35, # Number of frames in the gif ) self.caveman_texture.repeat_x = False self.caveman_texture.repeat_y = False @@ -69,10 +77,7 @@ def on_render(self, time, frame_time): self.offscreen_texture.use(location=0) self.quad_fs.render(self.texture_program) - # if self.wnd.frames < 100: - # screenshot.create(self.ctx.screen) - - def render_sprite(self, texture, blend=False, frame=0, position=(0, 0)): + def render_sprite(self, texture: moderngl.TextureArray, blend=False, frame=0, position=(0, 0)): if blend: self.ctx.enable(moderngl.BLEND) @@ -86,5 +91,4 @@ def render_sprite(self, texture, blend=False, frame=0, position=(0, 0)): if __name__ == "__main__": - mglw.run_window_config(Test) - # Test.run() + AnimatedGif.run() diff --git a/examples/advanced/compute_render_to_texture.py b/examples/advanced/compute_render_to_texture.py index dfaf6c3..67b0fff 100644 --- a/examples/advanced/compute_render_to_texture.py +++ b/examples/advanced/compute_render_to_texture.py @@ -44,4 +44,4 @@ def on_render(self, time, frame_time): if __name__ == "__main__": - mglw.run_window_config(ComputeRenderToTexture) + ComputeRenderToTexture.run() diff --git a/examples/advanced/shader_includes.py b/examples/advanced/shader_includes.py index 868343b..264ceb1 100644 --- a/examples/advanced/shader_includes.py +++ b/examples/advanced/shader_includes.py @@ -31,4 +31,4 @@ def on_render(self, time, frame_time): if __name__ == "__main__": - mglw.run_window_config(ShaderInclude) + ShaderInclude.run() diff --git a/examples/geometry_quad_fs_mouse_scroll.py b/examples/geometry_quad_fs_mouse_scroll.py index 5ba629b..e4d2126 100644 --- a/examples/geometry_quad_fs_mouse_scroll.py +++ b/examples/geometry_quad_fs_mouse_scroll.py @@ -38,4 +38,4 @@ def on_mouse_drag_event(self, x, y, dx, dy): if __name__ == "__main__": - moderngl_window.run_window_config(QuadFullscreenScroll) + QuadFullscreenScroll.run() diff --git a/examples/window_config.py b/examples/window_config.py index e269184..08a6b88 100644 --- a/examples/window_config.py +++ b/examples/window_config.py @@ -12,7 +12,7 @@ class BasicWindowConfig(moderngl_window.WindowConfig): def __init__(self, **kwargs): super().__init__(**kwargs) - def on_render(self, time, frametime): + def on_render(self, time: float, frametime: float): self.ctx.clear( (math.sin(time) + 1.0) / 2, (math.sin(time + 2) + 1.0) / 2,