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