Mercurial > touhou
comparison pytouhou/ui/gamerunner.py @ 419:1c92721f8e49
Re-implement frame control
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 06 Feb 2013 18:45:12 +0100 |
parents | 63f59be04a54 |
children | 3a7b36324611 |
comparison
equal
deleted
inserted
replaced
418:63f59be04a54 | 419:1c92721f8e49 |
---|---|
34 | 34 |
35 | 35 |
36 logger = get_logger(__name__) | 36 logger = get_logger(__name__) |
37 | 37 |
38 | 38 |
39 class Clock(object): | |
40 def __init__(self, fps=None): | |
41 self._target_fps = 0 | |
42 self._ref_tick = 0 | |
43 self._ref_frame = 0 | |
44 self._fps_tick = 0 | |
45 self._fps_frame = 0 | |
46 self._rate = 0 | |
47 self.set_target_fps(fps) | |
48 | |
49 | |
50 def set_target_fps(self, fps): | |
51 self._target_fps = fps | |
52 self._ref_tick = 0 | |
53 self._fps_tick = 0 | |
54 | |
55 | |
56 def get_fps(self): | |
57 return self._rate | |
58 | |
59 | |
60 def tick(self): | |
61 current = sdl.get_ticks() | |
62 | |
63 if not self._ref_tick: | |
64 self._ref_tick = current | |
65 self._ref_frame = 0 | |
66 | |
67 if self._fps_frame >= (self._target_fps or 60): | |
68 self._rate = self._fps_frame * 1000. / (current - self._fps_tick) | |
69 self._fps_tick = current | |
70 self._fps_frame = 0 | |
71 | |
72 self._ref_frame += 1 | |
73 self._fps_frame += 1 | |
74 | |
75 target_tick = self._ref_tick | |
76 if self._target_fps: | |
77 target_tick += int(self._ref_frame * 1000 / self._target_fps) | |
78 | |
79 if current <= target_tick: | |
80 sdl.delay(target_tick - current) | |
81 else: | |
82 self._ref_tick = current | |
83 self._ref_frame = 0 | |
84 | |
85 | |
39 class GameRunner(GameRenderer): | 86 class GameRunner(GameRenderer): |
40 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False, skip=False): | 87 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False, skip=False): |
41 GameRenderer.__init__(self, resource_loader, game, background) | 88 GameRenderer.__init__(self, resource_loader, game, background) |
42 | 89 |
43 sdl.init(sdl.INIT_VIDEO) | 90 sdl.init(sdl.INIT_VIDEO) |
70 self.vbo, self.back_vbo = vbo_array | 117 self.vbo, self.back_vbo = vbo_array |
71 | 118 |
72 if game: | 119 if game: |
73 self.load_game(game, background, replay) | 120 self.load_game(game, background, replay) |
74 | 121 |
75 #self.clock = pyglet.clock.get_default() | 122 self.clock = Clock(self.fps_limit) |
76 | 123 |
77 | 124 |
78 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None): | 125 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None): |
79 GameRenderer.load_game(self, game, background) | 126 GameRenderer.load_game(self, game, background) |
80 self.set_input(replay) | 127 self.set_input(replay) |
123 101010101./2010101., 101010101./10101.) | 170 101010101./2010101., 101010101./10101.) |
124 game_view = setup_camera(0, 0, 1) | 171 game_view = setup_camera(0, 0, 1) |
125 self.game_mvp = game_view * self.proj | 172 self.game_mvp = game_view * self.proj |
126 self.interface_mvp = ortho_2d(0., float(self.width), float(self.height), 0.) | 173 self.interface_mvp = ortho_2d(0., float(self.width), float(self.height), 0.) |
127 | 174 |
128 #if self.fps_limit > 0: | |
129 # pyglet.clock.set_fps_limit(self.fps_limit) | |
130 while not self.has_exit: | 175 while not self.has_exit: |
131 if not self.skip: | 176 if not self.skip: |
132 self.update() | 177 self.update() |
133 self.render_game() | 178 self.render_game() |
134 self.render_interface() | 179 self.render_interface() |
135 self.win.gl_swap_window() | 180 self.win.gl_swap_window() |
181 self.clock.tick() | |
136 else: | 182 else: |
137 self.update() | 183 self.update() |
138 | 184 |
139 if not self.use_fixed_pipeline: | 185 if not self.use_fixed_pipeline: |
140 vbo_array = (c_uint * 2)(self.vbo, self.back_vbo) | 186 vbo_array = (c_uint * 2)(self.vbo, self.back_vbo) |
206 glDisable(GL_SCISSOR_TEST) | 252 glDisable(GL_SCISSOR_TEST) |
207 | 253 |
208 | 254 |
209 def render_interface(self): | 255 def render_interface(self): |
210 interface = self.game.interface | 256 interface = self.game.interface |
211 #interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_ticks()) | 257 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps()) |
212 | 258 |
213 if self.use_fixed_pipeline: | 259 if self.use_fixed_pipeline: |
214 glMatrixMode(GL_MODELVIEW) | 260 glMatrixMode(GL_MODELVIEW) |
215 glLoadMatrixf(self.interface_mvp.get_c_data()) | 261 glLoadMatrixf(self.interface_mvp.get_c_data()) |
216 glDisable(GL_FOG) | 262 glDisable(GL_FOG) |