-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgui_direct.py
76 lines (55 loc) · 1.98 KB
/
gui_direct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Direct integration of glfw and wgpu-py without using the
wgpu.gui Canvas abstraction/class hierarchy.
Demonstration for hardcore users that need total low-level control.
"""
# run_example = false
import time
import atexit
import glfw
from wgpu.backends.wgpu_native import GPUCanvasContext
from wgpu.gui.glfw import get_glfw_present_methods, poll_glfw_briefly
# from triangle import setup_drawing_sync
from cube import setup_drawing_sync
# Setup glfw
glfw.init()
atexit.register(glfw.terminate)
class MinimalGlfwCanvas: # implements WgpuCanvasInterface
"""Minimal canvas interface required by wgpu."""
def __init__(self, title):
# disable automatic API selection, we are not using opengl
glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
glfw.window_hint(glfw.RESIZABLE, True)
self.window = glfw.create_window(640, 480, title, None, None)
self.context = GPUCanvasContext(self, get_glfw_present_methods(self.window))
def get_physical_size(self):
"""get framebuffer size in integer pixels"""
psize = glfw.get_framebuffer_size(self.window)
return int(psize[0]), int(psize[1])
def get_context(self, kind="wgpu"):
return self.context
def main():
# create canvas
canvas = MinimalGlfwCanvas("wgpu gui direct")
draw_frame = setup_drawing_sync(canvas)
last_frame_time = time.perf_counter()
frame_count = 0
# render loop
while not glfw.window_should_close(canvas.window):
# process inputs
glfw.poll_events()
# draw a frame
draw_frame()
# present the frame to the screen
canvas.context.present()
# stats
frame_count += 1
etime = time.perf_counter() - last_frame_time
if etime > 1:
print(f"{frame_count / etime:0.1f} FPS")
last_frame_time, frame_count = time.perf_counter(), 0
# dispose resources
glfw.destroy_window(canvas.window)
poll_glfw_briefly()
if __name__ == "__main__":
main()