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 "map/Trigger.h"
40 #include "sdl/InitImage.h"
41 #include "sdl/InitScreen.h"
42 #include "sdl/InitSDL.h"
51 #include <SDL_image.h>
53 using app::Application;
56 using battle::BattleState;
58 using battle::Monster;
59 using battle::PartyLayout;
62 using common::Inventory;
65 using geometry::Vector;
66 using graphics::ComplexAnimation;
68 using graphics::Frame;
69 using graphics::Gauge;
71 using graphics::SimpleAnimation;
72 using graphics::Sprite;
74 using loader::Interpreter;
75 using loader::ParsedSource;
77 using loader::TypeDescription;
85 using sdl::InitScreen;
95 int main(int argc, char **argv) {
96 const int width = 800;
97 const int height = 480;
99 const int tileSize = 32;
100 const float walkSpeed = 128.0f;
102 const bool battle(false);
104 // std::srand(std::time(0));
108 InitImage image(IMG_INIT_PNG);
110 battle::Resources::CreateTypeDescription();
111 ComplexAnimation::CreateTypeDescription();
112 Font::CreateTypeDescription();
113 Frame::CreateTypeDescription();
114 Gauge::CreateTypeDescription();
115 Hero::CreateTypeDescription();
116 Ikari::CreateTypeDescription();
117 Interpreter::CreateTypeDescriptions();
118 Item::CreateTypeDescription();
119 graphics::MenuProperties::CreateTypeDescription();
120 Monster::CreateTypeDescription();
121 PartyLayout::CreateTypeDescription();
122 SimpleAnimation::CreateTypeDescription();
123 Spell::CreateTypeDescription();
124 Sprite::CreateTypeDescription();
125 Stats::CreateTypeDescription();
126 common::TargetingMode::CreateTypeDescription();
129 args.Read(argc, argv);
133 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
134 Parser(*i, source).Parse();
137 switch (args.DetectRunLevel()) {
138 case Arguments::WRITE:
140 int length(std::strlen(args.OutfilePath()));
141 switch (args.OutfilePath()[length - 1]) {
143 std::ofstream outstream(args.OutfilePath());
144 source.WriteHeader(outstream);
148 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
153 case Arguments::DUMP: {
154 std::cout << source << std::endl;
157 case Arguments::PLAY:
161 Interpreter intp(source);
164 if (intp.PostponedDefinitions().size() > 0) {
165 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
166 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
173 // temporary test data
174 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
175 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
176 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
178 Monster monster(*caster.GetMonster("lizard"));
179 Hero maxim(*caster.GetHero("maxim"));
180 Hero selan(*caster.GetHero("selan"));
181 Hero guy(*caster.GetHero("guy"));
182 Hero dekar(*caster.GetHero("dekar"));
184 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
186 maxim.AddSpell(caster.GetSpell("resetSpell"));
187 Spell *strongSpell(caster.GetSpell("strongSpell"));
188 maxim.AddSpell(strongSpell);
189 selan.AddSpell(strongSpell);
190 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
191 maxim.AddSpell(strongerSpell);
192 selan.AddSpell(strongerSpell);
193 Spell *championSpell(caster.GetSpell("championSpell"));
194 maxim.AddSpell(championSpell);
195 selan.AddSpell(championSpell);
196 Spell *rallySpell(caster.GetSpell("rallySpell"));
197 maxim.AddSpell(rallySpell);
198 selan.AddSpell(rallySpell);
199 selan.AddSpell(caster.GetSpell("escapeSpell"));
200 Spell *valorSpell(caster.GetSpell("valorSpell"));
201 maxim.AddSpell(valorSpell);
202 selan.AddSpell(valorSpell);
205 inventory.Add(caster.GetItem("antidoteItem"), 9);
206 inventory.Add(caster.GetItem("magicJarItem"), 4);
207 inventory.Add(caster.GetItem("hiPotionItem"), 4);
208 inventory.Add(caster.GetItem("powerPotionItem"), 4);
209 inventory.Add(caster.GetItem("escapeItem"), 2);
210 inventory.Add(caster.GetItem("sleepBallItem"), 1);
211 battleRes->inventory = &inventory;
213 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
214 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
215 maxim.SetShield(caster.GetItem("holyShieldItem"));
216 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
217 maxim.SetRing(caster.GetItem("sProRingItem"));
218 maxim.SetJewel(caster.GetItem("evilJewelItem"));
220 // selan.SetWeapon(cst.GetItem("zircoWhipItem"));
221 selan.SetArmor(caster.GetItem("zirconPlateItem"));
222 selan.SetShield(caster.GetItem("zircoGlovesItem"));
223 selan.SetHelmet(caster.GetItem("holyCapItem"));
224 selan.SetRing(caster.GetItem("ghostRingItem"));
225 selan.SetJewel(caster.GetItem("eagleRockItem"));
227 // guy.SetWeapon(cst.GetItem("zircoAxItem"));
228 guy.SetArmor(caster.GetItem("zirconArmorItem"));
229 guy.SetShield(caster.GetItem("megaShieldItem"));
230 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
231 guy.SetRing(caster.GetItem("powerRingItem"));
232 guy.SetJewel(caster.GetItem("evilJewelItem"));
234 // NOTE: this is actually Artea equipment
235 // dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
236 dekar.SetArmor(caster.GetItem("holyRobeItem"));
237 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
238 dekar.SetHelmet(caster.GetItem("holyCapItem"));
239 dekar.SetRing(caster.GetItem("rocketRingItem"));
240 dekar.SetJewel(caster.GetItem("krakenRockItem"));
244 tiles[ 0].SetOffset(Vector<int>(2, 1));
245 tiles[ 1].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_WEST);
246 tiles[ 2].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
247 tiles[ 3].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
248 tiles[ 4].SetOffset(Vector<int>(0, 1));
249 tiles[ 5].SetOffset(Vector<int>(2, 0));
250 tiles[ 6].SetOffset(Vector<int>(2, 0));
251 tiles[ 7].SetOffset(Vector<int>(2, 0));
253 tiles[ 8].SetOffset(Vector<int>(2, 1));
254 tiles[ 9].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
255 tiles[10].SetOffset(Vector<int>(3, 0));
256 tiles[11].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
257 tiles[12].SetOffset(Vector<int>(0, 2));
258 tiles[13].SetOffset(Vector<int>(1, 2));
259 tiles[14].SetOffset(Vector<int>(1, 2));
260 tiles[15].SetOffset(Vector<int>(1, 2));
262 tiles[16].SetOffset(Vector<int>(2, 1));
263 tiles[17].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
264 tiles[18].SetOffset(Vector<int>(3, 0));
265 tiles[19].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
266 tiles[20].SetOffset(Vector<int>(0, 3));
267 tiles[21].SetOffset(Vector<int>(1, 3));
268 tiles[22].SetOffset(Vector<int>(1, 3));
269 tiles[23].SetOffset(Vector<int>(2, 3));
271 tiles[24].SetOffset(Vector<int>(2, 1));
272 tiles[25].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
273 tiles[26].SetOffset(Vector<int>(3, 0));
274 tiles[27].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
275 tiles[28].SetOffset(Vector<int>(0, 4));
276 tiles[29].SetOffset(Vector<int>(1, 4));
277 tiles[30].SetOffset(Vector<int>(1, 4));
278 tiles[31].SetOffset(Vector<int>(2, 4));
280 tiles[32].SetOffset(Vector<int>(2, 1));
281 tiles[33].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
282 tiles[34].SetOffset(Vector<int>(3, 0));
283 tiles[35].SetOffset(Vector<int>(3, 0));
284 tiles[36].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
285 tiles[37].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
286 tiles[38].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH);
287 tiles[39].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_NORTH | Tile::BLOCK_EAST);
289 tiles[40].SetOffset(Vector<int>(2, 1));
290 tiles[41].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
291 tiles[42].SetOffset(Vector<int>(3, 0));
292 tiles[43].SetOffset(Vector<int>(3, 0));
293 tiles[44].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
294 tiles[45].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
295 tiles[46].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH);
296 tiles[47].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
298 tiles[48].SetOffset(Vector<int>(2, 1));
299 tiles[49].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_WEST);
300 tiles[50].SetOffset(Vector<int>(3, 0));
301 tiles[51].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_EAST);
302 tiles[52].SetOffset(Vector<int>(0, 0));
303 tiles[53].SetOffset(Vector<int>(1, 0));
304 tiles[54].SetOffset(Vector<int>(1, 0));
305 tiles[55].SetOffset(Vector<int>(1, 0));
307 tiles[56].SetOffset(Vector<int>(2, 1));
308 tiles[57].SetOffset(Vector<int>(4, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_WEST);
309 tiles[58].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH);
310 tiles[59].SetOffset(Vector<int>(3, 0)).SetFlags(Tile::BLOCK_SOUTH | Tile::BLOCK_EAST);
311 tiles[60].SetOffset(Vector<int>(0, 1));
312 tiles[61].SetOffset(Vector<int>(1, 1));
313 tiles[62].SetOffset(Vector<int>(1, 1));
314 tiles[63].SetOffset(Vector<int>(1, 1));
317 areas[0].SetTiles(tiles, 64);
318 areas[0].SetWidth(8);
321 triggers[0].SetTilePosition(Vector<int>(2, 0));
323 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
324 Sprite tileset(tilesetImg, tileSize, tileSize);
327 map.SetAreas(areas, 1);
328 map.SetTileset(&tileset);
329 map.SetTriggers(triggers, 1);
332 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
333 Sprite mapMaximSprite(mapMaximImg, 32, 64);
334 SimpleAnimation mapMaximAnimation(&mapMaximSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
336 mapMaxim.SetAnimation(&mapMaximAnimation);
337 mapMaxim.Position() = Vector<float>(64, 128);
338 mapMaxim.SpriteOffset() = Vector<float>(0, -32);
340 SDL_Surface *mapSelanImg(IMG_Load("test-data/selan-map.png"));
341 Sprite mapSelanSprite(mapSelanImg, 32, 64);
342 SimpleAnimation mapSelanAnimation(&mapSelanSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
344 mapSelan.SetAnimation(&mapSelanAnimation);
345 mapSelan.Position() = Vector<float>(64, 128);
346 mapSelan.SpriteOffset() = Vector<float>(0, -32);
347 mapMaxim.AddFollower(&mapSelan);
349 SDL_Surface *mapGuyImg(IMG_Load("test-data/guy-map.png"));
350 Sprite mapGuySprite(mapGuyImg, 32, 64);
351 SimpleAnimation mapGuyAnimation(&mapGuySprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
353 mapGuy.SetAnimation(&mapGuyAnimation);
354 mapGuy.Position() = Vector<float>(64, 128);
355 mapGuy.SpriteOffset() = Vector<float>(0, -32);
356 mapSelan.AddFollower(&mapGuy);
358 SDL_Surface *mapDekarImg(IMG_Load("test-data/dekar-map.png"));
359 Sprite mapDekarSprite(mapDekarImg, 32, 64);
360 SimpleAnimation mapDekarAnimation(&mapDekarSprite, (tileSize/walkSpeed) / 2 * 1000, 2, 0, 0, true);
362 mapDekar.SetAnimation(&mapDekarAnimation);
363 mapDekar.Position() = Vector<float>(64, 128);
364 mapDekar.SpriteOffset() = Vector<float>(0, -32);
365 mapGuy.AddFollower(&mapDekar);
367 InitScreen screen(width, height);
369 app::State *state(0);
372 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
373 battleState->AddMonster(monster);
374 battleState->AddMonster(monster);
375 battleState->AddMonster(monster);
376 battleState->AddMonster(monster);
377 battleState->AddHero(maxim);
378 battleState->AddHero(selan);
379 battleState->AddHero(guy);
380 battleState->AddHero(dekar);
383 MapState *mapState(new MapState(&map));
384 mapState->AddEntity(&mapMaxim);
385 mapState->AddEntity(&mapSelan);
386 mapState->AddEntity(&mapGuy);
387 mapState->AddEntity(&mapDekar);
388 mapState->ControlEntity(&mapMaxim);
389 mapState->SetWalkingSpeed(walkSpeed);
393 Application app(&screen, state);
394 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
395 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
396 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
397 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
398 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
399 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
400 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
401 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
402 app.Buttons().MapKey(SDLK_RETURN, Input::START);
403 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
404 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
405 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
409 } catch (Parser::Error &e) {
410 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
412 } catch (exception &e) {
413 cerr << "exception in main(): " << e.what() << endl;