Mercurial > touhou
annotate pytouhou/opengl/texture.py @ 25:cc864aadc733
Preload enemy and background textures
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Fri, 12 Aug 2011 19:25:08 +0200 |
parents | 66ce9bb440ac |
children | ab826bc29aa2 |
rev | line source |
---|---|
14 | 1 import pygame |
2 import os | |
3 from io import BytesIO | |
4 | |
5 import OpenGL | |
6 OpenGL.FORWARD_COMPATIBLE_ONLY = True | |
7 from OpenGL.GL import * | |
8 from OpenGL.GLU import * | |
9 | |
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 | 20 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
21 |
25
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
22 def preload(self, anms): |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
23 for anm in anms: |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
24 key = anm.first_name, anm.secondary_name |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
25 texture = self[key] |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
26 |
cc864aadc733
Preload enemy and background textures
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
27 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
28 def set_archive(self, archive): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
29 self.archive = archive |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
30 |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
31 |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
32 def load_texture(self, key): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
33 first_name, secondary_name = key |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
34 |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
35 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
|
36 textureSurface = pygame.image.load(image_file).convert_alpha() |
14 | 37 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
38 if secondary_name: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
39 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
|
40 alphaSurface = pygame.image.load(alpha_image_file) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
41 assert textureSurface.get_size() == alphaSurface.get_size() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
42 for x in range(alphaSurface.get_width()): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
43 for y in range(alphaSurface.get_height()): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
44 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
|
45 color2 = alphaSurface.get_at((x, y)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
46 textureSurface.set_at((x, y), (r, g, b, color2[0])) |
14 | 47 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
48 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1) |
14 | 49 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
50 width = textureSurface.get_width() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
51 height = textureSurface.get_height() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
52 |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
53 texture = glGenTextures(1) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
54 glBindTexture(GL_TEXTURE_2D, texture) |
14 | 55 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
56 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
|
57 GL_UNSIGNED_BYTE, textureData) |
15 | 58 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
59 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
|
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) |
14 | 61 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
62 return texture |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
63 |