annotate pytouhou/opengl/texture.py @ 54:b383b09bc735

Fix anim cycling
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 23:43:40 +0200
parents ab826bc29aa2
children fc0294c745b6
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):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
26 def __init__(self, archive):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
27 self.archive = archive
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
25
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
36 def preload(self, anms):
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
37 for anm in anms:
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
38 key = anm.first_name, anm.secondary_name
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
39 texture = self[key]
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
40
cc864aadc733 Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
41
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
42 def set_archive(self, archive):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
43 self.archive = archive
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
44
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
45
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
46 def load_texture(self, key):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
47 first_name, secondary_name = key
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
48
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
49 image_file = BytesIO(self.archive.extract(os.path.basename(first_name)))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
50 textureSurface = pygame.image.load(image_file).convert_alpha()
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
52 if secondary_name:
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
53 alpha_image_file = BytesIO(self.archive.extract(os.path.basename(secondary_name)))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
54 alphaSurface = pygame.image.load(alpha_image_file)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
55 assert textureSurface.get_size() == alphaSurface.get_size()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
56 for x in range(alphaSurface.get_width()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
57 for y in range(alphaSurface.get_height()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
58 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
59 color2 = alphaSurface.get_at((x, y))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
60 textureSurface.set_at((x, y), (r, g, b, color2[0]))
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
62 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
64 width = textureSurface.get_width()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
65 height = textureSurface.get_height()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
66
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
67 texture = glGenTextures(1)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
68 glBindTexture(GL_TEXTURE_2D, texture)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
70 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
71 GL_UNSIGNED_BYTE, textureData)
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
72
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
73 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
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
76 return texture
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
77