comparison pytouhou/ui/sdl/texture.pyx @ 512:b39ad30c6620

Add a pure SDL backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 01:55:39 +0100
parents
children 43ecf0f98f4d
comparison
equal deleted inserted replaced
511:2e8ceaa85d5c 512:b39ad30c6620
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 from pytouhou.lib.sdl cimport load_png, create_rgb_surface
16
17 import os
18
19
20 cdef class TextureManager:
21 def __init__(self, loader, window):
22 self.loader = loader
23 self.window = window
24
25
26 cdef void load(self, dict anms):
27 for anm in sorted(anms.values(), key=is_ascii):
28 for entry in anm:
29 if not hasattr(entry, 'texture'):
30 texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
31 #elif not isinstance(entry.texture, self.texture_class):
32 # texture = entry.texture
33 entry.texture = self.load_texture(texture)
34 anms.clear()
35
36
37 cdef load_texture(self, Surface surface):
38 return self.window.create_texture_from_surface(surface)
39
40
41 def is_ascii(anm):
42 return anm[0].first_name.endswith('ascii.png')
43
44
45 cdef Surface decode_png(loader, first_name, secondary_name):
46 image_file = load_png(loader.get_file(os.path.basename(first_name)))
47 width, height = image_file.surface.w, image_file.surface.h
48
49 # Support only 32 bits RGBA. Paletted surfaces are awful to work with.
50 #TODO: verify it doesn’t blow up on big-endian systems.
51 new_image = create_rgb_surface(width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
52 new_image.blit(image_file)
53
54 if secondary_name:
55 alpha_file = load_png(loader.get_file(os.path.basename(secondary_name)))
56 assert (width == alpha_file.surface.w and height == alpha_file.surface.h)
57
58 new_alpha_file = create_rgb_surface(width, height, 24)
59 new_alpha_file.blit(alpha_file)
60
61 new_image.set_alpha(new_alpha_file)
62
63 return new_image