Mercurial > touhou
comparison pytouhou/ui/texture.pyx @ 505:bfea9e9a6845
Manage the texture-specific indices in the Texture, and some more renderer optimisations.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 01 Nov 2013 14:45:53 +0100 |
parents | 69c73023f7a0 |
children | 2e8ceaa85d5c |
comparison
equal
deleted
inserted
replaced
504:69c73023f7a0 | 505:bfea9e9a6845 |
---|---|
14 | 14 |
15 from pytouhou.lib.opengl cimport \ | 15 from pytouhou.lib.opengl cimport \ |
16 (glTexParameteri, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, | 16 (glTexParameteri, GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, |
17 GL_LINEAR, GL_BGRA, GL_RGBA, 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, GL_TEXTURE_2D, GLuint, | 19 glGenTextures, glBindTexture, glTexImage2D, GL_TEXTURE_2D, GLuint) |
20 glDeleteTextures) | |
21 | 20 |
22 from pytouhou.lib.sdl cimport load_png, create_rgb_surface, Font | 21 from pytouhou.lib.sdl cimport load_png, create_rgb_surface |
23 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere? | 22 from pytouhou.formats.thtx import Texture #TODO: perhaps define that elsewhere? |
24 from pytouhou.game.text cimport NativeText | 23 from pytouhou.game.text cimport NativeText |
25 | 24 |
26 import os | 25 import os |
27 | 26 |
28 | 27 |
29 class TextureId(int): | 28 cdef class TextureManager: |
30 def __del__(self): | 29 def __init__(self, loader=None, renderer=None, texture_class=None): |
31 cdef GLuint texture = self | 30 self.loader = loader |
32 glDeleteTextures(1, &texture) | 31 self.renderer = renderer |
33 self.renderer.remove_texture(self) | 32 self.texture_class = texture_class |
34 | 33 |
35 | 34 |
36 class TextureManager(object): | 35 cdef load(self, dict anms): |
37 def __init__(self, loader=None, renderer=None): | 36 for anm in sorted(anms.values(), key=is_ascii): |
38 self.loader = loader | |
39 self.renderer = renderer | |
40 | |
41 | |
42 def load(self, anms): | |
43 for anm in sorted(anms.values(), key=lambda x: x[0].first_name.endswith('ascii.png')): | |
44 for entry in anm: | 37 for entry in anm: |
45 if not hasattr(entry, 'texture'): | 38 if not hasattr(entry, 'texture'): |
46 texture = decode_png(self.loader, entry.first_name, entry.secondary_name) | 39 texture = decode_png(self.loader, entry.first_name, entry.secondary_name) |
47 entry.texture = load_texture(texture) | 40 elif not isinstance(entry.texture, self.texture_class): |
48 elif not isinstance(entry.texture, TextureId): | 41 texture = entry.texture |
49 entry.texture = load_texture(entry.texture) | 42 entry.texture = self.texture_class(load_texture(texture), self.renderer) |
50 self.renderer.add_texture(entry.texture) | |
51 entry.texture.renderer = self.renderer | |
52 anms.clear() | 43 anms.clear() |
53 | 44 |
54 | 45 |
46 def is_ascii(anm): | |
47 return anm[0].first_name.endswith('ascii.png') | |
48 | |
49 | |
55 cdef class FontManager: | 50 cdef class FontManager: |
56 cdef Font font | 51 def __init__(self, fontname, fontsize=16, renderer=None, texture_class=None): |
57 cdef object renderer | |
58 | |
59 def __init__(self, fontname, fontsize=16, renderer=None): | |
60 self.font = Font(fontname, fontsize) | 52 self.font = Font(fontname, fontsize) |
61 self.renderer = renderer | 53 self.renderer = renderer |
54 self.texture_class = texture_class | |
62 | 55 |
63 | 56 |
64 def load(self, label_list): | 57 cdef load(self, list labels): |
65 cdef NativeText label | 58 cdef NativeText label |
66 | 59 |
67 for label in label_list: | 60 for label in labels: |
68 if label.texture is None: | 61 if label.texture is None: |
69 surface = self.font.render(label.text) | 62 surface = self.font.render(label.text) |
70 label.width, label.height = surface.surface.w, surface.surface.h | 63 label.width, label.height = surface.surface.w, surface.surface.h |
71 | 64 |
72 if label.align == 'center': | 65 if label.align == 'center': |
75 label.x -= label.width | 68 label.x -= label.width |
76 else: | 69 else: |
77 assert label.align == 'left' | 70 assert label.align == 'left' |
78 | 71 |
79 texture = Texture(label.width, label.height, -4, surface.pixels) | 72 texture = Texture(label.width, label.height, -4, surface.pixels) |
80 label.texture = load_texture(texture) | 73 label.texture = self.texture_class(load_texture(texture), self.renderer) |
81 label.texture.renderer = self.renderer | |
82 | 74 |
83 | 75 |
84 cdef decode_png(loader, first_name, secondary_name): | 76 cdef decode_png(loader, first_name, secondary_name): |
85 image_file = load_png(loader.get_file(os.path.basename(first_name))) | 77 image_file = load_png(loader.get_file(os.path.basename(first_name))) |
86 width, height = image_file.surface.w, image_file.surface.h | 78 width, height = image_file.surface.w, image_file.surface.h |
100 new_image.set_alpha(new_alpha_file) | 92 new_image.set_alpha(new_alpha_file) |
101 | 93 |
102 return Texture(width, height, -4, new_image.pixels) | 94 return Texture(width, height, -4, new_image.pixels) |
103 | 95 |
104 | 96 |
105 cdef load_texture(thtx): | 97 cdef GLuint load_texture(thtx): |
106 cdef GLuint texture | 98 cdef GLuint texture |
107 | 99 |
108 if thtx.fmt == 1: | 100 if thtx.fmt == 1: |
109 format_ = GL_BGRA | 101 format_ = GL_BGRA |
110 type_ = GL_UNSIGNED_BYTE | 102 type_ = GL_UNSIGNED_BYTE |
138 thtx.width, thtx.height, | 130 thtx.width, thtx.height, |
139 0, | 131 0, |
140 format_, type_, | 132 format_, type_, |
141 <char*>thtx.data) | 133 <char*>thtx.data) |
142 | 134 |
143 return TextureId(texture) | 135 return texture |