CPreGame.cpp 113 KB

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