]> git.localhorst.tv Git - l2e.git/blob - src/main.cpp
5e14001b8454e922348c7260ffd7bbdec9b1e95b
[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 "geometry/Point.h"
18 #include "graphics/Font.h"
19 #include "graphics/Frame.h"
20 #include "graphics/Gauge.h"
21 #include "graphics/Menu.h"
22 #include "graphics/Sprite.h"
23 #include "sdl/InitImage.h"
24 #include "sdl/InitScreen.h"
25 #include "sdl/InitSDL.h"
26
27 #include <exception>
28 #include <iostream>
29 #include <SDL.h>
30 #include <SDL_image.h>
31
32 using app::Application;
33 using app::Input;
34 using battle::BattleState;
35 using battle::Hero;
36 using battle::Monster;
37 using battle::PartyLayout;
38 using common::Inventory;
39 using common::Item;
40 using geometry::Point;
41 using graphics::Font;
42 using graphics::Frame;
43 using graphics::Gauge;
44 using graphics::Menu;
45 using graphics::Sprite;
46 using sdl::InitImage;
47 using sdl::InitScreen;
48 using sdl::InitSDL;
49
50 using std::cerr;
51 using std::cout;
52 using std::endl;
53 using std::exception;
54
55 int main(int argc, char **argv) {
56         const int width = 800;
57         const int height = 480;
58
59         try {
60                 InitSDL sdl;
61                 InitImage image(IMG_INIT_PNG);
62                 InitScreen screen(width, height);
63
64                 // temporary test data
65                 SDL_Surface *bg(IMG_Load("test-data/battle-bg.png"));
66                 PartyLayout monstersLayout;
67                 monstersLayout.AddPosition(Point<Uint8>(88, 104));
68                 monstersLayout.AddPosition(Point<Uint8>(128, 104));
69                 monstersLayout.AddPosition(Point<Uint8>(168, 104));
70                 monstersLayout.AddPosition(Point<Uint8>(208, 104));
71                 PartyLayout heroesLayout;
72                 heroesLayout.AddPosition(Point<Uint8>(27, 219));
73                 heroesLayout.AddPosition(Point<Uint8>(104, 227));
74                 heroesLayout.AddPosition(Point<Uint8>(66, 238));
75                 heroesLayout.AddPosition(Point<Uint8>(143, 246));
76
77                 SDL_Surface *monsterImg(IMG_Load("test-data/monster.png"));
78                 Sprite dummySprite(monsterImg, 64, 64);
79                 Monster monster;
80                 monster.SetSprite(&dummySprite);
81                 monster.SetMaxHealth(10);
82                 monster.SetHealth(10);
83
84                 SDL_Surface *maximImg(IMG_Load("test-data/maxim.png"));
85                 Sprite maximSprite(maximImg, 64, 64);
86                 Hero maxim;
87                 maxim.SetName("Maxim");
88                 maxim.SetLevel(1);
89                 maxim.SetSprite(&maximSprite);
90                 maxim.SetMaxHealth(33);
91                 maxim.SetHealth(33);
92                 maxim.SetMaxMana(20);
93                 maxim.SetMana(20);
94                 maxim.SetIP(0);
95
96                 SDL_Surface *selanImg(IMG_Load("test-data/selan.png"));
97                 Sprite selanSprite(selanImg, 64, 64);
98                 Hero selan;
99                 selan.SetName("Selan");
100                 selan.SetLevel(1);
101                 selan.SetSprite(&selanSprite);
102                 selan.SetMaxHealth(28);
103                 selan.SetHealth(28);
104                 selan.SetMaxMana(23);
105                 selan.SetMana(23);
106                 selan.SetIP(0);
107
108                 SDL_Surface *guyImg(IMG_Load("test-data/guy.png"));
109                 Sprite guySprite(guyImg, 64, 64);
110                 Hero guy;
111                 guy.SetName("Guy");
112                 guy.SetLevel(1);
113                 guy.SetSprite(&guySprite);
114                 guy.SetMaxHealth(38);
115                 guy.SetHealth(38);
116                 guy.SetMaxMana(0);
117                 guy.SetMana(0);
118                 guy.SetIP(0);
119
120                 SDL_Surface *dekarImg(IMG_Load("test-data/dekar.png"));
121                 Sprite dekarSprite(dekarImg, 64, 64);
122                 Hero dekar;
123                 dekar.SetName("Dekar");
124                 dekar.SetLevel(1);
125                 dekar.SetSprite(&dekarSprite);
126                 dekar.SetMaxHealth(38);
127                 dekar.SetHealth(38);
128                 dekar.SetMaxMana(0);
129                 dekar.SetMana(0);
130                 dekar.SetIP(0);
131
132                 battle::Resources battleRes;
133
134                 SDL_Surface *attackIconsImg(IMG_Load("test-data/attack-type-icons.png"));
135                 Sprite attackIconsSprite(attackIconsImg, 32, 32);
136                 battleRes.attackIcons = &attackIconsSprite;
137                 SDL_Surface *attackChoiceIconsImg(IMG_Load("test-data/attack-choice-icons.png"));
138                 Sprite attackChoiceIconsSprite(attackChoiceIconsImg, 16, 16);
139                 battleRes.attackChoiceIcons = &attackChoiceIconsSprite;
140                 SDL_Surface *moveIconsImg(IMG_Load("test-data/move-icons.png"));
141                 Sprite moveIconsSprite(moveIconsImg, 32, 32);
142                 battleRes.moveIcons = &moveIconsSprite;
143                 SDL_Surface *heroTagImg(IMG_Load("test-data/hero-tag-sprites.png"));
144                 Sprite heroTagSprite(heroTagImg, 32, 16);
145                 battleRes.heroTagLabels = &heroTagSprite;
146                 SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png"));
147                 Sprite numbersSprite(numbersImg, 16, 16);
148                 Font heroTagFont(&numbersSprite);
149                 battleRes.heroTagFont = &heroTagFont;
150                 SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png"));
151                 Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33);
152                 battleRes.heroTagFrame = &heroTagFrame;
153                 Frame activeHeroTagFrame(tagFramesImg, 16, 16);
154                 battleRes.activeHeroTagFrame = &activeHeroTagFrame;
155
156                 SDL_Surface *gauges(IMG_Load("test-data/gauges.png"));
157                 Gauge healthGauge(gauges, 0, 16, 0, 0, 16, 6, 1, 6);
158                 battleRes.healthGauge = &healthGauge;
159                 Gauge manaGauge(gauges, 0, 32, 0, 0, 16, 6, 1, 6);
160                 battleRes.manaGauge = &manaGauge;
161                 Gauge ikariGauge(gauges, 0, 48, 0, 0, 16, 6, 1, 6);
162                 battleRes.ikariGauge = &ikariGauge;
163
164                 SDL_Surface *selectFrameImg(IMG_Load("test-data/select-frame.png"));
165                 Frame selectFrame(selectFrameImg, 16, 16);
166                 battleRes.selectFrame = &selectFrame;
167
168                 SDL_Surface *normalFontImg(IMG_Load("test-data/normal-font.png"));
169                 Sprite normalFontSprite(normalFontImg, 16, 16);
170                 Font normalFont(&normalFontSprite);
171                 normalFont.MapRange('A', 'M', 0, 1);
172                 normalFont.MapRange('N', 'Z', 0, 2);
173                 normalFont.MapRange('a', 'm', 0, 3);
174                 normalFont.MapRange('n', 'z', 0, 4);
175                 normalFont.MapChar(':', 10, 0);
176                 normalFont.MapChar('!', 10, 0);
177                 normalFont.MapChar('?', 10, 0);
178                 // TODO: add '.' and '-' characters
179                 battleRes.normalFont = &normalFont;
180
181                 SDL_Surface *disabledFontImg(IMG_Load("test-data/disabled-font.png"));
182                 Sprite disabledFontSprite(disabledFontImg, 16, 16);
183                 Font disabledFont(&disabledFontSprite);
184                 disabledFont.MapRange('A', 'M', 0, 1);
185                 disabledFont.MapRange('N', 'Z', 0, 2);
186                 disabledFont.MapRange('a', 'm', 0, 3);
187                 disabledFont.MapRange('n', 'z', 0, 4);
188                 disabledFont.MapChar(':', 10, 0);
189                 disabledFont.MapChar('!', 10, 0);
190                 disabledFont.MapChar('?', 10, 0);
191                 // TODO: add '.' and '-' characters
192                 battleRes.disabledFont = &disabledFont;
193
194                 SDL_Surface *handCursorImg(IMG_Load("test-data/cursor-hand.png"));
195                 Sprite handCursorSprite(handCursorImg, 32, 32);
196                 battleRes.menuCursor = &handCursorSprite;
197
198                 SDL_Surface *targetingIconsImg(IMG_Load("test-data/targeting-icons.png"));
199                 Sprite weaponTargetCursor(targetingIconsImg, 32, 32);
200                 battleRes.weaponTargetCursor = &weaponTargetCursor;
201                 // TODO: add image for magic targeting cursor
202                 battleRes.magicTargetCursor = &weaponTargetCursor;
203                 // TODO: add image for item targeting cursor
204                 battleRes.itemTargetCursor = &weaponTargetCursor;
205
206                 battleRes.spellMenuHeadline = "Please choose a spell.";
207                 battleRes.spellMenuPrototype = Menu</* Spell */ void *>(&normalFont, &disabledFont, &handCursorSprite, 12, 6, 8, 0, 2, 32);
208                 battleRes.spellMenuPrototype.Add("Reset    : 0", 0, false);
209                 battleRes.spellMenuPrototype.Add("Strong   : 3", 0);
210                 battleRes.spellMenuPrototype.Add("Stronger : 8", 0);
211                 battleRes.spellMenuPrototype.Add("Champion :16", 0);
212                 battleRes.spellMenuPrototype.Add("Rally    :10", 0);
213                 battleRes.spellMenuPrototype.Add("Escape   : 8", 0, false);
214                 battleRes.spellMenuPrototype.Add("Valor    :30", 0);
215                 battleRes.spellMenuPrototype.Add("Poison   : 2", 0);
216                 battleRes.spellMenuPrototype.Add("Warp     : 8", 0, false);
217                 battleRes.spellMenuPrototype.Add("Release  : 2", 0);
218                 battleRes.spellMenuPrototype.Add("Waken    : 4", 0);
219                 battleRes.spellMenuPrototype.Add("Light    : 0", 0, false);
220                 battleRes.spellMenuPrototype.Add("Fake     : 4", 0);
221                 battleRes.spellMenuPrototype.Add("Trick    : 5", 0);
222                 battleRes.spellMenuPrototype.Add("Flash    : 5", 0);
223                 battleRes.spellMenuPrototype.Add("Fireball : 6", 0);
224                 battleRes.spellMenuPrototype.Add("Vortex   : 7", 0);
225                 battleRes.spellMenuPrototype.Add("Blizzard : 8", 0);
226                 battleRes.spellMenuPrototype.Add("Spark    : 3", 0);
227
228                 SDL_Surface *itemIcons(IMG_Load("test-data/item-icons.png"));
229                 Sprite potionIcon(itemIcons, 16, 16);
230                 Sprite ballIcon(itemIcons, 16, 16, 0, 16);
231                 Sprite crankIcon(itemIcons, 16, 16, 0, 32);
232                 Sprite spearIcon(itemIcons, 16, 16, 0, 48);
233                 Sprite swordIcon(itemIcons, 16, 16, 0, 64);
234                 Sprite axIcon(itemIcons, 16, 16, 0, 80);
235                 Sprite rodIcon(itemIcons, 16, 16, 0, 96);
236                 Sprite armorIcon(itemIcons, 16, 16, 0, 112);
237                 Sprite shieldIcon(itemIcons, 16, 16, 0, 128);
238                 Sprite helmetIcon(itemIcons, 16, 16, 0, 144);
239                 Sprite ringIcon(itemIcons, 16, 16, 0, 160);
240                 Sprite stoneIcon(itemIcons, 16, 16, 0, 176);
241
242                 Inventory inventory;
243                 Item antidote;
244                 antidote.SetName("Antidote");
245                 antidote.SetMenuIcon(&potionIcon);
246                 antidote.SetUsableInBattle();
247                 inventory.Add(&antidote, 9);
248                 Item magicJar;
249                 magicJar.SetName("Magic jar");
250                 magicJar.SetMenuIcon(&potionIcon);
251                 magicJar.SetUsableInBattle();
252                 inventory.Add(&magicJar, 4);
253                 Item hiPotion;
254                 hiPotion.SetName("Hi-Potion");
255                 hiPotion.SetMenuIcon(&potionIcon);
256                 hiPotion.SetUsableInBattle();
257                 inventory.Add(&hiPotion, 4);
258                 Item powerPotion;
259                 powerPotion.SetName("Power potion");
260                 powerPotion.SetMenuIcon(&potionIcon);
261                 inventory.Add(&powerPotion, 4);
262                 Item escape;
263                 escape.SetName("Escape");
264                 inventory.Add(&escape, 2);
265                 Item sleepBall;
266                 sleepBall.SetName("Sleep ball");
267                 sleepBall.SetMenuIcon(&ballIcon);
268                 sleepBall.SetUsableInBattle();
269                 inventory.Add(&sleepBall, 1);
270                 Item figgoru;
271                 figgoru.SetName("Figgoru");
272                 figgoru.SetMenuIcon(&crankIcon);
273                 inventory.Add(&figgoru, 1);
274                 battleRes.inventory = &inventory;
275
276                 battleRes.itemMenuHeadline = "Please choose an item.";
277                 battleRes.itemMenuPrototype = Menu<const common::Item *>(&normalFont, &disabledFont, &handCursorSprite, 15, 6, 8, 16, 1, 32, 2, ':');
278
279                 battleRes.ikariMenuHeadline = "Please choose equipment.";
280                 battleRes.ikariMenuPrototype = Menu</* Item */ void *>(&normalFont, &disabledFont, &handCursorSprite, 26, 6, 8, 16, 1, 32);
281                 battleRes.ikariMenuPrototype.Add("Zirco whip   Thundershriek", 0, false, &swordIcon);
282                 battleRes.ikariMenuPrototype.Add("Zircon plate Sudden cure", 0, true, &armorIcon);
283                 battleRes.ikariMenuPrototype.Add("Zirco gloves Forcefield", 0, true, &shieldIcon);
284                 battleRes.ikariMenuPrototype.Add("Holy cap     Vulnerable", 0, false, &helmetIcon);
285                 battleRes.ikariMenuPrototype.Add("Ghost ring   Destroy", 0, true, &ringIcon);
286                 battleRes.ikariMenuPrototype.Add("Eagle rock   Dive", 0, true, &stoneIcon);
287
288                 BattleState *battleState(new BattleState(bg, monstersLayout, heroesLayout, &battleRes));
289                 battleState->AddMonster(monster);
290                 battleState->AddMonster(monster);
291                 battleState->AddMonster(monster);
292                 battleState->AddMonster(monster);
293                 battleState->AddHero(maxim);
294                 battleState->AddHero(selan);
295                 battleState->AddHero(guy);
296                 battleState->AddHero(dekar);
297                 Application app(&screen, battleState);
298                 app.Buttons().MapKey(SDLK_w, Input::PAD_UP);
299                 app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT);
300                 app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN);
301                 app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT);
302                 app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A);
303                 app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B);
304                 app.Buttons().MapKey(SDLK_UP, Input::ACTION_X);
305                 app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y);
306                 app.Buttons().MapKey(SDLK_RETURN, Input::START);
307                 app.Buttons().MapKey(SDLK_SPACE, Input::SELECT);
308                 app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT);
309                 app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT);
310                 app.Run();
311
312                 return 0;
313         } catch (exception &e) {
314                 cerr << "exception in main(): " << e.what() << endl;
315                 return 1;
316         }
317 }