CPreGame.cpp 112 KB

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