changeset 533:de778a80820a

Optimise Clock a bit more.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 10 Feb 2014 18:03:50 +0100
parents dacdcca59b66
children 346cbf266856
files pytouhou/ui/sdl/gamerenderer.py pytouhou/ui/window.pxd pytouhou/ui/window.pyx
diffstat 3 files changed, 9 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/ui/sdl/gamerenderer.py
+++ b/pytouhou/ui/sdl/gamerenderer.py
@@ -68,7 +68,7 @@ class GameRenderer(object):
 
 
     def render_interface(self, interface, boss):
-        interface.labels['framerate'].set_text('%.2ffps' % self.window.clock.get_fps())
+        interface.labels['framerate'].set_text('%.2ffps' % self.window.get_fps())
 
         self.window.win.render_set_viewport(Rect(0, 0, interface.width, interface.height))
         self.window.win.render_set_clip_rect(Rect(0, 0, interface.width, interface.height))
--- a/pytouhou/ui/window.pxd
+++ b/pytouhou/ui/window.pxd
@@ -4,10 +4,9 @@ from pytouhou.lib cimport sdl
 cdef class Clock:
     cdef unsigned long _ref_tick
     cdef long _target_fps, _ref_frame, _fps_tick, _fps_frame
-    cdef double _rate
+    cdef double fps
 
     cdef void set_target_fps(self, long fps) nogil
-    cdef double get_fps(self) nogil
     cdef void tick(self) nogil except *
 
 
--- a/pytouhou/ui/window.pyx
+++ b/pytouhou/ui/window.pyx
@@ -12,6 +12,8 @@
 ## GNU General Public License for more details.
 ##
 
+cimport cython
+
 IF USE_OPENGL:
     from pytouhou.lib.opengl cimport \
              (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND,
@@ -29,7 +31,7 @@ cdef class Clock:
         self._ref_frame = 0
         self._fps_tick = 0
         self._fps_frame = 0
-        self._rate = 0
+        self.fps = 0
         self.set_target_fps(fps)
 
 
@@ -39,10 +41,7 @@ cdef class Clock:
         self._fps_tick = 0
 
 
-    cdef double get_fps(self) nogil:
-        return self._rate
-
-
+    @cython.cdivision(True)
     cdef void tick(self) nogil except *:
         current = sdl.get_ticks()
 
@@ -51,12 +50,12 @@ cdef class Clock:
             self._ref_frame = 0
 
         if self._fps_frame >= (self._target_fps if self._target_fps > 0 else 60):
-            self._rate = self._fps_frame * 1000. / (current - self._fps_tick)
+            self.fps = self._fps_frame * 1000. / (current - self._fps_tick)
             self._fps_tick = current
             self._fps_frame = 0
             # If we are relying on vsync, but vsync doesn't work or is higher
             # than 60 fps, limit ourselves to 60 fps.
-            if self._target_fps < 0 and self._rate > 64.:
+            if self._target_fps < 0 and self.fps > 64.:
                 self._target_fps = 60
 
         self._ref_frame += 1
@@ -162,4 +161,4 @@ cdef class Window:
 
 
     cdef double get_fps(self) nogil:
-        return self.clock.get_fps()
+        return self.clock.fps