Debian Codegraph @

experimental/win32priority.py

raw symbols SCIP
1 # Utilities to patch Pyglet to provide more precise event timing on Windows.
2 # HighTimerResolution is generally the most important of the bunch,
3 # since most 60+fps games in Windows will not work right without
4 # changing the timer resolution for the app
5
6 import pyglet
7 import ctypes
8
9 try:
10 winmm = ctypes.windll.winmm
11 k32 = ctypes.windll.kernel32
12 except AttributeError:
13 raise NotImplementedError("This is not a Win32 compatible platform.")
14
15
16 class HighTimerResolution:
17 """
18 A context manager to adjust the main Win32 platform event loop.
19
20 Events inside the context manager will use the specified resolution of the
21 multimedia timer to provide proper timings for 60fps+ framerates.
22 On exiting the context manager, the default resolution timer is restored.
23
24 Most 60fps games can use a resolution of 4, but you may need to go as low
25 as 1 to get the most consistent results.
26
27 Example:
28
29 with HighTimerResolution():
30 pyglet.app.run()
31
32 """
33
34 def __init__(self, resolution=1):
35 self.resolution = resolution
36
37 def __enter__(self):
38 winmm.timeBeginPeriod(self.resolution)
39
40 def __exit__(self, *a):
41 winmm.timeEndPeriod()
42
43
44 def pin_thread(pin_thread_mask=0x01):
45 """
46 Pin the main Pyglet app thread to one or more cores by way of a thread affinity mask.
47 The default thread mask is 1.
48 """
49 k32.SetThreadAffinityMask(
50 pyglet.app.platform_event_loop._event_thread, pin_thread_mask
51 )
52
53
54 def pin_process():
55 """
56 Pins the entire Python interpreter process to the first CPU core.
57 This may slightly improve performance due to affinity.
58 """
59 k32.SetProcessAffinityMask(k32.GetCurrentProcess(), 1)
60
61
62 def raise_priority(
63 priority_class=0x080,
64 ):
65 """
66 Raises the process priority to High. Other priorities can be provided.
67 """
68 k32.SetPriorityClass(-1, priority_class)