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 "common/GameConfig.h"
17 #include "common/GameState.h"
18 #include "common/Hero.h"
19 #include "common/Ikari.h"
20 #include "common/Inventory.h"
21 #include "common/Item.h"
22 #include "common/Script.h"
23 #include "common/Spell.h"
24 #include "common/Stats.h"
25 #include "geometry/Vector.h"
26 #include "graphics/ComplexAnimation.h"
27 #include "graphics/Font.h"
28 #include "graphics/Frame.h"
29 #include "graphics/Gauge.h"
30 #include "graphics/Menu.h"
31 #include "graphics/SimpleAnimation.h"
32 #include "graphics/Sprite.h"
33 #include "graphics/Texture.h"
34 #include "loader/Caster.h"
35 #include "loader/Interpreter.h"
36 #include "loader/ParsedSource.h"
37 #include "loader/Parser.h"
38 #include "loader/TypeDescription.h"
40 #include "map/Entity.h"
42 #include "map/MapState.h"
44 #include "map/Trigger.h"
45 #include "menu/Resources.h"
46 #include "sdl/InitImage.h"
47 #include "sdl/InitScreen.h"
48 #include "sdl/InitSDL.h"
57 #include <SDL_image.h>
59 using app::Application;
62 using battle::BattleState;
63 using battle::Monster;
64 using battle::PartyLayout;
65 using common::GameConfig;
66 using common::GameState;
68 using geometry::Vector;
69 using graphics::Texture;
71 using loader::Interpreter;
72 using loader::ParsedSource;
74 using loader::TypeDescription;
78 using sdl::InitScreen;
88 int main(int argc, char **argv) {
89 const int width = 512;
90 const int height = 448;
92 const float walkSpeed = 128.0f;
96 // std::srand(std::time(0));
100 InitImage image(IMG_INIT_PNG);
102 Interpreter::CreateTypeDescriptions();
104 battle::Resources::CreateTypeDescription();
105 battle::Monster::CreateTypeDescription();
106 battle::PartyLayout::CreateTypeDescription();
108 common::Hero::CreateTypeDescription();
109 common::Ikari::CreateTypeDescription();
110 common::Item::CreateTypeDescription();
111 common::Stats::CreateTypeDescription();
112 common::Spell::CreateTypeDescription();
113 common::TargetingMode::CreateTypeDescription();
115 graphics::Animation::CreateTypeDescription();
116 graphics::ComplexAnimation::CreateTypeDescription();
117 graphics::Font::CreateTypeDescription();
118 graphics::Frame::CreateTypeDescription();
119 graphics::Gauge::CreateTypeDescription();
120 graphics::MenuProperties::CreateTypeDescription();
121 graphics::SimpleAnimation::CreateTypeDescription();
122 graphics::Sprite::CreateTypeDescription();
124 map::Area::CreateTypeDescription();
125 map::Entity::CreateTypeDescription();
126 map::Map::CreateTypeDescription();
127 map::Tile::CreateTypeDescription();
128 map::Trigger::CreateTypeDescription();
131 args.Read(argc, argv);
135 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
136 Parser(*i, source).Parse();
139 switch (args.GetRunLevel()) {
140 case Arguments::WRITE:
142 int length(std::strlen(args.OutfilePath()));
143 switch (args.OutfilePath()[length - 1]) {
145 std::ofstream outstream(args.OutfilePath());
146 source.WriteHeader(outstream);
150 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
155 case Arguments::DUMP: {
156 std::cout << source << std::endl;
159 case Arguments::SOURCE_WIKI: {
160 TypeDescription::WriteSourceWiki(std::cout);
163 case Arguments::BATTLE:
166 case Arguments::PLAY:
171 Interpreter intp(source);
174 if (intp.PostponedDefinitions().size() > 0) {
175 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
176 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
185 gameState.heroes[0] = *caster.GetHero("maxim");
186 gameState.heroes[1] = *caster.GetHero("selan");
187 gameState.heroes[2] = *caster.GetHero("guy");
188 gameState.heroes[3] = *caster.GetHero("dekar");
190 gameState.party[0] = &gameState.heroes[0];
191 gameState.party[1] = &gameState.heroes[1];
192 gameState.party[2] = &gameState.heroes[2];
193 gameState.party[3] = &gameState.heroes[3];
194 gameState.partySize = 4;
196 GameConfig gameConfig;
197 gameConfig.state = &gameState;
198 gameConfig.heroesLayout = caster.GetPartyLayout("heroesLayout");
199 gameConfig.battleResources = caster.GetBattleResources("battleResources");
201 // temporary test data
202 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
203 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
205 Monster monster(*caster.GetMonster("lizard"));
207 gameState.heroes[0].AddSpell(caster.GetSpell("resetSpell"));
208 Spell *strongSpell(caster.GetSpell("strongSpell"));
209 gameState.heroes[0].AddSpell(strongSpell);
210 gameState.heroes[1].AddSpell(strongSpell);
211 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
212 gameState.heroes[0].AddSpell(strongerSpell);
213 gameState.heroes[1].AddSpell(strongerSpell);
214 Spell *championSpell(caster.GetSpell("championSpell"));
215 gameState.heroes[0].AddSpell(championSpell);
216 gameState.heroes[1].AddSpell(championSpell);
217 Spell *rallySpell(caster.GetSpell("rallySpell"));
218 gameState.heroes[0].AddSpell(rallySpell);
219 gameState.heroes[1].AddSpell(rallySpell);
220 gameState.heroes[1].AddSpell(caster.GetSpell("escapeSpell"));
221 Spell *valorSpell(caster.GetSpell("valorSpell"));
222 gameState.heroes[0].AddSpell(valorSpell);
223 gameState.heroes[1].AddSpell(valorSpell);
225 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
226 gameState.inventory.Add(caster.GetItem("antidoteItem"), 9);
227 gameState.inventory.Add(caster.GetItem("powerRingItem"));
228 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
229 gameState.inventory.Add(caster.GetItem("sProRingItem"));
230 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
231 gameState.inventory.Add(caster.GetItem("powerRingItem"));
232 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
233 gameState.inventory.Add(caster.GetItem("zircoSwordItem"));
234 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
235 gameState.inventory.Add(caster.GetItem("zircoHelmetItem"));
236 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
237 gameState.inventory.Add(caster.GetItem("zirconPlateItem"));
239 gameState.heroes[0].SetWeapon(caster.GetItem("zircoSwordItem"));
240 gameState.heroes[0].SetArmor(caster.GetItem("zirconArmorItem"));
241 gameState.heroes[0].SetShield(caster.GetItem("holyShieldItem"));
242 gameState.heroes[0].SetHelmet(caster.GetItem("legendHelmItem"));
243 gameState.heroes[0].SetRing(caster.GetItem("sProRingItem"));
244 gameState.heroes[0].SetJewel(caster.GetItem("evilJewelItem"));
246 // gameState.heroes[1].SetWeapon(cst.GetItem("zircoWhipItem"));
247 gameState.heroes[1].SetArmor(caster.GetItem("zirconPlateItem"));
248 gameState.heroes[1].SetShield(caster.GetItem("zircoGlovesItem"));
249 gameState.heroes[1].SetHelmet(caster.GetItem("holyCapItem"));
250 gameState.heroes[1].SetRing(caster.GetItem("ghostRingItem"));
251 gameState.heroes[1].SetJewel(caster.GetItem("eagleRockItem"));
253 // gameState.heroes[2].SetWeapon(cst.GetItem("zircoAxItem"));
254 gameState.heroes[2].SetArmor(caster.GetItem("zirconArmorItem"));
255 gameState.heroes[2].SetShield(caster.GetItem("megaShieldItem"));
256 gameState.heroes[2].SetHelmet(caster.GetItem("zircoHelmetItem"));
257 gameState.heroes[2].SetRing(caster.GetItem("powerRingItem"));
258 gameState.heroes[2].SetJewel(caster.GetItem("evilJewelItem"));
260 // NOTE: this is actually Artea equipment
261 // gameState.heroes[3].SetWeapon(cst.GetItem("lizardBlowItem"));
262 gameState.heroes[3].SetArmor(caster.GetItem("holyRobeItem"));
263 gameState.heroes[3].SetShield(caster.GetItem("zircoGlovesItem"));
264 gameState.heroes[3].SetHelmet(caster.GetItem("holyCapItem"));
265 gameState.heroes[3].SetRing(caster.GetItem("rocketRingItem"));
266 gameState.heroes[3].SetJewel(caster.GetItem("krakenRockItem"));
268 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
270 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
271 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
272 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
274 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
275 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
276 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
278 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
279 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
280 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
282 menu::Resources menuResources;
283 gameConfig.menuResources = &menuResources;
286 menubg.SetSurface(IMG_Load("test-data/menubg.png"));
287 menubg.SetSize(Vector<int>(64, 64));
288 menuResources.menubg = &menubg;
290 menuResources.statusFont = gameConfig.battleResources->normalFont;
292 graphics::Sprite statusLabels(IMG_Load("test-data/status-labels.png"), 32, 16);
293 menuResources.statusLabels = &statusLabels;
295 graphics::Frame statusFrame(IMG_Load("test-data/status-frame.png"), 32, 32, 32, 32);
296 menuResources.statusFrame = &statusFrame;
298 graphics::Sprite menuFontSprite(IMG_Load("test-data/menu-font.png"), 16, 16);
299 graphics::Font menuFont(&menuFontSprite, 0, -2);
301 menuResources.normalFont = &menuFont;
303 graphics::Sprite menuCursor(IMG_Load("test-data/menu-cursor.png"), 32, 16);
304 menuResources.menuCursor = &menuCursor;
305 graphics::Sprite menuActiveCursor(IMG_Load("test-data/menu-cursor-active.png"), 32, 18);
306 menuResources.menuActiveCursor = &menuActiveCursor;
308 graphics::MenuProperties mainMenuProperties;
309 mainMenuProperties.cols = 2;
310 mainMenuProperties.rows = 4;
311 mainMenuProperties.charsPerEntry = 8;
312 mainMenuProperties.rowGap = 8;
313 mainMenuProperties.colGap = 32;
314 mainMenuProperties.cursor = &menuCursor;
315 mainMenuProperties.font = &menuFont;
316 mainMenuProperties.wrapX = true;
317 mainMenuProperties.wrapY = true;
318 menuResources.mainMenuProperties = &mainMenuProperties;
320 menuResources.mainMenuItemText = "ITEM";
321 menuResources.mainMenuSpellText = "SPELL";
322 menuResources.mainMenuCapsuleText = "CAPSULE";
323 menuResources.mainMenuEquipmentText = "EQUIP";
324 menuResources.mainMenuStatusText = "STATUS";
325 menuResources.mainMenuChangeText = "CHANGE";
326 menuResources.mainMenuConfigText = "CONFIG";
327 menuResources.mainMenuScenarioText = "SCENARIO";
329 menuResources.mainMenuTimeText = "TIME";
330 menuResources.mainMenuGoldText = "GOLD";
332 graphics::Sprite heroCursor(IMG_Load("test-data/hero-cursor.png"), 64, 16);
333 menuResources.heroCursor = &heroCursor;
334 menuResources.heroCursorBlinkTime = 532;
336 menuResources.noEquipmentText = "No equip";
338 graphics::Sprite shoulderNav(IMG_Load("test-data/shoulder-nav.png"), 160, 16);
339 menuResources.shoulderNav = &shoulderNav;
341 menuResources.atpLabel = "ATP";
342 menuResources.dfpLabel = "DFP";
343 menuResources.strLabel = "STR";
344 menuResources.aglLabel = "AGL";
345 menuResources.intLabel = "INT";
346 menuResources.gutLabel = "GUT";
347 menuResources.mgrLabel = "MGR";
349 menuResources.ipLabel = "IP";
350 menuResources.experienceLabel = "NOW EXP";
351 menuResources.nextLevelLabel = "NEXT LEVEL";
353 graphics::MenuProperties statusMenuProperties;
354 statusMenuProperties.cols = 2;
355 statusMenuProperties.rows = 1;
356 statusMenuProperties.charsPerEntry = 6;
357 statusMenuProperties.rowGap = 0;
358 statusMenuProperties.colGap = 16;
359 statusMenuProperties.cursor = &menuCursor;
360 statusMenuProperties.font = &menuFont;
361 statusMenuProperties.wrapX = true;
362 menuResources.statusMenuProperties = &statusMenuProperties;
364 menuResources.nextLabel = "NEXT";
365 menuResources.returnLabel = "RETURN";
367 graphics::MenuProperties itemMenuProperties;
368 itemMenuProperties.cols = 3;
369 itemMenuProperties.rows = 1;
370 itemMenuProperties.charsPerEntry = 5;
371 itemMenuProperties.rowGap = 8;
372 itemMenuProperties.colGap = 16;
373 itemMenuProperties.cursor = &menuCursor;
374 itemMenuProperties.selectedCursor = &menuActiveCursor;
375 itemMenuProperties.font = &menuFont;
376 itemMenuProperties.wrapX = true;
377 itemMenuProperties.wrapY = true;
378 menuResources.itemMenuProperties = &itemMenuProperties;
379 menuResources.itemMenuUseText = "USE";
380 menuResources.itemMenuSortText = "SORT";
381 menuResources.itemMenuDropText = "DROP";
383 graphics::MenuProperties inventoryMenuProperties;
384 inventoryMenuProperties.cols = 1;
385 inventoryMenuProperties.rows = 6;
386 inventoryMenuProperties.charsPerEntry = 13;
387 inventoryMenuProperties.rowGap = 8;
388 inventoryMenuProperties.cursor = &menuCursor;
389 inventoryMenuProperties.selectedCursor = &menuActiveCursor;
390 inventoryMenuProperties.font = &menuFont;
391 // TODO: disabled font
392 inventoryMenuProperties.disabledFont = &menuFont;
393 inventoryMenuProperties.iconSpace = 16;
394 inventoryMenuProperties.charsPerNumber = 2;
395 inventoryMenuProperties.delimiter = ':';
396 menuResources.inventoryMenuProperties = &inventoryMenuProperties;
398 graphics::MenuProperties spellMenuProperties;
399 spellMenuProperties.cols = 2;
400 spellMenuProperties.rows = 6;
401 spellMenuProperties.charsPerEntry = 8;
402 spellMenuProperties.rowGap = 8;
403 spellMenuProperties.colGap = 48;
404 spellMenuProperties.cursor = &menuCursor;
405 spellMenuProperties.selectedCursor = &menuActiveCursor;
406 spellMenuProperties.font = &menuFont;
407 // TODO: disabled font
408 spellMenuProperties.disabledFont = &menuFont;
409 spellMenuProperties.iconSpace = 0;
410 spellMenuProperties.charsPerNumber = 2;
411 spellMenuProperties.delimiter = ':';
412 menuResources.spellMenuProperties = &spellMenuProperties;
414 graphics::MenuProperties equipmentActionMenuProperties;
415 equipmentActionMenuProperties.cols = 1;
416 equipmentActionMenuProperties.rows = 5;
417 equipmentActionMenuProperties.charsPerEntry = 10;
418 equipmentActionMenuProperties.rowGap = 8;
419 equipmentActionMenuProperties.cursor = &menuCursor;
420 equipmentActionMenuProperties.selectedCursor = &menuActiveCursor;
421 equipmentActionMenuProperties.font = &menuFont;
422 equipmentActionMenuProperties.iconSpace = 0;
423 menuResources.equipmentActionMenuProperties = &equipmentActionMenuProperties;
425 graphics::MenuProperties equipmentMenuProperties;
426 equipmentMenuProperties.cols = 1;
427 equipmentMenuProperties.rows = 6;
428 equipmentMenuProperties.charsPerEntry = 12;
429 equipmentMenuProperties.rowGap = 16;
430 equipmentMenuProperties.cursor = &menuCursor;
431 equipmentMenuProperties.selectedCursor = &menuActiveCursor;
432 equipmentMenuProperties.font = menuResources.statusFont;
433 // TODO: disabled font
434 equipmentMenuProperties.disabledFont = menuResources.statusFont;
435 equipmentMenuProperties.iconSpace = 16;
436 menuResources.equipmentMenuProperties = &equipmentMenuProperties;
438 menuResources.equipMenuEquipLabel = "EQUIP";
439 menuResources.equipMenuStrongestLabel = "STRONGEST";
440 menuResources.equipMenuRemoveLabel = "REMOVE";
441 menuResources.equipMenuRemoveAllLabel = "REMOVE ALL";
442 menuResources.equipMenuDropLabel = "DROP";
444 InitScreen screen(width, height);
446 app::State *state(0);
449 BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
450 battleState->AddMonster(monster);
451 battleState->AddMonster(monster);
452 battleState->AddMonster(monster);
453 battleState->AddMonster(monster);
454 battleState->AddHero(gameState.heroes[0]);
455 battleState->AddHero(gameState.heroes[1]);
456 battleState->AddHero(gameState.heroes[2]);
457 battleState->AddHero(gameState.heroes[3]);
460 MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
462 mapState->ControlEntity(&gameState.heroes[0].MapEntity());
463 mapState->SetWalkingSpeed(walkSpeed);
468 Application app(screen, state);
469 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
470 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
471 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
472 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
473 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
474 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
475 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
476 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
477 app.Buttons().MapKey(SDLK_RETURN, Input::START);
478 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
479 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
480 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
481 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
482 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
483 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
484 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
488 } catch (Parser::Error &e) {
489 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
491 } catch (exception &e) {
492 cerr << "exception in main(): " << e.what() << endl;