Mercurial > touhou
annotate stageviewer.py @ 21:bf225780973f
Small refactoring, and Rumia \o/
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Thu, 11 Aug 2011 12:39:12 +0200 |
parents | 66ce9bb440ac |
children | a10e3f44a883 |
rev | line source |
---|---|
4 | 1 #!/usr/bin/env python |
2 | |
3 import sys | |
4 import os | |
5 | |
6 import struct | |
7 from math import degrees, radians | |
8 from io import BytesIO | |
9 from itertools import chain | |
10 | |
11 import pygame | |
12 | |
13 | 13 from pytouhou.formats.pbg3 import PBG3 |
15 | 14 from pytouhou.formats.std import Stage |
15 from pytouhou.formats.anm0 import Animations | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
16 from pytouhou.game.sprite import AnmWrapper |
13 | 17 from pytouhou.game.background import Background |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
18 from pytouhou.opengl.texture import TextureManager |
13 | 19 |
4 | 20 import OpenGL |
21 OpenGL.FORWARD_COMPATIBLE_ONLY = True | |
22 from OpenGL.GL import * | |
23 from OpenGL.GLU import * | |
24 | |
25 | |
26 def main(path, stage_num): | |
27 # Initialize pygame | |
28 pygame.init() | |
29 window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF) | |
30 | |
31 # Initialize OpenGL | |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
32 glMatrixMode(GL_PROJECTION) |
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
33 glLoadIdentity() |
10
059ea0ea8d38
Use the original game's near and far planes.
Thibaut Girka <thib@sitedethib.com>
parents:
9
diff
changeset
|
34 gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.) |
5
aa201d8cfc19
Fix camera handling, thanks, elghinn!
Thibaut Girka <thib@sitedethib.com>
parents:
4
diff
changeset
|
35 |
4 | 36 glEnable(GL_DEPTH_TEST) |
37 glEnable(GL_BLEND) | |
38 glEnable(GL_TEXTURE_2D) | |
7
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
39 glEnable(GL_FOG) |
14 | 40 glHint(GL_FOG_HINT, GL_NICEST) |
41 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | |
42 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
4 | 43 glEnableClientState(GL_VERTEX_ARRAY) |
44 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
45 | |
46 # Load data | |
47 with open(path, 'rb') as file: | |
48 archive = PBG3.read(file) | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
49 texture_manager = TextureManager(archive) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
50 |
15 | 51 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num) |
52 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
53 background = Background(stage, AnmWrapper((background_anim,))) |
4 | 54 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
55 print(background.stage.name) |
8 | 56 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
57 frame = 0 |
4 | 58 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
59 # Main loop |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
60 clock = pygame.time.Clock() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
61 while True: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
62 # Check events |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
63 for event in pygame.event.get(): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
64 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
65 sys.exit(0) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
66 elif event.type == pygame.KEYDOWN: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
67 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
68 pygame.display.toggle_fullscreen() |
4 | 69 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
70 # Update game |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
71 background.update(frame) |
13 | 72 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
73 # Draw everything |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
74 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) |
4 | 75 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
76 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
77 x, y, z = background.position_interpolator.values |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
78 unknownx, dy, dz = background.position2_interpolator.values |
4 | 79 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
80 glFogi(GL_FOG_MODE, GL_LINEAR) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
81 glFogf(GL_FOG_START, fog_start) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
82 glFogf(GL_FOG_END, fog_end) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
83 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) |
7
02a5f5314a19
Add preliminary fog support
Thibaut Girka <thib@sitedethib.com>
parents:
6
diff
changeset
|
84 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
85 #TODO |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
86 glMatrixMode(GL_MODELVIEW) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
87 glLoadIdentity() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
88 # Some explanations on the magic constants: |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
89 # 192. = 384. / 2. = width / 2. |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
90 # 224. = 448. / 2. = height / 2. |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
91 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
92 # This is so that objects on the (O, x, y) plane use pixel coordinates |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
93 gluLookAt(192., 224., - 835.979370 * dz, |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
94 192., 224. - dy, 750 - 835.979370 * dz, 0., -1., 0.) #TODO: 750 might not be accurate |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
95 #print(glGetFloat(GL_MODELVIEW_MATRIX)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
96 glTranslatef(-x, -y, -z) |
4 | 97 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
98 for texture_key, (nb_vertices, vertices, uvs) in background.objects_by_texture.items(): |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
99 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
100 glVertexPointer(3, GL_FLOAT, 0, vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
101 glTexCoordPointer(2, GL_FLOAT, 0, uvs) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
102 glDrawArrays(GL_QUADS, 0, nb_vertices) |
4 | 103 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
104 #TODO: show the game itself |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
105 # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
106 # using a camera at (192, 224, -835.979370) looking right behind itself |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
107 # Depth test should be disabled when rendering the game |
11
548662d70860
Add some explanations to the magic constants
Thibaut Girka <thib@sitedethib.com>
parents:
10
diff
changeset
|
108 |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
109 pygame.display.flip() |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
110 clock.tick(120) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
111 frame += 1 |
4 | 112 |
113 | |
114 | |
115 try: | |
116 file_path, stage_num = sys.argv[1:] | |
117 stage_num = int(stage_num) | |
118 except ValueError: | |
119 print('Usage: %s std_dat_path stage_num' % sys.argv[0]) | |
120 else: | |
121 main(file_path, stage_num) | |
122 |