annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
15 from pytouhou.lib import sdl
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
16
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
17 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable,
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
18 glHint, glEnableClientState, glViewport, glScissor,
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
19 glLoadMatrixf, glGenBuffers, glDeleteBuffers,
203
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
20 GL_MODELVIEW, GL_PROJECTION,
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
21 GL_TEXTURE_2D, GL_BLEND, GL_FOG,
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
22 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
23 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
24 GL_SCISSOR_TEST)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
26 from pytouhou.utils.helpers import get_logger
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
27 from pytouhou.utils.maths import perspective, setup_camera, ortho_2d
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
28
205
ee6dfd14a785 Rename pytouhou.opengl to pytouhou.ui, makes much more sense that way.
Thibaut Girka <thib@sitedethib.com>
parents: 203
diff changeset
29 from .gamerenderer import GameRenderer
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 378
diff changeset
30 from .music import MusicPlayer, SFXPlayer, NullPlayer
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
31 from .shaders.eosd import GameShader, BackgroundShader
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
33 from ctypes import c_uint, byref
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
34
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
36 logger = get_logger(__name__)
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
37
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
38
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
39 class Clock(object):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
40 def __init__(self, fps=None):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
41 self._target_fps = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
42 self._ref_tick = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
43 self._ref_frame = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
44 self._fps_tick = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
45 self._fps_frame = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
46 self._rate = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
47 self.set_target_fps(fps)
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
48
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
49
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
50 def set_target_fps(self, fps):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
51 self._target_fps = fps
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
52 self._ref_tick = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
53 self._fps_tick = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
54
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
55
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
56 def get_fps(self):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
57 return self._rate
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
58
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
59
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
60 def tick(self):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
61 current = sdl.get_ticks()
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
62
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
63 if not self._ref_tick:
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
64 self._ref_tick = current
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
65 self._ref_frame = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
66
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
67 if self._fps_frame >= (self._target_fps or 60):
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
68 self._rate = self._fps_frame * 1000. / (current - self._fps_tick)
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
69 self._fps_tick = current
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
70 self._fps_frame = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
71
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
72 self._ref_frame += 1
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
73 self._fps_frame += 1
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
74
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
75 target_tick = self._ref_tick
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
76 if self._target_fps:
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
77 target_tick += int(self._ref_frame * 1000 / self._target_fps)
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
78
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
79 if current <= target_tick:
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
80 sdl.delay(target_tick - current)
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
81 else:
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
82 self._ref_tick = current
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
83 self._ref_frame = 0
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
84
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
85
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
86 class GameRunner(GameRenderer):
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
87 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False, skip=False):
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 GameRenderer.__init__(self, resource_loader, game, background)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
89
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
90 sdl.init(sdl.INIT_VIDEO)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
91 sdl.gl_set_attribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
92 sdl.gl_set_attribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
93 sdl.gl_set_attribute(sdl.GL_DOUBLEBUFFER, int(double_buffer))
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
94 sdl.gl_set_attribute(sdl.GL_DEPTH_SIZE, 24)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
95
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
96 self.width, self.height = (game.interface.width, game.interface.height) if game else (640, 480)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
97 self.win = sdl.Window('PyTouhou',
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
98 sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
99 self.width, self.height,
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
100 sdl.WINDOW_OPENGL | sdl.WINDOW_SHOWN)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
101 self.win.gl_create_context()
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
102
334
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
103 self.fps_limit = fps_limit
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
104 self.use_fixed_pipeline = fixed_pipeline
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
105 self.replay_level = None
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
106 self.skip = skip
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
107 self.has_exit = False
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
108 self.keystate = 0
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
109
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
110 if not self.use_fixed_pipeline:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
111 self.game_shader = GameShader()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
112 self.background_shader = BackgroundShader()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
113 self.interface_shader = self.game_shader
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
114
399
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
115 vbo_array = (c_uint * 2)()
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
116 glGenBuffers(2, vbo_array)
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
117 self.vbo, self.back_vbo = vbo_array
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
118
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
119 if game:
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
120 self.load_game(game, background, replay)
330
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
121
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
122 self.clock = Clock(self.fps_limit)
330
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
123
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
124
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
125 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None):
330
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
126 GameRenderer.load_game(self, game, background)
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
127 self.set_input(replay)
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
128 if replay and replay.levels[game.stage - 1]:
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
129 game.players[0].state.lives = self.replay_level.lives
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
130 game.players[0].state.power = self.replay_level.power
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
131 game.players[0].state.bombs = self.replay_level.bombs
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
132 game.difficulty = self.replay_level.difficulty
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
133
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
134 self.save_keystates = save_keystates
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
135
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
136 game.music = MusicPlayer(game.resource_loader, bgms)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
137 game.music.play(0)
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 378
diff changeset
138 game.sfx_player = SFXPlayer(game.resource_loader) if not self.skip else NullPlayer()
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 341
diff changeset
139
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
141 def set_input(self, replay=None):
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
142 if not replay or not replay.levels[self.game.stage-1]:
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
143 self.replay_level = None
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
144 else:
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
145 self.replay_level = replay.levels[self.game.stage-1]
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
146 self.keys = self.replay_level.iter_keystates()
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
147
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
148
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
149 def set_size(self, width, height):
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
150 self.win.set_window_size(width, height)
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
151
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
152
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
153 def start(self, width=None, height=None):
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
154 width = width or (self.game.interface.width if self.game else 640)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
155 height = height or (self.game.interface.height if self.game else 480)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
156 if (width, height) != (self.width, self.height):
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
157 self.set_size(width, height)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159 # Initialize OpenGL
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 glEnable(GL_BLEND)
394
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
161 if self.use_fixed_pipeline:
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
162 glEnable(GL_TEXTURE_2D)
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
163 glHint(GL_FOG_HINT, GL_NICEST)
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
164 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
165 glEnableClientState(GL_COLOR_ARRAY)
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
166 glEnableClientState(GL_VERTEX_ARRAY)
346614f788f1 Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 387
diff changeset
167 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
169 self.proj = perspective(30, float(self.game.width) / float(self.game.height),
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
170 101010101./2010101., 101010101./10101.)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
171 game_view = setup_camera(0, 0, 1)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
172 self.game_mvp = game_view * self.proj
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
173 self.interface_mvp = ortho_2d(0., float(self.width), float(self.height), 0.)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
174
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
175 while not self.has_exit:
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
176 if not self.skip:
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
177 self.update()
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
178 self.render_game()
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
179 self.render_interface()
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
180 self.win.gl_swap_window()
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
181 self.clock.tick()
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
182 else:
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
183 self.update()
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
184
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
185 if not self.use_fixed_pipeline:
399
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
186 vbo_array = (c_uint * 2)(self.vbo, self.back_vbo)
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 396
diff changeset
187 glDeleteBuffers(2, vbo_array)
396
34a91f918e7c Use Buffer Objects instead of host pointers.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 394
diff changeset
188
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
189 self.win.gl_delete_context()
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
190 self.win.destroy_window()
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
191 sdl.quit()
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
192
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
193
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
194 def update(self):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
195 if self.background:
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 146
diff changeset
196 self.background.update(self.game.frame)
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
197 for event in sdl.poll_events():
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
198 type_ = event[0]
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
199 if type_ == sdl.KEYDOWN:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
200 scancode = event[1]
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
201 if scancode == sdl.SCANCODE_ESCAPE:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
202 self.has_exit = True #TODO: implement the pause.
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
203 elif type_ == sdl.QUIT:
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
204 self.has_exit = True
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
205 if self.game:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
206 if not self.replay_level:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
207 #TODO: allow user settings
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
208 keys = sdl.get_keyboard_state()
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
209 keystate = 0
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
210 if keys[sdl.SCANCODE_Z]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
211 keystate |= 1
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
212 if keys[sdl.SCANCODE_X]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
213 keystate |= 2
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
214 if keys[sdl.SCANCODE_LSHIFT]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
215 keystate |= 4
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
216 if keys[sdl.SCANCODE_UP]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
217 keystate |= 16
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
218 if keys[sdl.SCANCODE_DOWN]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
219 keystate |= 32
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
220 if keys[sdl.SCANCODE_LEFT]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
221 keystate |= 64
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
222 if keys[sdl.SCANCODE_RIGHT]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
223 keystate |= 128
418
63f59be04a54 Replace Pyglet with SDL2 for window creation and events; disables framerate control/display and sound.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
224 if keys[sdl.SCANCODE_LCTRL]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
225 keystate |= 256
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
226 else:
374
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
227 try:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
228 keystate = self.keys.next()
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
229 except StopIteration:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
230 keystate = 0
378
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
231 if self.skip:
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
232 self.set_input()
11d895b6c0dc Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
233 self.skip = False
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 378
diff changeset
234 self.game.sfx_player = SFXPlayer(self.game.resource_loader)
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
235
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
236 if self.save_keystates is not None:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
237 self.save_keystates.append(keystate)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
238
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
239 self.game.run_iter(keystate)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
240
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
241
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
242 def render_game(self):
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
243 # Switch to game projection
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
244 #TODO: move that to GameRenderer?
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
245 x, y = self.game.interface.game_pos
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
246 glViewport(x, y, self.game.width, self.game.height)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
247 glScissor(x, y, self.game.width, self.game.height)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
248 glEnable(GL_SCISSOR_TEST)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
249
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
250 GameRenderer.render(self)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
251
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
252 glDisable(GL_SCISSOR_TEST)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
253
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
254
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
255 def render_interface(self):
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
256 interface = self.game.interface
419
1c92721f8e49 Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents: 418
diff changeset
257 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps())
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
258
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
259 if self.use_fixed_pipeline:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
260 glMatrixMode(GL_MODELVIEW)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
261 glLoadMatrixf(self.interface_mvp.get_c_data())
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
262 glDisable(GL_FOG)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
263 else:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
264 self.interface_shader.bind()
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 353
diff changeset
265 self.interface_shader.uniform_matrixf('mvp', self.interface_mvp.get_c_data())
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
266 glViewport(0, 0, self.width, self.height)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
267
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
268 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
269 labels = interface.labels.values()
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
270
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
271 if items:
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
272 # Redraw all the interface
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
273 self.render_elements(items)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
274 else:
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
275 # Redraw only changed labels
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
276 labels = [label for label in labels if label.changed]
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
277
387
e1f5dcd4b83e Display something at the start of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
278 self.render_elements(interface.level_start)
e1f5dcd4b83e Display something at the start of a stage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
279
345
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
280 if self.game.boss:
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
281 self.render_elements(interface.boss_items)
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
282
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
283 self.render_elements(labels)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
284 for label in labels:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
285 label.changed = False
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
286