comparison pytouhou/ui/renderer.pyx @ 456:cae1ae9de430

Add native text support, MSG instructions 3 and 8, and text at the beginning of a stage.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:11:40 +0200
parents d56536ef28e8
children 6af3854ed826
comparison
equal deleted inserted replaced
455:6864a38b2413 456:cae1ae9de430
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from libc.stdlib cimport malloc, free 15 from libc.stdlib cimport malloc, free
16 from libc.string cimport memset 16 from libc.string cimport memset
17 from os.path import join
17 18
18 from pytouhou.lib.opengl cimport \ 19 from pytouhou.lib.opengl cimport \
19 (glVertexPointer, glTexCoordPointer, glColorPointer, 20 (glVertexPointer, glTexCoordPointer, glColorPointer,
20 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc, 21 glVertexAttribPointer, glEnableVertexAttribArray, glBlendFunc,
21 glBindTexture, glDrawElements, glBindBuffer, glBufferData, 22 glBindTexture, glDrawElements, glBindBuffer, glBufferData,
22 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE, 23 GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW, GL_UNSIGNED_BYTE,
23 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA, 24 GL_UNSIGNED_SHORT, GL_INT, GL_FLOAT, GL_SRC_ALPHA,
24 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES, 25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D, GL_TRIANGLES,
25 glGenBuffers) 26 glGenBuffers)
26 27
28 from pytouhou.lib.sdl import SDLError
29
27 from pytouhou.game.element cimport Element 30 from pytouhou.game.element cimport Element
28 from .sprite cimport get_sprite_rendering_data 31 from .sprite cimport get_sprite_rendering_data
29 from .texture import TextureManager 32 from .texture import TextureManager, FontManager
33
34 from pytouhou.utils.helpers import get_logger
35
36 logger = get_logger(__name__)
30 37
31 38
32 DEF MAX_ELEMENTS = 640*4*3 39 DEF MAX_ELEMENTS = 640*4*3
33 40
34 41
57 free(self.vertex_buffer) 64 free(self.vertex_buffer)
58 65
59 66
60 def __init__(self, resource_loader): 67 def __init__(self, resource_loader):
61 self.texture_manager = TextureManager(resource_loader, self) 68 self.texture_manager = TextureManager(resource_loader, self)
69 font_name = join(resource_loader.game_dir, 'font.ttf')
70 try:
71 self.font_manager = FontManager(font_name, 16, self)
72 except SDLError:
73 self.font_manager = None
74 logger.error('Font file ā€œ%sā€ not found, disabling text rendering altogether.', font_name)
62 75
63 if not self.use_fixed_pipeline: 76 if not self.use_fixed_pipeline:
64 glGenBuffers(1, &self.vbo) 77 glGenBuffers(1, &self.vbo)
65 78
66 79
147 glBindTexture(GL_TEXTURE_2D, texture) 160 glBindTexture(GL_TEXTURE_2D, texture)
148 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, self.indices[blendfunc][texture]) 161 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, self.indices[blendfunc][texture])
149 162
150 if not self.use_fixed_pipeline: 163 if not self.use_fixed_pipeline:
151 glBindBuffer(GL_ARRAY_BUFFER, 0) 164 glBindBuffer(GL_ARRAY_BUFFER, 0)
165
166
167 cpdef render_quads(self, rects, colors, texture):
168 # There is nothing that batch more than two quads on the same texture, currently.
169 cdef Vertex buf[8]
170 cdef unsigned short indices[12]
171 indices[:] = [0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4]
172
173 length = len(rects)
174 assert length == len(colors)
175
176 glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
177
178 #TODO: find a way to use offsetof() instead of those ugly hardcoded values.
179 glVertexAttribPointer(0, 3, GL_INT, False, sizeof(Vertex), <void*>0)
180 glEnableVertexAttribArray(0)
181 glVertexAttribPointer(1, 2, GL_FLOAT, False, sizeof(Vertex), <void*>12)
182 glEnableVertexAttribArray(1)
183 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, sizeof(Vertex), <void*>20)
184 glEnableVertexAttribArray(2)
185
186 for i, r in enumerate(rects):
187 c1, c2, c3, c4 = colors[i]
188
189 buf[4*i] = Vertex(r.x, r.y, 0, 0, 0, c1.r, c1.g, c1.b, c1.a)
190 buf[4*i+1] = Vertex(r.x + r.w, r.y, 0, 1, 0, c2.r, c2.g, c2.b, c2.a)
191 buf[4*i+2] = Vertex(r.x + r.w, r.y + r.h, 0, 1, 1, c3.r, c3.g, c3.b, c3.a)
192 buf[4*i+3] = Vertex(r.x, r.y + r.h, 0, 0, 1, c4.r, c4.g, c4.b, c4.a)
193
194 glBufferData(GL_ARRAY_BUFFER, 4 * length * sizeof(Vertex), buf, GL_DYNAMIC_DRAW)
195 glBindTexture(GL_TEXTURE_2D, texture)
196 glDrawElements(GL_TRIANGLES, 6 * length, GL_UNSIGNED_SHORT, indices)
197 glBindBuffer(GL_ARRAY_BUFFER, 0)