Skip to content

Commit

Permalink
basic docs + example
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Nov 28, 2024
1 parent cb54ebd commit b5cc682
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
26 changes: 13 additions & 13 deletions docs/guide/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ and access simpler shortcut methods for loading resources.
self.vao = self.ctx.vertex_array(...)
self.texture = self.ctx.texture(self.wnd.size, 4)
def render(self, time, frametime):
def on_render(self, time: float, frametime: float):
# This method is called every frame
self.vao.render()
# Blocking call entering rendering/event loop
mglw.run_window_config(Test)
Test.run()
The :py:class:`~moderngl_window.context.base.window.WindowConfig`
instance will by default receive three external instances in ``__init__``
Expand Down Expand Up @@ -125,24 +125,24 @@ Window events

.. code:: python
def resize(self, width: int, height: int):
def on_resize(self, width: int, height: int):
print("Window was resized. buffer size is {} x {}".format(width, height))
def close(self):
def on_close(self):
print("The window is closing")
def iconify(self, iconify: bool):
def on_iconify(self, iconify: bool):
print("Window was iconified:", iconify)
Keyboard input
--------------

Implement the ``key_event`` and ``unicode_char_entered`` method to handle
Implement the ``on_key_event`` and ``on_unicode_char_entered`` method to handle
key events.

.. code:: python
def key_event(self, key, action, modifiers):
def on_key_event(self, key, action, modifiers):
# Key presses
if action == self.wnd.keys.ACTION_PRESS:
if key == self.wnd.keys.SPACE:
Expand All @@ -161,7 +161,7 @@ key events.
if key == self.wnd.keys.SPACE:
print("SPACE key was released")
def unicode_char_entered(self, char: str):
def on_unicode_char_entered(self, char: str):
print('character entered:', char)
Expand All @@ -172,17 +172,17 @@ Implement the ``mouse_*`` methods to handle mouse input.

.. code:: python
def mouse_position_event(self, x, y, dx, dy):
def on_mouse_position_event(self, x, y, dx, dy):
print("Mouse position:", x, y, dx, dy)
def mouse_drag_event(self, x, y, dx, dy):
def on_mouse_drag_event(self, x, y, dx, dy):
print("Mouse drag:", x, y, dx, dy)
def mouse_scroll_event(self, x_offset: float, y_offset: float):
def on_mouse_scroll_event(self, x_offset: float, y_offset: float):
print("Mouse wheel:", x_offset, y_offset)
def mouse_press_event(self, x, y, button):
def on_mouse_press_event(self, x, y, button):
print("Mouse button {} pressed at {}, {}".format(button, x, y))
def mouse_release_event(self, x: int, y: int, button: int):
def on_mouse_release_event(self, x: int, y: int, button: int):
print("Mouse button {} released at {}, {}".format(button, x, y))
10 changes: 5 additions & 5 deletions examples/cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class Cubes(moderngl_window.WindowConfig):
title = "Cubes"
resizable = True
aspect_ratio = None
aspect_ratio = 16 / 9
resource_dir = Path(__file__).parent.resolve() / "resources"

def __init__(self, **kwargs):
Expand All @@ -28,7 +28,7 @@ def __init__(self, **kwargs):
self.box_t2_c3_v3 = self.load_scene("scenes/box/box-T2F_C3F_V3F.obj")
self.box_t2_n3_v3 = self.load_scene("scenes/box/box-T2F_N3F_V3F.obj")

self.resize(*self.wnd.size)
self.on_resize(*self.wnd.size)

def on_render(self, time, frame_time):
self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE)
Expand Down Expand Up @@ -58,9 +58,9 @@ def on_render(self, time, frame_time):
view = glm.translate(glm.vec3(5, -2, -10))
self.box_t2_n3_v3.draw(self.projection, view * rot)

def resize(self, width, height):
self.ctx.viewport = 0, 0, width, height
self.projection = glm.perspective(glm.radians(45), width / height, 1, 50)
def on_resize(self, width, height):
super().on_resize(width, height)
self.projection = glm.perspective(glm.radians(45), self.aspect_ratio, 1, 50)


if __name__ == "__main__":
Expand Down

0 comments on commit b5cc682

Please sign in to comment.