annotate pytouhou/ui/window.pyx @ 579:b8df946d394d

Add grouping for OpenGL calls, making traces much more readable.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 17 Aug 2014 16:16:58 +0200
parents 00f228b57840
children 7a82c4b52b16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
15 cimport cython
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
16
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
18 cdef class Clock:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
19 def __init__(self, long fps=-1):
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 self._target_fps = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 self._ref_tick = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 self._ref_frame = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 self._fps_tick = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 self._fps_frame = 0
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
25 self.fps = 0
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 self.set_target_fps(fps)
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
29 cdef void set_target_fps(self, long fps) nogil:
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 self._target_fps = fps
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 self._ref_tick = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 self._fps_tick = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
35 @cython.cdivision(True)
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
36 cdef void tick(self) nogil except *:
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 current = sdl.get_ticks()
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 if not self._ref_tick:
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 self._ref_tick = current
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 self._ref_frame = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
452
1f5156093785 By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents: 450
diff changeset
43 if self._fps_frame >= (self._target_fps if self._target_fps > 0 else 60):
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
44 self.fps = self._fps_frame * 1000. / (current - self._fps_tick)
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 self._fps_tick = current
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 self._fps_frame = 0
452
1f5156093785 By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents: 450
diff changeset
47 # If we are relying on vsync, but vsync doesn't work or is higher
1f5156093785 By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents: 450
diff changeset
48 # than 60 fps, limit ourselves to 60 fps.
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
49 if self._target_fps < 0 and self.fps > 64.:
452
1f5156093785 By default, only enable fps limiting if vsync doesn't do the job.
Thibaut Girka <thib@sitedethib.com>
parents: 450
diff changeset
50 self._target_fps = 60
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 self._ref_frame += 1
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 self._fps_frame += 1
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 target_tick = self._ref_tick
496
104c737ce8b3 Test target FPS for natural instead of non-zero.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 491
diff changeset
56 if self._target_fps > 0:
491
2276229282fd Fix gcc’s warnings with -Wall -Wextra.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 469
diff changeset
57 target_tick += <unsigned long>(self._ref_frame * 1000 / self._target_fps)
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if current <= target_tick:
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 sdl.delay(target_tick - current)
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 else:
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 self._ref_tick = current
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 self._ref_frame = 0
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
463
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
67 cdef class Runner:
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
68 cdef void start(self) except *:
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
69 pass
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
70
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
71 cdef void finish(self) except *:
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
72 pass
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
73
578
00f228b57840 Make Runner.update callable from Python code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
74 cpdef bint update(self) except? False:
463
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
75 return False
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
76
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
77
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
78
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
79 cdef class Window:
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
80 def __init__(self, backend, long fps_limit=-1):
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 self.runner = None
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
83 if backend is not None:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
84 self.win = backend.create_window(
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
85 'PyTouhou',
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
86 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
87 640, 480) #XXX
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
552
aad758aef26d Don’t store framerate limit in the Window while it already is in the Clock.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 533
diff changeset
89 self.clock = Clock(fps_limit)
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
92 cdef void set_size(self, int width, int height) nogil:
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 self.win.set_window_size(width, height)
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95
463
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
96 cpdef set_runner(self, Runner runner=None):
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 self.runner = runner
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
98 if runner is not None:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
99 runner.start()
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
102 cpdef run(self):
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 try:
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 while self.run_frame():
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 pass
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 finally:
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 self.runner.finish()
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109
455
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
110 cdef bint run_frame(self) except? False:
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
111 cdef bint running = False
6864a38b2413 Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 452
diff changeset
112 if self.runner is not None:
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 running = self.runner.update()
553
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
114 if self.win is not None:
8f51e34d911c Refactor graphics backend selection, to make them fallbackable and optional.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 552
diff changeset
115 self.win.present()
422
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 self.clock.tick()
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 return running
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118
52829ebe2561 Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
463
11708a1d0a1a Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 460
diff changeset
120 cdef double get_fps(self) nogil:
533
de778a80820a Optimise Clock a bit more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
121 return self.clock.fps