CPreGame.cpp 83 KB

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