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 bool battle(false);
99 // std::srand(std::time(0));
103 InitImage image(IMG_INIT_PNG);
105 battle::Resources::CreateTypeDescription();
106 ComplexAnimation::CreateTypeDescription();
107 Font::CreateTypeDescription();
108 Frame::CreateTypeDescription();
109 Gauge::CreateTypeDescription();
110 Hero::CreateTypeDescription();
111 Ikari::CreateTypeDescription();
112 Interpreter::CreateTypeDescriptions();
113 Item::CreateTypeDescription();
114 graphics::MenuProperties::CreateTypeDescription();
115 Monster::CreateTypeDescription();
116 PartyLayout::CreateTypeDescription();
117 SimpleAnimation::CreateTypeDescription();
118 Spell::CreateTypeDescription();
119 Sprite::CreateTypeDescription();
120 Stats::CreateTypeDescription();
121 common::TargetingMode::CreateTypeDescription();
124 args.Read(argc, argv);
128 for (vector<char *>::const_iterator i(args.Infiles().begin()), end(args.Infiles().end()); i != end; ++i) {
129 Parser(*i, source).Parse();
132 switch (args.DetectRunLevel()) {
133 case Arguments::WRITE:
135 int length(std::strlen(args.OutfilePath()));
136 switch (args.OutfilePath()[length - 1]) {
138 std::ofstream outstream(args.OutfilePath());
139 source.WriteHeader(outstream);
143 throw std::runtime_error(string("don't know how to write file ") + args.OutfilePath());
148 case Arguments::DUMP: {
149 std::cout << source << std::endl;
152 case Arguments::PLAY:
156 Interpreter intp(source);
159 if (intp.PostponedDefinitions().size() > 0) {
160 for (vector<Interpreter::PostponedDefinition>::const_iterator i(intp.PostponedDefinitions().begin()), end(intp.PostponedDefinitions().end()); i != end; ++i) {
161 std::cerr << "missing definition of " << TypeDescription::Get(i->linkedType).TypeName() << " " << i->identifier << std::endl;
168 // temporary test data
169 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
170 PartyLayout monstersLayout(*caster.GetPartyLayout("monstersLayout"));
171 PartyLayout heroesLayout(*caster.GetPartyLayout("heroesLayout"));
173 Monster monster(*caster.GetMonster("lizard"));
174 Hero maxim(*caster.GetHero("maxim"));
175 Hero selan(*caster.GetHero("selan"));
176 Hero guy(*caster.GetHero("guy"));
177 Hero dekar(*caster.GetHero("dekar"));
179 battle::Resources *battleRes(caster.GetBattleResources("battleResources"));
181 maxim.AddSpell(caster.GetSpell("resetSpell"));
182 Spell *strongSpell(caster.GetSpell("strongSpell"));
183 maxim.AddSpell(strongSpell);
184 selan.AddSpell(strongSpell);
185 Spell *strongerSpell(caster.GetSpell("strongerSpell"));
186 maxim.AddSpell(strongerSpell);
187 selan.AddSpell(strongerSpell);
188 Spell *championSpell(caster.GetSpell("championSpell"));
189 maxim.AddSpell(championSpell);
190 selan.AddSpell(championSpell);
191 Spell *rallySpell(caster.GetSpell("rallySpell"));
192 maxim.AddSpell(rallySpell);
193 selan.AddSpell(rallySpell);
194 selan.AddSpell(caster.GetSpell("escapeSpell"));
195 Spell *valorSpell(caster.GetSpell("valorSpell"));
196 maxim.AddSpell(valorSpell);
197 selan.AddSpell(valorSpell);
200 inventory.Add(caster.GetItem("antidoteItem"), 9);
201 inventory.Add(caster.GetItem("magicJarItem"), 4);
202 inventory.Add(caster.GetItem("hiPotionItem"), 4);
203 inventory.Add(caster.GetItem("powerPotionItem"), 4);
204 inventory.Add(caster.GetItem("escapeItem"), 2);
205 inventory.Add(caster.GetItem("sleepBallItem"), 1);
206 battleRes->inventory = &inventory;
208 maxim.SetWeapon(caster.GetItem("zircoSwordItem"));
209 maxim.SetArmor(caster.GetItem("zirconArmorItem"));
210 maxim.SetShield(caster.GetItem("holyShieldItem"));
211 maxim.SetHelmet(caster.GetItem("legendHelmItem"));
212 maxim.SetRing(caster.GetItem("sProRingItem"));
213 maxim.SetJewel(caster.GetItem("evilJewelItem"));
215 // selan.SetWeapon(cst.GetItem("zircoWhipItem"));
216 selan.SetArmor(caster.GetItem("zirconPlateItem"));
217 selan.SetShield(caster.GetItem("zircoGlovesItem"));
218 selan.SetHelmet(caster.GetItem("holyCapItem"));
219 selan.SetRing(caster.GetItem("ghostRingItem"));
220 selan.SetJewel(caster.GetItem("eagleRockItem"));
222 // guy.SetWeapon(cst.GetItem("zircoAxItem"));
223 guy.SetArmor(caster.GetItem("zirconArmorItem"));
224 guy.SetShield(caster.GetItem("megaShieldItem"));
225 guy.SetHelmet(caster.GetItem("zircoHelmetItem"));
226 guy.SetRing(caster.GetItem("powerRingItem"));
227 guy.SetJewel(caster.GetItem("evilJewelItem"));
229 // NOTE: this is actually Artea equipment
230 // dekar.SetWeapon(cst.GetItem("lizardBlowItem"));
231 dekar.SetArmor(caster.GetItem("holyRobeItem"));
232 dekar.SetShield(caster.GetItem("zircoGlovesItem"));
233 dekar.SetHelmet(caster.GetItem("holyCapItem"));
234 dekar.SetRing(caster.GetItem("rocketRingItem"));
235 dekar.SetJewel(caster.GetItem("krakenRockItem"));
239 tiles[ 0].SetOffset(Vector<int>(2, 1));
240 tiles[ 1].SetOffset(Vector<int>(4, 0));
241 tiles[ 2].SetOffset(Vector<int>(3, 0));
242 tiles[ 3].SetOffset(Vector<int>(3, 0));
243 tiles[ 4].SetOffset(Vector<int>(0, 1));
244 tiles[ 5].SetOffset(Vector<int>(2, 0));
245 tiles[ 6].SetOffset(Vector<int>(2, 0));
246 tiles[ 7].SetOffset(Vector<int>(2, 0));
248 tiles[ 8].SetOffset(Vector<int>(2, 1));
249 tiles[ 9].SetOffset(Vector<int>(4, 0));
250 tiles[10].SetOffset(Vector<int>(3, 0));
251 tiles[11].SetOffset(Vector<int>(3, 0));
252 tiles[12].SetOffset(Vector<int>(0, 2));
253 tiles[13].SetOffset(Vector<int>(1, 2));
254 tiles[14].SetOffset(Vector<int>(1, 2));
255 tiles[15].SetOffset(Vector<int>(1, 2));
257 tiles[16].SetOffset(Vector<int>(2, 1));
258 tiles[17].SetOffset(Vector<int>(4, 0));
259 tiles[18].SetOffset(Vector<int>(3, 0));
260 tiles[19].SetOffset(Vector<int>(3, 0));
261 tiles[20].SetOffset(Vector<int>(0, 3));
262 tiles[21].SetOffset(Vector<int>(1, 3));
263 tiles[22].SetOffset(Vector<int>(1, 3));
264 tiles[23].SetOffset(Vector<int>(2, 3));
266 tiles[24].SetOffset(Vector<int>(2, 1));
267 tiles[25].SetOffset(Vector<int>(4, 0));
268 tiles[26].SetOffset(Vector<int>(3, 0));
269 tiles[27].SetOffset(Vector<int>(3, 0));
270 tiles[28].SetOffset(Vector<int>(0, 4));
271 tiles[29].SetOffset(Vector<int>(1, 4));
272 tiles[30].SetOffset(Vector<int>(1, 4));
273 tiles[31].SetOffset(Vector<int>(2, 4));
275 tiles[32].SetOffset(Vector<int>(2, 1));
276 tiles[33].SetOffset(Vector<int>(4, 0));
277 tiles[34].SetOffset(Vector<int>(3, 0));
278 tiles[35].SetOffset(Vector<int>(3, 0));
279 tiles[36].SetOffset(Vector<int>(3, 0));
280 tiles[37].SetOffset(Vector<int>(3, 0));
281 tiles[38].SetOffset(Vector<int>(3, 0));
282 tiles[39].SetOffset(Vector<int>(3, 0));
284 tiles[40].SetOffset(Vector<int>(2, 1));
285 tiles[41].SetOffset(Vector<int>(4, 0));
286 tiles[42].SetOffset(Vector<int>(3, 0));
287 tiles[43].SetOffset(Vector<int>(3, 0));
288 tiles[44].SetOffset(Vector<int>(3, 0));
289 tiles[45].SetOffset(Vector<int>(4, 0));
290 tiles[46].SetOffset(Vector<int>(4, 0));
291 tiles[47].SetOffset(Vector<int>(4, 0));
293 tiles[48].SetOffset(Vector<int>(2, 1));
294 tiles[49].SetOffset(Vector<int>(4, 0));
295 tiles[50].SetOffset(Vector<int>(3, 0));
296 tiles[51].SetOffset(Vector<int>(3, 0));
297 tiles[52].SetOffset(Vector<int>(0, 0));
298 tiles[53].SetOffset(Vector<int>(1, 0));
299 tiles[54].SetOffset(Vector<int>(1, 0));
300 tiles[55].SetOffset(Vector<int>(1, 0));
302 tiles[56].SetOffset(Vector<int>(2, 1));
303 tiles[57].SetOffset(Vector<int>(4, 0));
304 tiles[58].SetOffset(Vector<int>(3, 0));
305 tiles[59].SetOffset(Vector<int>(3, 0));
306 tiles[60].SetOffset(Vector<int>(0, 1));
307 tiles[61].SetOffset(Vector<int>(1, 1));
308 tiles[62].SetOffset(Vector<int>(1, 1));
309 tiles[63].SetOffset(Vector<int>(1, 1));
312 area.SetTiles(tiles, 64);
315 SDL_Surface *tilesetImg(IMG_Load("test-data/tileset.png"));
316 Sprite tileset(tilesetImg, 32, 32);
319 map.SetAreas(&area, 1);
320 map.SetTileset(&tileset);
323 SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png"));
324 Sprite mapMaximSprite(mapMaximImg, 32, 64);
326 mapMaxim.SetSprite(&mapMaximSprite);
327 mapMaxim.Position() = Vector<float>(80, 160);
329 InitScreen screen(width, height);
331 app::State *state(0);
334 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, battleRes));
335 battleState->AddMonster(monster);
336 battleState->AddMonster(monster);
337 battleState->AddMonster(monster);
338 battleState->AddMonster(monster);
339 battleState->AddHero(maxim);
340 battleState->AddHero(selan);
341 battleState->AddHero(guy);
342 battleState->AddHero(dekar);
345 MapState *mapState(new MapState(&map));
346 mapState->AddEntity(&mapMaxim);
347 mapState->ControlEntity(&mapMaxim);
351 Application app(&screen, state);
352 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
353 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
354 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
355 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
356 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
357 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
358 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
359 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
360 app.Buttons().MapKey(SDLK_RETURN, Input::START);
361 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
362 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
363 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
367 } catch (Parser::Error &e) {
368 cerr << "parsing exception in file " << e.File() << " on line " << e.Line() << ": " << e.what() << endl;
370 } catch (exception &e) {
371 cerr << "exception in main(): " << e.what() << endl;