Mercurial > touhou
annotate pytouhou/ui/texture.pyx @ 392:45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 07 Jan 2013 22:06:02 +0100 |
parents | 71cd4461bb7f |
children | 9e2cbb2c2c64 |
rev | line source |
---|---|
52
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
2 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
4 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
8 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
12 ## GNU General Public License for more details. |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
13 ## |
ab826bc29aa2
Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents:
25
diff
changeset
|
14 |
368 | 15 from libc.stdlib cimport malloc, free |
16 | |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
17 import pyglet |
203
df8b2ab54639
Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
18 from pyglet.gl import (glTexParameteri, |
df8b2ab54639
Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents:
158
diff
changeset
|
19 GL_TEXTURE_MIN_FILTER, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
14 | 20 import os |
21 | |
22 | |
223
98c64ffcbdff
Make pytouhou.ui.{background,texture} Cython modules as they are only used by Cython modules.
Thibaut Girka <thib@sitedethib.com>
parents:
205
diff
changeset
|
23 cdef class TextureManager: |
97 | 24 def __init__(self, loader=None): |
25 self.loader = loader | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
26 self.textures = {} |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
27 |
158 | 28 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
29 def __getitem__(self, key): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
30 if not key in self.textures: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
31 self.textures[key] = self.load_texture(key) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
32 return self.textures[key] |
14 | 33 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
34 |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
35 def preload(self, anm_wrapper): |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
36 try: |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
37 anms = anm_wrapper.anm_files |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
38 except AttributeError: |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
39 anms = anm_wrapper |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
40 |
25
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
41 for anm in anms: |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
42 key = anm.first_name, anm.secondary_name |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
43 texture = self[key] |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
44 |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
45 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
46 def load_texture(self, key): |
392
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
47 cdef char *image, *alpha, *new_data |
368 | 48 cdef unsigned int i, width, height, pixels |
49 | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
50 first_name, secondary_name = key |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
51 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
52 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name))) |
14 | 53 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
54 if secondary_name: |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
55 alpha_file = pyglet.image.load(secondary_name, file=self.loader.get_file(os.path.basename(secondary_name))) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
56 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height) |
14 | 57 |
368 | 58 width, height = image_file.width, image_file.height |
59 pixels = width * height | |
392
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
60 |
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
61 image_data = image_file.get_data('RGB', width * 3) |
368 | 62 alpha_data = alpha_file.get_data('RGB', width * 3) |
392
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
63 image = <char *>image_data |
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
64 alpha = <char *>alpha_data |
14 | 65 |
368 | 66 # TODO: further optimizations |
67 new_data = <char *>malloc(pixels * 4) | |
68 for i in range(pixels): | |
392
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
69 new_data[i*4] = image[i*3] |
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
70 new_data[i*4+1] = image[i*3+1] |
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
71 new_data[i*4+2] = image[i*3+2] |
45e1a9a37e66
When merging RGB and alpha data, get the C arrays only at the start of the loop.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
368
diff
changeset
|
72 new_data[i*4+3] = alpha[i*3] |
368 | 73 image_file = pyglet.image.ImageData(width, height, 'RGBA', new_data[:(pixels * 4)]) |
74 free(new_data) | |
14 | 75 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
76 texture = image_file.get_texture() |
15 | 77 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
78 glTexParameteri(texture.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
79 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR) |
14 | 80 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
81 return texture |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
82 |