annotate pytouhou/opengl/texture.py @ 99:68aa8bf00c88

Change a debug string to avoid confusion
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:55:35 +0200
parents ac2e5e1c2c3c
children fad7b44cebf2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 import pygame
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 import os
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 from io import BytesIO
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 import OpenGL
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 OpenGL.FORWARD_COMPATIBLE_ONLY = True
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 from OpenGL.GL import *
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 from OpenGL.GLU import *
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
25 class TextureManager(object):
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
26 def __init__(self, loader=None):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
27 self.loader = loader
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
28 self.textures = {}
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
29
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
30 def __getitem__(self, key):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
31 if not key in self.textures:
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
32 self.textures[key] = self.load_texture(key)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
33 return self.textures[key]
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
35
83
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
36 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
37 try:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
38 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
39 except AttributeError:
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
40 anms = anm_wrapper
fc0294c745b6 Basic bullet handling! Clean up as soon as possible :p
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
41
25
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
42 for anm in anms:
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
43 key = anm.first_name, anm.secondary_name
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
44 texture = self[key]
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
45
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
46
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
47 def load_texture(self, key):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
48 first_name, secondary_name = key
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
49
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
50 image_file = self.loader.get_file(os.path.basename(first_name))
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
51 textureSurface = pygame.image.load(image_file).convert_alpha()
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
53 if secondary_name:
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 83
diff changeset
54 alpha_image_file = self.loader.get_file(os.path.basename(secondary_name))
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
55 alphaSurface = pygame.image.load(alpha_image_file)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
56 assert textureSurface.get_size() == alphaSurface.get_size()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
57 for x in range(alphaSurface.get_width()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
58 for y in range(alphaSurface.get_height()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
59 r, g, b, a = textureSurface.get_at((x, y))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
60 color2 = alphaSurface.get_at((x, y))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
61 textureSurface.set_at((x, y), (r, g, b, color2[0]))
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
63 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
65 width = textureSurface.get_width()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
66 height = textureSurface.get_height()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
67
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
68 texture = glGenTextures(1)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
69 glBindTexture(GL_TEXTURE_2D, texture)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
72 GL_UNSIGNED_BYTE, textureData)
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
73
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
77 return texture
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
78