CPreGame.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. #include "stdafx.h"
  2. #include "CPreGame.h"
  3. #include "SDL.h"
  4. #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
  5. #include "boost/algorithm/string.hpp"
  6. //#include "boost/foreach.hpp"
  7. #include "zlib.h"
  8. #include <sstream>
  9. extern SDL_Surface * ekran;
  10. extern SDL_Color tytulowy, tlo, zwykly ;
  11. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX;
  12. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  13. SDL_Color genRGB(int r, int g, int b, int a=0);
  14. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran);
  15. void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran);
  16. CPreGame * CPG;
  17. void updateRect (SDL_Rect * rect, SDL_Surface * scr = ekran);
  18. bool isItIn(const SDL_Rect * rect, int x, int y);
  19. namespace fs = boost::filesystem;
  20. void OverButton::show()
  21. {
  22. blitAt(imgs->ourImages[0].bitmap,pos.x,pos.y);
  23. updateRect(&pos);
  24. }
  25. void OverButton::press(bool down)
  26. {
  27. int i;
  28. if (down) state=i=1;
  29. else state=i=0;
  30. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  31. updateRect(&pos);
  32. }
  33. template <class T> void Button<T>::hover(bool on=true)
  34. {
  35. if (!highlightable) return;
  36. int i;
  37. if (on) state=i=2;
  38. else state=i=0;
  39. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  40. updateRect(&pos);
  41. }
  42. template <class T> void Button<T>::select(bool on)
  43. {
  44. int i;
  45. if (on) state=i=3;
  46. else state=i=0;
  47. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  48. updateRect(&pos);
  49. if (ourGroup && on && ourGroup->type==1)
  50. {
  51. if (ourGroup->selected && ourGroup->selected!=this)
  52. ourGroup->selected->select(false);
  53. ourGroup->selected =this;
  54. }
  55. }
  56. //void Slider::clickDown(int x, int y, bool bzgl=true);
  57. //void Slider::clickUp(int x, int y, bool bzgl=true);
  58. //void Slider::mMove(int x, int y, bool bzgl=true);
  59. void Slider<>::updateSlid()
  60. {
  61. float perc = ((float)whereAreWe)/((float)positionsAmnt-capacity);
  62. float myh=perc*((float)pos.h-48)+pos.y+16;
  63. SDL_FillRect(ekran,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0);
  64. blitAt(slider.imgs->ourImages[0].bitmap,pos.x,(int)myh);
  65. slider.pos.y=(int)myh;
  66. updateRect(&pos);
  67. }
  68. void Slider<>::moveDown()
  69. {
  70. if (whereAreWe<positionsAmnt-capacity)
  71. (CPG->*fun)(++whereAreWe);
  72. updateSlid();
  73. }
  74. void Slider<>::moveUp()
  75. {
  76. if (whereAreWe>0)
  77. (CPG->*fun)(--whereAreWe);
  78. updateSlid();
  79. }
  80. //void Slider::moveByOne(bool up);
  81. Slider<>::Slider(int x, int y, int h, int amnt, int cap)
  82. {
  83. positionsAmnt = amnt;
  84. capacity = cap;
  85. pos = genRect(h,16,x,y);
  86. down = Button<void(Slider::*)()>(genRect(16,16,x,y+h-16),&Slider::moveDown,CPG->slh->giveDef("SCNRBDN.DEF"),false);
  87. up = Button<void(Slider::*)()>(genRect(16,16,x,y),&Slider::moveUp,CPG->slh->giveDef("SCNRBUP.DEF"),false);
  88. slider = Button<void(Slider::*)()>(genRect(16,16,x,y+16),NULL,CPG->slh->giveDef("SCNRBSL.DEF"),false);
  89. moving = false;
  90. whereAreWe=0;
  91. pos = genRect(h,16,x,y);
  92. }
  93. void Slider<>::activate(MapSel * ms)
  94. {
  95. SDL_FillRect(ekran,&pos,0);
  96. up.show();
  97. down.show();
  98. slider.show();
  99. SDL_Flip(ekran);
  100. CPG->interested.push_back(this);
  101. }
  102. void Slider<>::handleIt(SDL_Event sEvent)
  103. {
  104. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  105. {
  106. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  107. {
  108. down.press();
  109. }
  110. else if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  111. {
  112. up.press();
  113. }
  114. else if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  115. {
  116. //slider.press();
  117. moving=true;
  118. }
  119. else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y))
  120. {
  121. float dy = sEvent.motion.y-pos.y-16;
  122. float pe = dy/((float)(pos.h-32));
  123. if (pe>1) pe=1;
  124. if (pe<0) pe=0;
  125. whereAreWe = pe*(positionsAmnt-capacity);
  126. if (whereAreWe<0)whereAreWe=0;
  127. updateSlid();
  128. (CPG->*fun)(whereAreWe);
  129. }
  130. }
  131. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  132. {
  133. if ((down.state==1) && isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  134. {
  135. (this->*down.fun)();
  136. }
  137. if ((up.state==1) && isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  138. {
  139. (this->*up.fun)();
  140. }
  141. if (down.state==1) down.press(false);
  142. if (up.state==1) up.press(false);
  143. if (moving)
  144. {
  145. //slider.press();
  146. moving=false;
  147. }
  148. }
  149. //else if (sEvent.type==SDL_KEYDOWN)
  150. //{
  151. // switch (sEvent.key.keysym.sym)
  152. // {
  153. // case (SDLK_UP):
  154. // moveUp();
  155. // break;
  156. // case (SDLK_DOWN):
  157. // moveDown();
  158. // break;
  159. // }
  160. //}
  161. else if (moving && sEvent.type==SDL_MOUSEMOTION)
  162. {
  163. if (isItIn(&genRect(pos.h,pos.w+64,pos.x-32,pos.y),sEvent.motion.x,sEvent.motion.y))
  164. {
  165. int my = sEvent.motion.y-(pos.y+16);
  166. int all =pos.h-48;
  167. float ile = (float)my / (float)all;
  168. if (ile>1) ile=1;
  169. if (ile<0) ile=0;
  170. int ktory = ile*(positionsAmnt-capacity);
  171. if (ktory!=whereAreWe)
  172. {
  173. whereAreWe=ktory;
  174. updateSlid();
  175. }
  176. (CPG->*fun)(whereAreWe);
  177. }
  178. }
  179. /*else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  180. {
  181. if (ourScenSel->pressed)
  182. {
  183. ourScenSel->pressed->press(false);
  184. ourScenSel->pressed=NULL;
  185. }
  186. for (int i=0;i<btns.size(); i++)
  187. {
  188. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  189. {
  190. if (btns[i]->selectable)
  191. btns[i]->select(true);
  192. if (btns[i]->fun)
  193. (this->*(btns[i]->fun))();
  194. return;
  195. }
  196. }
  197. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  198. {
  199. (this->*down.fun)();
  200. }
  201. if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  202. {
  203. (this->*up.fun)();
  204. }
  205. if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  206. {
  207. (this->*slider.fun)();
  208. }
  209. }*/
  210. }
  211. MapSel::~MapSel()
  212. {
  213. SDL_FreeSurface(bg);
  214. for (int i=0;i<scenImgs.size();i++)
  215. SDL_FreeSurface(scenImgs[i]);
  216. for (int i=0;i<scenList.size();i++)
  217. delete scenList[i];
  218. }
  219. int MapSel::countWL()
  220. {
  221. int ret=0;
  222. for (int i=0;i<ourMaps.size();i++)
  223. {
  224. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  225. continue;
  226. else ret++;
  227. }
  228. return ret;
  229. }
  230. void MapSel::printMaps(int from, int to, int at, bool abs)
  231. {
  232. if (true)//
  233. {
  234. int help=-1;
  235. for (int i=0;i<ourMaps.size();i++)
  236. {
  237. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  238. continue;
  239. else help++;
  240. if (help==from)
  241. {
  242. from=i;
  243. break;
  244. }
  245. }
  246. }
  247. SDL_Color nasz;
  248. for (int i=at;i<to;i++)
  249. {
  250. if ((i-at+from) > ourMaps.size()-1)
  251. {
  252. SDL_Surface * scenin = SDL_CreateRGBSurface(ekran->flags,351,25,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  253. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  254. blitAt(scenin,24,121+(i-at)*25);
  255. SDL_Flip(ekran);
  256. SDL_FreeSurface(scenin);
  257. continue;
  258. }
  259. if (sizeFilter && ((ourMaps[(i-at)+from].width) != sizeFilter))
  260. {
  261. to++;
  262. at++;
  263. from++;
  264. if (((i-at)+from)>ourMaps.size()-1) break;
  265. else continue;
  266. }
  267. if ((i-at+from) == selected)
  268. nasz=tytulowy;
  269. else nasz=zwykly;
  270. //SDL_Rect pier = genRect(25,351,24,126+(i*25));
  271. SDL_Surface * scenin = SDL_CreateRGBSurface(ekran->flags,351,25,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  272. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  273. int temp=-1;
  274. std::ostringstream ostr(std::ostringstream::out); ostr << ourMaps[(i-at)+from].playerAmnt << "/" << ourMaps[(i-at)+from].humenPlayers;
  275. printAt(ostr.str(),6,4,GEOR13,nasz,scenin);
  276. std::string temp2;
  277. switch (ourMaps[(i-at)+from].width)
  278. {
  279. case 36:
  280. temp2="S";
  281. break;
  282. case 72:
  283. temp2="M";
  284. break;
  285. case 108:
  286. temp2="L";
  287. break;
  288. case 144:
  289. temp2="XL";
  290. break;
  291. default:
  292. temp2="C";
  293. break;
  294. }
  295. printAt(temp2,43,5,GEOR13,nasz,scenin);
  296. switch (ourMaps[(i-at)+from].version)
  297. {
  298. case Eformat::SoD:
  299. temp=2;
  300. break;
  301. case Eformat::WoG:
  302. temp=3;
  303. break;
  304. }
  305. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  306. if (!(ourMaps[(i-at)+from].name.length()))
  307. ourMaps[(i-at)+from].name = "Unnamed";
  308. printAt(ourMaps[(i-at)+from].name,105,6,GEOR13,nasz,scenin);
  309. if (ourMaps[(i-at)+from].victoryCondition==EvictoryConditions::winStandard)
  310. temp=11;
  311. else temp=ourMaps[(i-at)+from].victoryCondition;
  312. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  313. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == ElossCon::lossStandard)
  314. temp=3;
  315. else temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  316. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  317. blitAt(scenin,24,121+(i-at)*25);
  318. SDL_Flip(ekran);
  319. SDL_FreeSurface(scenin);
  320. }
  321. }
  322. int MapSel::whichWL(int nr)
  323. {
  324. int help=-1;
  325. for (int i=0;i<ourMaps.size();i++)
  326. {
  327. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  328. continue;
  329. else help++;
  330. if (help==nr)
  331. {
  332. help=i;
  333. break;
  334. }
  335. }
  336. return help;
  337. }
  338. void MapSel::draw()
  339. {
  340. //blit bg
  341. blitAt(bg,2,6);
  342. printAt("Map Sizes",55,60,GEOR13);
  343. printAt("Select a Scenario to Play",110,25,TNRB16);
  344. //size buttons
  345. small.show();
  346. medium.show();
  347. large.show();
  348. xlarge.show();
  349. all.show();
  350. CPG->btns.push_back(&small);
  351. CPG->btns.push_back(&medium);
  352. CPG->btns.push_back(&large);
  353. CPG->btns.push_back(&xlarge);
  354. CPG->btns.push_back(&all);
  355. //print scenario list
  356. printMaps(0,18);
  357. slid->activate(this);
  358. SDL_Flip(ekran);
  359. }
  360. void MapSel::init()
  361. {
  362. bg = SDL_LoadBMP("h3bitmap.lod\\SCSELBCK.bmp");
  363. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  364. small.imgs = CPG->slh->giveDef("SCSMBUT.DEF");
  365. small.fun = NULL;
  366. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  367. small.ourGroup=NULL;
  368. medium.imgs = CPG->slh->giveDef("SCMDBUT.DEF");
  369. medium.fun = NULL;
  370. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  371. medium.ourGroup=NULL;
  372. large.imgs = CPG->slh->giveDef("SCLGBUT.DEF");
  373. large.fun = NULL;
  374. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  375. large.ourGroup=NULL;
  376. xlarge.imgs = CPG->slh->giveDef("SCXLBUT.DEF");
  377. xlarge.fun = NULL;
  378. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  379. xlarge.ourGroup=NULL;
  380. all.imgs = CPG->slh->giveDef("SCALBUT.DEF");
  381. all.fun = NULL;
  382. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  383. all.ourGroup=NULL;
  384. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  385. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  386. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  387. Dtypes = CPG->slh->giveDef("SCSELC.DEF");
  388. Dvic = CPG->slh->giveDef("SCNRVICT.DEF");
  389. Dloss = CPG->slh->giveDef("SCNRLOSS.DEF");
  390. Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  391. //get map files names
  392. std::vector<std::string> pliczkiTemp;
  393. boost::filesystem::path tie( (fs::initial_path<fs::path>())/"\maps" );
  394. fs::directory_iterator end_iter;
  395. for ( boost::filesystem::directory_iterator dir (tie); dir!=end_iter; ++dir )
  396. {
  397. if (boost::filesystem::is_regular(dir->status()));
  398. {
  399. if (boost::ends_with(dir->path().leaf(),std::string(".h3m")))
  400. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  401. }
  402. }
  403. for (int i=0; i<pliczkiTemp.size();i++)
  404. {
  405. gzFile tempf = gzopen(pliczkiTemp[i].c_str(),"rb");
  406. std::string sss;
  407. int iii=0;
  408. while(++iii)
  409. {
  410. if (iii>3300) break;
  411. int z = gzgetc (tempf);
  412. if (z>=0)
  413. {
  414. sss+=unsigned char(z);
  415. }
  416. else break;
  417. }
  418. gzclose(tempf);
  419. if (sss[0]<28) continue; //zly format
  420. if (!sss[4]) continue; //nie ma graczy? mapa niegrywalna
  421. unsigned char* file2 = new unsigned char[sss.length()];
  422. for (int j=0;j<sss.length();j++)
  423. file2[j]=sss[j];
  424. ourMaps.push_back(CMapInfo(pliczkiTemp[i],file2));
  425. }
  426. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(ESortBy::name));
  427. slid = new Slider<>(375,92,480,ourMaps.size(),18);
  428. slid->fun = &CPreGame::printMapsFrom;
  429. }
  430. void MapSel::select(int which)
  431. {
  432. //selected = which;
  433. //if ((slid->whereAreWe > which) || (slid->whereAreWe+18 < which))
  434. // selected = which;
  435. //else
  436. {
  437. selected = which;
  438. printMaps(slid->whereAreWe,18,0,true);
  439. }
  440. printSelectedInfo();
  441. }
  442. MapSel::MapSel():selected(0),sizeFilter(0)
  443. {
  444. }
  445. void MapSel::printSelectedInfo()
  446. {
  447. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),ekran,&genRect(399,337,412,29));
  448. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),ekran,&genRect(50,91,413,453));
  449. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bScens.pos);
  450. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bOptions.pos);
  451. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bRandom.pos);
  452. //blit texts
  453. printAt(CPG->preth->singleScenarioName,420,25,GEOR13);
  454. printAt("Scenario Description:",420,135,GEOR13);
  455. printAt("Victory Condition:",420,285,GEOR13);
  456. printAt("Loss Condition:",420,340,GEOR13);
  457. printAt("Allies:",420,406,GEOR13,zwykly);
  458. printAt("Enemies:",585,406,GEOR13,zwykly);
  459. int temp = ourMaps[selected].victoryCondition+1;
  460. if (temp>20) temp=0;
  461. std::string sss = CPG->preth->victoryConditions[temp];
  462. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0];
  463. printAt(sss,452,310,GEOR13,zwykly);
  464. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  465. if (temp>20) temp=0;
  466. sss = CPG->preth->lossCondtions[temp];
  467. printAt(sss,452,370,GEOR13,zwykly);
  468. //blit descrption
  469. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  470. for (int i=0;i<desc.size();i++)
  471. printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  472. if ((selected < 0) || (selected >= ourMaps.size()))
  473. return;
  474. if (ourMaps[selected].name.length())
  475. printAt(ourMaps[selected].name,420,41,GEORXX);
  476. else printAt("Unnamed",420,41,GEORXX);
  477. std::string diff;
  478. switch (ourMaps[selected].difficulty)
  479. {
  480. case 0:
  481. diff=gdiff(CPG->preth->singleEasy);
  482. break;
  483. case 1:
  484. diff=gdiff(CPG->preth->singleNormal);
  485. break;
  486. case 2:
  487. diff=gdiff(CPG->preth->singleHard);
  488. break;
  489. case 3:
  490. diff=gdiff(CPG->preth->singleExpert);
  491. break;
  492. case 4:
  493. diff=gdiff(CPG->preth->singleImpossible);
  494. break;
  495. }
  496. temp=-1;
  497. switch (ourMaps[selected].width)
  498. {
  499. case 36:
  500. temp=0;
  501. break;
  502. case 72:
  503. temp=1;
  504. break;
  505. case 108:
  506. temp=2;
  507. break;
  508. case 144:
  509. temp=3;
  510. break;
  511. default:
  512. temp=4;
  513. break;
  514. }
  515. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  516. temp=ourMaps[selected].victoryCondition;
  517. if (temp>12) temp=11;
  518. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  519. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  520. if (temp>12) temp=3;
  521. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  522. printAt(diff,435,470,GEOR13,zwykly);
  523. SDL_Flip(ekran);
  524. }
  525. std::string MapSel::gdiff(std::string ss)
  526. {
  527. std::string ret;
  528. for (int i=2;i<ss.length();i++)
  529. {
  530. if (ss[i]==' ')
  531. break;
  532. ret+=ss[i];
  533. }
  534. return ret;
  535. }
  536. void CPreGame::printMapsFrom(int from)
  537. {
  538. ourScenSel->mapsel.printMaps(from);
  539. }
  540. void CPreGame::showScenList()
  541. {
  542. ourScenSel->listShowed=true;
  543. ourScenSel->mapsel.draw();
  544. }
  545. CPreGame::CPreGame()
  546. {
  547. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  548. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  549. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  550. preth = new CPreGameTextHandler;
  551. preth->loadTexts();
  552. currentMessage=NULL;
  553. behindCurMes=NULL;
  554. initMainMenu();
  555. initNewMenu();
  556. initScenSel();
  557. showMainMenu();
  558. CPG=this;
  559. }
  560. void CPreGame::initScenSel()
  561. {
  562. ourScenSel = new ScenSel();
  563. ourScenSel->listShowed=false;
  564. if (rand()%2) ourScenSel->background=SDL_LoadBMP("h3bitmap.lod\\ZPIC1000.bmp");
  565. else ourScenSel->background=SDL_LoadBMP("h3bitmap.lod\\ZPIC1001.bmp");
  566. ourScenSel->pressed=NULL;
  567. ourScenSel->scenInf=SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp");
  568. ourScenSel->randMap=SDL_LoadBMP("h3bitmap.lod\\RANMAPBK.bmp");
  569. ourScenSel->options=SDL_LoadBMP("h3bitmap.lod\\ADVOPTBK.bmp");
  570. SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255));
  571. //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255));
  572. SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255));
  573. SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255));
  574. ourScenSel->difficulty = new CPoinGroup<>();
  575. ourScenSel->difficulty->type=1;
  576. ourScenSel->selectedDiff=-77;
  577. ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff;
  578. ourScenSel->bEasy = IntSelBut<>(genRect(0,0,506,456),NULL,slh->giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,1);
  579. ourScenSel->bNormal = IntSelBut<>(genRect(0,0,538,456),NULL,slh->giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,2);
  580. ourScenSel->bHard = IntSelBut<>(genRect(0,0,570,456),NULL,slh->giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,3);
  581. ourScenSel->bExpert = IntSelBut<>(genRect(0,0,602,456),NULL,slh->giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,4);
  582. ourScenSel->bImpossible = IntSelBut<>(genRect(0,0,634,456),NULL,slh->giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,5);
  583. ourScenSel->bBack = Button<>(genRect(0,0,584,535),&CPreGame::showNewMenu,slh->giveDef("SCNRBACK.DEF"));
  584. ourScenSel->bBegin = Button<>(genRect(0,0,414,535),&CPreGame::showNewMenu,slh->giveDef("SCNRBEG.DEF"));
  585. ourScenSel->bScens = Button<>(genRect(0,0,414,81),&CPreGame::showScenList,slh->giveDef("GSPBUTT.DEF"));
  586. for (int i=0; i<ourScenSel->bScens.imgs->ourImages.size(); i++)
  587. printAt("Show Available Scenarios",25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap);
  588. ourScenSel->bRandom = Button<>(genRect(0,0,414,105),&CPreGame::showScenList,slh->giveDef("GSPBUTT.DEF"));
  589. for (int i=0; i<ourScenSel->bRandom.imgs->ourImages.size(); i++)
  590. printAt("Random Map",25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap);
  591. ourScenSel->bOptions = Button<>(genRect(0,0,414,509),&CPreGame::showScenList,slh->giveDef("GSPBUTT.DEF"));
  592. for (int i=0; i<ourScenSel->bOptions.imgs->ourImages.size(); i++)
  593. printAt("Show Advanced Options",25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap);
  594. CPG=this;
  595. ourScenSel->mapsel.init();
  596. }
  597. void CPreGame::showScenSel()
  598. {
  599. state=EState::ScenarioList;
  600. SDL_BlitSurface(ourScenSel->background,NULL,ekran,NULL);
  601. SDL_BlitSurface(ourScenSel->scenInf,NULL,ekran,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,395,6));
  602. printAt("Map Diff:",427,438,GEOR13);
  603. printAt("Player Difficulty:",527,438,GEOR13);
  604. printAt("Rating:",685,438,GEOR13);
  605. //blit buttons
  606. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bEasy.pos);
  607. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bNormal.pos);
  608. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bHard.pos);
  609. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bExpert.pos);
  610. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bImpossible.pos);
  611. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBegin.pos);
  612. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBack.pos);
  613. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  614. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  615. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  616. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  617. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  618. //add buttons info
  619. btns.push_back(&ourScenSel->bEasy);
  620. btns.push_back(&ourScenSel->bNormal);
  621. btns.push_back(&ourScenSel->bHard);
  622. btns.push_back(&ourScenSel->bExpert);
  623. btns.push_back(&ourScenSel->bImpossible);
  624. btns.push_back(&ourScenSel->bScens);
  625. btns.push_back(&ourScenSel->bRandom);
  626. btns.push_back(&ourScenSel->bOptions);
  627. btns.push_back(&ourScenSel->bBegin);
  628. btns.push_back(&ourScenSel->bBack);
  629. ourScenSel->selectedDiff=1;
  630. ourScenSel->bNormal.select(true);
  631. for (int i=0;i<btns.size();i++)
  632. {
  633. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  634. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  635. }
  636. handleOther = &CPreGame::scenHandleEv;
  637. ourScenSel->mapsel.printSelectedInfo();
  638. SDL_Flip(ekran);
  639. }
  640. void CPreGame::showOptions()
  641. {}
  642. void CPreGame::initNewMenu()
  643. {
  644. ourNewMenu = new menuItems();
  645. ourNewMenu->bgAd = SDL_LoadBMP("h3bitmap.lod\\ZNEWGAM.bmp");
  646. ourNewMenu->background = SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  647. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  648. slh = new CSemiLodHandler();
  649. slh->openLod("H3sprite.lod");
  650. //loading menu buttons
  651. ourNewMenu->newGame = slh->giveDef("ZTSINGL.DEF");
  652. ourNewMenu->loadGame = slh->giveDef("ZTMULTI.DEF");
  653. ourNewMenu->highScores = slh->giveDef("ZTCAMPN.DEF");
  654. ourNewMenu->credits = slh->giveDef("ZTTUTOR.DEF");
  655. ourNewMenu->quit = slh->giveDef("ZTBACK.DEF");
  656. ok = slh->giveDef("IOKAY.DEF");
  657. cancel = slh->giveDef("ICANCEL.DEF");
  658. // single scenario
  659. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  660. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  661. ourNewMenu->lNewGame.x=545;
  662. ourNewMenu->lNewGame.y=4;
  663. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  664. //multiplayer
  665. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  666. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  667. ourNewMenu->lLoadGame.x=568;
  668. ourNewMenu->lLoadGame.y=120;
  669. //campaign
  670. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  671. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  672. ourNewMenu->lHighScores.x=541;
  673. ourNewMenu->lHighScores.y=233;
  674. //tutorial
  675. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  676. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  677. ourNewMenu->lCredits.x=545;
  678. ourNewMenu->lCredits.y=358;
  679. //back
  680. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  681. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  682. ourNewMenu->lQuit.x=582;
  683. ourNewMenu->lQuit.y=464;
  684. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  685. ourNewMenu->highlighted=0;
  686. }
  687. void CPreGame::showNewMenu()
  688. {
  689. btns.clear();
  690. handleOther=NULL;
  691. state = EState::newGame;
  692. SDL_BlitSurface(ourNewMenu->background,NULL,ekran,NULL);
  693. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lNewGame);
  694. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lLoadGame);
  695. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lHighScores);
  696. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lCredits);
  697. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lQuit);
  698. SDL_Flip(ekran);
  699. }
  700. void CPreGame::initMainMenu()
  701. {
  702. ourMainMenu = new menuItems();
  703. ourMainMenu->background = SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  704. CSemiLodHandler * slh = new CSemiLodHandler();
  705. slh->openLod("H3sprite.lod");
  706. //loading menu buttons
  707. ourMainMenu->newGame = slh->giveDef("ZMENUNG.DEF");
  708. ourMainMenu->loadGame = slh->giveDef("ZMENULG.DEF");
  709. ourMainMenu->highScores = slh->giveDef("ZMENUHS.DEF");
  710. ourMainMenu->credits = slh->giveDef("ZMENUCR.DEF");
  711. ourMainMenu->quit = slh->giveDef("ZMENUQT.DEF");
  712. ok = slh->giveDef("IOKAY.DEF");
  713. cancel = slh->giveDef("ICANCEL.DEF");
  714. // new game button location
  715. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  716. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  717. ourMainMenu->lNewGame.x=540;
  718. ourMainMenu->lNewGame.y=10;
  719. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  720. //load game location
  721. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  722. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  723. ourMainMenu->lLoadGame.x=532;
  724. ourMainMenu->lLoadGame.y=132;
  725. //high scores
  726. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  727. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  728. ourMainMenu->lHighScores.x=524;
  729. ourMainMenu->lHighScores.y=251;
  730. //credits
  731. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  732. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  733. ourMainMenu->lCredits.x=557;
  734. ourMainMenu->lCredits.y=359;
  735. //quit
  736. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  737. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  738. ourMainMenu->lQuit.x=586;
  739. ourMainMenu->lQuit.y=468;
  740. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  741. ourMainMenu->highlighted=0;
  742. handledLods.push_back(slh);
  743. delete slh;
  744. }
  745. void CPreGame::showMainMenu()
  746. {
  747. state = EState::mainMenu;
  748. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  749. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  750. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  751. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  752. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  753. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  754. SDL_Flip(ekran);
  755. }
  756. void CPreGame::highlightButton(int which, int on)
  757. {
  758. menuItems * current = currentItems();
  759. switch (which)
  760. {
  761. case 1:
  762. {
  763. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,ekran,&current->lNewGame);
  764. break;
  765. }
  766. case 2:
  767. {
  768. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,ekran,&current->lLoadGame);
  769. break;
  770. }
  771. case 3:
  772. {
  773. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,ekran,&current->lHighScores);
  774. break;
  775. }
  776. case 4:
  777. {
  778. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,ekran,&current->lCredits);
  779. break;
  780. }
  781. case 5:
  782. {
  783. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,ekran,&current->lQuit);
  784. break;
  785. }
  786. }
  787. SDL_Flip(ekran);
  788. }
  789. void CPreGame::showCenBox (std::string data)
  790. {
  791. CMessage * cmh = new CMessage();
  792. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data));
  793. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  794. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  795. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  796. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  797. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  798. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  799. SDL_FreeSurface(infoBox);
  800. currentMessage = new SDL_Rect(pos);
  801. delete cmh;
  802. }
  803. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  804. {
  805. CMessage * cmh = new CMessage();
  806. std::vector<CSemiDefHandler*> * przyciski = new std::vector<CSemiDefHandler*>(0);
  807. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  808. przyciski->push_back(ok);
  809. przyciski->push_back(cancel);
  810. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), EWindowType::yesOrNO, przyciski, btnspos);
  811. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  812. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  813. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  814. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  815. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  816. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  817. SDL_FreeSurface(infoBox);
  818. currentMessage = new SDL_Rect(pos);
  819. (*btnspos)[0].x+=pos.x;
  820. (*btnspos)[0].y+=pos.y;
  821. (*btnspos)[1].x+=pos.x;
  822. (*btnspos)[1].y+=pos.y;
  823. btns.push_back(new Button<>((*btnspos)[0],&CPreGame::quit,ok,false, NULL,2));
  824. btns.push_back(new Button<>((*btnspos)[1],(&CPreGame::hideBox),cancel,false, NULL,2));
  825. delete cmh;
  826. delete przyciski;
  827. delete btnspos;
  828. }
  829. void CPreGame::hideBox ()
  830. {
  831. SDL_BlitSurface(behindCurMes,NULL,ekran,currentMessage);
  832. SDL_UpdateRect
  833. (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  834. for (int i=0;i<btns.size();i++)
  835. {
  836. if (btns[i]->ID==2)
  837. {
  838. delete btns[i];
  839. btns.erase(btns.begin()+i);
  840. i--;
  841. }
  842. }
  843. SDL_FreeSurface(behindCurMes);
  844. delete currentMessage;
  845. currentMessage = NULL;
  846. behindCurMes=NULL;
  847. }
  848. CPreGame::menuItems * CPreGame::currentItems()
  849. {
  850. switch (state)
  851. {
  852. case EState::mainMenu:
  853. return ourMainMenu;
  854. case EState::newGame:
  855. return ourNewMenu;
  856. default:
  857. return NULL;
  858. }
  859. }
  860. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  861. {
  862. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  863. {
  864. for (int i=0;i<btns.size(); i++)
  865. {
  866. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  867. {
  868. btns[i]->press(true);
  869. ourScenSel->pressed=btns[i];
  870. }
  871. }
  872. if (ourScenSel->listShowed && (sEvent.button.y>121) &&(sEvent.button.y<570)
  873. && (sEvent.button.x>55) && (sEvent.button.x<372))
  874. {
  875. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  876. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  877. }
  878. }
  879. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  880. {
  881. for (int i=0;i<btns.size(); i++)
  882. {
  883. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  884. {
  885. if (btns[i]->selectable)
  886. btns[i]->select(true);
  887. if (btns[i]->fun)
  888. (this->*(btns[i]->fun))();
  889. int zz = btns.size();
  890. if (i>=zz)
  891. break;
  892. if (btns[i]->state && btns[i]->type==2)
  893. {
  894. ((IntBut<> *)(btns[i]))->set();
  895. ourScenSel->mapsel.slid->whereAreWe=0;
  896. ourScenSel->mapsel.slid->updateSlid();
  897. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  898. ourScenSel->mapsel.printMaps(0);
  899. }
  900. }
  901. }
  902. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  903. {
  904. ourScenSel->pressed->press(false);
  905. ourScenSel->pressed=NULL;
  906. }
  907. }
  908. }
  909. void CPreGame::runLoop()
  910. {
  911. SDL_Event sEvent;
  912. while(true)
  913. {
  914. try
  915. {
  916. if(SDL_PollEvent(&sEvent)) //wait for event...
  917. {
  918. menuItems * current = currentItems();
  919. if(sEvent.type==SDL_QUIT)
  920. return ;
  921. for (int i=0;i<interested.size();i++)
  922. interested[i]->handleIt(sEvent);
  923. if (!current)
  924. {
  925. (this->*handleOther)(sEvent);
  926. }
  927. else if (sEvent.type==SDL_KEYDOWN)
  928. {
  929. if (sEvent.key.keysym.sym==SDLK_q)
  930. {
  931. return ;
  932. break;
  933. }
  934. /*if (state==EState::newGame)
  935. {
  936. switch (sEvent.key.keysym.sym)
  937. {
  938. case SDLK_LEFT:
  939. {
  940. if(currentItems()->lNewGame.x>0)
  941. currentItems()->lNewGame.x--;
  942. break;
  943. }
  944. case (SDLK_RIGHT):
  945. {
  946. currentItems()->lNewGame.x++;
  947. break;
  948. }
  949. case (SDLK_UP):
  950. {
  951. if(currentItems()->lNewGame.y>0)
  952. currentItems()->lNewGame.y--;
  953. break;
  954. }
  955. case (SDLK_DOWN):
  956. {
  957. currentItems()->lNewGame.y++;
  958. break;
  959. }
  960. }
  961. showNewMenu();
  962. }*/
  963. }
  964. else if (sEvent.type==SDL_MOUSEMOTION)
  965. {
  966. if (currentMessage) continue;
  967. if (current->highlighted)
  968. {
  969. switch (current->highlighted)
  970. {
  971. case 1:
  972. {
  973. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  974. continue;
  975. else
  976. {
  977. current->highlighted=0;
  978. highlightButton(1,0);
  979. }
  980. break;
  981. }
  982. case 2:
  983. {
  984. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  985. continue;
  986. else
  987. {
  988. current->highlighted=0;
  989. highlightButton(2,0);
  990. }
  991. break;
  992. }
  993. case 3:
  994. {
  995. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  996. continue;
  997. else
  998. {
  999. current->highlighted=0;
  1000. highlightButton(3,0);
  1001. }
  1002. break;
  1003. }
  1004. case 4:
  1005. {
  1006. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1007. continue;
  1008. else
  1009. {
  1010. current->highlighted=0;
  1011. highlightButton(4,0);
  1012. }
  1013. break;
  1014. }
  1015. case 5:
  1016. {
  1017. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1018. continue;
  1019. else
  1020. {
  1021. current->highlighted=0;
  1022. highlightButton(5,0);
  1023. }
  1024. break;
  1025. }
  1026. } //switch (current->highlighted)
  1027. } // if (current->highlighted)
  1028. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1029. {
  1030. highlightButton(1,2);
  1031. current->highlighted=1;
  1032. }
  1033. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1034. {
  1035. highlightButton(2,2);
  1036. current->highlighted=2;
  1037. }
  1038. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1039. {
  1040. highlightButton(3,2);
  1041. current->highlighted=3;
  1042. }
  1043. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1044. {
  1045. highlightButton(4,2);
  1046. current->highlighted=4;
  1047. }
  1048. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1049. {
  1050. highlightButton(5,2);
  1051. current->highlighted=5;
  1052. }
  1053. }
  1054. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1055. {
  1056. mush->playClick();
  1057. for (int i=0;i<btns.size(); i++)
  1058. {
  1059. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1060. {
  1061. btns[i]->press(true);
  1062. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,ekran,&btns[i].pos);
  1063. //updateRect(&btns[i].pos);
  1064. }
  1065. }
  1066. if (currentMessage) continue;
  1067. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1068. {
  1069. highlightButton(1,1);
  1070. current->highlighted=1;
  1071. }
  1072. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1073. {
  1074. highlightButton(2,1);
  1075. current->highlighted=2;
  1076. }
  1077. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1078. {
  1079. highlightButton(3,1);
  1080. current->highlighted=3;
  1081. }
  1082. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1083. {
  1084. highlightButton(4,1);
  1085. current->highlighted=4;
  1086. }
  1087. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1088. {
  1089. highlightButton(5,1);
  1090. current->highlighted=5;
  1091. }
  1092. }
  1093. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1094. {
  1095. for (int i=0;i<btns.size(); i++)
  1096. {
  1097. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1098. (this->*(btns[i]->fun))();
  1099. else
  1100. {
  1101. btns[i]->press(false);
  1102. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,ekran,&btns[i].pos);
  1103. //updateRect(&btns[i].pos);
  1104. }
  1105. }
  1106. if (currentMessage) continue;
  1107. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1108. {
  1109. highlightButton(1,2);
  1110. current->highlighted=1;
  1111. (this->*(current->fNewGame))();
  1112. }
  1113. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1114. {
  1115. highlightButton(2,2);
  1116. current->highlighted=2;
  1117. }
  1118. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1119. {
  1120. highlightButton(3,2);
  1121. current->highlighted=3;
  1122. }
  1123. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1124. {
  1125. highlightButton(4,2);
  1126. current->highlighted=4;
  1127. }
  1128. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1129. {
  1130. highlightButton(5,2);
  1131. current->highlighted=5;
  1132. (this->*(current->fQuit))();
  1133. }
  1134. }
  1135. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  1136. {
  1137. if (currentMessage) continue;
  1138. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1139. {
  1140. showCenBox(buttonText(0));
  1141. }
  1142. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1143. {
  1144. showCenBox(buttonText(1));
  1145. }
  1146. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1147. {
  1148. showCenBox(buttonText(2));
  1149. }
  1150. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1151. {
  1152. showCenBox(buttonText(3));
  1153. }
  1154. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  1155. {
  1156. showCenBox(buttonText(4));
  1157. }
  1158. }
  1159. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  1160. {
  1161. hideBox();
  1162. }
  1163. }
  1164. }
  1165. catch(...)
  1166. { continue; }
  1167. SDL_Delay(30); //give time for other apps
  1168. }
  1169. }
  1170. std::string CPreGame::buttonText(int which)
  1171. {
  1172. if (state==EState::mainMenu)
  1173. {
  1174. switch (which)
  1175. {
  1176. case 0:
  1177. return preth->mainNewGame;
  1178. case 1:
  1179. return preth->mainLoadGame;
  1180. case 2:
  1181. return preth->mainHighScores;
  1182. case 3:
  1183. return preth->mainCredits;
  1184. case 4:
  1185. return preth->mainQuit;
  1186. }
  1187. }
  1188. else if (state==EState::newGame)
  1189. {
  1190. switch (which)
  1191. {
  1192. case 0:
  1193. return preth->ngSingleScenario;
  1194. case 1:
  1195. return preth->ngMultiplayer;
  1196. case 2:
  1197. return preth->ngCampain;
  1198. case 3:
  1199. return preth->ngTutorial;
  1200. case 4:
  1201. return preth->ngBack;
  1202. }
  1203. }
  1204. }
  1205. void CPreGame::quitAskBox()
  1206. {
  1207. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  1208. }