4 * Created on: Aug 1, 2012
8 #include "app/Application.h"
9 #include "app/Arguments.h"
10 #include "app/Input.h"
11 #include "battle/BattleState.h"
12 #include "battle/Hero.h"
13 #include "battle/Monster.h"
14 #include "battle/PartyLayout.h"
15 #include "battle/Resources.h"
16 #include "battle/Stats.h"
17 #include "common/Ikari.h"
18 #include "common/Inventory.h"
19 #include "common/Item.h"
20 #include "common/Spell.h"
21 #include "geometry/Vector.h"
22 #include "graphics/ComplexAnimation.h"
23 #include "graphics/Font.h"
24 #include "graphics/Frame.h"
25 #include "graphics/Gauge.h"
26 #include "graphics/Menu.h"
27 #include "graphics/SimpleAnimation.h"
28 #include "graphics/Sprite.h"
29 #include "loader/Caster.h"
30 #include "loader/Interpreter.h"
31 #include "loader/ParsedSource.h"
32 #include "loader/Parser.h"
33 #include "loader/TypeDescription.h"
35 #include "map/Entity.h"
37 #include "map/MapState.h"
39 #include "sdl/InitImage.h"
40 #include "sdl/InitScreen.h"
41 #include "sdl/InitSDL.h"
50 #include <SDL_image.h>
52 using app::Application;
55 using battle::BattleState;
57 using battle::Monster;
58 using battle::PartyLayout;
61 using common::Inventory;
64 using geometry::Vector;
65 using graphics::ComplexAnimation;
67 using graphics::Frame;
68 using graphics::Gauge;
70 using graphics::SimpleAnimation;
71 using graphics::Sprite;
73 using loader::Interpreter;
74 using loader::ParsedSource;
76 using loader::TypeDescription;
83 using sdl::InitScreen;
93 int main(int argc, char **argv) {
94 const int width = 800;
95 const int height = 480;
97 const float walkSpeed = 128.0f;
99 const bool battle(false);
101 // std::srand(std::time(0));
105 InitImage image(IMG_INIT_PNG);
107 battle::Resources::CreateTypeDescription();
108 ComplexAnimation::CreateTypeDescription();
109 Font::CreateTypeDescription();
110 Frame::CreateTypeDescription();
111 Gauge::CreateTypeDescription();
112 Hero::CreateTypeDescription();
113 Ikari::CreateTypeDescription();
114 Interpreter::CreateTypeDescriptions();
115 Item::CreateTypeDescription();
116 graphics::MenuProperties::CreateTypeDescription();
117 Monster::CreateTypeDescription();
118 PartyLayout::CreateTypeDescription();
119 SimpleAnimation::CreateTypeDescription();
120 Spell::CreateTypeDescription();
121 Sprite::CreateTypeDescription();
122 Stats::CreateTypeDescription();
123 common::TargetingMode::CreateTypeDescription();
126 args.Read(argc, argv);
130 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
131 Parser(*i, source).Parse();
134 switch (args.DetectRunLevel()) {
135 case Arguments::WRITE:
137 int length(std::strlen(args.OutfilePath()));
138 switch (args.OutfilePath()[length - 1]) {
140 std::ofstream outstream(args.OutfilePath());
141 source.WriteHeader(outstream);
145 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
150 case Arguments::DUMP: {
151 std::cout << source << std::endl;
154 case Arguments::PLAY:
158 Interpreter intp(source);
161 if (intp.PostponedDefinitions().size() > 0) {
162 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
163 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
170 // temporary test data
171 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
172 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
173 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
175 Monster monster(*caster.GetMonster("lizard"));
176 Hero maxim(*caster.GetHero("maxim"));
177 Hero selan(*caster.GetHero("selan"));
178 Hero guy(*caster.GetHero("guy"));
179 Hero dekar(*caster.GetHero("dekar"));
181 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
183 maxim.AddSpell(caster.GetSpell("resetSpell"));
184 Spell *strongSpell(caster.GetSpell("strongSpell"));
185 maxim.AddSpell(strongSpell);
186 selan.AddSpell(strongSpell);
187 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
188 maxim.AddSpell(strongerSpell);
189 selan.AddSpell(strongerSpell);
190 Spell *championSpell(caster.GetSpell("championSpell"));
191 maxim.AddSpell(championSpell);
192 selan.AddSpell(championSpell);
193 Spell *rallySpell(caster.GetSpell("rallySpell"));
194 maxim.AddSpell(rallySpell);
195 selan.AddSpell(rallySpell);
196 selan.AddSpell(caster.GetSpell("escapeSpell"));
197 Spell *valorSpell(caster.GetSpell("valorSpell"));
198 maxim.AddSpell(valorSpell);
199 selan.AddSpell(valorSpell);
202 inventory.Add(caster.GetItem("antidoteItem"), 9);
203 inventory.Add(caster.GetItem("magicJarItem"), 4);
204 inventory.Add(caster.GetItem("hiPotionItem"), 4);
205 inventory.Add(caster.GetItem("powerPotionItem"), 4);
206 inventory.Add(caster.GetItem("escapeItem"), 2);
207 inventory.Add(caster.GetItem("sleepBallItem"), 1);
208 battleRes->inventory = &inventory;
210 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
211 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
212 maxim.SetShield(caster.GetItem("holyShieldItem"));
213 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
214 maxim.SetRing(caster.GetItem("sProRingItem"));
215 maxim.SetJewel(caster.GetItem("evilJewelItem"));
217 // selan.SetWeapon(cst.GetItem("zircoWhipItem"));
218 selan.SetArmor(caster.GetItem("zirconPlateItem"));
219 selan.SetShield(caster.GetItem("zircoGlovesItem"));
220 selan.SetHelmet(caster.GetItem("holyCapItem"));
221 selan.SetRing(caster.GetItem("ghostRingItem"));
222 selan.SetJewel(caster.GetItem("eagleRockItem"));
224 // guy.SetWeapon(cst.GetItem("zircoAxItem"));
225 guy.SetArmor(caster.GetItem("zirconArmorItem"));
226 guy.SetShield(caster.GetItem("megaShieldItem"));
227 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
228 guy.SetRing(caster.GetItem("powerRingItem"));
229 guy.SetJewel(caster.GetItem("evilJewelItem"));
231 // NOTE: this is actually Artea equipment
232 // dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
233 dekar.SetArmor(caster.GetItem("holyRobeItem"));
234 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
235 dekar.SetHelmet(caster.GetItem("holyCapItem"));
236 dekar.SetRing(caster.GetItem("rocketRingItem"));
237 dekar.SetJewel(caster.GetItem("krakenRockItem"));
241 tiles[ 0].SetOffset(Vector<int>(2, 1));
242 tiles[ 1].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_WEST);
243 tiles[ 2].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
244 tiles[ 3].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
245 tiles[ 4].SetOffset(Vector<int>(0, 1));
246 tiles[ 5].SetOffset(Vector<int>(2, 0));
247 tiles[ 6].SetOffset(Vector<int>(2, 0));
248 tiles[ 7].SetOffset(Vector<int>(2, 0));
250 tiles[ 8].SetOffset(Vector<int>(2, 1));
251 tiles[ 9].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
252 tiles[10].SetOffset(Vector<int>(3, 0));
253 tiles[11].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
254 tiles[12].SetOffset(Vector<int>(0, 2));
255 tiles[13].SetOffset(Vector<int>(1, 2));
256 tiles[14].SetOffset(Vector<int>(1, 2));
257 tiles[15].SetOffset(Vector<int>(1, 2));
259 tiles[16].SetOffset(Vector<int>(2, 1));
260 tiles[17].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
261 tiles[18].SetOffset(Vector<int>(3, 0));
262 tiles[19].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
263 tiles[20].SetOffset(Vector<int>(0, 3));
264 tiles[21].SetOffset(Vector<int>(1, 3));
265 tiles[22].SetOffset(Vector<int>(1, 3));
266 tiles[23].SetOffset(Vector<int>(2, 3));
268 tiles[24].SetOffset(Vector<int>(2, 1));
269 tiles[25].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
270 tiles[26].SetOffset(Vector<int>(3, 0));
271 tiles[27].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
272 tiles[28].SetOffset(Vector<int>(0, 4));
273 tiles[29].SetOffset(Vector<int>(1, 4));
274 tiles[30].SetOffset(Vector<int>(1, 4));
275 tiles[31].SetOffset(Vector<int>(2, 4));
277 tiles[32].SetOffset(Vector<int>(2, 1));
278 tiles[33].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
279 tiles[34].SetOffset(Vector<int>(3, 0));
280 tiles[35].SetOffset(Vector<int>(3, 0));
281 tiles[36].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
282 tiles[37].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
283 tiles[38].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
284 tiles[39].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
286 tiles[40].SetOffset(Vector<int>(2, 1));
287 tiles[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
288 tiles[42].SetOffset(Vector<int>(3, 0));
289 tiles[43].SetOffset(Vector<int>(3, 0));
290 tiles[44].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
291 tiles[45].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
292 tiles[46].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
293 tiles[47].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
295 tiles[48].SetOffset(Vector<int>(2, 1));
296 tiles[49].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
297 tiles[50].SetOffset(Vector<int>(3, 0));
298 tiles[51].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
299 tiles[52].SetOffset(Vector<int>(0, 0));
300 tiles[53].SetOffset(Vector<int>(1, 0));
301 tiles[54].SetOffset(Vector<int>(1, 0));
302 tiles[55].SetOffset(Vector<int>(1, 0));
304 tiles[56].SetOffset(Vector<int>(2, 1));
305 tiles[57].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_WEST);
306 tiles[58].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
307 tiles[59].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
308 tiles[60].SetOffset(Vector<int>(0, 1));
309 tiles[61].SetOffset(Vector<int>(1, 1));
310 tiles[62].SetOffset(Vector<int>(1, 1));
311 tiles[63].SetOffset(Vector<int>(1, 1));
314 area.SetTiles(tiles, 64);
317 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
318 Sprite tileset(tilesetImg, 32, 32);
321 map.SetAreas(&area, 1);
322 map.SetTileset(&tileset);
325 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
326 Sprite mapMaximSprite(mapMaximImg, 32, 64);
328 mapMaxim.SetSprite(&mapMaximSprite);
329 mapMaxim.Position() = Vector<float>(80, 128);
331 InitScreen screen(width, height);
333 app::State *state(0);
336 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
337 battleState->AddMonster(monster);
338 battleState->AddMonster(monster);
339 battleState->AddMonster(monster);
340 battleState->AddMonster(monster);
341 battleState->AddHero(maxim);
342 battleState->AddHero(selan);
343 battleState->AddHero(guy);
344 battleState->AddHero(dekar);
347 MapState *mapState(new MapState(&map));
348 mapState->AddEntity(&mapMaxim);
349 mapState->ControlEntity(&mapMaxim);
350 mapState->SetWalkingSpeed(walkSpeed);
354 Application app(&screen, state);
355 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
356 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
357 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
358 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
359 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
360 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
361 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
362 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
363 app.Buttons().MapKey(SDLK_RETURN, Input::START);
364 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
365 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
366 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
370 } catch (Parser::Error &e) {
371 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
373 } catch (exception &e) {
374 cerr << "exception in main(): " << e.what() << endl;