comparison pytouhou/ui/window.pyx @ 533:de778a80820a

Optimise Clock a bit more.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 10 Feb 2014 18:03:50 +0100
parents 5e3e0b09a531
children aad758aef26d
comparison
equal deleted inserted replaced
532:dacdcca59b66 533:de778a80820a
9 ## This program is distributed in the hope that it will be useful, 9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14
15 cimport cython
14 16
15 IF USE_OPENGL: 17 IF USE_OPENGL:
16 from pytouhou.lib.opengl cimport \ 18 from pytouhou.lib.opengl cimport \
17 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, 19 (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
18 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 20 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
27 self._target_fps = 0 29 self._target_fps = 0
28 self._ref_tick = 0 30 self._ref_tick = 0
29 self._ref_frame = 0 31 self._ref_frame = 0
30 self._fps_tick = 0 32 self._fps_tick = 0
31 self._fps_frame = 0 33 self._fps_frame = 0
32 self._rate = 0 34 self.fps = 0
33 self.set_target_fps(fps) 35 self.set_target_fps(fps)
34 36
35 37
36 cdef void set_target_fps(self, long fps) nogil: 38 cdef void set_target_fps(self, long fps) nogil:
37 self._target_fps = fps 39 self._target_fps = fps
38 self._ref_tick = 0 40 self._ref_tick = 0
39 self._fps_tick = 0 41 self._fps_tick = 0
40 42
41 43
42 cdef double get_fps(self) nogil: 44 @cython.cdivision(True)
43 return self._rate
44
45
46 cdef void tick(self) nogil except *: 45 cdef void tick(self) nogil except *:
47 current = sdl.get_ticks() 46 current = sdl.get_ticks()
48 47
49 if not self._ref_tick: 48 if not self._ref_tick:
50 self._ref_tick = current 49 self._ref_tick = current
51 self._ref_frame = 0 50 self._ref_frame = 0
52 51
53 if self._fps_frame >= (self._target_fps if self._target_fps > 0 else 60): 52 if self._fps_frame >= (self._target_fps if self._target_fps > 0 else 60):
54 self._rate = self._fps_frame * 1000. / (current - self._fps_tick) 53 self.fps = self._fps_frame * 1000. / (current - self._fps_tick)
55 self._fps_tick = current 54 self._fps_tick = current
56 self._fps_frame = 0 55 self._fps_frame = 0
57 # If we are relying on vsync, but vsync doesn't work or is higher 56 # If we are relying on vsync, but vsync doesn't work or is higher
58 # than 60 fps, limit ourselves to 60 fps. 57 # than 60 fps, limit ourselves to 60 fps.
59 if self._target_fps < 0 and self._rate > 64.: 58 if self._target_fps < 0 and self.fps > 64.:
60 self._target_fps = 60 59 self._target_fps = 60
61 60
62 self._ref_frame += 1 61 self._ref_frame += 1
63 self._fps_frame += 1 62 self._fps_frame += 1
64 63
160 self.clock.tick() 159 self.clock.tick()
161 return running 160 return running
162 161
163 162
164 cdef double get_fps(self) nogil: 163 cdef double get_fps(self) nogil:
165 return self.clock.get_fps() 164 return self.clock.fps