annotate pytouhou/opengl/texture.py @ 14:07a7f28c8aaa

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 14:54:32 +0200
parents
children 07fba4e1da65
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
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 def load_texture(archive, anim):
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 image_file = BytesIO(archive.extract(os.path.basename(anim.first_name)))
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 textureSurface = pygame.image.load(image_file).convert_alpha()
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 if anim.secondary_name:
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 alpha_image_file = BytesIO(archive.extract(os.path.basename(anim.secondary_name)))
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 alphaSurface = pygame.image.load(alpha_image_file)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 assert textureSurface.get_size() == alphaSurface.get_size()
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 for x in range(alphaSurface.get_width()):
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 for y in range(alphaSurface.get_height()):
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 r, g, b, a = textureSurface.get_at((x, y))
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 color2 = alphaSurface.get_at((x, y))
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 textureSurface.set_at((x, y), (r, g, b, color2[0]))
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 width = textureSurface.get_width()
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 height = textureSurface.get_height()
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 texture = glGenTextures(1)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 glBindTexture(GL_TEXTURE_2D, texture)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 GL_UNSIGNED_BYTE, textureData)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 return texture, width, height
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37