Mercurial > touhou
annotate pytouhou/ui/sdl/texture.pyx @ 612:73f134f84c7f
Request a RGB888 context, since SDL2’s default of RGB332 sucks.
On X11/GLX, it will select the first config available, that is the best
one, while on EGL it will iterate over them to select the one closest
to what the application requested.
Of course, anything lower than RGB888 looks bad and we really don’t
want that.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 26 Mar 2015 20:20:37 +0100 |
parents | 3c2f96f1d715 |
children | a6af3ff86612 |
rev | line source |
---|---|
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 from pytouhou.lib.sdl cimport load_png, create_rgb_surface |
544
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
16 from pytouhou.lib.sdl import SDLError |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
17 from pytouhou.game.text cimport NativeText |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 import os |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
544
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
21 from pytouhou.utils.helpers import get_logger |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
22 logger = get_logger(__name__) |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
23 |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 cdef class TextureManager: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 def __init__(self, loader, window): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 self.loader = loader |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 self.window = window |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 |
606
3c2f96f1d715
Fix compilation under Cython 0.22, by making the pyx and the pxd declarations’ except clause similar.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
544
diff
changeset
|
31 cdef void load(self, dict anms) except *: |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 for anm in sorted(anms.values(), key=is_ascii): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 for entry in anm: |
525
43ecf0f98f4d
Precalculate the inverse of the texture size at ANM load, to not recalculate at every sprite change.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
512
diff
changeset
|
34 if entry.texture is None: |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 texture = decode_png(self.loader, entry.first_name, entry.secondary_name) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 #elif not isinstance(entry.texture, self.texture_class): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 # texture = entry.texture |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 entry.texture = self.load_texture(texture) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 anms.clear() |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 cdef load_texture(self, Surface surface): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 return self.window.create_texture_from_surface(surface) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 def is_ascii(anm): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 return anm[0].first_name.endswith('ascii.png') |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
48 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 |
544
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
50 cdef class FontManager: |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
51 def __init__(self, fontname, fontsize=16, window=None): |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
52 self.font = Font(fontname, fontsize) |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
53 self.window = window |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
54 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
55 |
606
3c2f96f1d715
Fix compilation under Cython 0.22, by making the pyx and the pxd declarations’ except clause similar.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
544
diff
changeset
|
56 cdef void load(self, dict labels) except *: |
544
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
57 cdef NativeText label |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
58 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
59 for i, label in labels.items(): |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
60 if label.texture is None: |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
61 try: |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
62 surface = self.font.render(label.text) |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
63 except SDLError as e: |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
64 logger.error(u'Rendering of label “%s” failed: %s', label.text, e) |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
65 del labels[i] # Prevents it from retrying to render. |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
66 continue |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
67 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
68 label.width, label.height = surface.surface.w, surface.surface.h |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
69 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
70 if label.align == 'center': |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
71 label.x -= label.width // 2 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
72 elif label.align == 'right': |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
73 label.x -= label.width |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
74 else: |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
75 assert label.align == 'left' |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
76 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
77 label.texture = self.window.create_texture_from_surface(surface) |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
78 |
b895ed2de71f
Implement text rendering for the SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
79 |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 cdef Surface decode_png(loader, first_name, secondary_name): |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 image_file = load_png(loader.get_file(os.path.basename(first_name))) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 width, height = image_file.surface.w, image_file.surface.h |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 # Support only 32 bits RGBA. Paletted surfaces are awful to work with. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 #TODO: verify it doesn’t blow up on big-endian systems. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 new_image = create_rgb_surface(width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 new_image.blit(image_file) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 if secondary_name: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 alpha_file = load_png(loader.get_file(os.path.basename(secondary_name))) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 assert (width == alpha_file.surface.w and height == alpha_file.surface.h) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 new_alpha_file = create_rgb_surface(width, height, 24) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 new_alpha_file.blit(alpha_file) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 new_image.set_alpha(new_alpha_file) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 return new_image |