comparison pytouhou/ui/texture.pyx @ 223:98c64ffcbdff

Make pytouhou.ui.{background,texture} Cython modules as they are only used by Cython modules.
author Thibaut Girka <thib@sitedethib.com>
date Sun, 18 Dec 2011 21:23:51 +0100
parents pytouhou/ui/texture.py@ee6dfd14a785
children 71cd4461bb7f
comparison
equal deleted inserted replaced
222:5cac48b328ad 223:98c64ffcbdff
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 import pyglet
16 from pyglet.gl import (glTexParameteri,
17 GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
18 import os
19
20
21 cdef class TextureManager:
22 def __init__(self, loader=None):
23 self.loader = loader
24 self.textures = {}
25
26
27 def __getitem__(self, key):
28 if not key in self.textures:
29 self.textures[key] = self.load_texture(key)
30 return self.textures[key]
31
32
33 def preload(self, anm_wrapper):
34 try:
35 anms = anm_wrapper.anm_files
36 except AttributeError:
37 anms = anm_wrapper
38
39 for anm in anms:
40 key = anm.first_name, anm.secondary_name
41 texture = self[key]
42
43
44 def load_texture(self, key):
45 first_name, secondary_name = key
46
47 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name)))
48
49 if secondary_name:
50 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)
52
53 data = image_file.get_data('RGB', image_file.width * 3)
54 alpha_data = alpha_file.get_data('RGB', image_file.width * 3)
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)))
56
57 #TODO: improve perfs somehow
58
59 texture = image_file.get_texture()
60
61 glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
62 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
63
64 return texture
65