Debian Codegraph @

src/pygame_sdl2/pygame_time.pyx

raw
1 # Copyright 2014 Patrick Dawson <[email protected]>
2 #
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the authors be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18
19 import cython
20 import math
21 from sdl2 cimport *
22 from pygame_sdl2.error import error
23 import pygame_sdl2
24
25 cdef int timer_id = 0
26
27 @pygame_sdl2.register_init
28 def init():
29 pygame_sdl2.display.sdl_main_init()
30
31 if SDL_InitSubSystem(SDL_INIT_TIMER):
32 raise error()
33
34 @pygame_sdl2.register_quit
35 def quit(): # @ReservedAssignment
36 SDL_QuitSubSystem(SDL_INIT_TIMER)
37
38 def get_ticks():
39 return SDL_GetTicks()
40
41 def wait(int milliseconds):
42 cdef int start
43
44 start = SDL_GetTicks()
45 with nogil:
46 SDL_Delay(milliseconds)
47 return SDL_GetTicks() - start
48
49 def delay(milliseconds):
50 # SDL_Delay() should be accurate enough.
51 return wait(milliseconds)
52
53 cdef Uint32 timer_callback(Uint32 interval, void *param) nogil:
54 cdef SDL_Event e
55 e.type = <int>param
56 e.user.code = 0
57 e.user.data1 = NULL
58 e.user.data2 = NULL
59 SDL_PushEvent(&e)
60 return interval
61
62 # A map from eventid to SDL_Timer_ID.
63 cdef dict timer_by_event = { }
64
65 def set_timer(eventid, milliseconds):
66
67 cdef int timer_id = timer_by_event.get(eventid, 0)
68
69 if timer_id != 0:
70 SDL_RemoveTimer(timer_id)
71 timer_id = 0
72
73 if milliseconds > 0:
74 timer_id = SDL_AddTimer(milliseconds, <SDL_TimerCallback>timer_callback, <void*><int>eventid)
75 if timer_id == 0:
76 raise error()
77
78 timer_by_event[eventid] = timer_id
79
80 class Clock:
81 def __init__(self):
82 self.last = SDL_GetTicks()
83 self.last_frames = []
84 self.frametime = 0
85 self.raw_frametime = 0
86
87 def tick(self, framerate=0):
88 cdef int now = SDL_GetTicks()
89 self.raw_frametime = now - self.last
90 while len(self.last_frames) > 9:
91 self.last_frames.pop(0)
92 if framerate == 0:
93 self.last = now
94 self.last_frames.append(self.raw_frametime)
95 return self.raw_frametime
96 cdef int frame_duration = 1.0 / framerate * 1000
97 if self.raw_frametime < frame_duration:
98 delay(frame_duration - self.raw_frametime)
99 now = SDL_GetTicks()
100 self.frametime = now - self.last
101 self.last = now
102 self.last_frames.append(self.frametime)
103 return self.frametime
104
105 def tick_busy_loop(self, framerate=0):
106 return self.tick(framerate)
107
108 def get_time(self):
109 return self.frametime
110
111 def get_rawtime(self):
112 return self.raw_frametime
113
114 @cython.cdivision(True)
115 def get_fps(self):
116 cdef int total_time = sum(self.last_frames)
117 cdef float average_time = total_time / 1000.0 / len(self.last_frames)
118 cdef float average_fps = 1.0 / average_time
119 return 0 if math.isnan(average_fps) else average_fps