Mercurial > touhou
annotate pytouhou/ui/gamerenderer.pyx @ 301:6f1ca1cb5238
Avoid segfaults in the unlikely case a huge number of sprites is rendered.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 10 Mar 2012 20:30:55 +0100 |
parents | 94c636f8f863 |
children | f3099ebf4f61 |
rev | line source |
---|---|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 ## |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
15 |
201 | 16 from itertools import chain |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
17 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
18 from pyglet.gl import * |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
19 |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
20 from .renderer cimport Renderer |
223
98c64ffcbdff
Make pytouhou.ui.{background,texture} Cython modules as they are only used by Cython modules.
Thibaut Girka <thib@sitedethib.com>
parents:
222
diff
changeset
|
21 from .background cimport get_background_rendering_data |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
22 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
23 |
125
0313ca2c50e9
Small refactoring and massive performance improvements
Thibaut Girka <thib@sitedethib.com>
parents:
123
diff
changeset
|
24 |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
25 cdef class GameRenderer(Renderer): |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
26 cdef public game |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
27 cdef public background |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
28 |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
29 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
30 def __init__(self, resource_loader, game=None, background=None): |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
31 Renderer.__init__(self, resource_loader) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
32 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
33 self.game = game |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
34 self.background = background |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
35 |
254
6bd565019f9a
Preload textures to avoid slowdowns during gameplay
Thibaut Girka <thib@sitedethib.com>
parents:
242
diff
changeset
|
36 if game: |
6bd565019f9a
Preload textures to avoid slowdowns during gameplay
Thibaut Girka <thib@sitedethib.com>
parents:
242
diff
changeset
|
37 # Preload textures |
6bd565019f9a
Preload textures to avoid slowdowns during gameplay
Thibaut Girka <thib@sitedethib.com>
parents:
242
diff
changeset
|
38 self.texture_manager.preload(game.resource_loader.instanced_anms.values()) |
6bd565019f9a
Preload textures to avoid slowdowns during gameplay
Thibaut Girka <thib@sitedethib.com>
parents:
242
diff
changeset
|
39 |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
40 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
130
diff
changeset
|
41 def render(self): |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
42 glClear(GL_DEPTH_BUFFER_BIT) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
43 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
44 back = self.background |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
45 game = self.game |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
46 texture_manager = self.texture_manager |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
47 |
221
5c3600e0f0cd
Don’t try to render effect if there is no game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
217
diff
changeset
|
48 if game is not None and game.effect is not None: |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
49 self.setup_camera(0, 0, 1) |
217
577f45454402
Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
205
diff
changeset
|
50 |
577f45454402
Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
205
diff
changeset
|
51 glDisable(GL_FOG) |
577f45454402
Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
205
diff
changeset
|
52 self.render_elements([game.effect]) |
577f45454402
Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
205
diff
changeset
|
53 glEnable(GL_FOG) |
577f45454402
Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
205
diff
changeset
|
54 elif back is not None: |
111
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
55 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
56 x, y, z = back.position_interpolator.values |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
57 dx, dy, dz = back.position2_interpolator.values |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
58 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
59 glFogi(GL_FOG_MODE, GL_LINEAR) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
60 glFogf(GL_FOG_START, fog_start) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
61 glFogf(GL_FOG_END, fog_end) |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
62 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.)) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
63 |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
64 self.setup_camera(dx, dy, dz) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
65 glTranslatef(-x, -y, -z) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
66 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
67 glEnable(GL_DEPTH_TEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
68 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back): |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
69 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
70 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
71 glVertexPointer(3, GL_FLOAT, 0, vertices) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
72 glTexCoordPointer(2, GL_FLOAT, 0, uvs) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
73 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
74 glDrawArrays(GL_QUADS, 0, nb_vertices) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
75 glDisable(GL_DEPTH_TEST) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
76 else: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
77 glClear(GL_COLOR_BUFFER_BIT) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
78 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
79 if game is not None: |
222
5cac48b328ad
Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents:
221
diff
changeset
|
80 self.setup_camera(0, 0, 1) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
81 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
82 glDisable(GL_FOG) |
271
80e73b8245a4
Fix visibility handling
Thibaut Girka <thib@sitedethib.com>
parents:
254
diff
changeset
|
83 self.render_elements(chain(*(enemy.objects() for enemy in game.enemies if enemy._visible))) |
80e73b8245a4
Fix visibility handling
Thibaut Girka <thib@sitedethib.com>
parents:
254
diff
changeset
|
84 self.render_elements(enemy for enemy in game.enemies if enemy._visible) |
166
dcf32488a2c9
Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
164
diff
changeset
|
85 self.render_elements(game.effects) |
201 | 86 self.render_elements(chain(game.players_bullets, |
294
94c636f8f863
Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
286
diff
changeset
|
87 game.lasers_sprites(), |
201 | 88 game.players, |
286
4838e9bab0f9
Implement dialogs (MSG files).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
274
diff
changeset
|
89 game.msg_sprites(), |
201 | 90 *(player.objects() for player in game.players))) |
274
f037bca24f2d
Partially implement lasers.
Thibaut Girka <thib@sitedethib.com>
parents:
271
diff
changeset
|
91 self.render_elements(chain(game.bullets, game.lasers, game.cancelled_bullets, game.items)) |
150
4f46717390aa
Introduce items, implement ECL instruction 83
Thibaut Girka <thib@sitedethib.com>
parents:
148
diff
changeset
|
92 #TODO: display item indicators |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
93 glEnable(GL_FOG) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
94 |