comparison pytouhou/opengl/texture.py @ 16:66ce9bb440ac

Refactor in order to support multiple textures
author Thibaut Girka <thib@sitedethib.com>
date Sat, 06 Aug 2011 12:36:25 +0200
parents 07fba4e1da65
children cc864aadc733
comparison
equal deleted inserted replaced
15:07fba4e1da65 16:66ce9bb440ac
6 OpenGL.FORWARD_COMPATIBLE_ONLY = True 6 OpenGL.FORWARD_COMPATIBLE_ONLY = True
7 from OpenGL.GL import * 7 from OpenGL.GL import *
8 from OpenGL.GLU import * 8 from OpenGL.GLU import *
9 9
10 10
11 def load_texture(archive, anim): 11 class TextureManager(object):
12 image_file = BytesIO(archive.extract(os.path.basename(anim.first_name))) 12 def __init__(self, archive):
13 textureSurface = pygame.image.load(image_file).convert_alpha() 13 self.archive = archive
14 self.textures = {}
14 15
15 if anim.secondary_name: 16 def __getitem__(self, key):
16 alpha_image_file = BytesIO(archive.extract(os.path.basename(anim.secondary_name))) 17 if not key in self.textures:
17 alphaSurface = pygame.image.load(alpha_image_file) 18 self.textures[key] = self.load_texture(key)
18 assert textureSurface.get_size() == alphaSurface.get_size() 19 return self.textures[key]
19 for x in range(alphaSurface.get_width()):
20 for y in range(alphaSurface.get_height()):
21 r, g, b, a = textureSurface.get_at((x, y))
22 color2 = alphaSurface.get_at((x, y))
23 textureSurface.set_at((x, y), (r, g, b, color2[0]))
24 20
25 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
26 21
27 width = textureSurface.get_width() 22 def set_archive(self, archive):
28 height = textureSurface.get_height() 23 self.archive = archive
29 24
30 texture = glGenTextures(1)
31 glBindTexture(GL_TEXTURE_2D, texture)
32 25
33 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, 26 def load_texture(self, key):
34 GL_UNSIGNED_BYTE, textureData) 27 first_name, secondary_name = key
35 28
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) 29 image_file = BytesIO(self.archive.extract(os.path.basename(first_name)))
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) 30 textureSurface = pygame.image.load(image_file).convert_alpha()
38 31
39 return texture, width, height 32 if secondary_name:
33 alpha_image_file = BytesIO(self.archive.extract(os.path.basename(secondary_name)))
34 alphaSurface = pygame.image.load(alpha_image_file)
35 assert textureSurface.get_size() == alphaSurface.get_size()
36 for x in range(alphaSurface.get_width()):
37 for y in range(alphaSurface.get_height()):
38 r, g, b, a = textureSurface.get_at((x, y))
39 color2 = alphaSurface.get_at((x, y))
40 textureSurface.set_at((x, y), (r, g, b, color2[0]))
40 41
42 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
43
44 width = textureSurface.get_width()
45 height = textureSurface.get_height()
46
47 texture = glGenTextures(1)
48 glBindTexture(GL_TEXTURE_2D, texture)
49
50 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
51 GL_UNSIGNED_BYTE, textureData)
52
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
55
56 return texture
57