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("antidoteItem"), 9);
226 gameState.inventory.Add(caster.GetItem("magicJarItem"), 4);
227 gameState.inventory.Add(caster.GetItem("hiPotionItem"), 4);
228 gameState.inventory.Add(caster.GetItem("powerPotionItem"), 4);
229 gameState.inventory.Add(caster.GetItem("escapeItem"), 2);
230 gameState.inventory.Add(caster.GetItem("sleepBallItem"), 1);
232 gameState.heroes[0].SetWeapon(caster.GetItem("zircoSwordItem"));
233 gameState.heroes[0].SetArmor(caster.GetItem("zirconArmorItem"));
234 gameState.heroes[0].SetShield(caster.GetItem("holyShieldItem"));
235 gameState.heroes[0].SetHelmet(caster.GetItem("legendHelmItem"));
236 gameState.heroes[0].SetRing(caster.GetItem("sProRingItem"));
237 gameState.heroes[0].SetJewel(caster.GetItem("evilJewelItem"));
239 // gameState.heroes[1].SetWeapon(cst.GetItem("zircoWhipItem"));
240 gameState.heroes[1].SetArmor(caster.GetItem("zirconPlateItem"));
241 gameState.heroes[1].SetShield(caster.GetItem("zircoGlovesItem"));
242 gameState.heroes[1].SetHelmet(caster.GetItem("holyCapItem"));
243 gameState.heroes[1].SetRing(caster.GetItem("ghostRingItem"));
244 gameState.heroes[1].SetJewel(caster.GetItem("eagleRockItem"));
246 // gameState.heroes[2].SetWeapon(cst.GetItem("zircoAxItem"));
247 gameState.heroes[2].SetArmor(caster.GetItem("zirconArmorItem"));
248 gameState.heroes[2].SetShield(caster.GetItem("megaShieldItem"));
249 gameState.heroes[2].SetHelmet(caster.GetItem("zircoHelmetItem"));
250 gameState.heroes[2].SetRing(caster.GetItem("powerRingItem"));
251 gameState.heroes[2].SetJewel(caster.GetItem("evilJewelItem"));
253 // NOTE: this is actually Artea equipment
254 // gameState.heroes[3].SetWeapon(cst.GetItem("lizardBlowItem"));
255 gameState.heroes[3].SetArmor(caster.GetItem("holyRobeItem"));
256 gameState.heroes[3].SetShield(caster.GetItem("zircoGlovesItem"));
257 gameState.heroes[3].SetHelmet(caster.GetItem("holyCapItem"));
258 gameState.heroes[3].SetRing(caster.GetItem("rocketRingItem"));
259 gameState.heroes[3].SetJewel(caster.GetItem("krakenRockItem"));
261 gameState.heroes[0].MapEntity().Position() = Vector<float>(64, 128);
263 gameState.heroes[1].MapEntity().Position() = Vector<float>(64, 128);
264 gameState.heroes[1].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
265 gameState.heroes[0].MapEntity().AddFollower(&gameState.heroes[1].MapEntity());
267 gameState.heroes[2].MapEntity().Position() = Vector<float>(64, 128);
268 gameState.heroes[2].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
269 gameState.heroes[1].MapEntity().AddFollower(&gameState.heroes[2].MapEntity());
271 gameState.heroes[3].MapEntity().Position() = Vector<float>(64, 128);
272 gameState.heroes[3].MapEntity().SetFlags(Entity::FLAG_NONBLOCKING);
273 gameState.heroes[2].MapEntity().AddFollower(&gameState.heroes[3].MapEntity());
275 menu::Resources menuResources;
276 gameConfig.menuResources = &menuResources;
279 menubg.SetSurface(IMG_Load("test-data/menubg.png"));
280 menubg.SetSize(Vector<int>(64, 64));
281 menuResources.menubg = &menubg;
283 menuResources.statusFont = gameConfig.battleResources->normalFont;
285 graphics::Sprite statusLabels(IMG_Load("test-data/status-labels.png"), 32, 16);
286 menuResources.statusLabels = &statusLabels;
288 graphics::Frame statusFrame(IMG_Load("test-data/status-frame.png"), 32, 32, 32, 32);
289 menuResources.statusFrame = &statusFrame;
291 graphics::Sprite menuFontSprite(IMG_Load("test-data/menu-font.png"), 16, 16);
292 graphics::Font menuFont(&menuFontSprite, 0, -2);
294 menuResources.normalFont = &menuFont;
296 graphics::Sprite menuCursor(IMG_Load("test-data/menu-cursor.png"), 32, 16);
298 graphics::MenuProperties mainMenuProperties;
299 mainMenuProperties.cols = 2;
300 mainMenuProperties.rows = 4;
301 mainMenuProperties.charsPerEntry = 8;
302 mainMenuProperties.rowGap = 8;
303 mainMenuProperties.colGap = 32;
304 mainMenuProperties.cursor = &menuCursor;
305 mainMenuProperties.font = &menuFont;
306 mainMenuProperties.wrapX = true;
307 mainMenuProperties.wrapY = true;
308 menuResources.mainMenuProperties = &mainMenuProperties;
310 menuResources.mainMenuItemText = "ITEM";
311 menuResources.mainMenuSpellText = "SPELL";
312 menuResources.mainMenuCapsuleText = "CAPSULE";
313 menuResources.mainMenuEquipmentText = "EQUIP";
314 menuResources.mainMenuStatusText = "STATUS";
315 menuResources.mainMenuChangeText = "CHANGE";
316 menuResources.mainMenuConfigText = "CONFIG";
317 menuResources.mainMenuScenarioText = "SCENARIO";
319 menuResources.mainMenuTimeText = "TIME";
320 menuResources.mainMenuGoldText = "GOLD";
322 graphics::Sprite heroCursor(IMG_Load("test-data/hero-cursor.png"), 64, 16);
323 menuResources.heroCursor = &heroCursor;
324 menuResources.heroCursorBlinkTime = 532;
326 menuResources.noEquipmentText = "No equip";
328 graphics::Sprite shoulderNav(IMG_Load("test-data/shoulder-nav.png"), 160, 16);
329 menuResources.shoulderNav = &shoulderNav;
331 menuResources.atpLabel = "ATP";
332 menuResources.dfpLabel = "DFP";
333 menuResources.strLabel = "STR";
334 menuResources.aglLabel = "AGL";
335 menuResources.intLabel = "INT";
336 menuResources.gutLabel = "GUT";
337 menuResources.mgrLabel = "MGR";
339 menuResources.ipLabel = "IP";
340 menuResources.experienceLabel = "NOW EXP";
341 menuResources.nextLevelLabel = "NEXT LEVEL";
343 graphics::MenuProperties statusMenuProperties;
344 statusMenuProperties.cols = 2;
345 statusMenuProperties.rows = 1;
346 statusMenuProperties.charsPerEntry = 6;
347 statusMenuProperties.rowGap = 0;
348 statusMenuProperties.colGap = 16;
349 statusMenuProperties.cursor = &menuCursor;
350 statusMenuProperties.font = &menuFont;
351 statusMenuProperties.wrapX = true;
352 menuResources.statusMenuProperties = &statusMenuProperties;
354 menuResources.nextLabel = "NEXT";
355 menuResources.returnLabel = "RETURN";
357 InitScreen screen(width, height);
359 app::State *state(0);
362 BattleState *battleState(new BattleState(&gameConfig, bg, &monstersLayout));
363 battleState->AddMonster(monster);
364 battleState->AddMonster(monster);
365 battleState->AddMonster(monster);
366 battleState->AddMonster(monster);
367 battleState->AddHero(gameState.heroes[0]);
368 battleState->AddHero(gameState.heroes[1]);
369 battleState->AddHero(gameState.heroes[2]);
370 battleState->AddHero(gameState.heroes[3]);
373 MapState *mapState(new MapState(&gameConfig, caster.GetMap("map1")));
375 mapState->ControlEntity(&gameState.heroes[0].MapEntity());
376 mapState->SetWalkingSpeed(walkSpeed);
381 Application app(screen, state);
382 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
383 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
384 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
385 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
386 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
387 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
388 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
389 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
390 app.Buttons().MapKey(SDLK_RETURN, Input::START);
391 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
392 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
393 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
394 app.Buttons().MapKey(SDLK_1, Input::DEBUG_1);
395 app.Buttons().MapKey(SDLK_2, Input::DEBUG_2);
396 app.Buttons().MapKey(SDLK_3, Input::DEBUG_3);
397 app.Buttons().MapKey(SDLK_4, Input::DEBUG_4);
401 } catch (Parser::Error &e) {
402 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
404 } catch (exception &e) {
405 cerr << "exception in main(): " << e.what() << endl;