comparison pytouhou/ui/texture.pyx @ 368:71cd4461bb7f

Minor optimizations
author Thibaut Girka <thib@sitedethib.com>
date Wed, 11 Jul 2012 15:19:44 +0200
parents 98c64ffcbdff
children 45e1a9a37e66
comparison
equal deleted inserted replaced
367:2674c789e0c3 368:71cd4461bb7f
9 ## This program is distributed in the hope that it will be useful, 9 ## This program is distributed in the hope that it will be useful,
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
15 from libc.stdlib cimport malloc, free
14 16
15 import pyglet 17 import pyglet
16 from pyglet.gl import (glTexParameteri, 18 from pyglet.gl import (glTexParameteri,
17 GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 19 GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
18 import os 20 import os
40 key = anm.first_name, anm.secondary_name 42 key = anm.first_name, anm.secondary_name
41 texture = self[key] 43 texture = self[key]
42 44
43 45
44 def load_texture(self, key): 46 def load_texture(self, key):
47 cdef bytes data, alpha_data
48 cdef char *new_data
49 cdef unsigned int i, width, height, pixels
50
45 first_name, secondary_name = key 51 first_name, secondary_name = key
46 52
47 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name))) 53 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name)))
48 54
49 if secondary_name: 55 if secondary_name:
50 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name))) 56 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name)))
51 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height) 57 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height)
52 58
53 data = image_file.get_data('RGB', image_file.width * 3) 59 width, height = image_file.width, image_file.height
54 alpha_data = alpha_file.get_data('RGB', image_file.width * 3) 60 pixels = width * height
55 image_file = pyglet.image.ImageData(image_file.width, image_file.height, 'RGBA', b''.join(data[i*3:i*3+3] + alpha_data[i*3] for i in range(image_file.width * image_file.height))) 61 data = image_file.get_data('RGB', width * 3)
62 alpha_data = alpha_file.get_data('RGB', width * 3)
56 63
57 #TODO: improve perfs somehow 64 # TODO: further optimizations
65 new_data = <char *>malloc(pixels * 4)
66 for i in range(pixels):
67 new_data[i*4] = (<char *>data)[i*3]
68 new_data[i*4+1] = (<char *>data)[i*3+1]
69 new_data[i*4+2] = (<char *>data)[i*3+2]
70 new_data[i*4+3] = (<char *>alpha_data)[i*3]
71 image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)])
72 free(new_data)
58 73
59 texture = image_file.get_texture() 74 texture = image_file.get_texture()
60 75
61 glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 76 glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
62 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 77 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)