comparison pytouhou/ui/opengl/renderer.pyx @ 515:b3193b43a86c

Add an indirection layer for textures, to cope with drivers assigning them random names.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 Dec 2013 19:02:42 +0100
parents 5e3e0b09a531
children 75ae628522c9
comparison
equal deleted inserted replaced
514:3d4410de78e1 515:b3193b43a86c
44 44
45 45
46 cdef class Texture: 46 cdef class Texture:
47 def __cinit__(self, GLuint texture, Renderer renderer): 47 def __cinit__(self, GLuint texture, Renderer renderer):
48 self.texture = texture 48 self.texture = texture
49
50 # Find an unused key in the textures array.
51 for key in xrange(MAX_TEXTURES):
52 if renderer.textures[key] == 0:
53 break
54 else:
55 raise MemoryError('Too many textures currently loaded, consider increasing MAX_TEXTURES (currently %d).' % MAX_TEXTURES)
56
57 self.key = key
58 self.pointer = &renderer.textures[key]
59 self.pointer[0] = texture
49 for i in xrange(2): 60 for i in xrange(2):
50 renderer.indices[texture][i] = self.indices[i] 61 renderer.indices[key][i] = self.indices[i]
51 62
52 def __dealloc__(self): 63 def __dealloc__(self):
53 glDeleteTextures(1, &self.texture) 64 if self.texture:
65 glDeleteTextures(1, &self.texture)
66 if self.pointer != NULL:
67 self.pointer[0] = 0
68 # The dangling pointers in renderer.indices doesn’t matter, since we
69 # won’t use them if no texture is loaded in that slot.
54 70
55 71
56 cdef long find_objects(Renderer self, object elements) except -1: 72 cdef long find_objects(Renderer self, object elements) except -1:
57 # Don’t type element as Element, or else the overriding of objects won’t work. 73 # Don’t type element as Element, or else the overriding of objects won’t work.
58 cdef Element obj 74 cdef Element obj
164 texture = key >> 1 180 texture = key >> 1
165 181
166 if blendfunc != previous_blendfunc: 182 if blendfunc != previous_blendfunc:
167 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) 183 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
168 if texture != previous_texture: 184 if texture != previous_texture:
169 glBindTexture(GL_TEXTURE_2D, texture) 185 glBindTexture(GL_TEXTURE_2D, self.textures[texture])
170 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, self.indices[texture][blendfunc]) 186 glDrawElements(GL_TRIANGLES, nb_indices, GL_UNSIGNED_SHORT, self.indices[texture][blendfunc])
171 187
172 previous_blendfunc = blendfunc 188 previous_blendfunc = blendfunc
173 previous_texture = texture 189 previous_texture = texture
174 190