comparison stageviewer.py @ 14:07a7f28c8aaa

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 14:54:32 +0200
parents 58bc264aba38
children 07fba4e1da65
comparison
equal deleted inserted replaced
13:58bc264aba38 14:07a7f28c8aaa
10 10
11 import pygame 11 import pygame
12 12
13 from pytouhou.formats.pbg3 import PBG3 13 from pytouhou.formats.pbg3 import PBG3
14 from pytouhou.game.background import Background 14 from pytouhou.game.background import Background
15 from pytouhou.opengl.texture import load_texture
15 16
16 import OpenGL 17 import OpenGL
17 OpenGL.FORWARD_COMPATIBLE_ONLY = True 18 OpenGL.FORWARD_COMPATIBLE_ONLY = True
18 from OpenGL.GL import * 19 from OpenGL.GL import *
19 from OpenGL.GLU import * 20 from OpenGL.GLU import *
20
21
22 def load_texture(image, alpha_image=None):
23 #TODO: move elsewhere
24 textureSurface = pygame.image.load(image).convert_alpha()
25
26 if alpha_image:
27 alphaSurface = pygame.image.load(alpha_image)
28 assert textureSurface.get_size() == alphaSurface.get_size()
29 for x in range(alphaSurface.get_width()):
30 for y in range(alphaSurface.get_height()):
31 r, g, b, a = textureSurface.get_at((x, y))
32 color2 = alphaSurface.get_at((x, y))
33 textureSurface.set_at((x, y), (r, g, b, color2[0]))
34
35 textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
36
37 width = textureSurface.get_width()
38 height = textureSurface.get_height()
39
40 texture = glGenTextures(1)
41 glBindTexture(GL_TEXTURE_2D, texture)
42
43 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
44 GL_UNSIGNED_BYTE, textureData)
45
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
48
49 return texture, width, height
50 21
51 22
52 def main(path, stage_num): 23 def main(path, stage_num):
53 # Initialize pygame 24 # Initialize pygame
54 pygame.init() 25 pygame.init()
57 # Initialize OpenGL 28 # Initialize OpenGL
58 glMatrixMode(GL_PROJECTION) 29 glMatrixMode(GL_PROJECTION)
59 glLoadIdentity() 30 glLoadIdentity()
60 gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.) 31 gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.)
61 32
33 glEnable(GL_DEPTH_TEST)
34 glEnable(GL_BLEND)
35 glEnable(GL_TEXTURE_2D)
36 glEnable(GL_FOG)
62 glHint(GL_FOG_HINT, GL_NICEST) 37 glHint(GL_FOG_HINT, GL_NICEST)
63 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 38 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
64 glEnable(GL_DEPTH_TEST) 39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
65 glEnable(GL_BLEND) 40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
66 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 41 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
67 glEnable(GL_TEXTURE_2D)
68 glEnable(GL_FOG)
69 glEnableClientState(GL_VERTEX_ARRAY) 42 glEnableClientState(GL_VERTEX_ARRAY)
70 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 43 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
71 44
72 # Load data 45 # Load data
73 with open(path, 'rb') as file: 46 with open(path, 'rb') as file:
74 archive = PBG3.read(file) 47 archive = PBG3.read(file)
75 background = Background(archive, stage_num) 48 background = Background(archive, stage_num)
76 49 texture = load_texture(archive, background.anim)
77 texture = load_texture(*background.texture_components)
78 50
79 print(background.stage.name) 51 print(background.stage.name)
80 52
81 frame = 0 53 frame = 0
82 54