66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from pathlib import Path
|
|
|
|
import moderngl_window as mglw
|
|
import numpy as np
|
|
|
|
|
|
class GameWindow(mglw.WindowConfig):
|
|
gl_version = 4, 3
|
|
window_size = 800, 600
|
|
resource_dir = (Path(__file__).parent / "resources").resolve()
|
|
buttons_held = [0, 0, 0]
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
|
|
self.program = self.load_program("render_to_screen.glsl")
|
|
self.texture = self.ctx.texture(self.window_size, 4)
|
|
|
|
buff = self.ctx.buffer(
|
|
# fmt: off
|
|
np.array([
|
|
-1.0, -1.0, 0.0,
|
|
1.0, -1.0, 0.0,
|
|
-1.0, 1.0, 0.0,
|
|
|
|
1.0, -1.0, 0.0,
|
|
1.0, 1.0, 0.0,
|
|
-1.0, 1.0, 0.0,
|
|
])
|
|
# fmt: on
|
|
.astype("f4").tobytes()
|
|
)
|
|
self.vao = self.ctx.vertex_array(self.program, buff, "in_position")
|
|
|
|
self.circle_brush_program = self.ctx.compute_shader(
|
|
self.load_text("circle_brush.glsl")
|
|
)
|
|
|
|
def on_mouse_drag_event(self, x, y, dx, dy):
|
|
if self.buttons_held[1]:
|
|
self.apply_brush(x, y, dx, dy)
|
|
|
|
def on_mouse_press_event(self, x, y, button):
|
|
self.buttons_held[button] = True
|
|
if button == 1:
|
|
self.apply_brush(x, y, 0, 0)
|
|
|
|
def on_mouse_release_event(self, x, y, button):
|
|
self.buttons_held[button] = False
|
|
|
|
def apply_brush(self, x, y, dx, dy):
|
|
self.circle_brush_program["texture0"] = 0
|
|
self.circle_brush_program["start"].value = (x, y)
|
|
self.circle_brush_program["stop"].value = (x + dx, y + dy)
|
|
self.texture.use(0)
|
|
self.circle_brush_program.run(*self.window_size)
|
|
|
|
def on_render(self, time, frametime):
|
|
self.program["texture0"] = 0
|
|
self.texture.bind_to_image(0, read=False, write=True)
|
|
self.vao.render()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
GameWindow.run()
|