]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
4f50463ba258fa6102f0ab6958e011812fddb3ca
[l2e.git] / src / main.cpp
1 /*
2  * main.cpp
3  *
4  *  Created on: Aug 1, 2012
5  *      Author: holy
6  */
7
8 #include "app/Application.h"
9 #include "app/Input.h"
10 #include "battle/BattleState.h"
11 #include "battle/Hero.h"
12 #include "battle/Monster.h"
13 #include "battle/PartyLayout.h"
14 #include "battle/Resources.h"
15 #include "common/Ikari.h"
16 #include "common/Inventory.h"
17 #include "common/Item.h"
18 #include "common/Spell.h"
19 #include "geometry/Point.h"
20 #include "graphics/Font.h"
21 #include "graphics/Frame.h"
22 #include "graphics/Gauge.h"
23 #include "graphics/Menu.h"
24 #include "graphics/Sprite.h"
25 #include "sdl/InitImage.h"
26 #include "sdl/InitScreen.h"
27 #include "sdl/InitSDL.h"
28
29 #include <exception>
30 #include <iostream>
31 #include <SDL.h>
32 #include <SDL_image.h>
33
34 using app::Application;
35 using app::Input;
36 using battle::BattleState;
37 using battle::Hero;
38 using battle::Monster;
39 using battle::PartyLayout;
40 using common::Ikari;
41 using common::Inventory;
42 using common::Item;
43 using common::Spell;
44 using geometry::Point;
45 using graphics::Font;
46 using graphics::Frame;
47 using graphics::Gauge;
48 using graphics::Menu;
49 using graphics::Sprite;
50 using sdl::InitImage;
51 using sdl::InitScreen;
52 using sdl::InitSDL;
53
54 using std::cerr;
55 using std::cout;
56 using std::endl;
57 using std::exception;
58
59 int main(int argc, char **argv) {
60         const int width = 800;
61         const int height = 480;
62
63         try {
64                 InitSDL sdl;
65                 InitImage image(IMG_INIT_PNG);
66                 InitScreen screen(width, height);
67
68                 // temporary test data
69                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
70                 PartyLayout monstersLayout;
71                 monstersLayout.AddPosition(Point<Uint8>(88, 104));
72                 monstersLayout.AddPosition(Point<Uint8>(128, 104));
73                 monstersLayout.AddPosition(Point<Uint8>(168, 104));
74                 monstersLayout.AddPosition(Point<Uint8>(208, 104));
75                 PartyLayout heroesLayout;
76                 heroesLayout.AddPosition(Point<Uint8>(27, 219));
77                 heroesLayout.AddPosition(Point<Uint8>(104, 227));
78                 heroesLayout.AddPosition(Point<Uint8>(66, 238));
79                 heroesLayout.AddPosition(Point<Uint8>(143, 246));
80
81                 SDL_Surface *monsterImg(IMG_Load("test-data/monster.png"));
82                 Sprite dummySprite(monsterImg, 64, 64);
83                 Monster monster;
84                 monster.SetSprite(&dummySprite);
85                 monster.SetMaxHealth(10);
86                 monster.SetHealth(10);
87
88                 SDL_Surface *maximImg(IMG_Load("test-data/maxim.png"));
89                 Sprite maximSprite(maximImg, 64, 64);
90                 Hero maxim;
91                 maxim.SetName("Maxim");
92                 maxim.SetLevel(1);
93                 maxim.SetSprite(&maximSprite);
94                 maxim.SetMaxHealth(33);
95                 maxim.SetHealth(33);
96                 maxim.SetMaxMana(20);
97                 maxim.SetMana(20);
98                 maxim.SetIP(100);
99
100                 SDL_Surface *selanImg(IMG_Load("test-data/selan.png"));
101                 Sprite selanSprite(selanImg, 64, 64);
102                 Hero selan;
103                 selan.SetName("Selan");
104                 selan.SetLevel(1);
105                 selan.SetSprite(&selanSprite);
106                 selan.SetMaxHealth(28);
107                 selan.SetHealth(28);
108                 selan.SetMaxMana(23);
109                 selan.SetMana(23);
110                 selan.SetIP(80);
111
112                 SDL_Surface *guyImg(IMG_Load("test-data/guy.png"));
113                 Sprite guySprite(guyImg, 64, 64);
114                 Hero guy;
115                 guy.SetName("Guy");
116                 guy.SetLevel(1);
117                 guy.SetSprite(&guySprite);
118                 guy.SetMaxHealth(38);
119                 guy.SetHealth(38);
120                 guy.SetMaxMana(0);
121                 guy.SetMana(0);
122                 guy.SetIP(85);
123
124                 SDL_Surface *dekarImg(IMG_Load("test-data/dekar.png"));
125                 Sprite dekarSprite(dekarImg, 64, 64);
126                 Hero dekar;
127                 dekar.SetName("Dekar");
128                 dekar.SetLevel(1);
129                 dekar.SetSprite(&dekarSprite);
130                 dekar.SetMaxHealth(38);
131                 dekar.SetHealth(38);
132                 dekar.SetMaxMana(0);
133                 dekar.SetMana(0);
134                 dekar.SetIP(50);
135
136                 battle::Resources battleRes;
137
138                 SDL_Surface *attackIconsImg(IMG_Load("test-data/attack-type-icons.png"));
139                 Sprite attackIconsSprite(attackIconsImg, 32, 32);
140                 battleRes.attackIcons = &attackIconsSprite;
141                 SDL_Surface *attackChoiceIconsImg(IMG_Load("test-data/attack-choice-icons.png"));
142                 Sprite attackChoiceIconsSprite(attackChoiceIconsImg, 16, 16);
143                 battleRes.attackChoiceIcons = &attackChoiceIconsSprite;
144                 SDL_Surface *moveIconsImg(IMG_Load("test-data/move-icons.png"));
145                 Sprite moveIconsSprite(moveIconsImg, 32, 32);
146                 battleRes.moveIcons = &moveIconsSprite;
147                 SDL_Surface *heroTagImg(IMG_Load("test-data/hero-tag-sprites.png"));
148                 Sprite heroTagSprite(heroTagImg, 32, 16);
149                 battleRes.heroTagLabels = &heroTagSprite;
150                 SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png"));
151                 Sprite numbersSprite(numbersImg, 16, 16);
152                 Font heroTagFont(&numbersSprite);
153                 battleRes.heroTagFont = &heroTagFont;
154                 SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png"));
155                 Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33);
156                 battleRes.heroTagFrame = &heroTagFrame;
157                 Frame activeHeroTagFrame(tagFramesImg, 16, 16);
158                 battleRes.activeHeroTagFrame = &activeHeroTagFrame;
159
160                 SDL_Surface *gauges(IMG_Load("test-data/gauges.png"));
161                 Gauge healthGauge(gauges, 0, 16, 0, 0, 16, 6, 1, 6);
162                 battleRes.healthGauge = &healthGauge;
163                 Gauge manaGauge(gauges, 0, 32, 0, 0, 16, 6, 1, 6);
164                 battleRes.manaGauge = &manaGauge;
165                 Gauge ikariGauge(gauges, 0, 48, 0, 0, 16, 6, 1, 6);
166                 battleRes.ikariGauge = &ikariGauge;
167
168                 SDL_Surface *selectFrameImg(IMG_Load("test-data/select-frame.png"));
169                 Frame selectFrame(selectFrameImg, 16, 16);
170                 battleRes.selectFrame = &selectFrame;
171
172                 SDL_Surface *normalFontImg(IMG_Load("test-data/normal-font.png"));
173                 Sprite normalFontSprite(normalFontImg, 16, 16);
174                 Font normalFont(&normalFontSprite);
175                 normalFont.MapRange('A', 'M', 0, 1);
176                 normalFont.MapRange('N', 'Z', 0, 2);
177                 normalFont.MapRange('a', 'm', 0, 3);
178                 normalFont.MapRange('n', 'z', 0, 4);
179                 normalFont.MapChar(':', 10, 0);
180                 normalFont.MapChar('!', 11, 0);
181                 normalFont.MapChar('?', 12, 0);
182                 // TODO: add '.' and '-' characters
183                 battleRes.normalFont = &normalFont;
184
185                 SDL_Surface *disabledFontImg(IMG_Load("test-data/disabled-font.png"));
186                 Sprite disabledFontSprite(disabledFontImg, 16, 16);
187                 Font disabledFont(&disabledFontSprite);
188                 disabledFont.MapRange('A', 'M', 0, 1);
189                 disabledFont.MapRange('N', 'Z', 0, 2);
190                 disabledFont.MapRange('a', 'm', 0, 3);
191                 disabledFont.MapRange('n', 'z', 0, 4);
192                 disabledFont.MapChar(':', 10, 0);
193                 disabledFont.MapChar('!', 11, 0);
194                 disabledFont.MapChar('?', 12, 0);
195                 // TODO: add '.' and '-' characters
196                 battleRes.disabledFont = &disabledFont;
197
198                 SDL_Surface *handCursorImg(IMG_Load("test-data/cursor-hand.png"));
199                 Sprite handCursorSprite(handCursorImg, 32, 32);
200                 battleRes.menuCursor = &handCursorSprite;
201
202                 SDL_Surface *targetingIconsImg(IMG_Load("test-data/targeting-icons.png"));
203                 Sprite weaponTargetCursor(targetingIconsImg, 32, 32);
204                 Sprite magicTargetCursor(targetingIconsImg, 32, 32, 0, 32);
205                 Sprite itemTargetCursor(targetingIconsImg, 32, 32, 0, 64);
206                 battleRes.weaponTargetCursor = &weaponTargetCursor;
207                 // TODO: add image for magic targeting cursor
208                 battleRes.magicTargetCursor = &magicTargetCursor;
209                 // TODO: add image for item targeting cursor
210                 battleRes.itemTargetCursor = &itemTargetCursor;
211
212                 Spell resetSpell;
213                 resetSpell.SetName("Reset");
214                 maxim.AddSpell(&resetSpell);
215                 Spell strongSpell;
216                 strongSpell.SetName("Strong");
217                 strongSpell.SetCost(3);
218                 strongSpell.SetUsableInBattle();
219                 strongSpell.GetTargetingMode().TargetMultipleAllies();
220                 maxim.AddSpell(&strongSpell);
221                 selan.AddSpell(&strongSpell);
222                 Spell strongerSpell;
223                 strongerSpell.SetName("Stronger");
224                 strongerSpell.SetCost(8);
225                 strongerSpell.SetUsableInBattle();
226                 strongerSpell.GetTargetingMode().TargetMultipleAllies();
227                 maxim.AddSpell(&strongerSpell);
228                 selan.AddSpell(&strongerSpell);
229                 Spell championSpell;
230                 championSpell.SetName("Champion");
231                 championSpell.SetCost(16);
232                 championSpell.SetUsableInBattle();
233                 championSpell.GetTargetingMode().TargetMultipleAllies();
234                 maxim.AddSpell(&championSpell);
235                 selan.AddSpell(&championSpell);
236                 Spell rallySpell;
237                 rallySpell.SetName("Rally");
238                 rallySpell.SetCost(10);
239                 rallySpell.SetUsableInBattle();
240                 rallySpell.GetTargetingMode().TargetMultipleAllies();
241                 maxim.AddSpell(&rallySpell);
242                 selan.AddSpell(&rallySpell);
243                 Spell escapeSpell;
244                 escapeSpell.SetName("Escape");
245                 escapeSpell.SetCost(8);
246                 selan.AddSpell(&escapeSpell);
247                 Spell valorSpell;
248                 valorSpell.SetName("Valor");
249                 valorSpell.SetCost(30);
250                 valorSpell.SetUsableInBattle();
251                 valorSpell.GetTargetingMode().TargetMultipleAllies();
252                 maxim.AddSpell(&valorSpell);
253                 selan.AddSpell(&valorSpell);
254
255                 battleRes.spellMenuHeadline = "Please choose a spell.";
256                 battleRes.spellMenuPrototype = Menu<const Spell *>(&normalFont, &disabledFont, &handCursorSprite, 9, 6, 8, 0, 2, 32, 2, ':');
257
258                 SDL_Surface *itemIcons(IMG_Load("test-data/item-icons.png"));
259                 Sprite potionIcon(itemIcons, 16, 16);
260                 Sprite ballIcon(itemIcons, 16, 16, 0, 16);
261                 Sprite crankIcon(itemIcons, 16, 16, 0, 32);
262                 Sprite spearIcon(itemIcons, 16, 16, 0, 48);
263                 Sprite swordIcon(itemIcons, 16, 16, 0, 64);
264                 Sprite axIcon(itemIcons, 16, 16, 0, 80);
265                 Sprite rodIcon(itemIcons, 16, 16, 0, 96);
266                 Sprite armorIcon(itemIcons, 16, 16, 0, 112);
267                 Sprite shieldIcon(itemIcons, 16, 16, 0, 128);
268                 Sprite helmetIcon(itemIcons, 16, 16, 0, 144);
269                 Sprite ringIcon(itemIcons, 16, 16, 0, 160);
270                 Sprite jewelIcon(itemIcons, 16, 16, 0, 176);
271
272                 Inventory inventory;
273                 Item antidote;
274                 antidote.SetName("Antidote");
275                 antidote.SetMenuIcon(&potionIcon);
276                 antidote.SetUsableInBattle();
277                 antidote.GetTargetingMode().TargetSingleAlly();
278                 inventory.Add(&antidote, 9);
279                 Item magicJar;
280                 magicJar.SetName("Magic jar");
281                 magicJar.SetMenuIcon(&potionIcon);
282                 magicJar.SetUsableInBattle();
283                 magicJar.GetTargetingMode().TargetSingleAlly();
284                 inventory.Add(&magicJar, 4);
285                 Item hiPotion;
286                 hiPotion.SetName("Hi-Potion");
287                 hiPotion.SetMenuIcon(&potionIcon);
288                 hiPotion.SetUsableInBattle();
289                 hiPotion.GetTargetingMode().TargetSingleAlly();
290                 inventory.Add(&hiPotion, 4);
291                 Item powerPotion;
292                 powerPotion.SetName("Power potion");
293                 powerPotion.SetMenuIcon(&potionIcon);
294                 inventory.Add(&powerPotion, 4);
295                 Item escape;
296                 escape.SetName("Escape");
297                 inventory.Add(&escape, 2);
298                 Item sleepBall;
299                 sleepBall.SetName("Sleep ball");
300                 sleepBall.SetMenuIcon(&ballIcon);
301                 sleepBall.SetUsableInBattle();
302                 sleepBall.GetTargetingMode().TargetSingleEnemy();
303                 inventory.Add(&sleepBall, 1);
304                 Item multiBall;
305                 multiBall.SetName("Multi-ball!");
306                 multiBall.SetMenuIcon(&ballIcon);
307                 multiBall.SetUsableInBattle();
308                 multiBall.GetTargetingMode().TargetMultipleEnemies();
309                 inventory.Add(&multiBall, 1);
310                 Item figgoru;
311                 figgoru.SetName("Figgoru");
312                 figgoru.SetMenuIcon(&crankIcon);
313                 figgoru.GetTargetingMode().TargetAllEnemies();
314                 inventory.Add(&figgoru, 1);
315                 battleRes.inventory = &inventory;
316
317                 battleRes.itemMenuHeadline = "Please choose an item.";
318                 battleRes.itemMenuPrototype = Menu<const common::Item *>(&normalFont, &disabledFont, &handCursorSprite, 15, 6, 8, 16, 1, 32, 2, ':');
319
320                 Item zircoSword;
321                 zircoSword.SetName("Zirco sword");
322                 zircoSword.SetMenuIcon(&swordIcon);
323                 Ikari firestorm;
324                 firestorm.SetName("Firestorm");
325                 firestorm.SetCost(88);
326                 firestorm.GetTargetingMode().TargetAllEnemies();
327                 zircoSword.SetIkari(&firestorm);
328                 maxim.SetWeapon(&zircoSword);
329                 Item zirconArmor;
330                 zirconArmor.SetName("Zircon armor");
331                 zirconArmor.SetMenuIcon(&armorIcon);
332                 Ikari magicCure;
333                 magicCure.SetName("Magic cure");
334                 magicCure.SetCost(51);
335                 magicCure.GetTargetingMode().TargetSingleAlly();
336                 zirconArmor.SetIkari(&magicCure);
337                 maxim.SetArmor(&zirconArmor);
338                 Item holyShield;
339                 holyShield.SetName("Holy shield");
340                 holyShield.SetMenuIcon(&shieldIcon);
341                 Ikari lightGuard;
342                 lightGuard.SetName("Light guard");
343                 lightGuard.SetCost(51);
344                 lightGuard.GetTargetingMode().TargetAllAllies(); // FIXME: actually only targets self
345                 holyShield.SetIkari(&lightGuard);
346                 maxim.SetShield(&holyShield);
347                 Item legendHelm;
348                 legendHelm.SetName("Legend helm");
349                 legendHelm.SetMenuIcon(&helmetIcon);
350                 Ikari boomerang;
351                 boomerang.SetName("Boomerang");
352                 boomerang.SetCost(65);
353                 boomerang.GetTargetingMode().TargetAllAllies(); // FIXME: actually only targets self
354                 legendHelm.SetIkari(&boomerang);
355                 maxim.SetHelmet(&legendHelm);
356                 Item sProRing;
357                 sProRing.SetName("S-pro ring");
358                 sProRing.SetMenuIcon(&ringIcon);
359                 Ikari courage;
360                 courage.SetName("Courage");
361                 courage.SetCost(26);
362                 courage.GetTargetingMode().TargetMultipleAllies();
363                 sProRing.SetIkari(&courage);
364                 maxim.SetRing(&sProRing);
365                 Item evilJewel;
366                 evilJewel.SetName("Evil jewel");
367                 evilJewel.SetMenuIcon(&jewelIcon);
368                 Ikari gloomy;
369                 gloomy.SetName("Gloomy");
370                 gloomy.SetCost(65);
371                 gloomy.GetTargetingMode().TargetAllEnemies();
372                 evilJewel.SetIkari(&gloomy);
373                 maxim.SetJewel(&evilJewel);
374
375                 Item zircoWhip;
376                 zircoWhip.SetName("Zirco whip");
377                 zircoWhip.SetMenuIcon(&rodIcon);
378                 selan.SetWeapon(&zircoWhip);
379                 Item zirconPlate;
380                 zirconPlate.SetName("Zircon plate");
381                 zirconPlate.SetMenuIcon(&armorIcon);
382                 selan.SetArmor(&zirconPlate);
383                 Item zircoGloves;
384                 zircoGloves.SetName("Zirco gloves");
385                 zircoGloves.SetMenuIcon(&shieldIcon);
386                 selan.SetShield(&zircoGloves);
387                 Item holyCap;
388                 holyCap.SetName("Holy cap");
389                 holyCap.SetMenuIcon(&helmetIcon);
390                 selan.SetHelmet(&holyCap);
391                 Item ghostRing;
392                 ghostRing.SetName("Ghost ring");
393                 ghostRing.SetMenuIcon(&ringIcon);
394                 selan.SetRing(&ghostRing);
395                 Item eagleRock;
396                 eagleRock.SetName("Eagle rock");
397                 eagleRock.SetMenuIcon(&jewelIcon);
398                 selan.SetJewel(&eagleRock);
399
400                 Item zircoAx;
401                 zircoAx.SetName("Zirco ax");
402                 zircoAx.SetMenuIcon(&axIcon);
403                 guy.SetWeapon(&zircoAx);
404                 guy.SetArmor(&zirconArmor);
405                 Item megaShield;
406                 megaShield.SetName("Mega shield");
407                 megaShield.SetMenuIcon(&shieldIcon);
408                 guy.SetShield(&megaShield);
409                 Item zircoHelmet;
410                 zircoHelmet.SetName("Zirco helmet");
411                 zircoHelmet.SetMenuIcon(&helmetIcon);
412                 guy.SetHelmet(&zircoHelmet);
413                 Item powerRing;
414                 powerRing.SetName("Power ring");
415                 powerRing.SetMenuIcon(&ringIcon);
416                 guy.SetRing(&powerRing);
417                 guy.SetJewel(&evilJewel);
418
419                 // NOTE: this is actually Artea equipment
420                 Item lizardBlow;
421                 lizardBlow.SetName("Lizard blow");
422                 lizardBlow.SetMenuIcon(&swordIcon);
423                 dekar.SetWeapon(&lizardBlow);
424                 Item holyRobe;
425                 holyRobe.SetName("Holy robe");
426                 holyRobe.SetMenuIcon(&armorIcon);
427                 dekar.SetArmor(&holyRobe);
428                 dekar.SetShield(&zircoGloves);
429                 dekar.SetHelmet(&holyCap);
430                 Item rocketRing;
431                 rocketRing.SetName("Rocket ring");
432                 rocketRing.SetMenuIcon(&ringIcon);
433                 dekar.SetRing(&rocketRing);
434                 Item krakenRock;
435                 krakenRock.SetName("Kraken rock");
436                 krakenRock.SetMenuIcon(&jewelIcon);
437                 dekar.SetJewel(&krakenRock);
438
439                 battleRes.ikariMenuHeadline = "Please choose equipment.";
440                 battleRes.ikariMenuPrototype = Menu<const Item *>(&normalFont, &disabledFont, &handCursorSprite, 26, 6, 8, 16, 1, 32);
441                 battleRes.ikariMenuPrototype.Add("Zirco whip   Thundershriek", 0, false, &swordIcon);
442                 battleRes.ikariMenuPrototype.Add("Zircon plate Sudden cure", 0, true, &armorIcon);
443                 battleRes.ikariMenuPrototype.Add("Zirco gloves Forcefield", 0, true, &shieldIcon);
444                 battleRes.ikariMenuPrototype.Add("Holy cap     Vulnerable", 0, false, &helmetIcon);
445                 battleRes.ikariMenuPrototype.Add("Ghost ring   Destroy", 0, true, &ringIcon);
446                 battleRes.ikariMenuPrototype.Add("Eagle rock   Dive", 0, true, &jewelIcon);
447
448                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &battleRes));
449                 battleState->AddMonster(monster);
450                 battleState->AddMonster(monster);
451                 battleState->AddMonster(monster);
452                 battleState->AddMonster(monster);
453                 battleState->AddHero(maxim);
454                 battleState->AddHero(selan);
455                 battleState->AddHero(guy);
456                 battleState->AddHero(dekar);
457                 Application app(&screen, battleState);
458                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
459                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
460                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
461                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
462                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
463                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
464                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
465                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
466                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
467                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
468                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
469                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
470                 app.Run();
471
472                 return 0;
473         } catch (exception &e) {
474                 cerr << "exception in main(): " << e.what() << endl;
475                 return 1;
476         }
477 }