CPreGame.cpp 71 KB

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