summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorReimar <Reimar@Leike.name>2015-12-08 11:31:35 +0100
committerReimar <Reimar@Leike.name>2015-12-08 11:31:35 +0100
commite3a66514d57ff4acf2f02df7d86bd2cf8be0e730 (patch)
tree686eabd8aac2f2eb9d0b3429fe0feee3f031904a /main.cpp
downloadRCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.gz
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.bz2
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.xz
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.zip
initial commit
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp817
1 files changed, 817 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..9cf73d4
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,817 @@
1#include "main.h"
2AbstractInteractive* getWeaponInfo(Weapon* w, SDL_Rect target, int textSize)
3{
4 SDL_Rect tRect = target;
5 tRect.w -= 10;
6 tRect.x = 0;
7 tRect.y = 0;
8 string info;
9 info += w->name + "\n";
10 info += "Cost: " + lltostr(w->cost) + "\n";
11 info += "DPS: " + dbltostr(w->getDPS(),1) + "\n";
12 info += "DPE: " + dbltostr(w->getDPE(),1) + "\n";
13 info += "Energyusage: " + dbltostr(w->energyUsage,1);
14 TextBox* tb = new TextBox(textField(info, tRect, textSize));
15 return new Scrollable(target, tb, tRect, 0, 0);
16}
17
18AbstractInteractive* getShipInfo(UserShip* s, SDL_Rect target, int textSize)
19{
20 SDL_Rect tRect = target;
21 tRect.w -= 10;
22 tRect.x = 0;
23 tRect.y = 0;
24 string info;
25 info += s->name + "\n";
26 info += "Cost: " + lltostr(s->cost) + "\n";
27 info += "HP: " + lltostr(s->hp) + "\n";
28 info += "Armor: " + lltostr(s->armor) + "\n";
29 info += "Energy Storage: " + lltostr(s->maxEnergy) + "\n";
30 info += "Energy Production: " + lltostr(s->energyProduction) + "\n";
31 info += "Acceleration: " + lltostr(s->handling) + "\n";
32 info += "Top Speed: " + lltostr(s->moveSpeed) + "\n";
33 info += "Slots: " + lltostr(s->weapons.size()) + "\n";
34 TextBox* tb = new TextBox(textField(info, tRect, textSize));
35 return new Scrollable(target, tb, tRect, 0, 0);
36}
37
38Shop::~Shop()
39{
40 for(int i = 0; i < (int) weapons.size(); ++i)
41 delete weapons[i];
42 for(int i = 0; i < (int) ships.size(); ++i)
43 delete ships[i];
44 delete menu;
45 delete back;
46 delete goldLabel;
47}
48
49Shop::Shop(Account** nuser, int ngameMenu)
50{
51 user = nuser;
52 SDL_Rect labelPos;
53 labelPos.x = 350;
54 labelPos.y = 18;
55 labelPos.h = 16;
56 goldLabel = new Label("gold: " + lltostr((*user)->gold),labelPos);
57 SDL_Rect subScreen;
58 subScreen.w = 400;
59 subScreen.h = 400;
60 subScreen.x = 50;
61 subScreen.y = 70;
62 gameMenu = ngameMenu;
63 currentWeapon = 0;
64 currentShip = 0;
65 SDL_Surface* BG = loadBMP("data/images/bg_stars.bmp");
66 SDL_Surface* mbg = SDL_CreateRGBSurface(0,400,400,32,0,0,0,0);
67 SDL_FillRect(mbg,NULL,0x000102);
68 SDL_SetColorKey(mbg,SDL_SRCCOLORKEY,0x000102);
69 vector<string> ts1;
70 vector<int> ti1;
71 tu = (*user)->ships[(*user)->current]->getUpgradeables();
72 for(int i = 0; i < (int) tu.size(); ++i)
73 {
74 ts1.push_back("Upgrade " + tu[i]->name);
75// if(tu[i]->cost <= user->gold)
76 ti1.push_back(i+(1<<7));
77// else
78// ti1.push_back(0);
79// cout << tu[i]->cost << " " << user->gold << endl;
80 }
81
82 vector<AbstractInteractive*> submenues;
83 Menu* upgradeMenu = new Menu(ts1, ti1, mbg, false, mbg);
84 for(int i = 0; i < (int) upgradeMenu->buttons.size(); ++i)
85 disabledUpgrades.push_back(&upgradeMenu->buttons[i]->deactivated);
86 submenues.push_back(upgradeMenu);
87 int height;
88
89 //reading available weapons
90 ifstream ins;
91 string path = "data/weapons/";
92 ins.open((path+"weapons.txt").c_str());
93 char workaround;
94 ins >> workaround;
95 vector<SDL_Surface*> weaponPics;
96 SDL_Rect targetRect;
97 targetRect.x = 400 -58;
98 targetRect.y = 0;
99 targetRect.w = 58;
100 targetRect.h = 400;
101 SDL_Rect infoRect;
102 infoRect.x = 50;
103 infoRect.y = 75;
104 infoRect.w = 400 - 68;
105 infoRect.h = 390;
106 while(ins.good())
107 {
108 string filepath;
109 getline(ins,filepath);
110 Weapon* w = new Weapon(path+workaround+filepath);
111 if(w->sellable)
112 {
113 weaponInfos.push_back(getWeaponInfo(w, infoRect, 15));
114 weaponPics.push_back(copyImage(w->getImage()));
115 weapons.push_back(w);
116 //cout << w->name << ": " << estimateValue(*w) << endl;
117 }
118 else
119 delete w;
120 ins >> workaround;
121 }
122 ins.close();
123 SDL_Rect subRect;
124 subRect.w = 48;
125 Menu* tempm = new Menu(weaponPics, subRect.w,height, true, false);
126 subRect.h = height;
127 Scrollable* temps = new Scrollable(targetRect, tempm, subRect, false, true);
128 submenues.push_back(temps);
129 path = "data/ships/user/";
130 ins.open((path+"userships.txt").c_str());
131 ins >> workaround;
132 vector<SDL_Surface*> shipPics;
133 Weapon empty = Weapon("data/weapons/empty.txt");
134 while(ins.good())
135 {
136 string filepath;
137 getline(ins,filepath);
138 UserShip* s = new UserShip(path+workaround+filepath);
139 for(int i = 0; i < (int) s->weapons.size(); ++i)
140 {
141 delete s->weapons[i];
142 s->weapons[i] = new Weapon(empty);
143 }
144 if(true)//s->sellable)
145 {
146 shipPics.push_back(copyImage(s->getImage()));
147 shipInfos.push_back(getShipInfo(s, infoRect, 16));
148// cout << s->name << " " << estimateValue(*s) << endl;
149 ships.push_back(s);
150 }
151 else
152 delete s;
153 ins >> workaround;
154 }
155 ins.close();
156 targetRect.x = 400 -58;
157 targetRect.y = 0;
158 targetRect.w = 58;
159 targetRect.h = 400;
160 subRect.w = 48;
161 tempm = new Menu(shipPics, subRect.w,height, true, false);
162 subRect.h = height;
163 temps = new Scrollable(targetRect, tempm, subRect, false, true);
164 submenues.push_back(temps);
165 SDL_Rect backPos;
166 backPos.x = 0;
167 backPos.y = subScreen.h + subScreen.y;
168 backPos.w = 500;
169 backPos.h = 500 - backPos.y;
170 int tsize = 0;
171 back = new Button(backPos, "back", tsize);
172 int NStates = 3;
173 int topmargin2 = 45;
174 backPos.x = 0;
175 backPos.y = topmargin2;
176 backPos.w = 500/NStates;
177 backPos.h = subScreen.y-topmargin2;
178 tsize = 0;
179 vector<Button*> choices;
180 choices.push_back(new Button(backPos, "Upgrades", tsize));
181 backPos.x = 500 / NStates;
182 backPos.y = topmargin2;
183 backPos.w = 500/NStates;
184 backPos.h = subScreen.y-topmargin2;
185 choices.push_back(new Button(backPos, "Weapons", tsize));
186 backPos.x = 2* 500 / NStates;
187 backPos.y = topmargin2;
188 backPos.w = 500/NStates;
189 backPos.h = subScreen.y - topmargin2;
190 choices.push_back(new Button(backPos, "Ships", tsize));
191 for(int i = 0; i < (int) choices.size(); ++i)
192 choices[i]->isDblClckButton = true;
193 menu = new CompositMenu(submenues, BG, subScreen, choices);
194}
195
196int Shop::handleEvents(SDL_Event event){
197 int result = 0;
198 if(back->handleEvents(event) == BUTTON_CLICKED)
199 result = gameMenu;
200 int bought = menu->handleEvents(event);
201 if(menu->getState() == 0 && (bought & 1<<7))
202 {
203 bought -= 1 << 7;
204// cout << "tried to buy something!" << endl;
205 if(tu[bought]->cost <= (*user)->gold)
206 {
207 (*user)->gold -= tu[bought]->cost;
208 double oldvalue = *tu[bought];
209 tu[bought]->upgrade();
210 double newvalue = *tu[bought];
211 cout << "Bought Upgrade from " << oldvalue << " to " << newvalue << endl;
212 refresh();
213 cout << "nextcost: " << tu[bought]->cost << endl;
214 }
215 }
216 if(menu->getState() == 1 && (bought & MENU_CLICK))
217 {
218 bought -= MENU_CLICK;
219 if(weapons[bought]->cost <= (*user)->gold)
220 {
221 (*user)->gold -= weapons[bought]->cost;
222 bool hasEmptySlot = false;
223 int EmptySlot;
224 for(int i = 0; i < (int) (*user)->ships[(*user)->current]->weapons.size(); ++i)
225 if((*user)->ships[(*user)->current]->weapons[i]->name == "empty Slot")
226 {
227 hasEmptySlot = true;
228 EmptySlot = i;
229 }
230 if(hasEmptySlot)
231 (*user)->ships[(*user)->current]->weapons[EmptySlot] = new Weapon(*weapons[bought]);
232 else
233 (*user)->weapons.push_back(new Weapon(*weapons[bought]));
234 cout << "Bought " << weapons[bought]->name << endl;
235 refresh();
236 }
237 }
238 if(menu->getState() == 1 && (bought & MENU_SELECT))
239 {
240 bought -= MENU_SELECT;
241 currentWeapon = bought;
242 }
243 if(menu->getState() == 2 && (bought & MENU_CLICK))
244 {
245 bought -= MENU_CLICK;
246 if(ships[bought]->cost <= (*user)->gold)
247 {
248 (*user)->gold -= ships[bought]->cost;
249 (*user)->current = (*user)->ships.size();
250 (*user)->ships.push_back(new UserShip(*ships[bought]));
251 cout << "Bought " << ships[bought]->name << endl;
252 refresh();
253 }
254 }
255 if(menu->getState() == 2 && (bought & MENU_SELECT))
256 {
257 bought -= MENU_SELECT;
258 currentShip = bought;
259 }
260 return result;
261}
262void Shop::frame(double time)
263{
264 menu->frame(time);
265}
266void Shop::draw(SDL_Surface* screen)
267{
268 menu->draw(screen);
269 SDL_Color textColor;
270 textColor.r = 255;
271 textColor.g = 255;
272 textColor.b = 255;
273 TTF_Font *font = TTF_OpenFont("data/fonts/OpenSans-Semibold.ttf", 30);
274 SDL_Surface* headline = NULL;
275 headline = TTF_RenderText_Solid(font, "Shop",textColor);
276 if(headline == NULL)
277 {
278 cout << "Error rendering headline of shop" << endl;
279 return;
280 }
281 TTF_CloseFont(font);
282 SDL_Rect headpos;
283 headpos.x = screen->w/2 - headline->w/2;
284 headpos.y = 5;
285 SDL_BlitSurface(headline, NULL, screen, &headpos);
286 SDL_FreeSurface(headline);
287 back->draw(screen);
288 goldLabel->draw(screen);
289 if(menu->getState() == 1)
290 weaponInfos[currentWeapon]->draw(screen);
291 if(menu->getState() == 2)
292 shipInfos[currentShip]->draw(screen);
293}
294
295void Shop::refresh()
296{
297 goldLabel->setCaption("gold: " + lltostr((*user)->gold));
298 menu->refresh();
299 tu = (*user)->ships[(*user)->current]->getUpgradeables();
300 for(int i = 0; i < (int) tu.size(); ++i)
301 *disabledUpgrades[i] = (*user)->gold < tu[i]->cost;
302}
303
304
305InventoryMenu::~InventoryMenu()
306{
307 delete BG;
308 delete back;
309 delete shipMenu;
310 delete weaponMenu;
311 for(int i = 0; i < slots.size(); ++i)
312 delete slots[i];
313 SDL_FreeSurface(ship);
314 delete title;
315}
316
317InventoryMenu::InventoryMenu(Account** nuser, int ngameMenu)
318{
319 MX = 0;
320 MY = 0;
321 user = nuser;
322 gameMenu = ngameMenu;
323 BG = new SlidingBackground(loadBMP("data/images/bg_stars.bmp"), 0, 100);
324 SDL_Rect backPos;
325 backPos.x = 0;
326 backPos.y = 460;
327 backPos.w = 500;
328 backPos.h = 500 - backPos.y;
329 int tsize = 0;
330 back = new Button(backPos, "back", tsize);
331 cursor = NULL;
332 sellDropzone.x = 100;
333 sellDropzone.h = 40;
334 sellDropzone.y = 460;
335 sellDropzone.w = 150;
336 sellLabel = new Label("SELL",sellDropzone);
337 SDL_Rect titlePos;
338 titlePos.x = 0;
339 titlePos.h = 50;
340 titlePos.y = 0;
341 titlePos.w = 500;
342 title = new Label("Inventory",titlePos);
343 vector<SDL_Surface*> tships;
344 for(int i = 0; i < (*user)->ships.size(); ++i)
345 {
346 ships.push_back(copyImage((*user)->ships[i]->getImage()));
347 tships.push_back(copyImage((*user)->ships[i]->getImage()));
348 }
349 int height = 0;
350 Menu* temp = new Menu(tships, 48, height, true, false);
351 SDL_Rect shipMenuPos;
352 shipMenuPos.x = 10;
353 shipMenuPos.y = 50;
354 shipMenuPos.h = 410;
355 shipMenuPos.w = 58;
356 SDL_Rect subRect;
357 subRect.h = height;
358 subRect.w = 48;
359 shipMenu = new Scrollable(shipMenuPos, temp, subRect, 0, 1);
360 vector<SDL_Surface*> tweapons;
361 for(int i = 0; i < (*user)->weapons.size(); ++i)
362 {
363 weapons.push_back(copyImage((*user)->weapons[i]->getImage()));
364 tweapons.push_back(copyImage((*user)->weapons[i]->getImage()));
365 }
366 height = 0;
367 temp = new Menu(tweapons, 48, height, false, true);
368 SDL_Rect weaponMenuPos;
369 weaponMenuPos.x = 500-68;
370 weaponMenuPos.y = 50;
371 weaponMenuPos.h = 410;
372 weaponMenuPos.w = 58;
373 subRect.h = height;
374 subRect.w = 48;
375 weaponMenu = new Scrollable(weaponMenuPos, temp, subRect, 0, 1);
376 ship = copyImage((*user)->ships[(*user)->current]->getImage());
377 int factor = 1;
378 while(ship->w < 190 && ship->h < 190)
379 {
380 SDL_Surface* temp = ship;
381 ship = rotozoomSurface(temp, 0, 2, 0);
382 SDL_FreeSurface(temp);
383 factor *= 2;
384 }
385 for(int i = 0; i < (*user)->ships[(*user)->current]->weapons.size(); ++i)
386 {
387 UserShip* s = (*user)->ships[(*user)->current];
388 SDL_Rect slotPos;
389 slotPos.x = 250 - 24 + factor * s->slotPositions[i].first;
390 slotPos.y = 250 - 24 + factor * s->slotPositions[i].second;
391 slotPos.w = 48;
392 slotPos.h = 48;
393 slots.push_back(new Button(slotPos, copyImage(s->weapons[i]->getImage()), 0));
394 }
395}
396
397void InventoryMenu::draw(SDL_Surface *screen)
398{
399 BG->draw(screen);
400 SDL_Rect shipPos;
401 shipPos.x = 250 - ship->w/2;
402 shipPos.y = 250 - ship->h/2;
403 SDL_BlitSurface(ship, NULL, screen, &shipPos);
404 title->draw(screen);
405 back->draw(screen);
406 sellLabel->draw(screen);
407 shipMenu->draw(screen);
408 weaponMenu->draw(screen);
409 for(int i = 0; i < slots.size(); ++i)
410 slots[i]->draw(screen);
411 if(cursor != NULL)
412 {
413 SDL_Rect cursorPos;
414 cursorPos.x = MX - cursor->w/2;
415 cursorPos.y = MY - cursor->h/2;
416 SDL_BlitSurface(ship,NULL,screen,&cursorPos);
417 }
418}
419
420/*SlidingBackground* BG;
421 Label* title;
422 Account** user;
423 int gameMenu;
424 Button* back;
425 Label* sellLabel;
426 SDL_Rect sellDropzone;
427 AbstractInteractive* shipMenu;
428 vector<SDL_Surface*> ships;
429 AbstractInteractive* weaponMenu;
430 vector<SDL_Surface*> weapons;
431 SDL_Rect weaponDropzone;
432 vector<Button*> slots;
433 SDL_Surface* ship;
434 SDL_Surface* cursor;*/
435int InventoryMenu::handleEvents(SDL_Event event)
436{
437 if(event.type == SDL_MOUSEMOTION)
438 {
439 MX = event.motion.x;
440 MY = event.motion.y;
441 }
442 int tempShip = shipMenu->handleEvents(event);
443 if(tempShip & MENU_SELECT)
444 {
445 tempShip -= MENU_SELECT;
446 cout << tempShip << endl;
447 (*user)->current = tempShip;
448 refresh();
449 }
450 int tempweapon = weaponMenu->handleEvents(event);
451 if(tempweapon & MENU_SELECT)
452 {
453 tempweapon -= MENU_SELECT;
454 cout << tempweapon << endl;
455 cursor = copyImage(weapons[tempweapon]);
456 dragged = tempweapon;
457 }
458
459 if(event.type == SDL_MOUSEBUTTONUP)
460 {
461 if(event.button.button == SDL_BUTTON_LEFT)
462 {
463 SDL_FreeSurface(cursor);
464 cursor = NULL;
465 int x = event.button.x;
466 int y = event.button.y;
467 }
468 }
469
470
471
472
473 int backb = back->handleEvents(event);
474 if(backb == BUTTON_CLICKED)
475 return gameMenu;
476 return 0;
477}
478
479void InventoryMenu::frame(double time)
480{
481 BG->frame(time);
482}
483
484void InventoryMenu::refresh()
485{
486 if(cursor != NULL)
487 {
488 SDL_FreeSurface(cursor);
489 cursor = NULL;
490 }
491}
492
493bool init(SDL_Surface*& screen)
494{
495 srand(time(0));
496
497 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
498 {
499 cout << "Error: Could not initialize SDL" << endl;
500 return 0;
501 }
502 screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
503 if(!screen)
504 {
505 cout << "could not initialize screen" << endl;
506 return 0;
507 }
508 if(TTF_Init() == -1)
509 {
510 cout << "could not initialize True Fonts" << endl;
511 return 0;
512 }
513 if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
514 {
515 return 0;
516 }
517
518 SDL_WM_SetCaption("RCade - v0.03", NULL);
519
520
521 return 1;
522}
523
524Menu* makeLoadGameMenu(SDL_Surface *screen)
525{
526 string bfile = "data/images/bg_stars.bmp";
527 SDL_Surface *bg = loadBMP(bfile);
528 vector<string> ts;
529 vector<int> ti;
530 ifstream ins;
531 ins.open("save/saves.txt");
532 char workaround;
533 ins >> workaround;
534 int counter = 0;
535 while(ins.good())
536 {
537 string account;
538 getline(ins, account);
539 account = workaround + account;
540 _tospace(account);
541 ts.push_back(account);
542 ti.push_back(counter | LOAD);
543 counter++;
544 ins >> workaround;
545 }
546 ins.close();
547 ts.push_back("back");
548 ti.push_back(MENU | 0);
549 return new Menu(ts,ti,bg,true,screen);
550}
551
552Menu* makeMainMenu(SDL_Surface *screen)
553{
554 string bfile = "data/images/bg_stars.bmp";
555 SDL_Surface *bg = loadBMP(bfile);
556 vector<string> ts;
557 ts.push_back("New Game");
558 ts.push_back("Load Game");
559 ts.push_back("Settings");
560 ts.push_back("Quit");
561 vector<int> ti;
562 ti.push_back(MENU | 5);
563 ti.push_back(MENU | 4);
564 ti.push_back(0);
565 ti.push_back(GAME_QUIT);
566 return new Menu(ts,ti,bg,true,screen);
567}
568
569Menu* makeGameMenu(SDL_Surface *screen)
570{
571 string bfile = "data/images/bg_stars.bmp";
572 SDL_Surface *bg = loadBMP(bfile);
573 vector<string> ts;
574 ts.push_back("Missions");
575 ts.push_back("Shop");
576 ts.push_back("Inventory");
577 ts.push_back("Skills");
578 ts.push_back("Settings");
579 ts.push_back("Save Game");
580 ts.push_back("Main Menu");
581 vector<int> ti;
582 ti.push_back(MENU | 2);
583 ti.push_back(MENU | 3);
584 ti.push_back(MENU | 6);
585 ti.push_back(0);
586 ti.push_back(0);
587 ti.push_back(SAVE);
588 ti.push_back(MENU | 0);
589 return new Menu(ts,ti,bg,true,screen);
590}
591
592Menu* makeLevelMenu(vector<string> levels, SDL_Surface *screen)
593{
594 string bfile = "data/images/bg_stars.bmp";
595 SDL_Surface *bg = loadBMP(bfile);
596 vector<string> ts;
597 vector<int> ti;
598 ifstream ins;
599 for(int i = 0; i < (int) levels.size(); ++i)
600 {
601 ins.open(levels[i].c_str());
602 string levelname;
603 getline(ins,levelname);
604 ins.close();
605 ts.push_back(levelname);
606 ti.push_back(LEVEL | i);
607 }
608 ts.push_back("back");
609 ti.push_back(MENU | 1);
610 return new Menu(ts,ti,bg,true,screen);
611}
612
613
614GameHandler::GameHandler()
615{
616 isGood = true;
617 screen = NULL;
618 if(!init(screen))
619 isGood = false;
620 //reading available levels
621 ifstream ins;
622 string path = "data/levels/";
623 ins.open((path+"levels.txt").c_str());
624 char workaround;
625 ins >> workaround;
626 while(ins.good())
627 {
628 string filepath;
629 getline(ins,filepath);
630 levelpaths.push_back(path+workaround+filepath);
631 ins >> workaround;
632 }
633 ins.close();
634 state = MENU | 0;
635 user = new Account();
636 menus.push_back(makeMainMenu(screen));
637 menus.push_back(makeGameMenu(screen));
638 menus.push_back(makeLevelMenu(levelpaths,screen));
639 menus.push_back(new Shop(&user, MENU | 1));
640 menus.push_back(makeLoadGameMenu(screen));
641 SDL_Surface* bg = loadBMP("data/images/bg_stars.bmp");
642 GetStringMenu* mt = new GetStringMenu("Enter your Nickname:", MENU | 0, LEVEL | 0, bg, screen);
643 acname = &mt->s;
644 menus.push_back(mt);
645 user->resetShips();
646 menus.push_back(new InventoryMenu(&user, MENU | 1));
647}
648
649GameHandler::~GameHandler()
650{
651 delete user;
652 if(state &LEVEL)
653 delete LG;
654 for(int i = 0; i < (int) menus.size(); ++i)
655 delete menus[i];
656
657
658 Mix_CloseAudio();
659 TTF_Quit();
660 SDL_Quit();
661 IMG_Quit();
662}
663
664int GameHandler::game()
665{
666// SDL_Rect nPos;
667// nPos.x = 10;
668// nPos.y = 10;
669// nPos.w = 200;
670// nPos.h = 400;
671// TextBox* blub = new TextBox(textField("Dies ist ein doofer Tesptsatzg.\nBBBBBBBBesser nicht allzu sehr beachten, denn der Inhalt ist unwichtig!",nPos,24));
672// cout << nPos.h << endl;
673
674 SDL_Event event;
675 if(!isGood)
676 return 1;
677 while(state != GAME_QUIT)
678 {
679 Uint32 start = SDL_GetTicks();
680 while(SDL_PollEvent(&event))
681 {
682 if(state & MENU)
683 {
684// blub->handleEvents(event);
685 int newstate = menus[state - MENU]->handleEvents(event);
686 if(newstate)
687 {
688 state = newstate;
689 if(state & LEVEL)
690 {
691 if(*acname != "")
692 {
693 delete user;
694 user = new Account("save/default account.txt");
695 user->name = *acname;
696 menus[5]->refresh();
697 menus[3]->refresh();
698 user->resetShips();
699 }
700 if(state - LEVEL < 0 || state - LEVEL > (int) levelpaths.size())
701 {
702 cout << "levellinking failed!" << endl;
703 return 1;
704 }
705 user->resetShips();
706 LG = new LevelGenerator(levelpaths[state - LEVEL], user, screen);
707 }
708 if(state & MENU)
709 menus[state - MENU]->refresh();
710 if(state & LOAD)
711 {
712 ifstream ins;
713 ins.open("save/saves.txt");
714 char workaround;
715 ins >> workaround;
716 int counter = 0;
717 while(ins.good())
718 {
719 string account;
720 getline(ins, account);
721 if(counter == state - LOAD)
722 {
723 account = workaround + account;
724 delete user;
725 user = new Account("save/" + account + ".txt");
726 menus[3]->refresh();
727 menus[6]->refresh();
728 user->resetShips();
729 state = MENU | 1;
730 }
731 counter++;
732 ins >> workaround;
733 }
734 ins.close();
735 }
736 if(state & SAVE)
737 {
738 user->save();
739 delete menus[4];
740 menus[4] = makeLoadGameMenu(screen);
741 state = MENU | 1;
742 cout << "Saved!" << endl;
743 }
744 }
745 }
746 if(event.type == SDL_QUIT)
747 state = GAME_QUIT;
748 else if(event.type == SDL_MOUSEBUTTONDOWN)
749 {
750// if(event.button.button == SDL_BUTTON_WHEELUP)
751// cout << "Wheel up!" << endl;
752// else
753// cout << "wheel down..." << endl;
754 }
755 }
756 Uint32 time = SDL_GetTicks()-start;
757 if(1000/60.0 - time > 0)
758 SDL_Delay(1000/60.0 - time);
759 else
760 cout << "Warning: Frame missed" << endl;
761 if(state & LEVEL)
762 {
763 int status = LG->frame(1/60.0);
764 if(status == LEVEL_FAILED)
765 {
766 state = 1 | MENU;
767 cout << "You lost the game!\n";
768 cout << "highscore: " << LG->OH->highscore << endl;
769 cout << "Level: " << user->getLevel() << endl;
770 user->highscore += LG->OH->highscore;
771 delete LG;
772 user->resetShips();
773 }
774 else if(status == LEVEL_COMPLETED)
775 {
776 state = 1 | MENU;
777 cout << "You have completed the level!\n";
778 cout << "highscore: " << LG->OH->highscore << endl;
779 cout << "gold: " << LG->OH->gold << endl;
780 cout << "exp: " << user->getExp() << endl;
781 cout << "Level: " << user->getLevel() << endl;
782 user->highscore += LG->OH->highscore;
783 user->gold += LG->OH->gold;
784 delete LG;
785 user->resetShips();
786 }
787 }
788 if(state & MENU)
789 {
790 menus[state - MENU]->frame(1/60.0);
791 menus[state - MENU]->draw(screen);
792// blub->frame(1/60.0);
793// blub->draw(screen);
794 }
795 SDL_Flip(screen);
796 }
797
798// delete blub;
799
800 return 0;
801}
802
803int main(int argc, char* args[])
804{
805 GameHandler* GH = new GameHandler();
806
807// Account a = Account();
808// a.name = "Start-Account";
809// a.save();
810// Account b = Account("save/Start-Account.txt");
811// b.save();
812// return 0;
813 if(GH->game())
814 return 1;
815 delete GH;
816 return 0;
817}