CPreGame.cpp 78 KB

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