CPreGame.cpp 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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. #include <cmath>
  13. #define CGI (CGameInfo::mainObj)
  14. extern SDL_Surface * ekran;
  15. extern SDL_Color tytulowy, tlo, zwykly ;
  16. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX;
  17. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  18. SDL_Color genRGB(int r, int g, int b, int a=0);
  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. bool isItIn(const SDL_Rect * rect, int x, int y);
  22. namespace fs = boost::filesystem;
  23. namespace s = CSDL_Ext;
  24. void HighButton::show()
  25. {
  26. blitAt(imgs->ourImages[state].bitmap,pos.x,pos.y);
  27. updateRect(&pos);
  28. }
  29. void HighButton::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. HighButton::press(down);
  42. }
  43. void HighButton::hover(bool on)
  44. {
  45. if (!highlightable) return;
  46. int i;
  47. if (on)
  48. {
  49. state=i=3;
  50. highlighted=true;
  51. }
  52. else
  53. {
  54. state=i=0;
  55. highlighted=false;
  56. }
  57. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  58. updateRect(&pos);
  59. }
  60. template <class T> void Button<T>::hover(bool on=true)
  61. {
  62. HighButton::hover(on);
  63. }
  64. template <class T> void Button<T>::select(bool on)
  65. {
  66. int i;
  67. if (on) state=i=3;
  68. else state=i=0;
  69. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  70. updateRect(&pos);
  71. if (ourGroup && on && ourGroup->type==1)
  72. {
  73. if (ourGroup->selected && ourGroup->selected!=this)
  74. ourGroup->selected->select(false);
  75. ourGroup->selected =this;
  76. }
  77. }
  78. //void Slider::clickDown(int x, int y, bool bzgl=true);
  79. //void Slider::clickUp(int x, int y, bool bzgl=true);
  80. //void Slider::mMove(int x, int y, bool bzgl=true);
  81. void Slider<>::updateSlid()
  82. {
  83. float perc = ((float)whereAreWe)/((float)positionsAmnt-capacity);
  84. float myh;
  85. if (vertical)
  86. {
  87. myh=perc*((float)pos.h-48)+pos.y+16;
  88. SDL_FillRect(ekran,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0);
  89. blitAt(slider.imgs->ourImages[0].bitmap,pos.x,(int)myh);
  90. slider.pos.y=(int)myh;
  91. }
  92. else
  93. {
  94. myh=perc*((float)pos.w-48)+pos.x+16;
  95. SDL_FillRect(ekran,&genRect(pos.h,pos.w-32,pos.x+16,pos.y),0);
  96. blitAt(slider.imgs->ourImages[0].bitmap,(int)myh,pos.y);
  97. slider.pos.x=(int)myh;
  98. }
  99. updateRect(&pos);
  100. }
  101. void Slider<>::moveDown()
  102. {
  103. if (whereAreWe<positionsAmnt-capacity)
  104. (CPG->*fun)(++whereAreWe);
  105. updateSlid();
  106. }
  107. void Slider<>::moveUp()
  108. {
  109. if (whereAreWe>0)
  110. (CPG->*fun)(--whereAreWe);
  111. updateSlid();
  112. }
  113. //void Slider::moveByOne(bool up);
  114. Slider<>::Slider(int x, int y, int h, int amnt, int cap, bool ver)
  115. {
  116. vertical=ver;
  117. positionsAmnt = amnt;
  118. capacity = cap;
  119. if (ver)
  120. {
  121. pos = genRect(h,16,x,y);
  122. down = Button<void(Slider::*)()>(genRect(16,16,x,y+h-16),&Slider::moveDown,CGI->spriteh->giveDef("SCNRBDN.DEF"),false);
  123. up = Button<void(Slider::*)()>(genRect(16,16,x,y),&Slider::moveUp,CGI->spriteh->giveDef("SCNRBUP.DEF"),false);
  124. slider = Button<void(Slider::*)()>(genRect(16,16,x,y+16),NULL,CGI->spriteh->giveDef("SCNRBSL.DEF"),false);
  125. }
  126. else
  127. {
  128. pos = genRect(16,h,x,y);
  129. down = Button<void(Slider::*)()>(genRect(16,16,x+h-16,y),&Slider::moveDown,CGI->spriteh->giveDef("SCNRBRT.DEF"),false);
  130. up = Button<void(Slider::*)()>(genRect(16,16,x,y),&Slider::moveUp,CGI->spriteh->giveDef("SCNRBLF.DEF"),false);
  131. slider = Button<void(Slider::*)()>(genRect(16,16,x+16,y),NULL,CGI->spriteh->giveDef("SCNRBSL.DEF"),false);
  132. }
  133. moving = false;
  134. whereAreWe=0;
  135. }
  136. void Slider<>::deactivate()
  137. {
  138. CPG->interested.erase(std::find(CPG->interested.begin(),CPG->interested.end(),this));
  139. }
  140. void Slider<>::activate()
  141. {
  142. SDL_FillRect(ekran,&pos,0);
  143. up.show();
  144. down.show();
  145. slider.show();
  146. //SDL_Flip(ekran);
  147. CSDL_Ext::update(ekran);
  148. CPG->interested.push_back(this);
  149. }
  150. void Slider<>::handleIt(SDL_Event sEvent)
  151. {
  152. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  153. {
  154. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  155. {
  156. down.press();
  157. }
  158. else if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  159. {
  160. up.press();
  161. }
  162. else if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  163. {
  164. //slider.press();
  165. moving=true;
  166. }
  167. else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y))
  168. {
  169. float dy;
  170. float pe;
  171. if (vertical)
  172. {
  173. dy = sEvent.motion.y-pos.y-16;
  174. pe = dy/((float)(pos.h-32));
  175. if (pe>1) pe=1;
  176. if (pe<0) pe=0;
  177. }
  178. else
  179. {
  180. dy = sEvent.motion.x-pos.x-16;
  181. pe = dy/((float)(pos.w-32));
  182. if (pe>1) pe=1;
  183. if (pe<0) pe=0;
  184. }
  185. whereAreWe = pe*(positionsAmnt-capacity);
  186. if (whereAreWe<0)whereAreWe=0;
  187. updateSlid();
  188. (CPG->*fun)(whereAreWe);
  189. }
  190. }
  191. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  192. {
  193. if ((down.state==1) && isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  194. {
  195. (this->*down.fun)();
  196. }
  197. if ((up.state==1) && isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  198. {
  199. (this->*up.fun)();
  200. }
  201. if (down.state==1) down.press(false);
  202. if (up.state==1) up.press(false);
  203. if (moving)
  204. {
  205. //slider.press();
  206. moving=false;
  207. }
  208. }
  209. else if (sEvent.type==SDL_KEYDOWN)
  210. {
  211. switch (sEvent.key.keysym.sym)
  212. {
  213. case (SDLK_UP):
  214. CPG->ourScenSel->mapsel.moveByOne(true);
  215. break;
  216. case (SDLK_DOWN):
  217. CPG->ourScenSel->mapsel.moveByOne(false);
  218. break;
  219. }
  220. }
  221. else if (moving && sEvent.type==SDL_MOUSEMOTION)
  222. {
  223. if (isItIn(&genRect(pos.h,pos.w+64,pos.x-32,pos.y),sEvent.motion.x,sEvent.motion.y))
  224. {
  225. int my;
  226. int all;
  227. float ile;
  228. if (vertical)
  229. {
  230. my = sEvent.motion.y-(pos.y+16);
  231. all =pos.h-48;
  232. ile = (float)my / (float)all;
  233. if (ile>1) ile=1;
  234. if (ile<0) ile=0;
  235. }
  236. else
  237. {
  238. my = sEvent.motion.x-(pos.x+16);
  239. all =pos.w-48;
  240. ile = (float)my / (float)all;
  241. if (ile>1) ile=1;
  242. if (ile<0) ile=0;
  243. }
  244. int ktory = ile*(positionsAmnt-capacity);
  245. if (ktory!=whereAreWe)
  246. {
  247. whereAreWe=ktory;
  248. updateSlid();
  249. }
  250. (CPG->*fun)(whereAreWe);
  251. }
  252. }
  253. /*else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  254. {
  255. if (ourScenSel->pressed)
  256. {
  257. ourScenSel->pressed->press(false);
  258. ourScenSel->pressed=NULL;
  259. }
  260. for (int i=0;i<btns.size(); i++)
  261. {
  262. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  263. {
  264. if (btns[i]->selectable)
  265. btns[i]->select(true);
  266. if (btns[i]->fun)
  267. (this->*(btns[i]->fun))();
  268. return;
  269. }
  270. }
  271. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  272. {
  273. (this->*down.fun)();
  274. }
  275. if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  276. {
  277. (this->*up.fun)();
  278. }
  279. if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  280. {
  281. (this->*slider.fun)();
  282. }
  283. }*/
  284. }
  285. /********************************************************************************************/
  286. void PreGameTab::show()
  287. {
  288. if (CPG->currentTab)
  289. CPG->currentTab->hide();
  290. showed=true;
  291. CPG->currentTab=this;
  292. }
  293. void PreGameTab::hide()
  294. {
  295. showed=false;
  296. CPG->currentTab=NULL;
  297. }
  298. PreGameTab::PreGameTab()
  299. {
  300. showed=false;
  301. }
  302. /********************************************************************************************/
  303. Options::PlayerOptions::PlayerOptions(int serial, int player)
  304. :Cleft(genRect(24,11,164,133+serial*50),CPG->ourOptions->left,true,-1), //left castle arrow
  305. Cright(genRect(24,11,225,133+serial*50),CPG->ourOptions->right,false,-1), //right castle arrow
  306. Hleft(genRect(24,11,240,133+serial*50),CPG->ourOptions->left,true,0), //left hero arrow
  307. Hright(genRect(24,11,301,133+serial*50),CPG->ourOptions->right,false,0), //right hero arrow
  308. Bleft(genRect(24,11,316,133+serial*50),CPG->ourOptions->left,true,1), //left bonus arrow
  309. Bright(genRect(24,11,377,133+serial*50),CPG->ourOptions->right,false,1), //right bonus arrow
  310. flag(genRect(50,42,14,130+serial*50),CPG->ourOptions->flags[player],player)
  311. {
  312. Bleft.playerID=Bright.playerID=Hleft.playerID=Hright.playerID=Cleft.playerID=Cright.playerID=player;
  313. Bleft.serialID=Bright.serialID=Hleft.serialID=Hright.serialID=Cleft.serialID=Cright.serialID=serial;
  314. }
  315. void Options::OptionSwitch::press(bool down)
  316. {
  317. HighButton::press(down);
  318. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[serialID];
  319. PlayerInfo * ourInf = &CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].players[playerID];
  320. int dir = (left) ? (-1) : (1);
  321. if (down) return;
  322. switch (which)
  323. {
  324. case -1:
  325. {
  326. if (ourOpt->castle==-2) //no castle - no change
  327. return;
  328. else if (ourOpt->castle==-1) //random => first/last available
  329. {
  330. int pom = (left) ? (F_NUMBER-1) : (0); // last or first
  331. for (;pom>=0 && pom<F_NUMBER;pom+=dir)
  332. {
  333. if (((int)pow((double)2,pom))&ourInf->allowedFactions)
  334. {
  335. ourOpt->castle=pom;
  336. break;
  337. }
  338. else continue;
  339. }
  340. }
  341. else
  342. {
  343. ourOpt->castle+=dir;
  344. if (ourOpt->castle>=F_NUMBER || ourOpt->castle<0)
  345. {
  346. ourOpt->castle=-1;
  347. ourOpt->hero = -1;
  348. CPG->ourOptions->showIcon(0, serialID, false);
  349. if( ourOpt->bonus == bresource )
  350. {
  351. ourOpt->bonus = brandom;
  352. CPG->ourOptions->showIcon(1, serialID, false);
  353. }
  354. }
  355. break;
  356. }
  357. break;
  358. }
  359. case 0:
  360. {
  361. if (ourOpt->castle<0)
  362. {
  363. break;
  364. }
  365. if (ourOpt->hero==-2) //no hero - no change
  366. return;
  367. else if (ourOpt->hero==-1) //random => first/last available
  368. {
  369. int pom = (left) ? (ourOpt->castle*HEROES_PER_TYPE*2+15) : (ourOpt->castle*HEROES_PER_TYPE*2); // last or first
  370. for (;pom>=0 && pom<CGI->heroh->heroes.size();pom+=dir)
  371. {
  372. //TODO: checked allowed heroes
  373. {
  374. ourOpt->hero=pom;
  375. break;
  376. }
  377. }
  378. }
  379. else
  380. {
  381. ourOpt->hero+=dir;
  382. if (ourOpt->hero>HEROES_PER_TYPE*ourOpt->castle*2+15 || ourOpt->hero<HEROES_PER_TYPE*ourOpt->castle*2)
  383. {
  384. ourOpt->hero=-1;
  385. if( ourOpt->bonus == bartifact )
  386. {
  387. ourOpt->bonus = brandom;
  388. CPG->ourOptions->showIcon(1, serialID, false);
  389. }
  390. }
  391. break;
  392. }
  393. break;
  394. }
  395. case 1:
  396. {
  397. if (dir>0 && ourOpt->bonus==bresource)
  398. ourOpt->bonus=brandom;
  399. else if (dir<0 && ourOpt->bonus==brandom)
  400. ourOpt->bonus=bresource;
  401. else ourOpt->bonus=(Ebonus)(ourOpt->bonus+dir);
  402. if (ourOpt->hero==-2 && ourOpt->bonus==bartifact) //no hero - can't be artifact
  403. {
  404. if (dir>0)
  405. ourOpt->bonus=brandom;
  406. else ourOpt->bonus=bgold;
  407. }
  408. if (ourOpt->castle==-1 && ourOpt->bonus==bresource)
  409. {
  410. if (dir<0)
  411. ourOpt->bonus=bgold;
  412. else ourOpt->bonus=brandom;
  413. }
  414. break;
  415. }
  416. }
  417. CPG->ourOptions->showIcon(which,serialID,false);
  418. }
  419. void Options::PlayerFlag::press(bool down)
  420. {
  421. HighButton::press(down);
  422. };
  423. void Options::PlayerFlag::hover(bool on)
  424. {
  425. HighButton::hover(on);
  426. }
  427. void Options::showIcon (int what, int nr, bool abs) //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  428. {
  429. if (what==-2)
  430. {
  431. showIcon(-1,nr,abs);
  432. showIcon(0,nr,abs);
  433. showIcon(1,nr,abs);
  434. }
  435. int ab, se;
  436. if (!abs)
  437. {
  438. ab = CPG->ret.playerInfos[nr].color;
  439. se = nr;
  440. }
  441. else
  442. {
  443. ab = nr;
  444. for (int i=0; i<CPG->ret.playerInfos.size();i++)
  445. {
  446. if (CPG->ret.playerInfos[i].color==nr)
  447. {
  448. se=i;
  449. break;
  450. }
  451. }
  452. }
  453. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[se];
  454. switch (what)
  455. {
  456. case -1:
  457. {
  458. int pom=ourOpt->castle;
  459. if (ourOpt->castle<F_NUMBER && ourOpt->castle>=0)
  460. {
  461. blitAtWR(CGI->townh->getPic(ourOpt->castle,true,false),176,130+50*se);
  462. }
  463. else if (ourOpt->castle==-1)
  464. {
  465. blitAtWR(CPG->ourOptions->rCastle,176,130+50*se);
  466. }
  467. else if (ourOpt->castle==-2)
  468. {
  469. blitAtWR(CPG->ourOptions->nCastle,176,130+50*se);
  470. }
  471. break;
  472. }
  473. case 0:
  474. {
  475. int pom=ourOpt->hero;
  476. if (ourOpt->hero==-1)
  477. {
  478. blitAtWR(CPG->ourOptions->rHero,252,130+50*se);
  479. }
  480. else if (ourOpt->hero==-2)
  481. {
  482. blitAtWR(CPG->ourOptions->nHero,252,130+50*se);
  483. }
  484. else
  485. {
  486. blitAtWR(CGI->heroh->heroes[pom]->portraitSmall,252,130+50*se);
  487. }
  488. break;
  489. }
  490. case 1:
  491. {
  492. int pom;
  493. switch (ourOpt->bonus)
  494. {
  495. case -1:
  496. pom=10;
  497. break;
  498. case 0:
  499. pom=9;
  500. break;
  501. case 1:
  502. pom=8;
  503. break;
  504. case 2:
  505. pom=CGI->townh->towns[ourOpt->castle].bonus;
  506. break;
  507. }
  508. blitAtWR(bonuses->ourImages[pom].bitmap,328,130+50*se);
  509. break;
  510. }
  511. }
  512. }
  513. Options::~Options()
  514. {
  515. if (!inited) return;
  516. for (int i=0; i<bgs.size();i++)
  517. SDL_FreeSurface(bgs[i]);
  518. for (int i=0; i<flags.size();i++)
  519. delete flags[i];
  520. SDL_FreeSurface(bg);
  521. SDL_FreeSurface(rHero);
  522. SDL_FreeSurface(rCastle);
  523. SDL_FreeSurface(nHero);
  524. SDL_FreeSurface(nCastle);
  525. delete turnLength;
  526. delete left;
  527. delete right;
  528. delete bonuses;
  529. }
  530. void Options::init()
  531. {
  532. inited=true;
  533. bg = CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  534. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  535. left = CGI->spriteh->giveDef("ADOPLFA.DEF");
  536. right = CGI->spriteh->giveDef("ADOPRTA.DEF");
  537. bonuses = CGI->spriteh->giveDef("SCNRSTAR.DEF");
  538. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPRPNL.bmp"));
  539. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPBPNL.bmp"));
  540. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPYPNL.bmp"));
  541. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPGPNL.bmp"));
  542. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPOPNL.bmp"));
  543. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPPPNL.bmp"));
  544. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPTPNL.bmp"));
  545. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPSPNL.bmp"));
  546. rHero = CGI->bitmaph->loadBitmap("HPSRAND1.bmp");
  547. rCastle = CGI->bitmaph->loadBitmap("HPSRAND0.bmp");
  548. nHero = CGI->bitmaph->loadBitmap("HPSRAND6.bmp");
  549. nCastle = CGI->bitmaph->loadBitmap("HPSRAND5.bmp");
  550. turnLength = new Slider<>(57,559,195,11,1,false);
  551. turnLength->fun=&CPreGame::setTurnLength;
  552. flags.push_back(CGI->spriteh->giveDef("AOFLGBR.DEF"));
  553. flags.push_back(CGI->spriteh->giveDef("AOFLGBB.DEF"));
  554. flags.push_back(CGI->spriteh->giveDef("AOFLGBY.DEF"));
  555. flags.push_back(CGI->spriteh->giveDef("AOFLGBG.DEF"));
  556. flags.push_back(CGI->spriteh->giveDef("AOFLGBO.DEF"));
  557. flags.push_back(CGI->spriteh->giveDef("AOFLGBP.DEF"));
  558. flags.push_back(CGI->spriteh->giveDef("AOFLGBT.DEF"));
  559. flags.push_back(CGI->spriteh->giveDef("AOFLGBS.DEF"));
  560. }
  561. void Options::show()
  562. {
  563. if (showed)return;
  564. PreGameTab::show();
  565. MapSel & ms = CPG->ourScenSel->mapsel;
  566. blitAt(bg,3,6);
  567. CPG->ourScenSel->listShowed=false;
  568. for (int i=0;i<CPG->btns.size();i++)
  569. {
  570. if (CPG->btns[i]->ID!=10) //leave only right panel buttons
  571. {
  572. CPG->btns.erase(CPG->btns.begin()+i);
  573. i--;
  574. }
  575. }
  576. CPG->interested.clear();
  577. CSDL_Ext::printAtMiddle("Advanced Options",225,35,GEORXX);
  578. int playersSoFar=0;
  579. for (int i=0;i<PLAYER_LIMIT;i++)
  580. {
  581. if (!(ms.ourMaps[ms.selected].players[i].canComputerPlay || ms.ourMaps[ms.selected].players[i].canComputerPlay))
  582. continue;
  583. blitAt(bgs[i],57,128+playersSoFar*50);
  584. poptions.push_back(new PlayerOptions(playersSoFar,i));
  585. poptions[poptions.size()-1]->nr=playersSoFar;
  586. poptions[poptions.size()-1]->color=(Ecolor)i;
  587. poptions[poptions.size()-1]->Cleft.show();
  588. poptions[poptions.size()-1]->Cright.show();
  589. poptions[poptions.size()-1]->Hleft.show();
  590. poptions[poptions.size()-1]->Hright.show();
  591. poptions[poptions.size()-1]->Bleft.show();
  592. poptions[poptions.size()-1]->Bright.show();
  593. poptions[poptions.size()-1]->flag.show();
  594. CPG->btns.push_back(&poptions[poptions.size()-1]->Cleft);
  595. CPG->btns.push_back(&poptions[poptions.size()-1]->Cright);
  596. CPG->btns.push_back(&poptions[poptions.size()-1]->Hleft);
  597. CPG->btns.push_back(&poptions[poptions.size()-1]->Hright);
  598. CPG->btns.push_back(&poptions[poptions.size()-1]->Bleft);
  599. CPG->btns.push_back(&poptions[poptions.size()-1]->Bright);
  600. CPG->btns.push_back(&poptions[poptions.size()-1]->flag);
  601. playersSoFar++;
  602. }
  603. turnLength->activate();
  604. for (int i=0;i<poptions.size();i++)
  605. showIcon(-2,i,false);
  606. SDL_Flip(ekran);
  607. }
  608. void Options::hide()
  609. {
  610. if (!showed) return;
  611. PreGameTab::hide();
  612. for (int i=0; i<CPG->btns.size();i++)
  613. if (CPG->btns[i]->ID==7)
  614. CPG->btns.erase(CPG->btns.begin()+i--);
  615. for (int i=0;i<poptions.size();i++)
  616. delete poptions[i];
  617. poptions.clear();
  618. turnLength->deactivate();
  619. }
  620. MapSel::~MapSel()
  621. {
  622. SDL_FreeSurface(bg);
  623. for (int i=0;i<scenImgs.size();i++)
  624. SDL_FreeSurface(scenImgs[i]);
  625. for (int i=0;i<scenList.size();i++)
  626. delete scenList[i];
  627. }
  628. int MapSel::countWL()
  629. {
  630. int ret=0;
  631. for (int i=0;i<ourMaps.size();i++)
  632. {
  633. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  634. continue;
  635. else ret++;
  636. }
  637. return ret;
  638. }
  639. void MapSel::printMaps(int from, int to, int at, bool abs)
  640. {
  641. if (true)//
  642. {
  643. int help=-1;
  644. for (int i=0;i<ourMaps.size();i++)
  645. {
  646. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  647. continue;
  648. else help++;
  649. if (help==from)
  650. {
  651. from=i;
  652. break;
  653. }
  654. }
  655. }
  656. SDL_Surface * scenin = SDL_CreateRGBSurface(ekran->flags,351,25,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  657. SDL_Color nasz;
  658. for (int i=at;i<to;i++)
  659. {
  660. if ((i-at+from) > ourMaps.size()-1)
  661. {
  662. SDL_Surface * scenin = SDL_CreateRGBSurface(ekran->flags,351,25,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  663. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  664. blitAt(scenin,24,121+(i-at)*25);
  665. //SDL_Flip(ekran);
  666. CSDL_Ext::update(ekran);
  667. SDL_FreeSurface(scenin);
  668. continue;
  669. }
  670. if (sizeFilter && ((ourMaps[(i-at)+from].width) != sizeFilter))
  671. {
  672. to++;
  673. at++;
  674. from++;
  675. if (((i-at)+from)>ourMaps.size()-1) break;
  676. else continue;
  677. }
  678. if ((i-at+from) == selected)
  679. nasz=tytulowy;
  680. else nasz=zwykly;
  681. //SDL_Rect pier = genRect(25,351,24,126+(i*25));
  682. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  683. int temp=-1;
  684. std::ostringstream ostr(std::ostringstream::out); ostr << ourMaps[(i-at)+from].playerAmnt << "/" << ourMaps[(i-at)+from].humenPlayers;
  685. CSDL_Ext::printAt(ostr.str(),6,4,GEOR13,nasz,scenin, 2);
  686. std::string temp2;
  687. switch (ourMaps[(i-at)+from].width)
  688. {
  689. case 36:
  690. temp2="S";
  691. break;
  692. case 72:
  693. temp2="M";
  694. break;
  695. case 108:
  696. temp2="L";
  697. break;
  698. case 144:
  699. temp2="XL";
  700. break;
  701. default:
  702. temp2="C";
  703. break;
  704. }
  705. CSDL_Ext::printAtMiddle(temp2,50,13,GEOR13,nasz,scenin, 2);
  706. switch (ourMaps[(i-at)+from].version)
  707. {
  708. case Eformat::SoD:
  709. temp=2;
  710. break;
  711. case Eformat::WoG:
  712. temp=3;
  713. break;
  714. }
  715. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  716. if (!(ourMaps[(i-at)+from].name.length()))
  717. ourMaps[(i-at)+from].name = "Unnamed";
  718. CSDL_Ext::printAtMiddle(ourMaps[(i-at)+from].name,192,13,GEOR13,nasz,scenin, 2);
  719. if (ourMaps[(i-at)+from].victoryCondition==EvictoryConditions::winStandard)
  720. temp=11;
  721. else temp=ourMaps[(i-at)+from].victoryCondition;
  722. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  723. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == ElossCon::lossStandard)
  724. temp=3;
  725. else temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  726. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  727. blitAt(scenin,24,121+(i-at)*25);
  728. SDL_UpdateRect(ekran,24,121+(i-at)*25,scenin->w,scenin->h);
  729. }
  730. SDL_FreeSurface(scenin);
  731. }
  732. int MapSel::whichWL(int nr)
  733. {
  734. int help=-1;
  735. for (int i=0;i<ourMaps.size();i++)
  736. {
  737. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  738. continue;
  739. else help++;
  740. if (help==nr)
  741. {
  742. help=i;
  743. break;
  744. }
  745. }
  746. return help;
  747. }
  748. void MapSel::hide()
  749. {
  750. if (!showed)return;
  751. PreGameTab::hide();
  752. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  753. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  754. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  755. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  756. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  757. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  758. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  759. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  760. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  761. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  762. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  763. slid->deactivate();
  764. };
  765. void MapSel::show()
  766. {
  767. if (showed)return;
  768. PreGameTab::show();
  769. //blit bg
  770. blitAt(bg,3,6);
  771. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  772. CSDL_Ext::printAt("Select a Scenario to Play",110,25,TNRB16);
  773. //size buttons
  774. small.show();
  775. medium.show();
  776. large.show();
  777. xlarge.show();
  778. all.show();
  779. CPG->btns.push_back(&small);
  780. CPG->btns.push_back(&medium);
  781. CPG->btns.push_back(&large);
  782. CPG->btns.push_back(&xlarge);
  783. CPG->btns.push_back(&all);
  784. //sort by buttons
  785. nrplayer.show();
  786. mapsize.show();
  787. type.show();
  788. name.show();
  789. viccon.show();
  790. loscon.show();
  791. CPG->btns.push_back(&nrplayer);
  792. CPG->btns.push_back(&mapsize);
  793. CPG->btns.push_back(&type);
  794. CPG->btns.push_back(&name);
  795. CPG->btns.push_back(&viccon);
  796. CPG->btns.push_back(&loscon);
  797. //print scenario list
  798. printMaps(0,18);
  799. slid->activate();
  800. //SDL_Flip(ekran);
  801. CSDL_Ext::update(ekran);
  802. }
  803. void MapSel::init()
  804. {
  805. bg = CGI->bitmaph->loadBitmap("SCSELBCK.bmp");
  806. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  807. small.imgs = CGI->spriteh->giveDef("SCSMBUT.DEF");
  808. small.fun = NULL;
  809. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  810. small.ourGroup=NULL;
  811. medium.imgs = CGI->spriteh->giveDef("SCMDBUT.DEF");
  812. medium.fun = NULL;
  813. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  814. medium.ourGroup=NULL;
  815. large.imgs = CGI->spriteh->giveDef("SCLGBUT.DEF");
  816. large.fun = NULL;
  817. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  818. large.ourGroup=NULL;
  819. xlarge.imgs = CGI->spriteh->giveDef("SCXLBUT.DEF");
  820. xlarge.fun = NULL;
  821. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  822. xlarge.ourGroup=NULL;
  823. all.imgs = CGI->spriteh->giveDef("SCALBUT.DEF");
  824. all.fun = NULL;
  825. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  826. all.ourGroup=NULL;
  827. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  828. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  829. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  830. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  831. nrplayer.imgs = CGI->spriteh->giveDef("SCBUTT1.DEF");
  832. nrplayer.fun = NULL;
  833. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  834. nrplayer.key=ESortBy::playerAm;
  835. mapsize.imgs = CGI->spriteh->giveDef("SCBUTT2.DEF");
  836. mapsize.fun = NULL;
  837. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  838. mapsize.key=ESortBy::size;
  839. type.imgs = CGI->spriteh->giveDef("SCBUTCP.DEF");
  840. type.fun = NULL;
  841. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  842. type.key=ESortBy::format;
  843. name.imgs = CGI->spriteh->giveDef("SCBUTT3.DEF");
  844. name.fun = NULL;
  845. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  846. name.key=ESortBy::name;
  847. viccon.imgs = CGI->spriteh->giveDef("SCBUTT4.DEF");
  848. viccon.fun = NULL;
  849. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  850. viccon.key=ESortBy::viccon;
  851. loscon.imgs = CGI->spriteh->giveDef("SCBUTT5.DEF");
  852. loscon.fun = NULL;
  853. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  854. loscon.key=ESortBy::loscon;
  855. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  856. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=&CPreGame::sortMaps;
  857. Dtypes = CGI->spriteh->giveDef("SCSELC.DEF");
  858. Dvic = CGI->spriteh->giveDef("SCNRVICT.DEF");
  859. Dloss = CGI->spriteh->giveDef("SCNRLOSS.DEF");
  860. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  861. Dsizes = CGI->spriteh->giveDef("SCNRMPSZ.DEF");
  862. //get map files names
  863. std::vector<std::string> pliczkiTemp;
  864. fs::path tie( (fs::initial_path<fs::path>())/"\maps" );
  865. fs::directory_iterator end_iter;
  866. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  867. {
  868. if (fs::is_regular(dir->status()));
  869. {
  870. if (boost::ends_with(dir->path().leaf(),std::string(".h3m")))
  871. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  872. }
  873. }
  874. for (int i=0; i<pliczkiTemp.size();i++)
  875. {
  876. gzFile tempf = gzopen(pliczkiTemp[i].c_str(),"rb");
  877. std::string sss;
  878. int iii=0;
  879. while(++iii)
  880. {
  881. if (iii>3300) break;
  882. int z = gzgetc (tempf);
  883. if (z>=0)
  884. {
  885. sss+=unsigned char(z);
  886. }
  887. else break;
  888. }
  889. gzclose(tempf);
  890. if (sss[0]<28) continue; //zly format
  891. if (!sss[4]) continue; //nie ma graczy? mapa niegrywalna //już to kiedyś komentowałem- - to bzdura //tu calkiem pasuje...
  892. unsigned char* file2 = new unsigned char[sss.length()];
  893. for (int j=0;j<sss.length();j++)
  894. file2[j]=sss[j];
  895. ourMaps.push_back(CMapInfo(pliczkiTemp[i],file2));
  896. delete file2;
  897. }
  898. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(ESortBy::name));
  899. slid = new Slider<>(375,92,480,ourMaps.size(),18,true);
  900. slid->fun = &CPreGame::printMapsFrom;
  901. }
  902. void MapSel::moveByOne(bool up)
  903. {
  904. int help=selected;
  905. if (up) selected--;
  906. else selected ++;
  907. for (int i=selected;i<ourMaps.size() && i>=0;)
  908. {
  909. help=i;
  910. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  911. break;
  912. if (up)
  913. {
  914. i--;
  915. }
  916. else
  917. {
  918. i++;
  919. if (i<0) break;
  920. }
  921. }
  922. select(help);
  923. slid->updateSlid();
  924. }
  925. void MapSel::select(int which, bool updateMapsList)
  926. {
  927. selected = which;
  928. if(updateMapsList)printMaps(slid->whereAreWe,18,0,true);
  929. printSelectedInfo();
  930. CPG->ret.playerInfos.clear();
  931. for (int i=0;i<PLAYER_LIMIT;i++)
  932. {
  933. if (!(ourMaps[selected].players[i].canComputerPlay || ourMaps[selected].players[i].canComputerPlay))
  934. continue; // this caused some serious problems becouse of lack of simple bijection between two sets of player's numbers (one is returned by CPreGame, second is used in h3m)
  935. StartInfo::PlayerSettings pset;
  936. pset.color=(Ecolor)i;
  937. pset.bonus=Ebonus::brandom;
  938. pset.castle=-2;
  939. if (ourMaps[which].players[i].canHumanPlay)
  940. {
  941. pset.name="Player";
  942. }
  943. else
  944. {
  945. pset.name="AI";
  946. }
  947. if ( (ourMaps[which].players[i].heroesNames.size()>1) ||
  948. (((ourMaps[which].players[i].heroesNames.size()>1)==1) && ourMaps[which].players[i].generateHeroAtMainTown==false) )
  949. pset.hero=-2;
  950. else if (ourMaps[which].players[i].heroesNames.size()==1)
  951. {
  952. pset.hero=ourMaps[which].players[i].heroesNames[0].heroID;
  953. }
  954. else
  955. pset.hero=-1;
  956. for (int j=0;j<F_NUMBER;j++)
  957. {
  958. if (((int)pow((double)2,j))&ourMaps[selected].players[i].allowedFactions)
  959. {
  960. if (pset.castle>=0)
  961. pset.castle=-1;
  962. if (pset.castle==-2)
  963. pset.castle=j;
  964. }
  965. }
  966. if (ourMaps[selected].players[i].generateHeroAtMainTown || ourMaps[selected].players[i].generateHero)
  967. pset.hero=-1;
  968. else if (ourMaps[selected].players[i].mainHeroName.length())
  969. {
  970. pset.heroName=ourMaps[selected].players[i].mainHeroName;
  971. pset.heroPortrait=ourMaps[selected].players[i].mainHeroPortrait;
  972. }
  973. else
  974. pset.hero=-2;
  975. pset.handicap=0;
  976. pset.heroPortrait=-1;
  977. CPG->ret.playerInfos.push_back(pset);
  978. }
  979. }
  980. MapSel::MapSel():selected(0),sizeFilter(0)
  981. {
  982. }
  983. void MapSel::printSelectedInfo()
  984. {
  985. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),ekran,&genRect(399,337,412,29));
  986. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),ekran,&genRect(50,91,413,453));
  987. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bScens.pos);
  988. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bOptions.pos);
  989. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bRandom.pos);
  990. //blit texts
  991. CSDL_Ext::printAt(CPG->preth->singleScenarioName,420,25,GEOR13);
  992. CSDL_Ext::printAt("Scenario Description:",420,135,GEOR13);
  993. CSDL_Ext::printAt("Victory Condition:",420,285,GEOR13);
  994. CSDL_Ext::printAt("Loss Condition:",420,340,GEOR13);
  995. CSDL_Ext::printAt("Allies:",420,406,GEOR13,zwykly);
  996. CSDL_Ext::printAt("Enemies:",585,406,GEOR13,zwykly);
  997. int temp = ourMaps[selected].victoryCondition+1;
  998. if (temp>20) temp=0;
  999. std::string sss = CPG->preth->victoryConditions[temp];
  1000. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0];
  1001. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1002. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  1003. if (temp>20) temp=0;
  1004. sss = CPG->preth->lossCondtions[temp];
  1005. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1006. //blit descrption
  1007. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  1008. for (int i=0;i<desc.size();i++)
  1009. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1010. if ((selected < 0) || (selected >= ourMaps.size()))
  1011. return;
  1012. if (ourMaps[selected].name.length())
  1013. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  1014. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1015. std::string diff;
  1016. switch (ourMaps[selected].difficulty)
  1017. {
  1018. case 0:
  1019. diff=gdiff(CPG->preth->singleEasy);
  1020. break;
  1021. case 1:
  1022. diff=gdiff(CPG->preth->singleNormal);
  1023. break;
  1024. case 2:
  1025. diff=gdiff(CPG->preth->singleHard);
  1026. break;
  1027. case 3:
  1028. diff=gdiff(CPG->preth->singleExpert);
  1029. break;
  1030. case 4:
  1031. diff=gdiff(CPG->preth->singleImpossible);
  1032. break;
  1033. }
  1034. temp=-1;
  1035. switch (ourMaps[selected].width)
  1036. {
  1037. case 36:
  1038. temp=0;
  1039. break;
  1040. case 72:
  1041. temp=1;
  1042. break;
  1043. case 108:
  1044. temp=2;
  1045. break;
  1046. case 144:
  1047. temp=3;
  1048. break;
  1049. default:
  1050. temp=4;
  1051. break;
  1052. }
  1053. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1054. temp=ourMaps[selected].victoryCondition;
  1055. if (temp>12) temp=11;
  1056. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1057. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  1058. if (temp>12) temp=3;
  1059. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1060. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1061. //SDL_Flip(ekran);
  1062. CSDL_Ext::update(ekran);
  1063. }
  1064. std::string MapSel::gdiff(std::string ss)
  1065. {
  1066. std::string ret;
  1067. for (int i=2;i<ss.length();i++)
  1068. {
  1069. if (ss[i]==' ')
  1070. break;
  1071. ret+=ss[i];
  1072. }
  1073. return ret;
  1074. }
  1075. void CPreGame::printMapsFrom(int from)
  1076. {
  1077. ourScenSel->mapsel.printMaps(from);
  1078. }
  1079. void CPreGame::showScenList()
  1080. {
  1081. if (currentTab!=&ourScenSel->mapsel)
  1082. {
  1083. ourScenSel->listShowed=true;
  1084. ourScenSel->mapsel.show();
  1085. }
  1086. else currentTab->hide();
  1087. }
  1088. CPreGame::CPreGame()
  1089. {
  1090. highlighted=NULL;
  1091. currentTab=NULL;
  1092. run=true;
  1093. timeHandler tmh;tmh.getDif();
  1094. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1095. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1096. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1097. preth = new CPreGameTextHandler;
  1098. preth->loadTexts();
  1099. CGI->preth=preth;
  1100. THC std::cout<<"\tCPreGame: loading txts: "<<tmh.getDif()<<std::endl;
  1101. currentMessage=NULL;
  1102. behindCurMes=NULL;
  1103. initMainMenu();
  1104. THC std::cout<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1105. initNewMenu();
  1106. THC std::cout<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1107. initScenSel();
  1108. THC std::cout<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1109. initOptions();
  1110. THC std::cout<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1111. showMainMenu();
  1112. THC std::cout<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1113. CPG=this;
  1114. }
  1115. void CPreGame::initOptions()
  1116. {
  1117. ourOptions = new Options();
  1118. ourOptions->init();
  1119. }
  1120. void CPreGame::initScenSel()
  1121. {
  1122. ourScenSel = new ScenSel();
  1123. ourScenSel->listShowed=false;
  1124. if (rand()%2) ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1000.bmp");
  1125. else ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1001.bmp");
  1126. ourScenSel->pressed=NULL;
  1127. ourScenSel->scenInf=CGI->bitmaph->loadBitmap("GSELPOP1.bmp");//SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp");
  1128. ourScenSel->randMap=CGI->bitmaph->loadBitmap("RANMAPBK.bmp");
  1129. ourScenSel->options=CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  1130. SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255));
  1131. //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255));
  1132. SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255));
  1133. SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255));
  1134. ourScenSel->difficulty = new CPoinGroup<>();
  1135. ourScenSel->difficulty->type=1;
  1136. ourScenSel->selectedDiff=-77;
  1137. ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff;
  1138. ourScenSel->bEasy = IntSelBut<>(genRect(0,0,506,456),NULL,CGI->spriteh->giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,1);
  1139. ourScenSel->bNormal = IntSelBut<>(genRect(0,0,538,456),NULL,CGI->spriteh->giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,2);
  1140. ourScenSel->bHard = IntSelBut<>(genRect(0,0,570,456),NULL,CGI->spriteh->giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,3);
  1141. ourScenSel->bExpert = IntSelBut<>(genRect(0,0,602,456),NULL,CGI->spriteh->giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,4);
  1142. ourScenSel->bImpossible = IntSelBut<>(genRect(0,0,634,456),NULL,CGI->spriteh->giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,5);
  1143. ourScenSel->bBack = Button<>(genRect(0,0,584,535),&CPreGame::showNewMenu,CGI->spriteh->giveDef("SCNRBACK.DEF"));
  1144. ourScenSel->bBegin = Button<>(genRect(0,0,414,535),&CPreGame::begin,CGI->spriteh->giveDef("SCNRBEG.DEF"));
  1145. ourScenSel->bScens = Button<>(genRect(0,0,414,81),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1146. for (int i=0; i<ourScenSel->bScens.imgs->ourImages.size(); i++)
  1147. CSDL_Ext::printAt("Show Available Scenarios",25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap);
  1148. ourScenSel->bRandom = Button<>(genRect(0,0,414,105),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1149. for (int i=0; i<ourScenSel->bRandom.imgs->ourImages.size(); i++)
  1150. CSDL_Ext::printAt("Random Map",25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap);
  1151. ourScenSel->bOptions = Button<>(genRect(0,0,414,509),&CPreGame::showOptions,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1152. for (int i=0; i<ourScenSel->bOptions.imgs->ourImages.size(); i++)
  1153. CSDL_Ext::printAt("Show Advanced Options",25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap);
  1154. CPG=this;
  1155. ourScenSel->mapsel.init();
  1156. }
  1157. void CPreGame::showScenSel()
  1158. {
  1159. state=EState::ScenarioList;
  1160. SDL_BlitSurface(ourScenSel->background,NULL,ekran,NULL);
  1161. SDL_BlitSurface(ourScenSel->scenInf,NULL,ekran,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1162. CSDL_Ext::printAt("Map Diff:",427,438,GEOR13);
  1163. CSDL_Ext::printAt("Player Difficulty:",527,438,GEOR13);
  1164. CSDL_Ext::printAt("Rating:",685,438,GEOR13);
  1165. //blit buttons
  1166. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bEasy.pos);
  1167. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bNormal.pos);
  1168. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bHard.pos);
  1169. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bExpert.pos);
  1170. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bImpossible.pos);
  1171. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBegin.pos);
  1172. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBack.pos);
  1173. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1174. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1175. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1176. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1177. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1178. //add buttons info
  1179. btns.push_back(&ourScenSel->bEasy);
  1180. btns.push_back(&ourScenSel->bNormal);
  1181. btns.push_back(&ourScenSel->bHard);
  1182. btns.push_back(&ourScenSel->bExpert);
  1183. btns.push_back(&ourScenSel->bImpossible);
  1184. btns.push_back(&ourScenSel->bScens);
  1185. btns.push_back(&ourScenSel->bRandom);
  1186. btns.push_back(&ourScenSel->bOptions);
  1187. btns.push_back(&ourScenSel->bBegin);
  1188. btns.push_back(&ourScenSel->bBack);
  1189. ourScenSel->selectedDiff=1;
  1190. ourScenSel->bNormal.select(true);
  1191. for (int i=0;i<btns.size();i++)
  1192. {
  1193. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1194. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1195. btns[i]->ID=10;
  1196. }
  1197. handleOther = &CPreGame::scenHandleEv;
  1198. ourScenSel->mapsel.select(0,false);
  1199. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1200. //SDL_Flip(ekran);
  1201. CSDL_Ext::update(ekran);
  1202. }
  1203. void CPreGame::showOptions()
  1204. {
  1205. ourOptions->show();
  1206. }
  1207. void CPreGame::initNewMenu()
  1208. {
  1209. ourNewMenu = new menuItems();
  1210. ourNewMenu->bgAd = CGI->bitmaph->loadBitmap("ZNEWGAM.bmp");
  1211. ourNewMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp");
  1212. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1213. slh = new CSemiLodHandler();
  1214. slh->openLod("H3sprite.lod");
  1215. //loading menu buttons
  1216. ourNewMenu->newGame = CGI->spriteh->giveDef("ZTSINGL.DEF");
  1217. ourNewMenu->loadGame = CGI->spriteh->giveDef("ZTMULTI.DEF");
  1218. ourNewMenu->highScores = CGI->spriteh->giveDef("ZTCAMPN.DEF");
  1219. ourNewMenu->credits = CGI->spriteh->giveDef("ZTTUTOR.DEF");
  1220. ourNewMenu->quit = CGI->spriteh->giveDef("ZTBACK.DEF");
  1221. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1222. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1223. // single scenario
  1224. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1225. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1226. ourNewMenu->lNewGame.x=545;
  1227. ourNewMenu->lNewGame.y=4;
  1228. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1229. //multiplayer
  1230. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1231. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1232. ourNewMenu->lLoadGame.x=568;
  1233. ourNewMenu->lLoadGame.y=120;
  1234. //campaign
  1235. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1236. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1237. ourNewMenu->lHighScores.x=541;
  1238. ourNewMenu->lHighScores.y=233;
  1239. //tutorial
  1240. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1241. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1242. ourNewMenu->lCredits.x=545;
  1243. ourNewMenu->lCredits.y=358;
  1244. //back
  1245. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1246. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1247. ourNewMenu->lQuit.x=582;
  1248. ourNewMenu->lQuit.y=464;
  1249. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1250. ourNewMenu->highlighted=0;
  1251. }
  1252. void CPreGame::showNewMenu()
  1253. {
  1254. btns.clear();
  1255. interested.clear();
  1256. handleOther=NULL;
  1257. state = EState::newGame;
  1258. SDL_BlitSurface(ourNewMenu->background,NULL,ekran,NULL);
  1259. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lNewGame);
  1260. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lLoadGame);
  1261. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lHighScores);
  1262. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lCredits);
  1263. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lQuit);
  1264. //SDL_Flip(ekran);
  1265. CSDL_Ext::update(ekran);
  1266. }
  1267. void CPreGame::initMainMenu()
  1268. {
  1269. ourMainMenu = new menuItems();
  1270. ourMainMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1271. CSemiLodHandler * slh = new CSemiLodHandler();
  1272. slh->openLod("H3sprite.lod");
  1273. //loading menu buttons
  1274. ourMainMenu->newGame = CGI->spriteh->giveDef("ZMENUNG.DEF");
  1275. ourMainMenu->loadGame = CGI->spriteh->giveDef("ZMENULG.DEF");
  1276. ourMainMenu->highScores = CGI->spriteh->giveDef("ZMENUHS.DEF");
  1277. ourMainMenu->credits = CGI->spriteh->giveDef("ZMENUCR.DEF");
  1278. ourMainMenu->quit = CGI->spriteh->giveDef("ZMENUQT.DEF");
  1279. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1280. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1281. // new game button location
  1282. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1283. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1284. ourMainMenu->lNewGame.x=540;
  1285. ourMainMenu->lNewGame.y=10;
  1286. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1287. //load game location
  1288. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1289. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1290. ourMainMenu->lLoadGame.x=532;
  1291. ourMainMenu->lLoadGame.y=132;
  1292. //high scores
  1293. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1294. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1295. ourMainMenu->lHighScores.x=524;
  1296. ourMainMenu->lHighScores.y=251;
  1297. //credits
  1298. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1299. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1300. ourMainMenu->lCredits.x=557;
  1301. ourMainMenu->lCredits.y=359;
  1302. //quit
  1303. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1304. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1305. ourMainMenu->lQuit.x=586;
  1306. ourMainMenu->lQuit.y=468;
  1307. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1308. ourMainMenu->highlighted=0;
  1309. handledLods.push_back(slh);
  1310. delete slh;
  1311. }
  1312. void CPreGame::showMainMenu()
  1313. {
  1314. state = EState::mainMenu;
  1315. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  1316. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  1317. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  1318. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  1319. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  1320. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  1321. //SDL_Flip(ekran);
  1322. CSDL_Ext::update(ekran);
  1323. }
  1324. void CPreGame::highlightButton(int which, int on)
  1325. {
  1326. menuItems * current = currentItems();
  1327. switch (which)
  1328. {
  1329. case 1:
  1330. {
  1331. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,ekran,&current->lNewGame);
  1332. break;
  1333. }
  1334. case 2:
  1335. {
  1336. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,ekran,&current->lLoadGame);
  1337. break;
  1338. }
  1339. case 3:
  1340. {
  1341. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,ekran,&current->lHighScores);
  1342. break;
  1343. }
  1344. case 4:
  1345. {
  1346. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,ekran,&current->lCredits);
  1347. break;
  1348. }
  1349. case 5:
  1350. {
  1351. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,ekran,&current->lQuit);
  1352. break;
  1353. }
  1354. }
  1355. //SDL_Flip(ekran);
  1356. CSDL_Ext::update(ekran);
  1357. }
  1358. void CPreGame::showCenBox (std::string data)
  1359. {
  1360. CMessage * cmh = new CMessage();
  1361. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data));
  1362. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  1363. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1364. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1365. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1366. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1367. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1368. SDL_FreeSurface(infoBox);
  1369. currentMessage = new SDL_Rect(pos);
  1370. delete cmh;
  1371. }
  1372. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1373. {
  1374. CMessage * cmh = new CMessage();
  1375. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1376. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1377. przyciski->push_back(ok);
  1378. przyciski->push_back(cancel);
  1379. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), EWindowType::yesOrNO, przyciski, btnspos);
  1380. behindCurMes = SDL_CreateRGBSurface(ekran->flags,infoBox->w,infoBox->h,ekran->format->BitsPerPixel,ekran->format->Rmask,ekran->format->Gmask,ekran->format->Bmask,ekran->format->Amask);
  1381. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1382. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1383. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1384. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1385. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1386. SDL_FreeSurface(infoBox);
  1387. currentMessage = new SDL_Rect(pos);
  1388. (*btnspos)[0].x+=pos.x;
  1389. (*btnspos)[0].y+=pos.y;
  1390. (*btnspos)[1].x+=pos.x;
  1391. (*btnspos)[1].y+=pos.y;
  1392. btns.push_back(new Button<>((*btnspos)[0],&CPreGame::quit,ok,false, NULL,2));
  1393. btns.push_back(new Button<>((*btnspos)[1],(&CPreGame::hideBox),cancel,false, NULL,2));
  1394. delete cmh;
  1395. delete przyciski;
  1396. delete btnspos;
  1397. }
  1398. void CPreGame::hideBox ()
  1399. {
  1400. SDL_BlitSurface(behindCurMes,NULL,ekran,currentMessage);
  1401. SDL_UpdateRect
  1402. (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1403. for (int i=0;i<btns.size();i++)
  1404. {
  1405. if (btns[i]->ID==2)
  1406. {
  1407. delete btns[i];
  1408. btns.erase(btns.begin()+i);
  1409. i--;
  1410. }
  1411. }
  1412. SDL_FreeSurface(behindCurMes);
  1413. delete currentMessage;
  1414. currentMessage = NULL;
  1415. behindCurMes=NULL;
  1416. }
  1417. CPreGame::menuItems * CPreGame::currentItems()
  1418. {
  1419. switch (state)
  1420. {
  1421. case EState::mainMenu:
  1422. return ourMainMenu;
  1423. case EState::newGame:
  1424. return ourNewMenu;
  1425. default:
  1426. return NULL;
  1427. }
  1428. }
  1429. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1430. {
  1431. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1432. {
  1433. for (int i=0;i<btns.size(); i++)
  1434. {
  1435. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1436. {
  1437. btns[i]->press(true);
  1438. ourScenSel->pressed=(Button<>*)btns[i];
  1439. }
  1440. }
  1441. if (ourScenSel->listShowed && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1442. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1443. {
  1444. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1445. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1446. }
  1447. }
  1448. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1449. {
  1450. Button<> * prnr=ourScenSel->pressed;
  1451. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1452. {
  1453. ourScenSel->pressed->press(false);
  1454. ourScenSel->pressed=NULL;
  1455. }
  1456. for (int i=0;i<btns.size(); i++)
  1457. {
  1458. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1459. {
  1460. if (btns[i]->selectable)
  1461. btns[i]->select(true);
  1462. if (btns[i]->type==1 && ((Button<>*)btns[i])->fun)
  1463. (this->*(((Button<>*)btns[i])->fun))();
  1464. int zz = btns.size();
  1465. if (i>=zz)
  1466. break;
  1467. if (btns[i]==prnr && btns[i]->type==2)
  1468. {
  1469. ((IntBut<> *)(btns[i]))->set();
  1470. ourScenSel->mapsel.slid->whereAreWe=0;
  1471. ourScenSel->mapsel.slid->updateSlid();
  1472. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1473. ourScenSel->mapsel.printMaps(0);
  1474. }
  1475. }
  1476. }
  1477. }
  1478. else if (sEvent.type==SDL_MOUSEMOTION)
  1479. {
  1480. if (highlighted)
  1481. {
  1482. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1483. return;
  1484. else
  1485. {
  1486. highlighted->hover(false);
  1487. highlighted = NULL;
  1488. }
  1489. }
  1490. for (int i=0;i<btns.size();i++)
  1491. {
  1492. if (!btns[i]->highlightable)
  1493. continue;
  1494. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1495. {
  1496. btns[i]->hover(true);
  1497. highlighted=btns[i];
  1498. return;
  1499. }
  1500. else if (btns[i]->highlighted)
  1501. btns[i]->hover(false);
  1502. }
  1503. }
  1504. }
  1505. StartInfo CPreGame::runLoop()
  1506. {
  1507. SDL_Event sEvent;
  1508. while(run)
  1509. {
  1510. try
  1511. {
  1512. if(SDL_PollEvent(&sEvent)) //wait for event...
  1513. {
  1514. menuItems * current = currentItems();
  1515. if(sEvent.type==SDL_QUIT)
  1516. return ret;
  1517. for (int i=0;i<interested.size();i++)
  1518. interested[i]->handleIt(sEvent);
  1519. if (!current)
  1520. {
  1521. (this->*handleOther)(sEvent);
  1522. }
  1523. else if (sEvent.type==SDL_KEYDOWN)
  1524. {
  1525. if (sEvent.key.keysym.sym==SDLK_q)
  1526. {
  1527. return ret;
  1528. break;
  1529. }
  1530. /*if (state==EState::newGame)
  1531. {
  1532. switch (sEvent.key.keysym.sym)
  1533. {
  1534. case SDLK_LEFT:
  1535. {
  1536. if(currentItems()->lNewGame.x>0)
  1537. currentItems()->lNewGame.x--;
  1538. break;
  1539. }
  1540. case (SDLK_RIGHT):
  1541. {
  1542. currentItems()->lNewGame.x++;
  1543. break;
  1544. }
  1545. case (SDLK_UP):
  1546. {
  1547. if(currentItems()->lNewGame.y>0)
  1548. currentItems()->lNewGame.y--;
  1549. break;
  1550. }
  1551. case (SDLK_DOWN):
  1552. {
  1553. currentItems()->lNewGame.y++;
  1554. break;
  1555. }
  1556. }
  1557. showNewMenu();
  1558. }*/
  1559. }
  1560. else if (sEvent.type==SDL_MOUSEMOTION)
  1561. {
  1562. if (currentMessage) continue;
  1563. if (current->highlighted)
  1564. {
  1565. switch (current->highlighted)
  1566. {
  1567. case 1:
  1568. {
  1569. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1570. continue;
  1571. else
  1572. {
  1573. current->highlighted=0;
  1574. highlightButton(1,0);
  1575. }
  1576. break;
  1577. }
  1578. case 2:
  1579. {
  1580. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1581. continue;
  1582. else
  1583. {
  1584. current->highlighted=0;
  1585. highlightButton(2,0);
  1586. }
  1587. break;
  1588. }
  1589. case 3:
  1590. {
  1591. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1592. continue;
  1593. else
  1594. {
  1595. current->highlighted=0;
  1596. highlightButton(3,0);
  1597. }
  1598. break;
  1599. }
  1600. case 4:
  1601. {
  1602. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1603. continue;
  1604. else
  1605. {
  1606. current->highlighted=0;
  1607. highlightButton(4,0);
  1608. }
  1609. break;
  1610. }
  1611. case 5:
  1612. {
  1613. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1614. continue;
  1615. else
  1616. {
  1617. current->highlighted=0;
  1618. highlightButton(5,0);
  1619. }
  1620. break;
  1621. }
  1622. } //switch (current->highlighted)
  1623. } // if (current->highlighted)
  1624. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1625. {
  1626. highlightButton(1,2);
  1627. current->highlighted=1;
  1628. }
  1629. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1630. {
  1631. highlightButton(2,2);
  1632. current->highlighted=2;
  1633. }
  1634. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1635. {
  1636. highlightButton(3,2);
  1637. current->highlighted=3;
  1638. }
  1639. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1640. {
  1641. highlightButton(4,2);
  1642. current->highlighted=4;
  1643. }
  1644. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1645. {
  1646. highlightButton(5,2);
  1647. current->highlighted=5;
  1648. }
  1649. }
  1650. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1651. {
  1652. mush->playClick();
  1653. for (int i=0;i<btns.size(); i++)
  1654. {
  1655. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1656. {
  1657. btns[i]->press(true);
  1658. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,ekran,&btns[i].pos);
  1659. //updateRect(&btns[i].pos);
  1660. }
  1661. }
  1662. if (currentMessage) continue;
  1663. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1664. {
  1665. highlightButton(1,1);
  1666. current->highlighted=1;
  1667. }
  1668. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1669. {
  1670. highlightButton(2,1);
  1671. current->highlighted=2;
  1672. }
  1673. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1674. {
  1675. highlightButton(3,1);
  1676. current->highlighted=3;
  1677. }
  1678. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1679. {
  1680. highlightButton(4,1);
  1681. current->highlighted=4;
  1682. }
  1683. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1684. {
  1685. highlightButton(5,1);
  1686. current->highlighted=5;
  1687. }
  1688. }
  1689. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1690. {
  1691. for (int i=0;i<btns.size(); i++)
  1692. {
  1693. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1694. (this->*(((Button<>*)btns[i])->fun))();
  1695. else
  1696. {
  1697. btns[i]->press(false);
  1698. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,ekran,&btns[i].pos);
  1699. //updateRect(&btns[i].pos);
  1700. }
  1701. }
  1702. if (currentMessage) continue;
  1703. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1704. {
  1705. highlightButton(1,2);
  1706. current->highlighted=1;
  1707. (this->*(current->fNewGame))();
  1708. }
  1709. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1710. {
  1711. highlightButton(2,2);
  1712. current->highlighted=2;
  1713. }
  1714. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1715. {
  1716. highlightButton(3,2);
  1717. current->highlighted=3;
  1718. }
  1719. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1720. {
  1721. highlightButton(4,2);
  1722. current->highlighted=4;
  1723. }
  1724. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1725. {
  1726. highlightButton(5,2);
  1727. current->highlighted=5;
  1728. (this->*(current->fQuit))();
  1729. }
  1730. }
  1731. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  1732. {
  1733. if (currentMessage) continue;
  1734. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1735. {
  1736. showCenBox(buttonText(0));
  1737. }
  1738. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1739. {
  1740. showCenBox(buttonText(1));
  1741. }
  1742. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1743. {
  1744. showCenBox(buttonText(2));
  1745. }
  1746. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1747. {
  1748. showCenBox(buttonText(3));
  1749. }
  1750. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  1751. {
  1752. showCenBox(buttonText(4));
  1753. }
  1754. }
  1755. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  1756. {
  1757. hideBox();
  1758. }
  1759. }
  1760. }
  1761. catch(...)
  1762. { continue; }
  1763. SDL_Delay(1); //give time for other apps
  1764. }
  1765. return ret;
  1766. }
  1767. std::string CPreGame::buttonText(int which)
  1768. {
  1769. if (state==EState::mainMenu)
  1770. {
  1771. switch (which)
  1772. {
  1773. case 0:
  1774. return preth->mainNewGame;
  1775. case 1:
  1776. return preth->mainLoadGame;
  1777. case 2:
  1778. return preth->mainHighScores;
  1779. case 3:
  1780. return preth->mainCredits;
  1781. case 4:
  1782. return preth->mainQuit;
  1783. }
  1784. }
  1785. else if (state==EState::newGame)
  1786. {
  1787. switch (which)
  1788. {
  1789. case 0:
  1790. return preth->ngSingleScenario;
  1791. case 1:
  1792. return preth->ngMultiplayer;
  1793. case 2:
  1794. return preth->ngCampain;
  1795. case 3:
  1796. return preth->ngTutorial;
  1797. case 4:
  1798. return preth->ngBack;
  1799. }
  1800. }
  1801. }
  1802. void CPreGame::quitAskBox()
  1803. {
  1804. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  1805. }
  1806. void CPreGame::sortMaps()
  1807. {
  1808. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  1809. ourScenSel->mapsel.select(0);
  1810. printMapsFrom(0);
  1811. }
  1812. void CPreGame::setTurnLength(int on)
  1813. {
  1814. int min;
  1815. switch (on)
  1816. {
  1817. case 0:
  1818. min=1;
  1819. break;
  1820. case 1:
  1821. min=2;
  1822. break;
  1823. case 2:
  1824. min=4;
  1825. break;
  1826. case 3:
  1827. min=6;
  1828. break;
  1829. case 4:
  1830. min=8;
  1831. break;
  1832. case 5:
  1833. min=10;
  1834. break;
  1835. case 6:
  1836. min=15;
  1837. break;
  1838. case 7:
  1839. min=20;
  1840. break;
  1841. case 8:
  1842. min=25;
  1843. break;
  1844. case 9:
  1845. min=30;
  1846. break;
  1847. case 10:
  1848. min=0;
  1849. break;
  1850. }
  1851. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),ekran,&genRect(23,134,258,553));
  1852. updateRect(&genRect(23,134,258,553));
  1853. if (min)
  1854. {
  1855. std::ostringstream os;
  1856. os<<min<<" Minutes";
  1857. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  1858. }
  1859. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  1860. }