Mercurial > touhou
comparison eclviewer.py @ 108:2a03940deea3
Move everything graphical to pytouhou.opengl!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 06 Sep 2011 00:26:13 +0200 |
parents | c7847bfed427 |
children | fad7b44cebf2 |
comparison
equal
deleted
inserted
replaced
107:5d9052b9a4e8 | 108:2a03940deea3 |
---|---|
14 ## | 14 ## |
15 | 15 |
16 import sys | 16 import sys |
17 import os | 17 import os |
18 | 18 |
19 import struct | |
20 from math import degrees, radians | |
21 from itertools import chain | |
22 | |
23 import pygame | 19 import pygame |
24 | 20 |
25 from pytouhou.resource.loader import Loader | 21 from pytouhou.resource.loader import Loader |
26 from pytouhou.game.background import Background | 22 from pytouhou.game.background import Background |
27 from pytouhou.opengl.texture import TextureManager | 23 from pytouhou.opengl.gamerenderer import GameRenderer |
28 from pytouhou.game.game import Game | 24 from pytouhou.game.game import Game |
29 from pytouhou.game.player import Player | 25 from pytouhou.game.player import Player |
30 | 26 |
31 import OpenGL | |
32 OpenGL.FORWARD_COMPATIBLE_ONLY = True | |
33 from OpenGL.GL import * | |
34 from OpenGL.GLU import * | |
35 | |
36 | 27 |
37 def main(path, stage_num): | 28 def main(path, stage_num): |
38 # Initialize pygame | |
39 pygame.init() | |
40 window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF) | |
41 | |
42 # Initialize OpenGL | |
43 glMatrixMode(GL_PROJECTION) | |
44 glLoadIdentity() | |
45 gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.) | |
46 | |
47 glEnable(GL_BLEND) | |
48 glEnable(GL_TEXTURE_2D) | |
49 glEnable(GL_FOG) | |
50 glHint(GL_FOG_HINT, GL_NICEST) | |
51 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | |
52 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
53 glEnableClientState(GL_COLOR_ARRAY) | |
54 glEnableClientState(GL_VERTEX_ARRAY) | |
55 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
56 | |
57 resource_loader = Loader() | 29 resource_loader = Loader() |
58 texture_manager = TextureManager(resource_loader) | |
59 resource_loader.scan_archives(os.path.join(path, name) | 30 resource_loader.scan_archives(os.path.join(path, name) |
60 for name in ('CM.DAT', 'ST.DAT')) | 31 for name in ('CM.DAT', 'ST.DAT')) |
61 game = Game(resource_loader, [Player()], stage_num, 3, 16) | 32 game = Game(resource_loader, [Player()], stage_num, 3, 16) |
62 | 33 |
63 # Load common data | |
64 etama_anm_wrappers = (resource_loader.get_anm_wrapper(('etama3.anm',)), | |
65 resource_loader.get_anm_wrapper(('etama4.anm',))) | |
66 effects_anm_wrapper = resource_loader.get_anm_wrapper(('eff00.anm',)) | |
67 | |
68 # Load stage data | 34 # Load stage data |
69 stage = resource_loader.get_stage('stage%d.std' % stage_num) | 35 stage = resource_loader.get_stage('stage%d.std' % stage_num) |
70 enemies_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage_num, | |
71 'stg%denm2.anm' % stage_num)) | |
72 | 36 |
73 background_anm_wrapper = resource_loader.get_anm_wrapper(('stg%dbg.anm' % stage_num,)) | 37 background_anm_wrapper = resource_loader.get_anm_wrapper(('stg%dbg.anm' % stage_num,)) |
74 background = Background(stage, background_anm_wrapper) | 38 background = Background(stage, background_anm_wrapper) |
75 | 39 |
76 # Preload textures | 40 # Renderer |
77 for anm_wrapper in chain(etama_anm_wrappers, | 41 renderer = GameRenderer(resource_loader, game, background) |
78 (background_anm_wrapper, enemies_anm_wrapper, | 42 renderer.start() |
79 effects_anm_wrapper)): | |
80 texture_manager.preload(anm_wrapper) | |
81 | 43 |
82 # Let's go! | 44 # Let's go! |
83 print(stage.name) | 45 print(stage.name) |
84 | 46 |
85 # Main loop | 47 # Main loop |
97 # Update game | 59 # Update game |
98 background.update(game.game_state.frame) #TODO | 60 background.update(game.game_state.frame) #TODO |
99 game.run_iter(keystate) | 61 game.run_iter(keystate) |
100 | 62 |
101 # Draw everything | 63 # Draw everything |
102 # glClearColor(0.0, 0.0, 1.0, 0) | 64 renderer.render() |
103 glClear(GL_DEPTH_BUFFER_BIT) | |
104 | |
105 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values | |
106 x, y, z = background.position_interpolator.values | |
107 dx, dy, dz = background.position2_interpolator.values | |
108 | |
109 glFogi(GL_FOG_MODE, GL_LINEAR) | |
110 glFogf(GL_FOG_START, fog_start) | |
111 glFogf(GL_FOG_END, fog_end) | |
112 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) | |
113 | |
114 #TODO | |
115 glMatrixMode(GL_MODELVIEW) | |
116 glLoadIdentity() | |
117 # Some explanations on the magic constants: | |
118 # 192. = 384. / 2. = width / 2. | |
119 # 224. = 448. / 2. = height / 2. | |
120 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | |
121 # This is so that objects on the (O, x, y) plane use pixel coordinates | |
122 gluLookAt(192., 224., - 835.979370 * dz, | |
123 192. + dx, 224. - dy, 0., 0., -1., 0.) | |
124 glTranslatef(-x, -y, -z) | |
125 | |
126 glEnable(GL_DEPTH_TEST) | |
127 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items(): | |
128 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | |
129 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) | |
130 glVertexPointer(3, GL_FLOAT, 0, vertices) | |
131 glTexCoordPointer(2, GL_FLOAT, 0, uvs) | |
132 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) | |
133 glDrawArrays(GL_QUADS, 0, nb_vertices) | |
134 glDisable(GL_DEPTH_TEST) | |
135 | |
136 #TODO | |
137 glMatrixMode(GL_MODELVIEW) | |
138 glLoadIdentity() | |
139 # Some explanations on the magic constants: | |
140 # 192. = 384. / 2. = width / 2. | |
141 # 224. = 448. / 2. = height / 2. | |
142 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | |
143 # This is so that objects on the (O, x, y) plane use pixel coordinates | |
144 gluLookAt(192., 224., - 835.979370, | |
145 192., 224., 0., 0., -1., 0.) | |
146 | |
147 glDisable(GL_FOG) | |
148 objects_by_texture = {} | |
149 for enemy in game.enemies: | |
150 enemy.get_objects_by_texture(objects_by_texture) | |
151 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items(): | |
152 nb_vertices = len(vertices) | |
153 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | |
154 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) | |
155 glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))) | |
156 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs))) | |
157 glColorPointer(4, GL_UNSIGNED_BYTE, 0, struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors))) | |
158 glDrawArrays(GL_QUADS, 0, nb_vertices) | |
159 | |
160 objects_by_texture = {} | |
161 for bullet in game.game_state.bullets: | |
162 bullet.get_objects_by_texture(objects_by_texture) | |
163 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items(): | |
164 nb_vertices = len(vertices) | |
165 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) | |
166 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) | |
167 glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))) | |
168 glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs))) | |
169 glColorPointer(4, GL_UNSIGNED_BYTE, 0, struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors))) | |
170 glDrawArrays(GL_QUADS, 0, nb_vertices) | |
171 glEnable(GL_FOG) | |
172 | 65 |
173 pygame.display.flip() | 66 pygame.display.flip() |
67 | |
174 clock.tick(120) | 68 clock.tick(120) |
175 | 69 |
176 | 70 |
177 | 71 |
178 try: | 72 try: |