CPreGame.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. #include "stdafx.h"
  2. #include "CPreGame.h"
  3. #include "hch/CDefHandler.h"
  4. #include <SDL.h>
  5. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  6. #include <boost/algorithm/string.hpp>
  7. //#include "boost/foreach.hpp"
  8. #include <zlib.h>
  9. #include "timeHandler.h"
  10. #include <sstream>
  11. #include "SDL_Extensions.h"
  12. #include "CGameInfo.h"
  13. #include "hch/CGeneralTextHandler.h"
  14. #include "CCursorHandler.h"
  15. #include "hch/CLodHandler.h"
  16. #include "hch/CTownHandler.h"
  17. #include "hch/CHeroHandler.h"
  18. #include <cmath>
  19. #include "client/Graphics.h"
  20. #include <boost/thread.hpp>
  21. #include <boost/bind.hpp>
  22. #include <cstdlib>
  23. extern SDL_Surface * screen;
  24. extern SDL_Color tytulowy, tlo, zwykly ;
  25. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  26. #ifdef min
  27. #undef min
  28. #endif
  29. #ifdef max
  30. #undef max
  31. #endif
  32. CPreGame * CPG;
  33. namespace fs = boost::filesystem;
  34. namespace s = CSDL_Ext;
  35. HighButton::HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel, int id)
  36. {
  37. type=0;
  38. imgs=Imgs;
  39. selectable=Sel;
  40. selected=false;
  41. state=0;
  42. pos=Pos;
  43. ID=id;
  44. highlightable=false;
  45. };
  46. HighButton::HighButton()
  47. {
  48. state=0;
  49. }
  50. void HighButton::show()
  51. {
  52. blitAt(imgs->ourImages[state].bitmap,pos.x,pos.y);
  53. updateRect(&pos);
  54. }
  55. void HighButton::press(bool down)
  56. {
  57. int i;
  58. if (down) state=i=1;
  59. else state=i=0;
  60. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  61. updateRect(&pos);
  62. }
  63. Button::Button( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel, CGroup* gr, int id)
  64. :HighButton(Pos,Imgs,Sel,id),ourGroup(gr),fun(Fun)
  65. {
  66. type=1;
  67. };
  68. Button::Button()
  69. {
  70. ourGroup=NULL;type=1;
  71. };
  72. SetrButton::SetrButton()
  73. {
  74. type=1;
  75. selectable=false;
  76. selected=false;
  77. state=0;
  78. highlightable=false;
  79. }
  80. void SetrButton::press(bool down)
  81. {
  82. #ifndef __GNUC__
  83. if (!down && state==1)
  84. *poin=key;
  85. #endif
  86. HighButton::press(down);
  87. }
  88. void HighButton::hover(bool on)
  89. {
  90. if (!highlightable) return;
  91. int i;
  92. if (on)
  93. {
  94. state=i=3;
  95. highlighted=true;
  96. }
  97. else
  98. {
  99. state=i=0;
  100. highlighted=false;
  101. }
  102. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  103. updateRect(&pos);
  104. }
  105. HighButton::~HighButton()
  106. {
  107. delete imgs;
  108. }
  109. void Button::hover(bool on)
  110. {
  111. HighButton::hover(on);
  112. }
  113. void Button::select(bool on)
  114. {
  115. int i;
  116. if (on) state=i=3;
  117. else state=i=0;
  118. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  119. updateRect(&pos);
  120. if (ourGroup && on && ourGroup->type==1)
  121. {
  122. if (ourGroup->selected && ourGroup->selected!=this)
  123. ourGroup->selected->select(false);
  124. ourGroup->selected =this;
  125. }
  126. }
  127. void Slider::updateSlid()
  128. {
  129. float perc = ((float)whereAreWe)/((float)positionsAmnt-capacity);
  130. float myh;
  131. if (vertical)
  132. {
  133. myh=perc*((float)pos.h-48)+pos.y+16;
  134. SDL_FillRect(screen,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0);
  135. blitAt(slider->imgs->ourImages[0].bitmap,pos.x,(int)myh);
  136. slider->pos.y=(int)myh;
  137. }
  138. else
  139. {
  140. myh=perc*((float)pos.w-48)+pos.x+16;
  141. SDL_FillRect(screen,&genRect(pos.h,pos.w-32,pos.x+16,pos.y),0);
  142. blitAt(slider->imgs->ourImages[0].bitmap,(int)myh,pos.y);
  143. slider->pos.x=(int)myh;
  144. }
  145. updateRect(&pos);
  146. }
  147. void Slider::moveDown()
  148. {
  149. if (whereAreWe<positionsAmnt-capacity)
  150. fun(++whereAreWe);
  151. updateSlid();
  152. }
  153. void Slider::moveUp()
  154. {
  155. if (whereAreWe>0)
  156. fun(--whereAreWe);
  157. updateSlid();
  158. }
  159. Slider::Slider(int x, int y, int h, int amnt, int cap, bool ver)
  160. {
  161. vertical=ver;
  162. positionsAmnt = amnt;
  163. capacity = cap;
  164. if (ver)
  165. {
  166. pos = genRect(h,16,x,y);
  167. down = new Button(genRect(16,16,x,y+h-16),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBDN.DEF"),false);
  168. up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBUP.DEF"),false);
  169. slider = new Button(genRect(16,16,x,y+16),boost::function<void()>(),CDefHandler::giveDef("SCNRBSL.DEF"),false);
  170. }
  171. else
  172. {
  173. pos = genRect(16,h,x,y);
  174. down = new Button(genRect(16,16,x+h-16,y),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBRT.DEF"),false);
  175. up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBLF.DEF"),false);
  176. slider = new Button(genRect(16,16,x+16,y),boost::function<void()>(),CDefHandler::giveDef("SCNRBSL.DEF"),false);
  177. }
  178. moving = false;
  179. whereAreWe=0;
  180. }
  181. void Slider::deactivate()
  182. {
  183. CPG->interested.erase(std::find(CPG->interested.begin(),CPG->interested.end(),this));
  184. }
  185. void Slider::activate()
  186. {
  187. SDL_FillRect(screen,&pos,0);
  188. up->show();
  189. down->show();
  190. slider->show();
  191. //SDL_Flip(screen);
  192. CSDL_Ext::update(screen);
  193. CPG->interested.push_back(this);
  194. }
  195. void Slider::handleIt(SDL_Event sEvent)
  196. {
  197. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  198. {
  199. if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  200. {
  201. down->press();
  202. }
  203. else if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  204. {
  205. up->press();
  206. }
  207. else if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y))
  208. {
  209. //slider->press();
  210. moving=true;
  211. }
  212. else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y))
  213. {
  214. float dy;
  215. float pe;
  216. if (vertical)
  217. {
  218. dy = sEvent.motion.y-pos.y-16;
  219. pe = dy/((float)(pos.h-32));
  220. if (pe>1) pe=1;
  221. if (pe<0) pe=0;
  222. }
  223. else
  224. {
  225. dy = sEvent.motion.x-pos.x-16;
  226. pe = dy/((float)(pos.w-32));
  227. if (pe>1) pe=1;
  228. if (pe<0) pe=0;
  229. }
  230. whereAreWe = pe*(positionsAmnt-capacity);
  231. if (whereAreWe<0)whereAreWe=0;
  232. updateSlid();
  233. fun(whereAreWe);
  234. }
  235. }
  236. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  237. {
  238. if ((down->state==1) && isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  239. {
  240. this->down->fun();
  241. }
  242. if ((up->state==1) && isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  243. {
  244. this->up->fun();
  245. }
  246. if (down->state==1) down->press(false);
  247. if (up->state==1) up->press(false);
  248. if (moving)
  249. {
  250. //slider->press();
  251. moving=false;
  252. }
  253. }
  254. else if (sEvent.type==SDL_KEYDOWN)
  255. {
  256. switch (sEvent.key.keysym.sym)
  257. {
  258. case (SDLK_UP):
  259. CPG->ourScenSel->mapsel.moveByOne(true);
  260. break;
  261. case (SDLK_DOWN):
  262. CPG->ourScenSel->mapsel.moveByOne(false);
  263. break;
  264. default:
  265. //TODO do something nasty here like logs entry..
  266. break;
  267. }
  268. }
  269. else if (moving && sEvent.type==SDL_MOUSEMOTION)
  270. {
  271. if (isItIn(&genRect(pos.h,pos.w+64,pos.x-32,pos.y),sEvent.motion.x,sEvent.motion.y))
  272. {
  273. int my;
  274. int all;
  275. float ile;
  276. if (vertical)
  277. {
  278. my = sEvent.motion.y-(pos.y+16);
  279. all =pos.h-48;
  280. ile = (float)my / (float)all;
  281. if (ile>1) ile=1;
  282. if (ile<0) ile=0;
  283. }
  284. else
  285. {
  286. my = sEvent.motion.x-(pos.x+16);
  287. all =pos.w-48;
  288. ile = (float)my / (float)all;
  289. if (ile>1) ile=1;
  290. if (ile<0) ile=0;
  291. }
  292. int ktory = ile*(positionsAmnt-capacity);
  293. if (ktory!=whereAreWe)
  294. {
  295. whereAreWe=ktory;
  296. updateSlid();
  297. }
  298. fun(whereAreWe);
  299. }
  300. }
  301. /*else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  302. {
  303. if (ourScenSel->pressed)
  304. {
  305. ourScenSel->pressed->press(false);
  306. ourScenSel->pressed=NULL;
  307. }
  308. for (int i=0;i<btns.size(); i++)
  309. {
  310. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  311. {
  312. if (btns[i]->selectable)
  313. btns[i]->select(true);
  314. if (btns[i]->fun)
  315. (this->*(btns[i]->fun))();
  316. return;
  317. }
  318. }
  319. if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  320. {
  321. (this->*down->fun)();
  322. }
  323. if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  324. {
  325. (this->*up->fun)();
  326. }
  327. if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y))
  328. {
  329. (this->*slider->fun)();
  330. }
  331. }*/
  332. }
  333. Slider::~Slider()
  334. {
  335. delete up;
  336. delete down;
  337. delete slider;
  338. }
  339. IntBut::IntBut()
  340. {
  341. type=2;
  342. fun=NULL;
  343. highlightable=false;
  344. }
  345. void IntBut::set()
  346. {
  347. *what=key;
  348. }
  349. void CPoinGroup::setYour(IntSelBut * your)
  350. {
  351. *gdzie=your->key;
  352. };
  353. IntSelBut::IntSelBut( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel, CPoinGroup* gr, int My)
  354. : Button(Pos,Fun,Imgs,Sel,gr),key(My)
  355. {
  356. ourPoinGroup=gr;
  357. };
  358. void IntSelBut::select(bool on)
  359. {
  360. Button::select(on);
  361. ourPoinGroup->setYour(this);
  362. CPG->printRating();
  363. }
  364. /********************************************************************************************/
  365. void PreGameTab::show()
  366. {
  367. if (CPG->currentTab)
  368. CPG->currentTab->hide();
  369. showed=true;
  370. CPG->currentTab=this;
  371. }
  372. void PreGameTab::hide()
  373. {
  374. showed=false;
  375. CPG->currentTab=NULL;
  376. }
  377. PreGameTab::PreGameTab()
  378. {
  379. showed=false;
  380. }
  381. /********************************************************************************************/
  382. Options::PlayerOptions::PlayerOptions(int serial, int player)
  383. :Cleft(genRect(24,11,164,133+serial*50),CPG->ourOptions->left,true,-1), //left castle arrow
  384. Cright(genRect(24,11,225,133+serial*50),CPG->ourOptions->right,false,-1), //right castle arrow
  385. Hleft(genRect(24,11,240,133+serial*50),CPG->ourOptions->left,true,0), //left hero arrow
  386. Hright(genRect(24,11,301,133+serial*50),CPG->ourOptions->right,false,0), //right hero arrow
  387. Bleft(genRect(24,11,316,133+serial*50),CPG->ourOptions->left,true,1), //left bonus arrow
  388. Bright(genRect(24,11,377,133+serial*50),CPG->ourOptions->right,false,1), //right bonus arrow
  389. flag(genRect(50,42,14,130+serial*50),CPG->ourOptions->flags[player],player)
  390. {
  391. Bleft.playerID=Bright.playerID=Hleft.playerID=Hright.playerID=Cleft.playerID=Cright.playerID=player;
  392. Bleft.serialID=Bright.serialID=Hleft.serialID=Hright.serialID=Cleft.serialID=Cright.serialID=serial;
  393. }
  394. bool Options::canUseThisHero(int ID)
  395. {
  396. //TODO: check if hero is allowed on selected map
  397. for(int i=0;i<CPG->ret.playerInfos.size();i++)
  398. if(CPG->ret.playerInfos[i].hero==ID)
  399. return false;
  400. return (usedHeroes.find(ID) == usedHeroes.end());
  401. }
  402. int Options::nextAllowedHero(int min, int max, int incl, int dir) //incl 0 - wlacznie; incl 1 - wylacznie; min-max - zakres szukania
  403. {
  404. if(dir>0)
  405. {
  406. for(int i=min+incl; i<=max-incl; i++)
  407. {
  408. if(canUseThisHero(i))
  409. return i;
  410. }
  411. }
  412. else
  413. {
  414. for(int i=max-incl; i>=min+incl; i--)
  415. {
  416. if(canUseThisHero(i))
  417. return i;
  418. }
  419. }
  420. return -1;
  421. }
  422. void Options::OptionSwitch::press(bool down)
  423. {
  424. HighButton::press(down);
  425. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[serialID];
  426. PlayerInfo * ourInf = &CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].players[playerID];
  427. int dir = (left) ? (-1) : (1);
  428. if (down) return;
  429. switch (which) //which button is this?
  430. {
  431. case -1: //castle change
  432. {
  433. int oCas = ourOpt->castle;
  434. if (ourOpt->castle==-2) //no castle - no change
  435. return;
  436. else if (ourOpt->castle==-1) //random => first/last available
  437. {
  438. int pom = (left) ? (F_NUMBER-1) : (0); // last or first
  439. for (;pom>=0 && pom<F_NUMBER;pom+=dir)
  440. {
  441. if (((int)pow((double)2,pom))&ourInf->allowedFactions)
  442. {
  443. ourOpt->castle=pom;
  444. break;
  445. }
  446. else continue;
  447. }
  448. }
  449. else // next/previous available
  450. {
  451. for (;;)
  452. {
  453. ourOpt->castle+=dir;
  454. if (((int)pow((double)2,(int)ourOpt->castle))&ourInf->allowedFactions)
  455. {
  456. break;
  457. }
  458. if (ourOpt->castle>=F_NUMBER || ourOpt->castle<0)
  459. {
  460. double p1 = log((double)ourInf->allowedFactions)/log(2.0f)+0.000001f;
  461. double check = p1-((int)p1);
  462. if (check < 0.001)
  463. ourOpt->castle=(int)p1;
  464. else
  465. ourOpt->castle=-1;
  466. break;
  467. }
  468. }
  469. }
  470. if (oCas!=ourOpt->castle) //changed castle
  471. {
  472. ourOpt->hero=-1;
  473. ourOpt->bonus = brandom;
  474. CPG->ourOptions->showIcon(0,serialID,false);
  475. CPG->ourOptions->showIcon(1,serialID,false);
  476. }
  477. break;
  478. }
  479. case 0: //hero change
  480. {
  481. if (ourOpt->castle<0)
  482. {
  483. break;
  484. }
  485. if (ourOpt->hero==-2) //no hero - no change
  486. return;
  487. else if (ourOpt->hero==-1) //random => first/last available
  488. {
  489. int max = (ourOpt->castle*HEROES_PER_TYPE*2+15),
  490. min = (ourOpt->castle*HEROES_PER_TYPE*2);
  491. ourOpt->hero = CPG->ourOptions->nextAllowedHero(min,max,0,dir);
  492. }
  493. else
  494. {
  495. if(dir>0)
  496. ourOpt->hero = CPG->ourOptions->nextAllowedHero(ourOpt->hero,(ourOpt->castle*HEROES_PER_TYPE*2+16),1,dir);
  497. else
  498. ourOpt->hero = CPG->ourOptions->nextAllowedHero(ourOpt->castle*HEROES_PER_TYPE*2-1,ourOpt->hero,1,dir);
  499. }
  500. break;
  501. }
  502. case 1: //bonus change
  503. {
  504. if (dir>0 && ourOpt->bonus==bresource)
  505. ourOpt->bonus=brandom;
  506. else if (dir<0 && ourOpt->bonus==brandom)
  507. ourOpt->bonus=bresource;
  508. else ourOpt->bonus=(Ebonus)(ourOpt->bonus+dir);
  509. if (ourOpt->hero==-2 && ourOpt->bonus==bartifact) //no hero - can't be artifact
  510. {
  511. if (dir>0)
  512. ourOpt->bonus=brandom;
  513. else ourOpt->bonus=bgold;
  514. }
  515. if (ourOpt->castle==-1 && ourOpt->bonus==bresource)
  516. {
  517. if (dir<0)
  518. ourOpt->bonus=bgold;
  519. else ourOpt->bonus=brandom;
  520. }
  521. break;
  522. }
  523. }
  524. CPG->ourOptions->showIcon(which,serialID,false);
  525. }
  526. void Options::PlayerFlag::press(bool down)
  527. {
  528. HighButton::press(down);
  529. size_t i=0;
  530. for(;i<CPG->ret.playerInfos.size();i++)
  531. if(CPG->ret.playerInfos[i].color==color)
  532. break;
  533. if (CPG->ret.playerInfos[i].human || (!CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].players[CPG->ret.playerInfos[i].color].canHumanPlay))
  534. return; //if this is already human player, or if human is forbidden
  535. size_t j=0;
  536. for(;j<CPG->ret.playerInfos.size();j++)
  537. if(CPG->ret.playerInfos[j].human)
  538. break;
  539. CPG->ret.playerInfos[i].human = true;
  540. CPG->ret.playerInfos[j].human = false;
  541. std::string pom = CPG->ret.playerInfos[i].name;
  542. CPG->ret.playerInfos[i].name = CPG->ret.playerInfos[j].name;
  543. CPG->ret.playerInfos[j].name = pom;
  544. SDL_BlitSurface(CPG->ourOptions->bgs[CPG->ret.playerInfos[i].color],&genRect(19,99,5,1),screen,&genRect(19,99,62,129+i*50));
  545. SDL_UpdateRect(screen,62,129+50*i,99,19);
  546. SDL_BlitSurface(CPG->ourOptions->bgs[CPG->ret.playerInfos[j].color],&genRect(19,99,5,1),screen,&genRect(19,99,62,129+j*50));
  547. SDL_UpdateRect(screen,62,129+50*j,99,19);
  548. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[i].name,111,137+i*50,GEOR13,zwykly);
  549. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[j].name,111,137+j*50,GEOR13,zwykly);
  550. CPG->playerColor = CPG->ret.playerInfos[i].color;
  551. CPG->ourScenSel->mapsel.printFlags();
  552. };
  553. void Options::PlayerFlag::hover(bool on)
  554. {
  555. HighButton::hover(on);
  556. }
  557. void Options::showIcon (int what, int nr, bool abs) //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  558. {
  559. if (what==-2)
  560. {
  561. showIcon(-1,nr,abs);
  562. showIcon(0,nr,abs);
  563. showIcon(1,nr,abs);
  564. }
  565. int ab, se;
  566. if (!abs)
  567. {
  568. ab = CPG->ret.playerInfos[nr].color;
  569. se = nr;
  570. }
  571. else
  572. {
  573. ab = nr;
  574. for (size_t i=0; i<CPG->ret.playerInfos.size();++i)
  575. {
  576. if (CPG->ret.playerInfos[i].color==nr)
  577. {
  578. se=i;
  579. break;
  580. }
  581. }
  582. }
  583. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[se];
  584. switch (what)
  585. {
  586. case -1:
  587. {
  588. int pom=ourOpt->castle;
  589. if (ourOpt->castle<F_NUMBER && ourOpt->castle>=0)
  590. {
  591. blitAtWR(graphics->getPic(ourOpt->castle,true,false),176,130+50*se);
  592. }
  593. else if (ourOpt->castle==-1)
  594. {
  595. blitAtWR(CPG->ourOptions->rCastle,176,130+50*se);
  596. }
  597. else if (ourOpt->castle==-2)
  598. {
  599. blitAtWR(CPG->ourOptions->nCastle,176,130+50*se);
  600. }
  601. break;
  602. }
  603. case 0:
  604. {
  605. int pom=ourOpt->hero;
  606. if (ourOpt->hero==-1)
  607. {
  608. blitAtWR(CPG->ourOptions->rHero,252,130+50*se);
  609. }
  610. else if (ourOpt->hero==-2)
  611. {
  612. if(ourOpt->heroPortrait>=0)
  613. {
  614. blitAtWR(graphics->portraitSmall[ourOpt->heroPortrait],252,130+50*se);
  615. }
  616. else
  617. {
  618. blitAtWR(CPG->ourOptions->nHero,252,130+50*se);
  619. }
  620. }
  621. else
  622. {
  623. blitAtWR(graphics->portraitSmall[pom],252,130+50*se);
  624. }
  625. break;
  626. }
  627. case 1:
  628. {
  629. int pom;
  630. switch (ourOpt->bonus)
  631. {
  632. case -1:
  633. pom=10;
  634. break;
  635. case 0:
  636. pom=9;
  637. break;
  638. case 1:
  639. pom=8;
  640. break;
  641. case 2:
  642. pom=CGI->townh->towns[ourOpt->castle].bonus;
  643. break;
  644. }
  645. blitAtWR(bonuses->ourImages[pom].bitmap,328,130+50*se);
  646. break;
  647. }
  648. }
  649. }
  650. Options::~Options()
  651. {
  652. if (!inited) {
  653. return;
  654. }
  655. for (size_t i=0; i<bgs.size();i++) {
  656. SDL_FreeSurface(bgs[i]);
  657. }
  658. for (size_t i=0; i<flags.size();i++) {
  659. delete flags[i];
  660. }
  661. SDL_FreeSurface(bg);
  662. SDL_FreeSurface(rHero);
  663. SDL_FreeSurface(rCastle);
  664. SDL_FreeSurface(nHero);
  665. SDL_FreeSurface(nCastle);
  666. delete turnLength;
  667. delete left;
  668. delete right;
  669. delete bonuses;
  670. }
  671. void Options::init()
  672. {
  673. inited=true;
  674. bg = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  675. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  676. left = CDefHandler::giveDef("ADOPLFA.DEF");
  677. right = CDefHandler::giveDef("ADOPRTA.DEF");
  678. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  679. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  680. rCastle = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  681. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  682. nCastle = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  683. turnLength = new Slider(57,557,195,11,1,false);
  684. turnLength->fun=boost::bind(&CPreGame::setTurnLength,CPG,_1);
  685. flags.push_back(CDefHandler::giveDef("AOFLGBR.DEF"));
  686. flags.push_back(CDefHandler::giveDef("AOFLGBB.DEF"));
  687. flags.push_back(CDefHandler::giveDef("AOFLGBY.DEF"));
  688. flags.push_back(CDefHandler::giveDef("AOFLGBG.DEF"));
  689. flags.push_back(CDefHandler::giveDef("AOFLGBO.DEF"));
  690. flags.push_back(CDefHandler::giveDef("AOFLGBP.DEF"));
  691. flags.push_back(CDefHandler::giveDef("AOFLGBT.DEF"));
  692. flags.push_back(CDefHandler::giveDef("AOFLGBS.DEF"));
  693. bgs.push_back(BitmapHandler::loadBitmap("ADOPRPNL.bmp"));
  694. bgs.push_back(BitmapHandler::loadBitmap("ADOPBPNL.bmp"));
  695. bgs.push_back(BitmapHandler::loadBitmap("ADOPYPNL.bmp"));
  696. bgs.push_back(BitmapHandler::loadBitmap("ADOPGPNL.bmp"));
  697. bgs.push_back(BitmapHandler::loadBitmap("ADOPOPNL.bmp"));
  698. bgs.push_back(BitmapHandler::loadBitmap("ADOPPPNL.bmp"));
  699. bgs.push_back(BitmapHandler::loadBitmap("ADOPTPNL.bmp"));
  700. bgs.push_back(BitmapHandler::loadBitmap("ADOPSPNL.bmp"));
  701. }
  702. void Options::show()
  703. {
  704. if (showed)return;
  705. PreGameTab::show();
  706. MapSel & ms = CPG->ourScenSel->mapsel;
  707. blitAt(bg,3,6);
  708. CPG->ourScenSel->listShowed=false;
  709. for (size_t i=0; i < CPG->btns.size(); ++i)
  710. {
  711. if (CPG->btns[i]->ID!=10) //leave only right panel buttons
  712. {
  713. CPG->btns.erase(CPG->btns.begin()+i);
  714. i--;
  715. }
  716. }
  717. CPG->interested.clear();
  718. CSDL_Ext::printAtMiddle("Advanced Options",225,35,GEORXX);
  719. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[521],224,544,GEOR13); // Player Turn Duration
  720. int playersSoFar=0;
  721. for (size_t i=0; i < PLAYER_LIMIT; ++i)
  722. {
  723. if (!(ms.ourMaps[ms.selected].players[i].canComputerPlay || ms.ourMaps[ms.selected].players[i].canComputerPlay))
  724. continue;
  725. for (size_t hi=0; hi<ms.ourMaps[ms.selected].players[i].heroesNames.size(); hi++) {
  726. usedHeroes.insert(ms.ourMaps[ms.selected].players[i].heroesNames[hi].heroID);
  727. }
  728. blitAt(bgs[i],57,128+playersSoFar*50);
  729. poptions.push_back(new PlayerOptions(playersSoFar,i));
  730. poptions[poptions.size()-1]->nr=playersSoFar;
  731. poptions[poptions.size()-1]->color=(Ecolor)i;
  732. poptions[poptions.size()-1]->Cleft.show();
  733. poptions[poptions.size()-1]->Cright.show();
  734. poptions[poptions.size()-1]->Hleft.show();
  735. poptions[poptions.size()-1]->Hright.show();
  736. poptions[poptions.size()-1]->Bleft.show();
  737. poptions[poptions.size()-1]->Bright.show();
  738. CPG->btns.push_back(&poptions[poptions.size()-1]->Cleft);
  739. CPG->btns.push_back(&poptions[poptions.size()-1]->Cright);
  740. CPG->btns.push_back(&poptions[poptions.size()-1]->Hleft);
  741. CPG->btns.push_back(&poptions[poptions.size()-1]->Hright);
  742. CPG->btns.push_back(&poptions[poptions.size()-1]->Bleft);
  743. CPG->btns.push_back(&poptions[poptions.size()-1]->Bright);
  744. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[playersSoFar].name,111,137+playersSoFar*50,GEOR13,zwykly);
  745. if (ms.ourMaps[ms.selected].players[i].canHumanPlay)
  746. {
  747. poptions[poptions.size()-1]->flag.show();
  748. CPG->btns.push_back(&poptions[poptions.size()-1]->flag);
  749. if (ms.ourMaps[ms.selected].players[i].canComputerPlay) {
  750. CSDL_Ext::printAtMiddleWB("Human or CPU",86,163+playersSoFar*50,GEORM,7,zwykly);
  751. }
  752. else {
  753. CSDL_Ext::printAtMiddleWB("Human",86,163+playersSoFar*50,GEORM,6,zwykly);
  754. }
  755. }
  756. else {
  757. CSDL_Ext::printAtMiddleWB("CPU",86,163+playersSoFar*50,GEORM,6,zwykly);
  758. }
  759. playersSoFar++;
  760. }
  761. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->allTexts[516],221,63,GEOR13,55,zwykly);
  762. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[256].second),109,109,GEOR13,14);
  763. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[259].second),201,109,GEOR13,10);
  764. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[260].second),275,109,GEOR13,10);
  765. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[261].second),354,109,GEOR13,10);
  766. turnLength->activate();
  767. for (size_t i=0; i < poptions.size(); ++i) {
  768. showIcon(-2,i,false);
  769. }
  770. for(int i=0;i<12;i++)
  771. turnLength->moveDown();
  772. //SDL_Flip(screen);
  773. CSDL_Ext::update(screen);
  774. }
  775. void Options::hide()
  776. {
  777. if (!showed) {
  778. return;
  779. }
  780. PreGameTab::hide();
  781. for (size_t i=0; i < CPG->btns.size(); ++i)
  782. if (CPG->btns[i]->ID==7)
  783. CPG->btns.erase(CPG->btns.begin()+i--);
  784. for (size_t i=0;i<poptions.size();i++) {
  785. delete poptions[i];
  786. }
  787. poptions.clear();
  788. turnLength->deactivate();
  789. }
  790. MapSel::~MapSel()
  791. {
  792. SDL_FreeSurface(bg);
  793. for (size_t i=0; i < scenImgs.size(); ++i) {
  794. SDL_FreeSurface(scenImgs[i]);
  795. }
  796. for (size_t i=0; i < scenList.size(); ++i) {
  797. delete scenList[i];
  798. }
  799. delete sFlags;
  800. }
  801. int MapSel::countWL()
  802. {
  803. int ret=0;
  804. for (int i=0;i<ourMaps.size();i++)
  805. {
  806. if (sizeFilter && ((ourMaps[i].width) != sizeFilter)) {
  807. continue;
  808. }
  809. else {
  810. ret++;
  811. }
  812. }
  813. return ret;
  814. }
  815. void MapSel::printMaps(int from, int to, int at, bool abs)
  816. {
  817. if (true)//
  818. {
  819. int help=-1;
  820. for (size_t i=0; i < ourMaps.size(); ++i)
  821. {
  822. if (sizeFilter && ((ourMaps[i].width) != sizeFilter)) {
  823. continue;
  824. }
  825. else {
  826. help++;
  827. }
  828. if (help==from)
  829. {
  830. from=i;
  831. break;
  832. }
  833. }
  834. }
  835. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  836. SDL_Color nasz;
  837. for (int i=at;i<to;i++)
  838. {
  839. if ((i-at+from) > ourMaps.size()-1)
  840. {
  841. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  842. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  843. blitAt(scenin,24,121+(i-at)*25);
  844. //SDL_Flip(screen);
  845. CSDL_Ext::update(screen);
  846. SDL_FreeSurface(scenin);
  847. continue;
  848. }
  849. if (sizeFilter && ((ourMaps[(i-at)+from].width) != sizeFilter))
  850. {
  851. to++;
  852. at++;
  853. from++;
  854. if (((i-at)+from)>ourMaps.size()-1) break;
  855. else continue;
  856. }
  857. if ((i-at+from) == selected)
  858. nasz=tytulowy;
  859. else nasz=zwykly;
  860. //SDL_Rect pier = genRect(25,351,24,126+(i*25));
  861. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  862. int temp=-1;
  863. std::ostringstream ostr(std::ostringstream::out); ostr << ourMaps[(i-at)+from].playerAmnt << "/" << ourMaps[(i-at)+from].humenPlayers;
  864. CSDL_Ext::printAt(ostr.str(),6,4,GEOR13,nasz,scenin, 2);
  865. std::string temp2;
  866. switch (ourMaps[(i-at)+from].width)
  867. {
  868. case 36:
  869. temp2="S";
  870. break;
  871. case 72:
  872. temp2="M";
  873. break;
  874. case 108:
  875. temp2="L";
  876. break;
  877. case 144:
  878. temp2="XL";
  879. break;
  880. default:
  881. temp2="C";
  882. break;
  883. }
  884. CSDL_Ext::printAtMiddle(temp2,50,13,GEOR13,nasz,scenin, 2);
  885. switch (ourMaps[(i-at)+from].version)
  886. {
  887. case RoE:
  888. temp=0;
  889. break;
  890. case AB:
  891. temp=1;
  892. break;
  893. case SoD:
  894. temp=2;
  895. break;
  896. case WoG:
  897. temp=3;
  898. break;
  899. }
  900. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  901. if (!(ourMaps[(i-at)+from].name.length()))
  902. ourMaps[(i-at)+from].name = "Unnamed";
  903. CSDL_Ext::printAtMiddle(ourMaps[(i-at)+from].name,192,13,GEOR13,nasz,scenin, 2);
  904. if (ourMaps[(i-at)+from].victoryCondition.condition == winStandard)
  905. temp=11;
  906. else
  907. temp=ourMaps[(i-at)+from].victoryCondition.condition;
  908. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  909. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == lossStandard)
  910. temp=3;
  911. else
  912. temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  913. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  914. blitAt(scenin,24,121+(i-at)*25);
  915. SDL_UpdateRect(screen,24,121+(i-at)*25,scenin->w,scenin->h);
  916. }
  917. SDL_FreeSurface(scenin);
  918. }
  919. int MapSel::whichWL(int nr)
  920. {
  921. int help=-1;
  922. for (int i=0;i<ourMaps.size();i++)
  923. {
  924. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  925. continue;
  926. else help++;
  927. if (help==nr)
  928. {
  929. help=i;
  930. break;
  931. }
  932. }
  933. return help;
  934. }
  935. void MapSel::hide()
  936. {
  937. if (!showed)return;
  938. PreGameTab::hide();
  939. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  940. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  941. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  942. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  943. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  944. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  945. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  946. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  947. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  948. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  949. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  950. slid->deactivate();
  951. CPG->currentTab = NULL;
  952. };
  953. void MapSel::show()
  954. {
  955. if (showed)return;
  956. PreGameTab::show();
  957. //blit bg
  958. blitAt(bg,3,6);
  959. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  960. CSDL_Ext::printAt("Select a Scenario to Play",110,25,TNRB16);
  961. //size buttons
  962. small.show();
  963. medium.show();
  964. large.show();
  965. xlarge.show();
  966. all.show();
  967. CPG->btns.push_back(&small);
  968. CPG->btns.push_back(&medium);
  969. CPG->btns.push_back(&large);
  970. CPG->btns.push_back(&xlarge);
  971. CPG->btns.push_back(&all);
  972. //sort by buttons
  973. nrplayer.show();
  974. mapsize.show();
  975. type.show();
  976. name.show();
  977. viccon.show();
  978. loscon.show();
  979. CPG->btns.push_back(&nrplayer);
  980. CPG->btns.push_back(&mapsize);
  981. CPG->btns.push_back(&type);
  982. CPG->btns.push_back(&name);
  983. CPG->btns.push_back(&viccon);
  984. CPG->btns.push_back(&loscon);
  985. //print scenario list
  986. printMaps(0,18);
  987. slid->activate();
  988. //SDL_Flip(screen);
  989. CSDL_Ext::update(screen);
  990. }
  991. void MapSel::processMaps(std::vector<std::string> &pliczkiTemp, int &index)
  992. {
  993. static boost::mutex mx;
  994. int pom=-1, read;
  995. unsigned char sss[1000];
  996. while(true)
  997. {
  998. mx.lock();
  999. if(index>=pliczkiTemp.size())
  1000. {
  1001. mx.unlock();
  1002. break;
  1003. }
  1004. else
  1005. {
  1006. pom = index++;
  1007. mx.unlock();
  1008. }
  1009. gzFile tempf = gzopen(pliczkiTemp[pom].c_str(),"rb");
  1010. read = gzread(tempf, sss, 1000);
  1011. gzclose(tempf);
  1012. if(read < 50)
  1013. {
  1014. tlog3<<"\t\tWarning: corrupted map file: "<<pliczkiTemp[pom]<<std::endl;
  1015. continue;
  1016. }
  1017. if (!sss[4]) //not a valid map
  1018. {
  1019. //tlog3 << "\t\tSkipping " << pliczkiTemp[pom] << " - map marked as unplayable.\n";
  1020. continue;
  1021. }
  1022. CMapInfo mi(pliczkiTemp[pom],sss);
  1023. mx.lock();
  1024. ourMaps.push_back(mi);
  1025. mx.unlock();
  1026. }
  1027. }
  1028. void MapSel::init()
  1029. {
  1030. //get map files names
  1031. std::vector<std::string> pliczkiTemp;
  1032. fs::path tie( (fs::initial_path<fs::path>())/"/maps" );
  1033. fs::directory_iterator end_iter;
  1034. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  1035. {
  1036. if (fs::is_regular_file(dir->status()));
  1037. {
  1038. if (boost::ends_with(dir->path().filename(),".h3m"))
  1039. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  1040. }
  1041. }
  1042. ourMaps.reserve(pliczkiTemp.size());
  1043. int mapInd=0;
  1044. boost::thread_group group;
  1045. int cores = std::max((unsigned int)1,boost::thread::hardware_concurrency());
  1046. for(int ti=0;ti<cores;ti++)
  1047. group.create_thread(boost::bind(&MapSel::processMaps,this,boost::ref(pliczkiTemp),boost::ref(mapInd)));
  1048. bg = BitmapHandler::loadBitmap("SCSELBCK.bmp");
  1049. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1050. small.imgs = CDefHandler::giveDef("SCSMBUT.DEF");
  1051. small.fun = NULL;
  1052. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  1053. small.ourGroup=NULL;
  1054. medium.imgs = CDefHandler::giveDef("SCMDBUT.DEF");
  1055. medium.fun = NULL;
  1056. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  1057. medium.ourGroup=NULL;
  1058. large.imgs = CDefHandler::giveDef("SCLGBUT.DEF");
  1059. large.fun = NULL;
  1060. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  1061. large.ourGroup=NULL;
  1062. xlarge.imgs = CDefHandler::giveDef("SCXLBUT.DEF");
  1063. xlarge.fun = NULL;
  1064. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  1065. xlarge.ourGroup=NULL;
  1066. all.imgs = CDefHandler::giveDef("SCALBUT.DEF");
  1067. all.fun = NULL;
  1068. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  1069. all.ourGroup=NULL;
  1070. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  1071. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  1072. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  1073. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  1074. nrplayer.imgs = CDefHandler::giveDef("SCBUTT1.DEF");
  1075. nrplayer.fun = NULL;
  1076. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  1077. nrplayer.key=_playerAm;
  1078. mapsize.imgs = CDefHandler::giveDef("SCBUTT2.DEF");
  1079. mapsize.fun = NULL;
  1080. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  1081. mapsize.key=_size;
  1082. type.imgs = CDefHandler::giveDef("SCBUTCP.DEF");
  1083. type.fun = NULL;
  1084. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  1085. type.key=_format;
  1086. name.imgs = CDefHandler::giveDef("SCBUTT3.DEF");
  1087. name.fun = NULL;
  1088. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  1089. name.key=_name;
  1090. viccon.imgs = CDefHandler::giveDef("SCBUTT4.DEF");
  1091. viccon.fun = NULL;
  1092. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  1093. viccon.key=_viccon;
  1094. loscon.imgs = CDefHandler::giveDef("SCBUTT5.DEF");
  1095. loscon.fun = NULL;
  1096. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  1097. loscon.key=_loscon;
  1098. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  1099. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=boost::bind(&CPreGame::sortMaps,CPG);
  1100. Dtypes = CDefHandler::giveDef("SCSELC.DEF");
  1101. Dvic = CDefHandler::giveDef("SCNRVICT.DEF");
  1102. Dloss = CDefHandler::giveDef("SCNRLOSS.DEF");
  1103. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  1104. Dsizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1105. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  1106. group.join_all();
  1107. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(_name));
  1108. slid = new Slider(375,92,480,ourMaps.size(),18,true);
  1109. slid->fun = boost::bind(&CPreGame::printMapsFrom,CPG,_1);
  1110. }
  1111. void MapSel::moveByOne(bool up)
  1112. {
  1113. int help=selected;
  1114. if (up) selected--;
  1115. else selected ++;
  1116. for (int i=selected;i<ourMaps.size() && i>=0;)
  1117. {
  1118. help=i;
  1119. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  1120. break;
  1121. if (up)
  1122. {
  1123. i--;
  1124. }
  1125. else
  1126. {
  1127. i++;
  1128. if (i<0) break;
  1129. }
  1130. }
  1131. select(help);
  1132. slid->updateSlid();
  1133. }
  1134. void MapSel::select(int which, bool updateMapsList, bool forceSettingsUpdate)
  1135. {
  1136. bool dontSaveSettings = ((selected!=which) || (CPG->ret.playerInfos.size()==0) || forceSettingsUpdate);
  1137. selected = which;
  1138. CPG->ret.mapname = ourMaps[selected].filename;
  1139. if(updateMapsList)
  1140. printMaps(slid->whereAreWe,18,0,true);
  1141. int serialC=0;
  1142. if(dontSaveSettings)
  1143. {
  1144. CPG->ret.playerInfos.clear();
  1145. bool wasntpl = true;
  1146. for (int i=0;i<PLAYER_LIMIT;i++)
  1147. {
  1148. if (!(ourMaps[selected].players[i].canComputerPlay || ourMaps[selected].players[i].canComputerPlay))
  1149. 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)
  1150. StartInfo::PlayerSettings pset;
  1151. pset.color=(Ecolor)i;
  1152. pset.serial = serialC;
  1153. serialC++;
  1154. pset.bonus=brandom;
  1155. pset.castle=-2;
  1156. if (ourMaps[which].players[i].canHumanPlay && wasntpl)
  1157. {
  1158. pset.name=CGI->generaltexth->allTexts[434]; //Player
  1159. pset.human = true;
  1160. CPG->playerColor = i;
  1161. wasntpl = false;
  1162. }
  1163. else
  1164. {
  1165. pset.name=CGI->generaltexth->allTexts[468];//Computer
  1166. pset.human = false;
  1167. }
  1168. for (int j=0;j<F_NUMBER;j++)
  1169. {
  1170. if (((int)pow((double)2,j))&ourMaps[selected].players[i].allowedFactions)
  1171. {
  1172. if (pset.castle>=0)
  1173. pset.castle=-1;
  1174. if (pset.castle==-2)
  1175. pset.castle=j;
  1176. }
  1177. }
  1178. pset.heroPortrait=-1;
  1179. if (!((ourMaps[which].players[i].generateHeroAtMainTown && ourMaps[which].players[i].hasMainTown) || ourMaps[which].players[i].p8))
  1180. pset.hero=-2;
  1181. else
  1182. pset.hero=-1;
  1183. if(ourMaps[which].players[i].mainHeroName.length())
  1184. {
  1185. pset.heroName = ourMaps[which].players[i].mainHeroName;
  1186. if((pset.heroPortrait = ourMaps[which].players[i].mainHeroPortrait)==255)
  1187. pset.heroPortrait = ourMaps[which].players[i].p9;
  1188. }
  1189. pset.handicap=0;
  1190. CPG->ret.playerInfos.push_back(pset);
  1191. }
  1192. }
  1193. printSelectedInfo();
  1194. }
  1195. MapSel::MapSel():selected(0),sizeFilter(0)
  1196. {
  1197. }
  1198. void MapSel::printSelectedInfo()
  1199. {
  1200. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),screen,&genRect(399,337,413,29));
  1201. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),screen,&genRect(50,91,414,453));
  1202. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bScens.pos);
  1203. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bOptions.pos);
  1204. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bRandom.pos);
  1205. //blit texts
  1206. CSDL_Ext::printAt(CGI->generaltexth->zelp[21].second,420,25,GEOR13);
  1207. CSDL_Ext::printAt(CGI->generaltexth->allTexts[496],420,135,GEOR13);
  1208. CSDL_Ext::printAt(CGI->generaltexth->allTexts[497],420,285,GEOR13);
  1209. CSDL_Ext::printAt(CGI->generaltexth->allTexts[498],420,340,GEOR13);
  1210. CSDL_Ext::printAt(CGI->generaltexth->allTexts[390],420,406,GEOR13,zwykly);
  1211. CSDL_Ext::printAt(CGI->generaltexth->allTexts[391],585,406,GEOR13,zwykly);
  1212. int temp = ourMaps[selected].victoryCondition.condition+1;
  1213. if (temp>20) temp=0;
  1214. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1215. if (temp && ourMaps[selected].victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1216. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1217. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  1218. if (temp>20) temp=0;
  1219. sss = CGI->generaltexth->lossCondtions[temp];
  1220. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1221. //blit descrption
  1222. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  1223. for (int i=0;i<desc.size();i++)
  1224. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1225. if ((selected < 0) || (selected >= ourMaps.size()))
  1226. return;
  1227. if (ourMaps[selected].name.length())
  1228. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  1229. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1230. std::string diff;
  1231. switch (ourMaps[selected].difficulty)
  1232. {
  1233. case 0:
  1234. diff=gdiff(CGI->generaltexth->zelp[24].second);
  1235. break;
  1236. case 1:
  1237. diff=gdiff(CGI->generaltexth->zelp[25].second);
  1238. break;
  1239. case 2:
  1240. diff=gdiff(CGI->generaltexth->zelp[26].second);
  1241. break;
  1242. case 3:
  1243. diff=gdiff(CGI->generaltexth->zelp[27].second);
  1244. break;
  1245. case 4:
  1246. diff=gdiff(CGI->generaltexth->zelp[28].second);
  1247. break;
  1248. }
  1249. temp=-1;
  1250. switch (ourMaps[selected].width)
  1251. {
  1252. case 36:
  1253. temp=0;
  1254. break;
  1255. case 72:
  1256. temp=1;
  1257. break;
  1258. case 108:
  1259. temp=2;
  1260. break;
  1261. case 144:
  1262. temp=3;
  1263. break;
  1264. default:
  1265. temp=4;
  1266. break;
  1267. }
  1268. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1269. temp = ourMaps[selected].victoryCondition.condition;
  1270. if (temp>12) temp=11;
  1271. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1272. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  1273. if (temp>12) temp=3;
  1274. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1275. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1276. //SDL_Flip(screen);
  1277. printFlags();
  1278. CSDL_Ext::update(screen);
  1279. }
  1280. void MapSel::printFlags()
  1281. {
  1282. int hy=405, fx=460, ex=640, myT;
  1283. if (ourMaps[selected].howManyTeams)
  1284. myT = ourMaps[selected].players[CPG->playerColor].team;
  1285. else myT = -1;
  1286. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1287. {
  1288. if (myT>=0)
  1289. {
  1290. if(ourMaps[selected].players[CPG->ret.playerInfos[i].color].team==myT)
  1291. {
  1292. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1293. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1294. }
  1295. else
  1296. {
  1297. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1298. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1299. }
  1300. }
  1301. else
  1302. {
  1303. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1304. {
  1305. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1306. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1307. }
  1308. else
  1309. {
  1310. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1311. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. std::string MapSel::gdiff(std::string ss)
  1317. {
  1318. std::string ret;
  1319. for (int i=2;i<ss.length();i++)
  1320. {
  1321. if (ss[i]==' ')
  1322. break;
  1323. ret+=ss[i];
  1324. }
  1325. return ret;
  1326. }
  1327. void CPreGame::printRating()
  1328. {
  1329. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(47,83,271,449),screen,&genRect(47,83,666,455));
  1330. updateRect(&genRect(47,83,666,455));
  1331. std::string tob;
  1332. switch (ourScenSel->selectedDiff)
  1333. {
  1334. case 0:
  1335. tob="80%";
  1336. break;
  1337. case 1:
  1338. tob="100%";
  1339. break;
  1340. case 2:
  1341. tob="130%";
  1342. break;
  1343. case 3:
  1344. tob="160%";
  1345. break;
  1346. case 4:
  1347. tob="200%";
  1348. break;
  1349. }
  1350. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1351. }
  1352. void CPreGame::printMapsFrom(int from)
  1353. {
  1354. ourScenSel->mapsel.printMaps(from);
  1355. }
  1356. void CPreGame::showScenList()
  1357. {
  1358. if (currentTab!=&ourScenSel->mapsel)
  1359. {
  1360. ourScenSel->listShowed=true;
  1361. ourScenSel->mapsel.show();
  1362. }
  1363. else
  1364. {
  1365. currentTab->hide();
  1366. showScenSel();
  1367. }
  1368. }
  1369. CPreGame::CPreGame()
  1370. {
  1371. CPG=this;
  1372. highlighted=NULL;
  1373. currentTab=NULL;
  1374. run=true;
  1375. timeHandler tmh;tmh.getDif();
  1376. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1377. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1378. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1379. currentMessage=NULL;
  1380. behindCurMes=NULL;
  1381. initMainMenu();
  1382. tlog0<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1383. initNewMenu();
  1384. tlog0<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1385. initLoadMenu();
  1386. tlog0<<"\tCPreGame: loadgame menu initialization: "<<tmh.getDif()<<std::endl;
  1387. initScenSel();
  1388. tlog0<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1389. initOptions();
  1390. tlog0<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1391. showMainMenu();
  1392. tlog0<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1393. playerName="Player";
  1394. }
  1395. void CPreGame::initOptions()
  1396. {
  1397. ourOptions = new Options();
  1398. ourOptions->init();
  1399. }
  1400. void CPreGame::initScenSel()
  1401. {
  1402. ourScenSel = new ScenSel();
  1403. tlog5 << "\t\tLoaded graphics\n";
  1404. ourScenSel->mapsel.init();
  1405. tlog5 << "\t\tLoaded maps\n";
  1406. }
  1407. void CPreGame::showScenSel()
  1408. {
  1409. state=ScenarioList;
  1410. SDL_BlitSurface(ourScenSel->background,NULL,screen,NULL);
  1411. SDL_BlitSurface(ourScenSel->scenInf,NULL,screen,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1412. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1413. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1414. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1415. //blit buttons
  1416. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bEasy.pos);
  1417. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bNormal.pos);
  1418. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bHard.pos);
  1419. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bExpert.pos);
  1420. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bImpossible.pos);
  1421. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBegin.pos);
  1422. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBack.pos);
  1423. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1424. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1425. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1426. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1427. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1428. //add buttons info
  1429. if(first)
  1430. {
  1431. btns.push_back(&ourScenSel->bEasy);
  1432. btns.push_back(&ourScenSel->bNormal);
  1433. btns.push_back(&ourScenSel->bHard);
  1434. btns.push_back(&ourScenSel->bExpert);
  1435. btns.push_back(&ourScenSel->bImpossible);
  1436. btns.push_back(&ourScenSel->bScens);
  1437. btns.push_back(&ourScenSel->bRandom);
  1438. btns.push_back(&ourScenSel->bOptions);
  1439. btns.push_back(&ourScenSel->bBegin);
  1440. btns.push_back(&ourScenSel->bBack);
  1441. ourScenSel->selectedDiff=1;
  1442. ourScenSel->bNormal.select(true);
  1443. handleOther = &CPreGame::scenHandleEv;
  1444. ourScenSel->mapsel.select(0,false);
  1445. for (size_t i=0; i < btns.size(); ++i)
  1446. {
  1447. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1448. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1449. btns[i]->ID=10;
  1450. }
  1451. }
  1452. else
  1453. {
  1454. ourScenSel->mapsel.select(ourScenSel->mapsel.selected,false);
  1455. switch (ourScenSel->selectedDiff)
  1456. {
  1457. case 0:
  1458. ourScenSel->bEasy.select(true);
  1459. break;
  1460. case 1:
  1461. ourScenSel->bNormal.select(true);
  1462. break;
  1463. case 2:
  1464. ourScenSel->bHard.select(true);
  1465. break;
  1466. case 3:
  1467. ourScenSel->bExpert.select(true);
  1468. break;
  1469. case 4:
  1470. ourScenSel->bImpossible.select(true);
  1471. break;
  1472. }
  1473. }
  1474. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1475. //SDL_Flip(screen);
  1476. CSDL_Ext::update(screen);
  1477. first = false;
  1478. }
  1479. void CPreGame::showOptions()
  1480. {
  1481. ourOptions->show();
  1482. }
  1483. void CPreGame::initNewMenu()
  1484. {
  1485. ourNewMenu = new menuItems();
  1486. ourNewMenu->bgAd = BitmapHandler::loadBitmap("ZNEWGAM.bmp");
  1487. ourNewMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  1488. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1489. //loading menu buttons
  1490. ourNewMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  1491. ourNewMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  1492. ourNewMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  1493. ourNewMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  1494. ourNewMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  1495. // single scenario
  1496. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1497. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1498. ourNewMenu->lNewGame.x=545;
  1499. ourNewMenu->lNewGame.y=4;
  1500. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1501. //multiplayer
  1502. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1503. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1504. ourNewMenu->lLoadGame.x=568;
  1505. ourNewMenu->lLoadGame.y=120;
  1506. //campaign
  1507. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1508. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1509. ourNewMenu->lHighScores.x=541;
  1510. ourNewMenu->lHighScores.y=233;
  1511. //tutorial
  1512. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1513. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1514. ourNewMenu->lCredits.x=545;
  1515. ourNewMenu->lCredits.y=358;
  1516. //back
  1517. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1518. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1519. ourNewMenu->lQuit.x=582;
  1520. ourNewMenu->lQuit.y=464;
  1521. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1522. ourNewMenu->highlighted=0;
  1523. }
  1524. void CPreGame::showNewMenu()
  1525. {
  1526. if (currentTab/*==&ourScenSel->mapsel*/)
  1527. currentTab->hide();
  1528. btns.clear();
  1529. interested.clear();
  1530. handleOther=NULL;
  1531. state = newGame;
  1532. SDL_BlitSurface(ourNewMenu->background,NULL,screen,NULL);
  1533. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lNewGame);
  1534. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lLoadGame);
  1535. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lHighScores);
  1536. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lCredits);
  1537. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lQuit);
  1538. //SDL_Flip(screen);
  1539. CSDL_Ext::update(screen);
  1540. first = true;
  1541. }
  1542. void CPreGame::initMainMenu()
  1543. {
  1544. ourMainMenu = new menuItems();
  1545. ourMainMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1546. //loading menu buttons
  1547. ourMainMenu->newGame = CDefHandler::giveDef("ZMENUNG.DEF");
  1548. ourMainMenu->loadGame = CDefHandler::giveDef("ZMENULG.DEF");
  1549. ourMainMenu->highScores = CDefHandler::giveDef("ZMENUHS.DEF");
  1550. ourMainMenu->credits = CDefHandler::giveDef("ZMENUCR.DEF");
  1551. ourMainMenu->quit = CDefHandler::giveDef("ZMENUQT.DEF");
  1552. ok = CDefHandler::giveDef("IOKAY.DEF");
  1553. cancel = CDefHandler::giveDef("ICANCEL.DEF");
  1554. // new game button location
  1555. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1556. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1557. ourMainMenu->lNewGame.x=540;
  1558. ourMainMenu->lNewGame.y=10;
  1559. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1560. //load game location
  1561. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1562. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1563. ourMainMenu->lLoadGame.x=532;
  1564. ourMainMenu->lLoadGame.y=132;
  1565. ourMainMenu->fLoadGame=&CPreGame::showLoadMenu;
  1566. //high scores
  1567. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1568. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1569. ourMainMenu->lHighScores.x=524;
  1570. ourMainMenu->lHighScores.y=251;
  1571. //credits
  1572. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1573. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1574. ourMainMenu->lCredits.x=557;
  1575. ourMainMenu->lCredits.y=359;
  1576. //quit
  1577. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1578. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1579. ourMainMenu->lQuit.x=586;
  1580. ourMainMenu->lQuit.y=468;
  1581. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1582. ourMainMenu->highlighted=0;
  1583. }
  1584. void CPreGame::showMainMenu()
  1585. {
  1586. state = mainMenu;
  1587. SDL_BlitSurface(ourMainMenu->background,NULL,screen,NULL);
  1588. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lNewGame);
  1589. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lLoadGame);
  1590. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lHighScores);
  1591. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lCredits);
  1592. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lQuit);
  1593. //SDL_Flip(screen);
  1594. CSDL_Ext::update(screen);
  1595. }
  1596. void CPreGame::highlightButton(int which, int on)
  1597. {
  1598. menuItems * current = currentItems();
  1599. switch (which)
  1600. {
  1601. case 1:
  1602. {
  1603. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,screen,&current->lNewGame);
  1604. break;
  1605. }
  1606. case 2:
  1607. {
  1608. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,screen,&current->lLoadGame);
  1609. break;
  1610. }
  1611. case 3:
  1612. {
  1613. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,screen,&current->lHighScores);
  1614. break;
  1615. }
  1616. case 4:
  1617. {
  1618. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,screen,&current->lCredits);
  1619. break;
  1620. }
  1621. case 5:
  1622. {
  1623. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,screen,&current->lQuit);
  1624. break;
  1625. }
  1626. }
  1627. //SDL_Flip(screen);
  1628. CSDL_Ext::update(screen);
  1629. }
  1630. void CPreGame::showCenBox (std::string data)
  1631. {
  1632. CMessage * cmh = new CMessage();
  1633. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data));
  1634. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1635. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1636. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1637. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1638. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1639. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1640. SDL_FreeSurface(infoBox);
  1641. currentMessage = new SDL_Rect(pos);
  1642. delete cmh;
  1643. }
  1644. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1645. {
  1646. CMessage * cmh = new CMessage();
  1647. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1648. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1649. przyciski->push_back(ok);
  1650. przyciski->push_back(cancel);
  1651. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data), yesOrNO, przyciski, btnspos);
  1652. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1653. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1654. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1655. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1656. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1657. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1658. SDL_FreeSurface(infoBox);
  1659. currentMessage = new SDL_Rect(pos);
  1660. (*btnspos)[0].x+=pos.x;
  1661. (*btnspos)[0].y+=pos.y;
  1662. (*btnspos)[1].x+=pos.x;
  1663. (*btnspos)[1].y+=pos.y;
  1664. btns.push_back(new Button((*btnspos)[0],boost::bind(&CPreGame::quit,this),ok,false, NULL,2));
  1665. btns.push_back(new Button((*btnspos)[1],boost::bind(&CPreGame::hideBox,this),cancel,false, NULL,2));
  1666. delete cmh;
  1667. delete przyciski;
  1668. delete btnspos;
  1669. }
  1670. void CPreGame::hideBox ()
  1671. {
  1672. SDL_BlitSurface(behindCurMes,NULL,screen,currentMessage);
  1673. SDL_UpdateRect
  1674. (screen,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1675. for (size_t i=0; i < btns.size(); ++i)
  1676. {
  1677. if (btns[i]->ID==2)
  1678. {
  1679. delete btns[i];
  1680. btns.erase(btns.begin()+i);
  1681. i--;
  1682. }
  1683. }
  1684. SDL_FreeSurface(behindCurMes);
  1685. delete currentMessage;
  1686. currentMessage = NULL;
  1687. behindCurMes=NULL;
  1688. }
  1689. CPreGame::menuItems * CPreGame::currentItems()
  1690. {
  1691. switch (state)
  1692. {
  1693. case mainMenu:
  1694. return ourMainMenu;
  1695. case newGame:
  1696. return ourNewMenu;
  1697. case loadGame:
  1698. return ourLoadMenu;
  1699. default:
  1700. return NULL;
  1701. }
  1702. }
  1703. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1704. {
  1705. if(sEvent.type == SDL_MOUSEMOTION)
  1706. {
  1707. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1708. }
  1709. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1710. {
  1711. for (size_t i=0; i < btns.size(); ++i)
  1712. {
  1713. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1714. {
  1715. btns[i]->press(true);
  1716. ourScenSel->pressed=(Button*)btns[i];
  1717. }
  1718. }
  1719. if ((currentTab==&ourScenSel->mapsel) && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1720. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1721. {
  1722. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1723. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1724. }
  1725. }
  1726. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1727. {
  1728. Button * prnr=ourScenSel->pressed;
  1729. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1730. {
  1731. ourScenSel->pressed->press(false);
  1732. ourScenSel->pressed=NULL;
  1733. }
  1734. for (size_t i=0; i < btns.size(); ++i)
  1735. {
  1736. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1737. {
  1738. if (btns[i]->selectable)
  1739. btns[i]->select(true);
  1740. if (btns[i]->type==1 && ((Button*)btns[i])->fun)
  1741. ((Button*)btns[i])->fun();
  1742. int zz = btns.size();
  1743. if (i>=zz)
  1744. break;
  1745. if (btns[i]==prnr && btns[i]->type==2)
  1746. {
  1747. ((IntBut*)(btns[i]))->set();
  1748. ourScenSel->mapsel.slid->whereAreWe=0;
  1749. ourScenSel->mapsel.slid->updateSlid();
  1750. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1751. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0));
  1752. ourScenSel->mapsel.printMaps(0);
  1753. }
  1754. }
  1755. }
  1756. }
  1757. else if (sEvent.type==SDL_MOUSEMOTION)
  1758. {
  1759. if (highlighted)
  1760. {
  1761. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1762. return;
  1763. else
  1764. {
  1765. highlighted->hover(false);
  1766. highlighted = NULL;
  1767. }
  1768. }
  1769. for (size_t i=0; i < btns.size(); ++i)
  1770. {
  1771. if (!btns[i]->highlightable)
  1772. continue;
  1773. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1774. {
  1775. btns[i]->hover(true);
  1776. highlighted=btns[i];
  1777. return;
  1778. }
  1779. else if (btns[i]->highlighted)
  1780. btns[i]->hover(false);
  1781. }
  1782. }
  1783. }
  1784. StartInfo CPreGame::runLoop()
  1785. {
  1786. SDL_Event sEvent;
  1787. while(run)
  1788. {
  1789. try
  1790. {
  1791. if(SDL_PollEvent(&sEvent)) //wait for event...
  1792. {
  1793. menuItems * current = currentItems();
  1794. if(sEvent.type==SDL_QUIT)
  1795. {
  1796. exit(EXIT_SUCCESS);
  1797. return ret;
  1798. }
  1799. for (size_t i=0; i < interested.size(); ++i) {
  1800. interested[i]->handleIt(sEvent);
  1801. }
  1802. if (!current)
  1803. {
  1804. (this->*handleOther)(sEvent);
  1805. }
  1806. else if (sEvent.type==SDL_KEYDOWN)
  1807. {
  1808. if (sEvent.key.keysym.sym==SDLK_q)
  1809. {
  1810. exit(EXIT_SUCCESS);
  1811. }
  1812. if(sEvent.key.keysym.sym==SDLK_F4 && (sEvent.key.keysym.mod & KMOD_LALT)) //Alt+F4
  1813. {
  1814. exit(EXIT_SUCCESS);
  1815. }
  1816. /*if (state==EState::newGame)
  1817. {
  1818. switch (sEvent.key.keysym.sym)
  1819. {
  1820. case SDLK_LEFT:
  1821. {
  1822. if(currentItems()->lNewGame.x>0)
  1823. currentItems()->lNewGame.x--;
  1824. break;
  1825. }
  1826. case (SDLK_RIGHT):
  1827. {
  1828. currentItems()->lNewGame.x++;
  1829. break;
  1830. }
  1831. case (SDLK_UP):
  1832. {
  1833. if(currentItems()->lNewGame.y>0)
  1834. currentItems()->lNewGame.y--;
  1835. break;
  1836. }
  1837. case (SDLK_DOWN):
  1838. {
  1839. currentItems()->lNewGame.y++;
  1840. break;
  1841. }
  1842. }
  1843. showNewMenu();
  1844. }*/
  1845. }
  1846. else if (sEvent.type==SDL_MOUSEMOTION)
  1847. {
  1848. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  1849. if (currentMessage) continue;
  1850. if (current->highlighted)
  1851. {
  1852. switch (current->highlighted)
  1853. {
  1854. case 1:
  1855. {
  1856. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1857. continue;
  1858. else
  1859. {
  1860. current->highlighted=0;
  1861. highlightButton(1,0);
  1862. }
  1863. break;
  1864. }
  1865. case 2:
  1866. {
  1867. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1868. continue;
  1869. else
  1870. {
  1871. current->highlighted=0;
  1872. highlightButton(2,0);
  1873. }
  1874. break;
  1875. }
  1876. case 3:
  1877. {
  1878. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1879. continue;
  1880. else
  1881. {
  1882. current->highlighted=0;
  1883. highlightButton(3,0);
  1884. }
  1885. break;
  1886. }
  1887. case 4:
  1888. {
  1889. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1890. continue;
  1891. else
  1892. {
  1893. current->highlighted=0;
  1894. highlightButton(4,0);
  1895. }
  1896. break;
  1897. }
  1898. case 5:
  1899. {
  1900. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1901. continue;
  1902. else
  1903. {
  1904. current->highlighted=0;
  1905. highlightButton(5,0);
  1906. }
  1907. break;
  1908. }
  1909. } //switch (current->highlighted)
  1910. } // if (current->highlighted)
  1911. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1912. {
  1913. highlightButton(1,2);
  1914. current->highlighted=1;
  1915. }
  1916. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1917. {
  1918. highlightButton(2,2);
  1919. current->highlighted=2;
  1920. }
  1921. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1922. {
  1923. highlightButton(3,2);
  1924. current->highlighted=3;
  1925. }
  1926. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1927. {
  1928. highlightButton(4,2);
  1929. current->highlighted=4;
  1930. }
  1931. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1932. {
  1933. highlightButton(5,2);
  1934. current->highlighted=5;
  1935. }
  1936. }
  1937. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1938. {
  1939. mush->playClick();
  1940. for (size_t i=0; i < btns.size(); ++i)
  1941. {
  1942. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1943. {
  1944. btns[i]->press(true);
  1945. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,screen,&btns[i].pos);
  1946. //updateRect(&btns[i].pos);
  1947. }
  1948. }
  1949. if (currentMessage) continue;
  1950. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1951. {
  1952. highlightButton(1,1);
  1953. current->highlighted=1;
  1954. }
  1955. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1956. {
  1957. highlightButton(2,1);
  1958. current->highlighted=2;
  1959. }
  1960. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1961. {
  1962. highlightButton(3,1);
  1963. current->highlighted=3;
  1964. }
  1965. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1966. {
  1967. highlightButton(4,1);
  1968. current->highlighted=4;
  1969. }
  1970. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1971. {
  1972. highlightButton(5,1);
  1973. current->highlighted=5;
  1974. }
  1975. }
  1976. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1977. {
  1978. for (size_t i=0; i < btns.size(); ++i)
  1979. {
  1980. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1981. ((Button*)btns[i])->fun();
  1982. else
  1983. {
  1984. btns[i]->press(false);
  1985. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,screen,&btns[i].pos);
  1986. //updateRect(&btns[i].pos);
  1987. }
  1988. }
  1989. if (currentMessage) continue;
  1990. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1991. {
  1992. highlightButton(1,2);
  1993. current->highlighted=1;
  1994. (this->*(current->fNewGame))();
  1995. }
  1996. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1997. {
  1998. highlightButton(2,2);
  1999. current->highlighted=2;
  2000. (this->*(current->fLoadGame))();
  2001. }
  2002. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2003. {
  2004. highlightButton(3,2);
  2005. current->highlighted=3;
  2006. }
  2007. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2008. {
  2009. highlightButton(4,2);
  2010. current->highlighted=4;
  2011. }
  2012. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2013. {
  2014. highlightButton(5,2);
  2015. current->highlighted=5;
  2016. (this->*(current->fQuit))();
  2017. }
  2018. }
  2019. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  2020. {
  2021. if (currentMessage) continue;
  2022. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2023. {
  2024. showCenBox(buttonText(0));
  2025. }
  2026. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2027. {
  2028. showCenBox(buttonText(1));
  2029. }
  2030. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2031. {
  2032. showCenBox(buttonText(2));
  2033. }
  2034. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2035. {
  2036. showCenBox(buttonText(3));
  2037. }
  2038. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  2039. {
  2040. showCenBox(buttonText(4));
  2041. }
  2042. }
  2043. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  2044. {
  2045. hideBox();
  2046. }
  2047. }
  2048. }
  2049. catch(...)
  2050. { continue; }
  2051. CGI->curh->draw1();
  2052. SDL_Flip(screen);
  2053. CGI->curh->draw2();
  2054. SDL_Delay(20); //give time for other apps
  2055. }
  2056. return ret;
  2057. }
  2058. std::string CPreGame::buttonText(int which)
  2059. {
  2060. if (state==mainMenu)
  2061. {
  2062. switch (which)
  2063. {
  2064. case 0:
  2065. return CGI->generaltexth->zelp[3].second;
  2066. case 1:
  2067. return CGI->generaltexth->zelp[4].second;
  2068. case 2:
  2069. return CGI->generaltexth->zelp[5].second;
  2070. case 3:
  2071. return CGI->generaltexth->zelp[6].second;
  2072. case 4:
  2073. return CGI->generaltexth->zelp[7].second;
  2074. }
  2075. }
  2076. else if (state==newGame || state==loadGame)
  2077. {
  2078. switch (which)
  2079. {
  2080. case 0:
  2081. return CGI->generaltexth->zelp[10].second;
  2082. case 1:
  2083. return CGI->generaltexth->zelp[11].second;
  2084. case 2:
  2085. return CGI->generaltexth->zelp[12].second;
  2086. case 3:
  2087. return CGI->generaltexth->zelp[13].second;
  2088. case 4:
  2089. return CGI->generaltexth->zelp[14].second;
  2090. }
  2091. }
  2092. return std::string();
  2093. }
  2094. void CPreGame::quitAskBox()
  2095. {
  2096. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  2097. }
  2098. void CPreGame::sortMaps()
  2099. {
  2100. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(_name));
  2101. if(ourScenSel->mapsel.sortBy != _name)
  2102. std::stable_sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  2103. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0),false,true);
  2104. ourScenSel->mapsel.slid->whereAreWe=0;
  2105. ourScenSel->mapsel.slid->updateSlid();
  2106. printMapsFrom(0);
  2107. }
  2108. void CPreGame::setTurnLength(int on)
  2109. {
  2110. int min;
  2111. switch (on)
  2112. {
  2113. case 0:
  2114. min=1;
  2115. break;
  2116. case 1:
  2117. min=2;
  2118. break;
  2119. case 2:
  2120. min=4;
  2121. break;
  2122. case 3:
  2123. min=6;
  2124. break;
  2125. case 4:
  2126. min=8;
  2127. break;
  2128. case 5:
  2129. min=10;
  2130. break;
  2131. case 6:
  2132. min=15;
  2133. break;
  2134. case 7:
  2135. min=20;
  2136. break;
  2137. case 8:
  2138. min=25;
  2139. break;
  2140. case 9:
  2141. min=30;
  2142. break;
  2143. case 10:
  2144. min=0;
  2145. break;
  2146. default:
  2147. min=0;
  2148. break;
  2149. }
  2150. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),screen,&genRect(23,134,258,553));
  2151. updateRect(&genRect(23,134,258,553));
  2152. if (min)
  2153. {
  2154. std::ostringstream os;
  2155. os<<min<<" Minutes";
  2156. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  2157. }
  2158. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  2159. }
  2160. void CPreGame::showLoadMenu()
  2161. {
  2162. if (currentTab/*==&ourScenSel->mapsel*/)
  2163. currentTab->hide();
  2164. btns.clear();
  2165. interested.clear();
  2166. handleOther=NULL;
  2167. state = loadGame;
  2168. SDL_BlitSurface(ourLoadMenu->background,NULL,screen,NULL);
  2169. SDL_BlitSurface(ourLoadMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lNewGame);
  2170. SDL_BlitSurface(ourLoadMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lLoadGame);
  2171. SDL_BlitSurface(ourLoadMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lHighScores);
  2172. SDL_BlitSurface(ourLoadMenu->credits->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lCredits);
  2173. SDL_BlitSurface(ourLoadMenu->quit->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lQuit);
  2174. //SDL_Flip(screen);
  2175. CSDL_Ext::update(screen);
  2176. first = true;
  2177. }
  2178. void CPreGame::initLoadMenu()
  2179. {
  2180. ourLoadMenu = new menuItems();
  2181. ourLoadMenu->bgAd = BitmapHandler::loadBitmap("ZLOADGAM.bmp");
  2182. ourLoadMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  2183. blitAt(ourLoadMenu->bgAd,114,312,ourLoadMenu->background);
  2184. //loading menu buttons
  2185. ourLoadMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  2186. ourLoadMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  2187. ourLoadMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  2188. ourLoadMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  2189. ourLoadMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  2190. // single scenario
  2191. ourLoadMenu->lNewGame.h=ourLoadMenu->newGame->ourImages[0].bitmap->h;
  2192. ourLoadMenu->lNewGame.w=ourLoadMenu->newGame->ourImages[0].bitmap->w;
  2193. ourLoadMenu->lNewGame.x=545;
  2194. ourLoadMenu->lNewGame.y=4;
  2195. ourLoadMenu->fNewGame=&CPreGame::showScenSel;
  2196. //multiplayer
  2197. ourLoadMenu->lLoadGame.h=ourLoadMenu->loadGame->ourImages[0].bitmap->h;
  2198. ourLoadMenu->lLoadGame.w=ourLoadMenu->loadGame->ourImages[0].bitmap->w;
  2199. ourLoadMenu->lLoadGame.x=568;
  2200. ourLoadMenu->lLoadGame.y=120;
  2201. //campaign
  2202. ourLoadMenu->lHighScores.h=ourLoadMenu->highScores->ourImages[0].bitmap->h;
  2203. ourLoadMenu->lHighScores.w=ourLoadMenu->highScores->ourImages[0].bitmap->w;
  2204. ourLoadMenu->lHighScores.x=541;
  2205. ourLoadMenu->lHighScores.y=233;
  2206. //tutorial
  2207. ourLoadMenu->lCredits.h=ourLoadMenu->credits->ourImages[0].bitmap->h;
  2208. ourLoadMenu->lCredits.w=ourLoadMenu->credits->ourImages[0].bitmap->w;
  2209. ourLoadMenu->lCredits.x=545;
  2210. ourLoadMenu->lCredits.y=358;
  2211. //back
  2212. ourLoadMenu->lQuit.h=ourLoadMenu->quit->ourImages[0].bitmap->h;
  2213. ourLoadMenu->lQuit.w=ourLoadMenu->quit->ourImages[0].bitmap->w;
  2214. ourLoadMenu->lQuit.x=582;
  2215. ourLoadMenu->lQuit.y=464;
  2216. ourLoadMenu->fQuit=&CPreGame::showMainMenu;
  2217. ourLoadMenu->highlighted=0;
  2218. }
  2219. CPreGame::~CPreGame()
  2220. {
  2221. delete ourMainMenu;
  2222. delete ourNewMenu;
  2223. delete ourLoadMenu;
  2224. delete ok;
  2225. delete cancel;
  2226. }
  2227. CPreGame::menuItems::menuItems()
  2228. {
  2229. }
  2230. CPreGame::menuItems::~menuItems()
  2231. {
  2232. delete this->newGame;
  2233. delete this->loadGame;
  2234. delete this->highScores;
  2235. delete this->credits;
  2236. delete this->quit;
  2237. SDL_FreeSurface(bgAd);
  2238. SDL_FreeSurface(background);
  2239. }
  2240. ScenSel::ScenSel()
  2241. :
  2242. difficulty(new CPoinGroup()),
  2243. bEasy(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,difficulty,0),
  2244. bNormal(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,difficulty,1),
  2245. bHard(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,difficulty,2),
  2246. bExpert(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,difficulty,3),
  2247. bImpossible(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,difficulty,4),
  2248. bBack(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,CPG),CDefHandler::giveDef("SCNRBACK.DEF")),
  2249. bBegin(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRBEG.DEF")),
  2250. bScens(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2251. bRandom(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2252. bOptions(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,CPG),CDefHandler::giveDef("GSPBUTT.DEF"))
  2253. {
  2254. pressed=NULL;
  2255. listShowed=false;
  2256. if (rand()%2)
  2257. background = BitmapHandler::loadBitmap("ZPIC1000.bmp");
  2258. else
  2259. background = BitmapHandler::loadBitmap("ZPIC1001.bmp");
  2260. scenInf = BitmapHandler::loadBitmap("GSELPOP1.bmp");
  2261. randMap = BitmapHandler::loadBitmap("RANMAPBK.bmp");
  2262. options = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  2263. SDL_SetColorKey(scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(scenInf->format,0,255,255));
  2264. SDL_SetColorKey(randMap,SDL_SRCCOLORKEY,SDL_MapRGB(randMap->format,0,255,255));
  2265. SDL_SetColorKey(options,SDL_SRCCOLORKEY,SDL_MapRGB(options->format,0,255,255));
  2266. difficulty->type=1;
  2267. selectedDiff=-77;
  2268. difficulty->gdzie = &selectedDiff;
  2269. for (size_t i=0; i < bScens.imgs->ourImages.size(); ++i) {
  2270. CSDL_Ext::printAt(
  2271. CGI->generaltexth->allTexts[500],
  2272. 25+i,
  2273. 2+i,
  2274. GEOR13,
  2275. zwykly,
  2276. bScens.imgs->ourImages[i].bitmap
  2277. ); //"Show Available Scenarios"
  2278. }
  2279. for (size_t i=0; i < bRandom.imgs->ourImages.size(); ++i) {
  2280. CSDL_Ext::printAt(
  2281. CGI->generaltexth->allTexts[740],
  2282. 25+i,
  2283. 2+i,
  2284. GEOR13,
  2285. zwykly,
  2286. bRandom.imgs->ourImages[i].bitmap
  2287. );
  2288. }
  2289. for (size_t i=0; i < bOptions.imgs->ourImages.size(); ++i) {
  2290. CSDL_Ext::printAt(
  2291. CGI->generaltexth->allTexts[501],
  2292. 25+i,
  2293. 2+i,
  2294. GEOR13,
  2295. zwykly,
  2296. bOptions.imgs->ourImages[i].bitmap
  2297. ); //"Show Advanced Options"
  2298. }
  2299. }
  2300. ScenSel::~ScenSel()
  2301. {
  2302. delete difficulty;
  2303. SDL_FreeSurface(scenInf);
  2304. SDL_FreeSurface(randMap);
  2305. SDL_FreeSurface(background);
  2306. SDL_FreeSurface(options);
  2307. }