comparison eclviewer.py @ 83:fc0294c745b6

Basic bullet handling! Clean up as soon as possible :p
author Thibaut Girka <thib@sitedethib.com>
date Sat, 03 Sep 2011 22:22:58 +0200
parents 6a08f44fa01b
children 85f3b8ba3f24
comparison
equal deleted inserted replaced
82:de48213a02bf 83:fc0294c745b6
29 from pytouhou.formats.anm0 import Animations 29 from pytouhou.formats.anm0 import Animations
30 from pytouhou.game.sprite import AnmWrapper 30 from pytouhou.game.sprite import AnmWrapper
31 from pytouhou.game.background import Background 31 from pytouhou.game.background import Background
32 from pytouhou.game.enemymanager import EnemyManager 32 from pytouhou.game.enemymanager import EnemyManager
33 from pytouhou.opengl.texture import TextureManager 33 from pytouhou.opengl.texture import TextureManager
34 from pytouhou.game.game import GameState 34 from pytouhou.game.game import GameState, Resources
35 from pytouhou.game.player import Player 35 from pytouhou.game.player import Player
36 36
37 import OpenGL 37 import OpenGL
38 OpenGL.FORWARD_COMPATIBLE_ONLY = True 38 OpenGL.FORWARD_COMPATIBLE_ONLY = True
39 from OpenGL.GL import * 39 from OpenGL.GL import *
58 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 58 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
59 glEnableClientState(GL_COLOR_ARRAY) 59 glEnableClientState(GL_COLOR_ARRAY)
60 glEnableClientState(GL_VERTEX_ARRAY) 60 glEnableClientState(GL_VERTEX_ARRAY)
61 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 61 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
62 62
63 # Load data 63 texture_manager = TextureManager()
64 with open(path, 'rb') as file: 64
65 # Load common data
66 with open(os.path.join(path, 'CM.DAT'), 'rb') as file:
65 archive = PBG3.read(file) 67 archive = PBG3.read(file)
66 texture_manager = TextureManager(archive) 68 texture_manager.set_archive(archive)
69 etama_anm_wrappers = (AnmWrapper([Animations.read(BytesIO(archive.extract('etama3.anm')))]),
70 AnmWrapper([Animations.read(BytesIO(archive.extract('etama4.anm')))]))
71 players_anm_wrappers = (AnmWrapper([Animations.read(BytesIO(archive.extract('player00.anm')))]),
72 AnmWrapper([Animations.read(BytesIO(archive.extract('player01.anm')))]))
73 effects_anm_wrapper = AnmWrapper([Animations.read(BytesIO(archive.extract('eff00.anm')))])
74
75 for anm_wrapper in etama_anm_wrappers:
76 texture_manager.preload(anm_wrapper)
77
78 for anm_wrapper in players_anm_wrappers:
79 texture_manager.preload(anm_wrapper)
80
81 texture_manager.preload(effects_anm_wrapper)
82
83 resources = Resources(etama_anm_wrappers, players_anm_wrappers, effects_anm_wrapper)
84
85 game_state = GameState(resources, [Player()], stage_num, 3, 16)
86
87 # Load stage data
88 with open(os.path.join(path, 'ST.DAT'), 'rb') as file:
89 archive = PBG3.read(file)
90 texture_manager.set_archive(archive)
67 91
68 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num) 92 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num)
69 93
70 ecl = ECL.read(BytesIO(archive.extract('ecldata%d.ecl' % stage_num))) 94 ecl = ECL.read(BytesIO(archive.extract('ecldata%d.ecl' % stage_num)))
71 enemies_anim = Animations.read(BytesIO(archive.extract('stg%denm.anm' % stage_num))) 95 enemies_anim = Animations.read(BytesIO(archive.extract('stg%denm.anm' % stage_num)))
74 enemies2_anim = Animations.read(BytesIO(archive.extract('stg%denm2.anm' % stage_num))) 98 enemies2_anim = Animations.read(BytesIO(archive.extract('stg%denm2.anm' % stage_num)))
75 except KeyError: 99 except KeyError:
76 pass 100 pass
77 else: 101 else:
78 anims.append(enemies2_anim) 102 anims.append(enemies2_anim)
79 enemy_manager = EnemyManager(stage, AnmWrapper(anims), ecl, GameState([Player()], stage_num, 0, 16)) 103 enemy_manager = EnemyManager(stage, AnmWrapper(anims), ecl, game_state)
80 texture_manager.preload(anims) 104 texture_manager.preload(anims)
81 105
82 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) 106 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
83 background = Background(stage, AnmWrapper((background_anim,))) 107 background = Background(stage, AnmWrapper((background_anim,)))
84 texture_manager.preload((background_anim,)) 108 texture_manager.preload((background_anim,))
85 109
86 print(enemy_manager.stage.name) 110 print(enemy_manager.stage.name)
87 111
88 frame = 0 112 frame = 0
89 113
90 # Main loop 114 # Main loop
91 clock = pygame.time.Clock() 115 clock = pygame.time.Clock()
92 while True: 116 while True:
93 # Check events 117 # Check events
94 for event in pygame.event.get(): 118 for event in pygame.event.get():
95 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)): 119 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)):
96 sys.exit(0) 120 sys.exit(0)
97 elif event.type == pygame.KEYDOWN: 121 elif event.type == pygame.KEYDOWN:
98 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: 122 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT:
99 pygame.display.toggle_fullscreen() 123 pygame.display.toggle_fullscreen()
100 124
101 # Update game 125 # Update game
102 enemy_manager.update(frame) 126 enemy_manager.update(frame)
103 background.update(frame) 127 background.update(frame)
104 128
105 frame += 1 129 frame += 1
106 130
107 # Draw everything 131 # Draw everything
108 # glClearColor(0.0, 0.0, 1.0, 0) 132 # glClearColor(0.0, 0.0, 1.0, 0)
109 glClear(GL_DEPTH_BUFFER_BIT) 133 glClear(GL_DEPTH_BUFFER_BIT)
110 134
111 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values 135 fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values
112 x, y, z = background.position_interpolator.values 136 x, y, z = background.position_interpolator.values
113 dx, dy, dz = background.position2_interpolator.values 137 dx, dy, dz = background.position2_interpolator.values
114 138
115 glFogi(GL_FOG_MODE, GL_LINEAR) 139 glFogi(GL_FOG_MODE, GL_LINEAR)
116 glFogf(GL_FOG_START, fog_start) 140 glFogf(GL_FOG_START, fog_start)
117 glFogf(GL_FOG_END, fog_end) 141 glFogf(GL_FOG_END, fog_end)
118 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.)) 142 glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.))
119 143
120 #TODO 144 #TODO
121 glMatrixMode(GL_MODELVIEW) 145 glMatrixMode(GL_MODELVIEW)
122 glLoadIdentity() 146 glLoadIdentity()
123 # Some explanations on the magic constants: 147 # Some explanations on the magic constants:
124 # 192. = 384. / 2. = width / 2. 148 # 192. = 384. / 2. = width / 2.
125 # 224. = 448. / 2. = height / 2. 149 # 224. = 448. / 2. = height / 2.
126 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) 150 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
127 # This is so that objects on the (O, x, y) plane use pixel coordinates 151 # This is so that objects on the (O, x, y) plane use pixel coordinates
128 gluLookAt(192., 224., - 835.979370 * dz, 152 gluLookAt(192., 224., - 835.979370 * dz,
129 192. + dx, 224. - dy, 0., 0., -1., 0.) 153 192. + dx, 224. - dy, 0., 0., -1., 0.)
130 glTranslatef(-x, -y, -z) 154 glTranslatef(-x, -y, -z)
131 155
132 glEnable(GL_DEPTH_TEST) 156 glEnable(GL_DEPTH_TEST)
133 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items(): 157 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items():
134 glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc]) 158 glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc])
135 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) 159 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
136 glVertexPointer(3, GL_FLOAT, 0, vertices) 160 glVertexPointer(3, GL_FLOAT, 0, vertices)
137 glTexCoordPointer(2, GL_FLOAT, 0, uvs) 161 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
138 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) 162 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
139 glDrawArrays(GL_QUADS, 0, nb_vertices) 163 glDrawArrays(GL_QUADS, 0, nb_vertices)
140 glDisable(GL_DEPTH_TEST) 164 glDisable(GL_DEPTH_TEST)
141 165
142 #TODO 166 #TODO
143 glMatrixMode(GL_MODELVIEW) 167 glMatrixMode(GL_MODELVIEW)
144 glLoadIdentity() 168 glLoadIdentity()
145 # Some explanations on the magic constants: 169 # Some explanations on the magic constants:
146 # 192. = 384. / 2. = width / 2. 170 # 192. = 384. / 2. = width / 2.
147 # 224. = 448. / 2. = height / 2. 171 # 224. = 448. / 2. = height / 2.
148 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) 172 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
149 # This is so that objects on the (O, x, y) plane use pixel coordinates 173 # This is so that objects on the (O, x, y) plane use pixel coordinates
150 gluLookAt(192., 224., - 835.979370, 174 gluLookAt(192., 224., - 835.979370,
151 192., 224., 0., 0., -1., 0.) 175 192., 224., 0., 0., -1., 0.)
152 176
153 glDisable(GL_FOG) 177 glDisable(GL_FOG)
154 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in enemy_manager.objects_by_texture.items(): 178 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in enemy_manager.objects_by_texture.items():
155 glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc]) 179 glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc])
156 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) 180 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
157 glVertexPointer(3, GL_FLOAT, 0, vertices) 181 glVertexPointer(3, GL_FLOAT, 0, vertices)
158 glTexCoordPointer(2, GL_FLOAT, 0, uvs) 182 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
159 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors) 183 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
160 glDrawArrays(GL_QUADS, 0, nb_vertices) 184 glDrawArrays(GL_QUADS, 0, nb_vertices)
161 glEnable(GL_FOG) 185 glEnable(GL_FOG)
162 186
163 pygame.display.flip() 187 pygame.display.flip()
164 clock.tick(120) 188 clock.tick(120)
165 189
166 190
167 191
168 try: 192 try:
169 file_path, stage_num = sys.argv[1:] 193 file_path, stage_num = sys.argv[1:]
170 stage_num = int(stage_num) 194 stage_num = int(stage_num)
171 except ValueError: 195 except ValueError:
172 print('Usage: %s std_dat_path stage_num' % sys.argv[0]) 196 print('Usage: %s game_dir_path stage_num' % sys.argv[0])
173 else: 197 else:
174 main(file_path, stage_num) 198 main(file_path, stage_num)
175 199