comparison pytouhou/ui/texture.pyx @ 423:d8630c086926

Replace Pyglet with our own Cython OpenGL wrapper.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:07:15 +0200
parents 3a7b36324611
children 5d7bb2fd74f7
comparison
equal deleted inserted replaced
422:52829ebe2561 423:d8630c086926
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from pyglet.gl import (glTexParameteri, GL_TEXTURE_MIN_FILTER, 15 from pytouhou.lib.opengl cimport \
16 GL_TEXTURE_MAG_FILTER, GL_LINEAR, GL_BGRA, GL_RGBA, 16 (glTexParameteri, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER,
17 GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE, 17 GL_LINEAR, GL_BGRA, GL_RGBA, GL_RGB, GL_LUMINANCE, GL_UNSIGNED_BYTE,
18 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV, 18 GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4_REV,
19 glGenTextures, glBindTexture, glTexImage2D, 19 glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint)
20 GL_TEXTURE_2D) 20
21 from ctypes import c_uint, byref
22 from pytouhou.lib.sdl import load_png, create_rgb_surface 21 from pytouhou.lib.sdl import load_png, create_rgb_surface
22 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
23
23 import os 24 import os
24
25 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere?
26 25
27 26
28 cdef class TextureManager: 27 cdef class TextureManager:
29 def __init__(self, loader=None): 28 def __init__(self, loader=None):
30 self.loader = loader 29 self.loader = loader
63 62
64 return Texture(width, height, -4, new_image.pixels) 63 return Texture(width, height, -4, new_image.pixels)
65 64
66 65
67 def load_texture(self, key): 66 def load_texture(self, key):
67 cdef GLuint id_
68
68 if not isinstance(key, Texture): 69 if not isinstance(key, Texture):
69 first_name, secondary_name = key 70 first_name, secondary_name = key
70 key = self.load_png_texture(first_name, secondary_name) 71 key = self.load_png_texture(first_name, secondary_name)
71 72
72 if key.fmt == 1: 73 if key.fmt == 1:
90 type_ = GL_UNSIGNED_BYTE 91 type_ = GL_UNSIGNED_BYTE
91 composants = GL_RGBA 92 composants = GL_RGBA
92 else: 93 else:
93 raise Exception('Unknown texture type') 94 raise Exception('Unknown texture type')
94 95
95 id_ = c_uint() 96 glGenTextures(1, &id_)
96 glGenTextures(1, byref(id_)) 97 glBindTexture(GL_TEXTURE_2D, id_)
97 glBindTexture(GL_TEXTURE_2D, id_.value)
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
100 100
101 glTexImage2D(GL_TEXTURE_2D, 0, 101 glTexImage2D(GL_TEXTURE_2D, 0,
102 composants, 102 composants,
103 key.width, key.height, 103 key.width, key.height,
104 0, 104 0,
105 format_, type_, 105 format_, type_,
106 key.data) 106 <char*>key.data)
107 107
108 return id_.value 108 return id_