comparison eclviewer.py @ 97:ac2e5e1c2c3c

Refactor \o/
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 23:50:00 +0200
parents ca571697ec83
children 5c40cc1b8019
comparison
equal deleted inserted replaced
96:54929d495654 97:ac2e5e1c2c3c
16 import sys 16 import sys
17 import os 17 import os
18 18
19 import struct 19 import struct
20 from math import degrees, radians 20 from math import degrees, radians
21 from io import BytesIO
22 from itertools import chain 21 from itertools import chain
23 22
24 import pygame 23 import pygame
25 24
26 from pytouhou.formats.pbg3 import PBG3 25 from pytouhou.resource.loader import Loader
27 from pytouhou.formats.std import Stage
28 from pytouhou.formats.ecl import ECL
29 from pytouhou.formats.anm0 import Animations
30 from pytouhou.game.sprite import AnmWrapper
31 from pytouhou.game.background import Background 26 from pytouhou.game.background import Background
32 from pytouhou.game.enemymanager import EnemyManager
33 from pytouhou.opengl.texture import TextureManager 27 from pytouhou.opengl.texture import TextureManager
34 from pytouhou.game.game import GameState, Resources 28 from pytouhou.game.game import Game
35 from pytouhou.game.player import Player 29 from pytouhou.game.player import Player
36 30
37 import OpenGL 31 import OpenGL
38 OpenGL.FORWARD_COMPATIBLE_ONLY = True 32 OpenGL.FORWARD_COMPATIBLE_ONLY = True
39 from OpenGL.GL import * 33 from OpenGL.GL import *
58 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 52 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
59 glEnableClientState(GL_COLOR_ARRAY) 53 glEnableClientState(GL_COLOR_ARRAY)
60 glEnableClientState(GL_VERTEX_ARRAY) 54 glEnableClientState(GL_VERTEX_ARRAY)
61 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 55 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
62 56
63 texture_manager = TextureManager() 57 resource_loader = Loader()
58 texture_manager = TextureManager(resource_loader)
59 resource_loader.scan_archives(os.path.join(path, name)
60 for name in ('CM.DAT', 'ST.DAT'))
61 game = Game(resource_loader, [Player()], stage_num, 3, 16)
64 62
65 # Load common data 63 # Load common data
66 with open(os.path.join(path, 'CM.DAT'), 'rb') as file: 64 etama_anm_wrappers = (resource_loader.get_anm_wrapper(('etama3.anm',)),
67 archive = PBG3.read(file) 65 resource_loader.get_anm_wrapper(('etama4.anm',)))
68 texture_manager.set_archive(archive) 66 effects_anm_wrapper = resource_loader.get_anm_wrapper(('eff00.anm',))
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 67
87 # Load stage data 68 # Load stage data
88 with open(os.path.join(path, 'ST.DAT'), 'rb') as file: 69 stage = resource_loader.get_stage('stage%d.std' % stage_num)
89 archive = PBG3.read(file) 70 enemies_anm_wrapper = resource_loader.get_anm_wrapper2(('stg%denm.anm' % stage_num,
90 texture_manager.set_archive(archive) 71 'stg%denm2.anm' % stage_num))
91 72
92 stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num) 73 background_anm_wrapper = resource_loader.get_anm_wrapper(('stg%dbg.anm' % stage_num,))
74 background = Background(stage, background_anm_wrapper)
93 75
94 ecl = ECL.read(BytesIO(archive.extract('ecldata%d.ecl' % stage_num))) 76 # Preload textures
95 enemies_anim = Animations.read(BytesIO(archive.extract('stg%denm.anm' % stage_num))) 77 for anm_wrapper in chain(etama_anm_wrappers,
96 anims = [enemies_anim] 78 (background_anm_wrapper, enemies_anm_wrapper,
97 try: 79 effects_anm_wrapper)):
98 enemies2_anim = Animations.read(BytesIO(archive.extract('stg%denm2.anm' % stage_num))) 80 texture_manager.preload(anm_wrapper)
99 except KeyError:
100 pass
101 else:
102 anims.append(enemies2_anim)
103 enemy_manager = EnemyManager(stage, AnmWrapper(anims), ecl, game_state)
104 texture_manager.preload(anims)
105 81
106 background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) 82 # Let's go!
107 background = Background(stage, AnmWrapper((background_anim,))) 83 print(stage.name)
108 texture_manager.preload((background_anim,))
109
110 print(enemy_manager.stage.name)
111
112 frame = 0
113 84
114 # Main loop 85 # Main loop
115 clock = pygame.time.Clock() 86 clock = pygame.time.Clock()
116 while True: 87 while True:
117 # Check events 88 # Check events
119 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)): 90 if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)):
120 sys.exit(0) 91 sys.exit(0)
121 elif event.type == pygame.KEYDOWN: 92 elif event.type == pygame.KEYDOWN:
122 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT: 93 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT:
123 pygame.display.toggle_fullscreen() 94 pygame.display.toggle_fullscreen()
95 keystate = 0 #TODO
124 96
125 # Update game 97 # Update game
126 enemy_manager.update(frame) 98 background.update(game.game_state.frame) #TODO
127 background.update(frame) 99 game.run_iter(keystate)
128
129 frame += 1
130 100
131 # Draw everything 101 # Draw everything
132 # glClearColor(0.0, 0.0, 1.0, 0) 102 # glClearColor(0.0, 0.0, 1.0, 0)
133 glClear(GL_DEPTH_BUFFER_BIT) 103 glClear(GL_DEPTH_BUFFER_BIT)
134 104
174 gluLookAt(192., 224., - 835.979370, 144 gluLookAt(192., 224., - 835.979370,
175 192., 224., 0., 0., -1., 0.) 145 192., 224., 0., 0., -1., 0.)
176 146
177 glDisable(GL_FOG) 147 glDisable(GL_FOG)
178 objects_by_texture = {} 148 objects_by_texture = {}
179 enemy_manager.get_objects_by_texture(objects_by_texture) 149 game.get_objects_by_texture(objects_by_texture)
180 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items(): 150 for (texture_key, blendfunc), (vertices, uvs, colors) in objects_by_texture.items():
181 nb_vertices = len(vertices) 151 nb_vertices = len(vertices)
182 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc]) 152 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
183 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key]) 153 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
184 glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices))) 154 glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices)))