comparison pytouhou/opengl/texture.py @ 97:ac2e5e1c2c3c

Refactor \o/
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:50:00 +0200
parents fc0294c745b6
children fad7b44cebf2
comparison
equal deleted inserted replaced
96:54929d495654 97:ac2e5e1c2c3c
21 from OpenGL.GL import * 21 from OpenGL.GL import *
22 from OpenGL.GLU import * 22 from OpenGL.GLU import *
23 23
24 24
25 class TextureManager(object): 25 class TextureManager(object):
26 def __init__(self, archive=None): 26 def __init__(self, loader=None):
27 self.archive = archive 27 self.loader = loader
28 self.textures = {} 28 self.textures = {}
29 29
30 def __getitem__(self, key): 30 def __getitem__(self, key):
31 if not key in self.textures: 31 if not key in self.textures:
32 self.textures[key] = self.load_texture(key) 32 self.textures[key] = self.load_texture(key)
42 for anm in anms: 42 for anm in anms:
43 key = anm.first_name, anm.secondary_name 43 key = anm.first_name, anm.secondary_name
44 texture = self[key] 44 texture = self[key]
45 45
46 46
47 def set_archive(self, archive):
48 self.archive = archive
49
50
51 def load_texture(self, key): 47 def load_texture(self, key):
52 first_name, secondary_name = key 48 first_name, secondary_name = key
53 49
54 image_file = BytesIO(self.archive.extract(os.path.basename(first_name))) 50 image_file = self.loader.get_file(os.path.basename(first_name))
55 textureSurface = pygame.image.load(image_file).convert_alpha() 51 textureSurface = pygame.image.load(image_file).convert_alpha()
56 52
57 if secondary_name: 53 if secondary_name:
58 alpha_image_file = BytesIO(self.archive.extract(os.path.basename(secondary_name))) 54 alpha_image_file = self.loader.get_file(os.path.basename(secondary_name))
59 alphaSurface = pygame.image.load(alpha_image_file) 55 alphaSurface = pygame.image.load(alpha_image_file)
60 assert textureSurface.get_size() == alphaSurface.get_size() 56 assert textureSurface.get_size() == alphaSurface.get_size()
61 for x in range(alphaSurface.get_width()): 57 for x in range(alphaSurface.get_width()):
62 for y in range(alphaSurface.get_height()): 58 for y in range(alphaSurface.get_height()):
63 r, g, b, a = textureSurface.get_at((x, y)) 59 r, g, b, a = textureSurface.get_at((x, y))