CPreGame.cpp 77 KB

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