-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgui_asyncio.py
50 lines (37 loc) · 1.24 KB
/
gui_asyncio.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
"""
Run the triangle/cube example in as custom event loop based on asyncio.
It uses the asynchronous path, which calls the async versions of the wgpu API.
Uses glfw as a GUI because it's loop-agnostic.
"""
# run_example = false
import time
import asyncio
import glfw
from gui_direct import MinimalGlfwCanvas, poll_glfw_briefly
# from triangle import setup_drawing_async
from cube import setup_drawing_async
async def main_loop():
# create canvas
canvas = MinimalGlfwCanvas("wgpu gui asyncio")
draw_frame = await setup_drawing_async(canvas)
last_frame_time = time.perf_counter()
frame_count = 0
while not glfw.window_should_close(canvas.window):
await asyncio.sleep(0.01)
# process inputs
glfw.poll_events()
# draw a frame
await 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__":
asyncio.run(main_loop())