CPreGame.cpp 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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() && i>=0;)
  490. {
  491. help=i;
  492. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  493. break;
  494. if (up)
  495. {
  496. i--;
  497. }
  498. else
  499. {
  500. i++;
  501. if (i<0) break;
  502. }
  503. }
  504. select(help);
  505. slid->updateSlid();
  506. }
  507. void MapSel::select(int which)
  508. {
  509. //selected = which;
  510. //if ((slid->whereAreWe > which) || (slid->whereAreWe+18 < which))
  511. // selected = which;
  512. //else
  513. {
  514. selected = which;
  515. printMaps(slid->whereAreWe,18,0,true);
  516. }
  517. printSelectedInfo();
  518. }
  519. MapSel::MapSel():selected(0),sizeFilter(0)
  520. {
  521. }
  522. void MapSel::printSelectedInfo()
  523. {
  524. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),ekran,&genRect(399,337,412,29));
  525. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),ekran,&genRect(50,91,413,453));
  526. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bScens.pos);
  527. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bOptions.pos);
  528. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bRandom.pos);
  529. //blit texts
  530. CSDL_Ext::printAt(CPG->preth->singleScenarioName,420,25,GEOR13);
  531. CSDL_Ext::printAt("Scenario Description:",420,135,GEOR13);
  532. CSDL_Ext::printAt("Victory Condition:",420,285,GEOR13);
  533. CSDL_Ext::printAt("Loss Condition:",420,340,GEOR13);
  534. CSDL_Ext::printAt("Allies:",420,406,GEOR13,zwykly);
  535. CSDL_Ext::printAt("Enemies:",585,406,GEOR13,zwykly);
  536. int temp = ourMaps[selected].victoryCondition+1;
  537. if (temp>20) temp=0;
  538. std::string sss = CPG->preth->victoryConditions[temp];
  539. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0];
  540. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  541. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  542. if (temp>20) temp=0;
  543. sss = CPG->preth->lossCondtions[temp];
  544. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  545. //blit descrption
  546. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  547. for (int i=0;i<desc.size();i++)
  548. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  549. if ((selected < 0) || (selected >= ourMaps.size()))
  550. return;
  551. if (ourMaps[selected].name.length())
  552. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  553. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  554. std::string diff;
  555. switch (ourMaps[selected].difficulty)
  556. {
  557. case 0:
  558. diff=gdiff(CPG->preth->singleEasy);
  559. break;
  560. case 1:
  561. diff=gdiff(CPG->preth->singleNormal);
  562. break;
  563. case 2:
  564. diff=gdiff(CPG->preth->singleHard);
  565. break;
  566. case 3:
  567. diff=gdiff(CPG->preth->singleExpert);
  568. break;
  569. case 4:
  570. diff=gdiff(CPG->preth->singleImpossible);
  571. break;
  572. }
  573. temp=-1;
  574. switch (ourMaps[selected].width)
  575. {
  576. case 36:
  577. temp=0;
  578. break;
  579. case 72:
  580. temp=1;
  581. break;
  582. case 108:
  583. temp=2;
  584. break;
  585. case 144:
  586. temp=3;
  587. break;
  588. default:
  589. temp=4;
  590. break;
  591. }
  592. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  593. temp=ourMaps[selected].victoryCondition;
  594. if (temp>12) temp=11;
  595. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  596. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  597. if (temp>12) temp=3;
  598. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  599. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  600. //SDL_Flip(ekran);
  601. CSDL_Ext::update(ekran);
  602. }
  603. std::string MapSel::gdiff(std::string ss)
  604. {
  605. std::string ret;
  606. for (int i=2;i<ss.length();i++)
  607. {
  608. if (ss[i]==' ')
  609. break;
  610. ret+=ss[i];
  611. }
  612. return ret;
  613. }
  614. void CPreGame::printMapsFrom(int from)
  615. {
  616. ourScenSel->mapsel.printMaps(from);
  617. }
  618. void CPreGame::showScenList()
  619. {
  620. ourScenSel->listShowed=true;
  621. ourScenSel->mapsel.draw();
  622. }
  623. CPreGame::CPreGame()
  624. {
  625. run=true;
  626. timeHandler tmh;tmh.getDif();
  627. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  628. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  629. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  630. preth = new CPreGameTextHandler;
  631. preth->loadTexts();
  632. THC std::cout<<"\tCPreGame: loading txts: "<<tmh.getDif()<<std::endl;
  633. currentMessage=NULL;
  634. behindCurMes=NULL;
  635. initMainMenu();
  636. THC std::cout<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  637. initNewMenu();
  638. THC std::cout<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  639. initScenSel();
  640. THC std::cout<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  641. showMainMenu();
  642. THC std::cout<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  643. CPG=this;
  644. }
  645. void CPreGame::initScenSel()
  646. {
  647. ourScenSel = new ScenSel();
  648. ourScenSel->listShowed=false;
  649. if (rand()%2) ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1000.bmp");
  650. else ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1001.bmp");
  651. ourScenSel->pressed=NULL;
  652. ourScenSel->scenInf=CGI->bitmaph->loadBitmap("GSELPOP1.bmp");//SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp");
  653. ourScenSel->randMap=CGI->bitmaph->loadBitmap("RANMAPBK.bmp");
  654. ourScenSel->options=CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  655. SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255));
  656. //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255));
  657. SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255));
  658. SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255));
  659. ourScenSel->difficulty = new CPoinGroup<>();
  660. ourScenSel->difficulty->type=1;
  661. ourScenSel->selectedDiff=-77;
  662. ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff;
  663. ourScenSel->bEasy = IntSelBut<>(genRect(0,0,506,456),NULL,CGI->spriteh->giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,1);
  664. ourScenSel->bNormal = IntSelBut<>(genRect(0,0,538,456),NULL,CGI->spriteh->giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,2);
  665. ourScenSel->bHard = IntSelBut<>(genRect(0,0,570,456),NULL,CGI->spriteh->giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,3);
  666. ourScenSel->bExpert = IntSelBut<>(genRect(0,0,602,456),NULL,CGI->spriteh->giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,4);
  667. ourScenSel->bImpossible = IntSelBut<>(genRect(0,0,634,456),NULL,CGI->spriteh->giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,5);
  668. ourScenSel->bBack = Button<>(genRect(0,0,584,535),&CPreGame::showNewMenu,CGI->spriteh->giveDef("SCNRBACK.DEF"));
  669. ourScenSel->bBegin = Button<>(genRect(0,0,414,535),&CPreGame::begin,CGI->spriteh->giveDef("SCNRBEG.DEF"));
  670. ourScenSel->bScens = Button<>(genRect(0,0,414,81),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  671. for (int i=0; i<ourScenSel->bScens.imgs->ourImages.size(); i++)
  672. CSDL_Ext::printAt("Show Available Scenarios",25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap);
  673. ourScenSel->bRandom = Button<>(genRect(0,0,414,105),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  674. for (int i=0; i<ourScenSel->bRandom.imgs->ourImages.size(); i++)
  675. CSDL_Ext::printAt("Random Map",25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap);
  676. ourScenSel->bOptions = Button<>(genRect(0,0,414,509),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  677. for (int i=0; i<ourScenSel->bOptions.imgs->ourImages.size(); i++)
  678. CSDL_Ext::printAt("Show Advanced Options",25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap);
  679. CPG=this;
  680. ourScenSel->mapsel.init();
  681. }
  682. void CPreGame::showScenSel()
  683. {
  684. state=EState::ScenarioList;
  685. SDL_BlitSurface(ourScenSel->background,NULL,ekran,NULL);
  686. SDL_BlitSurface(ourScenSel->scenInf,NULL,ekran,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,395,6));
  687. CSDL_Ext::printAt("Map Diff:",427,438,GEOR13);
  688. CSDL_Ext::printAt("Player Difficulty:",527,438,GEOR13);
  689. CSDL_Ext::printAt("Rating:",685,438,GEOR13);
  690. //blit buttons
  691. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bEasy.pos);
  692. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bNormal.pos);
  693. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bHard.pos);
  694. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bExpert.pos);
  695. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bImpossible.pos);
  696. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBegin.pos);
  697. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBack.pos);
  698. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  699. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  700. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  701. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  702. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  703. //add buttons info
  704. btns.push_back(&ourScenSel->bEasy);
  705. btns.push_back(&ourScenSel->bNormal);
  706. btns.push_back(&ourScenSel->bHard);
  707. btns.push_back(&ourScenSel->bExpert);
  708. btns.push_back(&ourScenSel->bImpossible);
  709. btns.push_back(&ourScenSel->bScens);
  710. btns.push_back(&ourScenSel->bRandom);
  711. btns.push_back(&ourScenSel->bOptions);
  712. btns.push_back(&ourScenSel->bBegin);
  713. btns.push_back(&ourScenSel->bBack);
  714. ourScenSel->selectedDiff=1;
  715. ourScenSel->bNormal.select(true);
  716. for (int i=0;i<btns.size();i++)
  717. {
  718. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  719. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  720. }
  721. handleOther = &CPreGame::scenHandleEv;
  722. ourScenSel->mapsel.printSelectedInfo();
  723. //SDL_Flip(ekran);
  724. CSDL_Ext::update(ekran);
  725. }
  726. void CPreGame::showOptions()
  727. {}
  728. void CPreGame::initNewMenu()
  729. {
  730. ourNewMenu = new menuItems();
  731. ourNewMenu->bgAd = CGI->bitmaph->loadBitmap("ZNEWGAM.bmp");
  732. ourNewMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp");
  733. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  734. slh = new CSemiLodHandler();
  735. slh->openLod("H3sprite.lod");
  736. //loading menu buttons
  737. ourNewMenu->newGame = CGI->spriteh->giveDef("ZTSINGL.DEF");
  738. ourNewMenu->loadGame = CGI->spriteh->giveDef("ZTMULTI.DEF");
  739. ourNewMenu->highScores = CGI->spriteh->giveDef("ZTCAMPN.DEF");
  740. ourNewMenu->credits = CGI->spriteh->giveDef("ZTTUTOR.DEF");
  741. ourNewMenu->quit = CGI->spriteh->giveDef("ZTBACK.DEF");
  742. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  743. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  744. // single scenario
  745. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  746. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  747. ourNewMenu->lNewGame.x=545;
  748. ourNewMenu->lNewGame.y=4;
  749. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  750. //multiplayer
  751. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  752. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  753. ourNewMenu->lLoadGame.x=568;
  754. ourNewMenu->lLoadGame.y=120;
  755. //campaign
  756. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  757. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  758. ourNewMenu->lHighScores.x=541;
  759. ourNewMenu->lHighScores.y=233;
  760. //tutorial
  761. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  762. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  763. ourNewMenu->lCredits.x=545;
  764. ourNewMenu->lCredits.y=358;
  765. //back
  766. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  767. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  768. ourNewMenu->lQuit.x=582;
  769. ourNewMenu->lQuit.y=464;
  770. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  771. ourNewMenu->highlighted=0;
  772. }
  773. void CPreGame::showNewMenu()
  774. {
  775. btns.clear();
  776. handleOther=NULL;
  777. state = EState::newGame;
  778. SDL_BlitSurface(ourNewMenu->background,NULL,ekran,NULL);
  779. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lNewGame);
  780. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lLoadGame);
  781. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lHighScores);
  782. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lCredits);
  783. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lQuit);
  784. //SDL_Flip(ekran);
  785. CSDL_Ext::update(ekran);
  786. }
  787. void CPreGame::initMainMenu()
  788. {
  789. ourMainMenu = new menuItems();
  790. ourMainMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  791. CSemiLodHandler * slh = new CSemiLodHandler();
  792. slh->openLod("H3sprite.lod");
  793. //loading menu buttons
  794. ourMainMenu->newGame = CGI->spriteh->giveDef("ZMENUNG.DEF");
  795. ourMainMenu->loadGame = CGI->spriteh->giveDef("ZMENULG.DEF");
  796. ourMainMenu->highScores = CGI->spriteh->giveDef("ZMENUHS.DEF");
  797. ourMainMenu->credits = CGI->spriteh->giveDef("ZMENUCR.DEF");
  798. ourMainMenu->quit = CGI->spriteh->giveDef("ZMENUQT.DEF");
  799. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  800. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  801. // new game button location
  802. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  803. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  804. ourMainMenu->lNewGame.x=540;
  805. ourMainMenu->lNewGame.y=10;
  806. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  807. //load game location
  808. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  809. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  810. ourMainMenu->lLoadGame.x=532;
  811. ourMainMenu->lLoadGame.y=132;
  812. //high scores
  813. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  814. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  815. ourMainMenu->lHighScores.x=524;
  816. ourMainMenu->lHighScores.y=251;
  817. //credits
  818. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  819. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  820. ourMainMenu->lCredits.x=557;
  821. ourMainMenu->lCredits.y=359;
  822. //quit
  823. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  824. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  825. ourMainMenu->lQuit.x=586;
  826. ourMainMenu->lQuit.y=468;
  827. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  828. ourMainMenu->highlighted=0;
  829. handledLods.push_back(slh);
  830. delete slh;
  831. }
  832. void CPreGame::showMainMenu()
  833. {
  834. state = EState::mainMenu;
  835. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  836. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  837. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  838. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  839. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  840. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  841. //SDL_Flip(ekran);
  842. CSDL_Ext::update(ekran);
  843. }
  844. void CPreGame::highlightButton(int which, int on)
  845. {
  846. menuItems * current = currentItems();
  847. switch (which)
  848. {
  849. case 1:
  850. {
  851. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,ekran,&current->lNewGame);
  852. break;
  853. }
  854. case 2:
  855. {
  856. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,ekran,&current->lLoadGame);
  857. break;
  858. }
  859. case 3:
  860. {
  861. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,ekran,&current->lHighScores);
  862. break;
  863. }
  864. case 4:
  865. {
  866. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,ekran,&current->lCredits);
  867. break;
  868. }
  869. case 5:
  870. {
  871. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,ekran,&current->lQuit);
  872. break;
  873. }
  874. }
  875. //SDL_Flip(ekran);
  876. CSDL_Ext::update(ekran);
  877. }
  878. void CPreGame::showCenBox (std::string data)
  879. {
  880. CMessage * cmh = new CMessage();
  881. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data));
  882. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  883. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  884. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  885. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  886. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  887. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  888. SDL_FreeSurface(infoBox);
  889. currentMessage = new SDL_Rect(pos);
  890. delete cmh;
  891. }
  892. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  893. {
  894. CMessage * cmh = new CMessage();
  895. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  896. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  897. przyciski->push_back(ok);
  898. przyciski->push_back(cancel);
  899. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), EWindowType::yesOrNO, przyciski, btnspos);
  900. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  901. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  902. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  903. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  904. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  905. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  906. SDL_FreeSurface(infoBox);
  907. currentMessage = new SDL_Rect(pos);
  908. (*btnspos)[0].x+=pos.x;
  909. (*btnspos)[0].y+=pos.y;
  910. (*btnspos)[1].x+=pos.x;
  911. (*btnspos)[1].y+=pos.y;
  912. btns.push_back(new Button<>((*btnspos)[0],&CPreGame::quit,ok,false, NULL,2));
  913. btns.push_back(new Button<>((*btnspos)[1],(&CPreGame::hideBox),cancel,false, NULL,2));
  914. delete cmh;
  915. delete przyciski;
  916. delete btnspos;
  917. }
  918. void CPreGame::hideBox ()
  919. {
  920. SDL_BlitSurface(behindCurMes,NULL,ekran,currentMessage);
  921. SDL_UpdateRect
  922. (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  923. for (int i=0;i<btns.size();i++)
  924. {
  925. if (btns[i]->ID==2)
  926. {
  927. delete btns[i];
  928. btns.erase(btns.begin()+i);
  929. i--;
  930. }
  931. }
  932. SDL_FreeSurface(behindCurMes);
  933. delete currentMessage;
  934. currentMessage = NULL;
  935. behindCurMes=NULL;
  936. }
  937. CPreGame::menuItems * CPreGame::currentItems()
  938. {
  939. switch (state)
  940. {
  941. case EState::mainMenu:
  942. return ourMainMenu;
  943. case EState::newGame:
  944. return ourNewMenu;
  945. default:
  946. return NULL;
  947. }
  948. }
  949. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  950. {
  951. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  952. {
  953. for (int i=0;i<btns.size(); i++)
  954. {
  955. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  956. {
  957. btns[i]->press(true);
  958. ourScenSel->pressed=btns[i];
  959. }
  960. }
  961. if (ourScenSel->listShowed && (sEvent.button.y>121) &&(sEvent.button.y<570)
  962. && (sEvent.button.x>55) && (sEvent.button.x<372))
  963. {
  964. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  965. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  966. }
  967. }
  968. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  969. {
  970. Button<> * prnr=ourScenSel->pressed;
  971. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  972. {
  973. ourScenSel->pressed->press(false);
  974. ourScenSel->pressed=NULL;
  975. }
  976. for (int i=0;i<btns.size(); i++)
  977. {
  978. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  979. {
  980. if (btns[i]->selectable)
  981. btns[i]->select(true);
  982. if (btns[i]->fun)
  983. (this->*(btns[i]->fun))();
  984. int zz = btns.size();
  985. if (i>=zz)
  986. break;
  987. if (btns[i]==prnr && btns[i]->type==2)
  988. {
  989. ((IntBut<> *)(btns[i]))->set();
  990. ourScenSel->mapsel.slid->whereAreWe=0;
  991. ourScenSel->mapsel.slid->updateSlid();
  992. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  993. ourScenSel->mapsel.printMaps(0);
  994. }
  995. }
  996. }
  997. }
  998. }
  999. void CPreGame::runLoop()
  1000. {
  1001. SDL_Event sEvent;
  1002. while(run)
  1003. {
  1004. try
  1005. {
  1006. if(SDL_PollEvent(&sEvent)) //wait for event...
  1007. {
  1008. menuItems * current = currentItems();
  1009. if(sEvent.type==SDL_QUIT)
  1010. return ;
  1011. for (int i=0;i<interested.size();i++)
  1012. interested[i]->handleIt(sEvent);
  1013. if (!current)
  1014. {
  1015. (this->*handleOther)(sEvent);
  1016. }
  1017. else if (sEvent.type==SDL_KEYDOWN)
  1018. {
  1019. if (sEvent.key.keysym.sym==SDLK_q)
  1020. {
  1021. return ;
  1022. break;
  1023. }
  1024. /*if (state==EState::newGame)
  1025. {
  1026. switch (sEvent.key.keysym.sym)
  1027. {
  1028. case SDLK_LEFT:
  1029. {
  1030. if(currentItems()->lNewGame.x>0)
  1031. currentItems()->lNewGame.x--;
  1032. break;
  1033. }
  1034. case (SDLK_RIGHT):
  1035. {
  1036. currentItems()->lNewGame.x++;
  1037. break;
  1038. }
  1039. case (SDLK_UP):
  1040. {
  1041. if(currentItems()->lNewGame.y>0)
  1042. currentItems()->lNewGame.y--;
  1043. break;
  1044. }
  1045. case (SDLK_DOWN):
  1046. {
  1047. currentItems()->lNewGame.y++;
  1048. break;
  1049. }
  1050. }
  1051. showNewMenu();
  1052. }*/
  1053. }
  1054. else if (sEvent.type==SDL_MOUSEMOTION)
  1055. {
  1056. if (currentMessage) continue;
  1057. if (current->highlighted)
  1058. {
  1059. switch (current->highlighted)
  1060. {
  1061. case 1:
  1062. {
  1063. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1064. continue;
  1065. else
  1066. {
  1067. current->highlighted=0;
  1068. highlightButton(1,0);
  1069. }
  1070. break;
  1071. }
  1072. case 2:
  1073. {
  1074. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1075. continue;
  1076. else
  1077. {
  1078. current->highlighted=0;
  1079. highlightButton(2,0);
  1080. }
  1081. break;
  1082. }
  1083. case 3:
  1084. {
  1085. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1086. continue;
  1087. else
  1088. {
  1089. current->highlighted=0;
  1090. highlightButton(3,0);
  1091. }
  1092. break;
  1093. }
  1094. case 4:
  1095. {
  1096. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1097. continue;
  1098. else
  1099. {
  1100. current->highlighted=0;
  1101. highlightButton(4,0);
  1102. }
  1103. break;
  1104. }
  1105. case 5:
  1106. {
  1107. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1108. continue;
  1109. else
  1110. {
  1111. current->highlighted=0;
  1112. highlightButton(5,0);
  1113. }
  1114. break;
  1115. }
  1116. } //switch (current->highlighted)
  1117. } // if (current->highlighted)
  1118. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1119. {
  1120. highlightButton(1,2);
  1121. current->highlighted=1;
  1122. }
  1123. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1124. {
  1125. highlightButton(2,2);
  1126. current->highlighted=2;
  1127. }
  1128. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1129. {
  1130. highlightButton(3,2);
  1131. current->highlighted=3;
  1132. }
  1133. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1134. {
  1135. highlightButton(4,2);
  1136. current->highlighted=4;
  1137. }
  1138. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1139. {
  1140. highlightButton(5,2);
  1141. current->highlighted=5;
  1142. }
  1143. }
  1144. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1145. {
  1146. mush->playClick();
  1147. for (int i=0;i<btns.size(); i++)
  1148. {
  1149. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1150. {
  1151. btns[i]->press(true);
  1152. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,ekran,&btns[i].pos);
  1153. //updateRect(&btns[i].pos);
  1154. }
  1155. }
  1156. if (currentMessage) continue;
  1157. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1158. {
  1159. highlightButton(1,1);
  1160. current->highlighted=1;
  1161. }
  1162. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1163. {
  1164. highlightButton(2,1);
  1165. current->highlighted=2;
  1166. }
  1167. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1168. {
  1169. highlightButton(3,1);
  1170. current->highlighted=3;
  1171. }
  1172. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1173. {
  1174. highlightButton(4,1);
  1175. current->highlighted=4;
  1176. }
  1177. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1178. {
  1179. highlightButton(5,1);
  1180. current->highlighted=5;
  1181. }
  1182. }
  1183. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1184. {
  1185. for (int i=0;i<btns.size(); i++)
  1186. {
  1187. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1188. (this->*(btns[i]->fun))();
  1189. else
  1190. {
  1191. btns[i]->press(false);
  1192. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,ekran,&btns[i].pos);
  1193. //updateRect(&btns[i].pos);
  1194. }
  1195. }
  1196. if (currentMessage) continue;
  1197. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1198. {
  1199. highlightButton(1,2);
  1200. current->highlighted=1;
  1201. (this->*(current->fNewGame))();
  1202. }
  1203. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1204. {
  1205. highlightButton(2,2);
  1206. current->highlighted=2;
  1207. }
  1208. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1209. {
  1210. highlightButton(3,2);
  1211. current->highlighted=3;
  1212. }
  1213. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1214. {
  1215. highlightButton(4,2);
  1216. current->highlighted=4;
  1217. }
  1218. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1219. {
  1220. highlightButton(5,2);
  1221. current->highlighted=5;
  1222. (this->*(current->fQuit))();
  1223. }
  1224. }
  1225. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  1226. {
  1227. if (currentMessage) continue;
  1228. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1229. {
  1230. showCenBox(buttonText(0));
  1231. }
  1232. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1233. {
  1234. showCenBox(buttonText(1));
  1235. }
  1236. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1237. {
  1238. showCenBox(buttonText(2));
  1239. }
  1240. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1241. {
  1242. showCenBox(buttonText(3));
  1243. }
  1244. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  1245. {
  1246. showCenBox(buttonText(4));
  1247. }
  1248. }
  1249. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  1250. {
  1251. hideBox();
  1252. }
  1253. }
  1254. }
  1255. catch(...)
  1256. { continue; }
  1257. SDL_Delay(1); //give time for other apps
  1258. }
  1259. }
  1260. std::string CPreGame::buttonText(int which)
  1261. {
  1262. if (state==EState::mainMenu)
  1263. {
  1264. switch (which)
  1265. {
  1266. case 0:
  1267. return preth->mainNewGame;
  1268. case 1:
  1269. return preth->mainLoadGame;
  1270. case 2:
  1271. return preth->mainHighScores;
  1272. case 3:
  1273. return preth->mainCredits;
  1274. case 4:
  1275. return preth->mainQuit;
  1276. }
  1277. }
  1278. else if (state==EState::newGame)
  1279. {
  1280. switch (which)
  1281. {
  1282. case 0:
  1283. return preth->ngSingleScenario;
  1284. case 1:
  1285. return preth->ngMultiplayer;
  1286. case 2:
  1287. return preth->ngCampain;
  1288. case 3:
  1289. return preth->ngTutorial;
  1290. case 4:
  1291. return preth->ngBack;
  1292. }
  1293. }
  1294. }
  1295. void CPreGame::quitAskBox()
  1296. {
  1297. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  1298. }
  1299. void CPreGame::sortMaps()
  1300. {
  1301. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  1302. ourScenSel->mapsel.select(0);
  1303. printMapsFrom(0);
  1304. }