Mercurial > touhou
annotate pytouhou/opengl/texture.py @ 200:300661f2ae8a
Fix orbs' original position
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 31 Oct 2011 17:54:06 +0100 |
parents | 7769ce7be03c |
children | df8b2ab54639 |
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 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
15 import pyglet |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
16 from pyglet.gl import * |
14 | 17 import os |
18 | |
19 | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
20 class TextureManager(object): |
97 | 21 def __init__(self, loader=None): |
22 self.loader = loader | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
23 self.textures = {} |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
24 |
158 | 25 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
26 def __getitem__(self, key): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
27 if not key in self.textures: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
28 self.textures[key] = self.load_texture(key) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
29 return self.textures[key] |
14 | 30 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
31 |
83
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
32 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
|
33 try: |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
34 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
|
35 except AttributeError: |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
36 anms = anm_wrapper |
fc0294c745b6
Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
37 |
25
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
38 for anm in anms: |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
39 key = anm.first_name, anm.secondary_name |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
40 texture = self[key] |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
41 |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
42 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
43 def load_texture(self, key): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
44 first_name, secondary_name = key |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
45 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
46 image_file = pyglet.image.load(first_name, file=self.loader.get_file(os.path.basename(first_name))) |
14 | 47 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
48 if secondary_name: |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
49 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
|
50 assert (image_file.width, image_file.height) == (alpha_file.width, image_file.height) |
14 | 51 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
52 data = image_file.get_data('RGB', image_file.width * 3) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
53 alpha_data = alpha_file.get_data('RGB', image_file.width * 3) |
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
54 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))) |
14 | 55 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
56 #TODO |
14 | 57 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
58 texture = image_file.get_texture() |
15 | 59 |
119
fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
Thibaut Girka <thib@sitedethib.com>
parents:
97
diff
changeset
|
60 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
|
61 glTexParameteri(texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR) |
14 | 62 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
63 return texture |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
64 |