annotate pytouhou/opengl/texture.py @ 16:66ce9bb440ac

Refactor in order to support multiple textures
author Thibaut Girka <thib@sitedethib.com>
date Sat, 06 Aug 2011 12:36:25 +0200
parents 07fba4e1da65
children cc864aadc733
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 import pygame
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 import os
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 from io import BytesIO
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 import OpenGL
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 OpenGL.FORWARD_COMPATIBLE_ONLY = True
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 from OpenGL.GL import *
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 from OpenGL.GLU import *
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
11 class TextureManager(object):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
12 def __init__(self, archive):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
13 self.archive = archive
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
14 self.textures = {}
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
15
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
16 def __getitem__(self, key):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
17 if not key in self.textures:
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
18 self.textures[key] = self.load_texture(key)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
19 return self.textures[key]
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
21
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
22 def set_archive(self, archive):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
23 self.archive = archive
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
24
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
25
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
26 def load_texture(self, key):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
27 first_name, secondary_name = key
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
28
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
29 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
30 textureSurface = pygame.image.load(image_file).convert_alpha()
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
32 if secondary_name:
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
33 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
34 alphaSurface = pygame.image.load(alpha_image_file)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
35 assert textureSurface.get_size() == alphaSurface.get_size()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
36 for x in range(alphaSurface.get_width()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
37 for y in range(alphaSurface.get_height()):
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
38 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
39 color2 = alphaSurface.get_at((x, y))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
40 textureSurface.set_at((x, y), (r, g, b, color2[0]))
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
42 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
44 width = textureSurface.get_width()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
45 height = textureSurface.get_height()
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
46
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
47 texture = glGenTextures(1)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
48 glBindTexture(GL_TEXTURE_2D, texture)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
50 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
51 GL_UNSIGNED_BYTE, textureData)
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
52
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
53 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
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
56 return texture
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
57