CPreGame.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  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==winStandard)
  905. temp=11;
  906. else temp=ourMaps[(i-at)+from].victoryCondition;
  907. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  908. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == lossStandard)
  909. temp=3;
  910. else temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  911. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  912. blitAt(scenin,24,121+(i-at)*25);
  913. SDL_UpdateRect(screen,24,121+(i-at)*25,scenin->w,scenin->h);
  914. }
  915. SDL_FreeSurface(scenin);
  916. }
  917. int MapSel::whichWL(int nr)
  918. {
  919. int help=-1;
  920. for (int i=0;i<ourMaps.size();i++)
  921. {
  922. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  923. continue;
  924. else help++;
  925. if (help==nr)
  926. {
  927. help=i;
  928. break;
  929. }
  930. }
  931. return help;
  932. }
  933. void MapSel::hide()
  934. {
  935. if (!showed)return;
  936. PreGameTab::hide();
  937. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  938. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  939. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  940. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  941. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  942. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  943. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  944. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  945. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  946. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  947. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  948. slid->deactivate();
  949. CPG->currentTab = NULL;
  950. };
  951. void MapSel::show()
  952. {
  953. if (showed)return;
  954. PreGameTab::show();
  955. //blit bg
  956. blitAt(bg,3,6);
  957. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  958. CSDL_Ext::printAt("Select a Scenario to Play",110,25,TNRB16);
  959. //size buttons
  960. small.show();
  961. medium.show();
  962. large.show();
  963. xlarge.show();
  964. all.show();
  965. CPG->btns.push_back(&small);
  966. CPG->btns.push_back(&medium);
  967. CPG->btns.push_back(&large);
  968. CPG->btns.push_back(&xlarge);
  969. CPG->btns.push_back(&all);
  970. //sort by buttons
  971. nrplayer.show();
  972. mapsize.show();
  973. type.show();
  974. name.show();
  975. viccon.show();
  976. loscon.show();
  977. CPG->btns.push_back(&nrplayer);
  978. CPG->btns.push_back(&mapsize);
  979. CPG->btns.push_back(&type);
  980. CPG->btns.push_back(&name);
  981. CPG->btns.push_back(&viccon);
  982. CPG->btns.push_back(&loscon);
  983. //print scenario list
  984. printMaps(0,18);
  985. slid->activate();
  986. //SDL_Flip(screen);
  987. CSDL_Ext::update(screen);
  988. }
  989. void MapSel::processMaps(std::vector<std::string> &pliczkiTemp, int &index)
  990. {
  991. static boost::mutex mx;
  992. int pom=-1, read;
  993. unsigned char sss[1000];
  994. while(true)
  995. {
  996. mx.lock();
  997. if(index>=pliczkiTemp.size())
  998. {
  999. mx.unlock();
  1000. break;
  1001. }
  1002. else
  1003. {
  1004. pom = index++;
  1005. mx.unlock();
  1006. }
  1007. gzFile tempf = gzopen(pliczkiTemp[pom].c_str(),"rb");
  1008. read = gzread(tempf, sss, 1000);
  1009. gzclose(tempf);
  1010. if(read < 50)
  1011. {
  1012. tlog3<<"\t\tWarning: corrupted map file: "<<pliczkiTemp[pom]<<std::endl;
  1013. continue;
  1014. }
  1015. if (!sss[4]) //not a valid map
  1016. {
  1017. //tlog3 << "\t\tSkipping " << pliczkiTemp[pom] << " - map marked as unplayable.\n";
  1018. continue;
  1019. }
  1020. CMapInfo mi(pliczkiTemp[pom],sss);
  1021. mx.lock();
  1022. ourMaps.push_back(mi);
  1023. mx.unlock();
  1024. }
  1025. }
  1026. void MapSel::init()
  1027. {
  1028. //get map files names
  1029. std::vector<std::string> pliczkiTemp;
  1030. fs::path tie( (fs::initial_path<fs::path>())/"/maps" );
  1031. fs::directory_iterator end_iter;
  1032. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  1033. {
  1034. if (fs::is_regular_file(dir->status()));
  1035. {
  1036. if (boost::ends_with(dir->path().filename(),".h3m"))
  1037. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  1038. }
  1039. }
  1040. ourMaps.reserve(pliczkiTemp.size());
  1041. int mapInd=0;
  1042. boost::thread_group group;
  1043. int cores = std::max((unsigned int)1,boost::thread::hardware_concurrency());
  1044. for(int ti=0;ti<cores;ti++)
  1045. group.create_thread(boost::bind(&MapSel::processMaps,this,boost::ref(pliczkiTemp),boost::ref(mapInd)));
  1046. bg = BitmapHandler::loadBitmap("SCSELBCK.bmp");
  1047. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1048. small.imgs = CDefHandler::giveDef("SCSMBUT.DEF");
  1049. small.fun = NULL;
  1050. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  1051. small.ourGroup=NULL;
  1052. medium.imgs = CDefHandler::giveDef("SCMDBUT.DEF");
  1053. medium.fun = NULL;
  1054. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  1055. medium.ourGroup=NULL;
  1056. large.imgs = CDefHandler::giveDef("SCLGBUT.DEF");
  1057. large.fun = NULL;
  1058. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  1059. large.ourGroup=NULL;
  1060. xlarge.imgs = CDefHandler::giveDef("SCXLBUT.DEF");
  1061. xlarge.fun = NULL;
  1062. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  1063. xlarge.ourGroup=NULL;
  1064. all.imgs = CDefHandler::giveDef("SCALBUT.DEF");
  1065. all.fun = NULL;
  1066. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  1067. all.ourGroup=NULL;
  1068. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  1069. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  1070. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  1071. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  1072. nrplayer.imgs = CDefHandler::giveDef("SCBUTT1.DEF");
  1073. nrplayer.fun = NULL;
  1074. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  1075. nrplayer.key=_playerAm;
  1076. mapsize.imgs = CDefHandler::giveDef("SCBUTT2.DEF");
  1077. mapsize.fun = NULL;
  1078. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  1079. mapsize.key=_size;
  1080. type.imgs = CDefHandler::giveDef("SCBUTCP.DEF");
  1081. type.fun = NULL;
  1082. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  1083. type.key=_format;
  1084. name.imgs = CDefHandler::giveDef("SCBUTT3.DEF");
  1085. name.fun = NULL;
  1086. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  1087. name.key=_name;
  1088. viccon.imgs = CDefHandler::giveDef("SCBUTT4.DEF");
  1089. viccon.fun = NULL;
  1090. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  1091. viccon.key=_viccon;
  1092. loscon.imgs = CDefHandler::giveDef("SCBUTT5.DEF");
  1093. loscon.fun = NULL;
  1094. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  1095. loscon.key=_loscon;
  1096. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  1097. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=boost::bind(&CPreGame::sortMaps,CPG);
  1098. Dtypes = CDefHandler::giveDef("SCSELC.DEF");
  1099. Dvic = CDefHandler::giveDef("SCNRVICT.DEF");
  1100. Dloss = CDefHandler::giveDef("SCNRLOSS.DEF");
  1101. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  1102. Dsizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1103. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  1104. group.join_all();
  1105. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(_name));
  1106. slid = new Slider(375,92,480,ourMaps.size(),18,true);
  1107. slid->fun = boost::bind(&CPreGame::printMapsFrom,CPG,_1);
  1108. }
  1109. void MapSel::moveByOne(bool up)
  1110. {
  1111. int help=selected;
  1112. if (up) selected--;
  1113. else selected ++;
  1114. for (int i=selected;i<ourMaps.size() && i>=0;)
  1115. {
  1116. help=i;
  1117. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  1118. break;
  1119. if (up)
  1120. {
  1121. i--;
  1122. }
  1123. else
  1124. {
  1125. i++;
  1126. if (i<0) break;
  1127. }
  1128. }
  1129. select(help);
  1130. slid->updateSlid();
  1131. }
  1132. void MapSel::select(int which, bool updateMapsList, bool forceSettingsUpdate)
  1133. {
  1134. bool dontSaveSettings = ((selected!=which) || (CPG->ret.playerInfos.size()==0) || forceSettingsUpdate);
  1135. selected = which;
  1136. CPG->ret.mapname = ourMaps[selected].filename;
  1137. if(updateMapsList)
  1138. printMaps(slid->whereAreWe,18,0,true);
  1139. int serialC=0;
  1140. if(dontSaveSettings)
  1141. {
  1142. CPG->ret.playerInfos.clear();
  1143. bool wasntpl = true;
  1144. for (int i=0;i<PLAYER_LIMIT;i++)
  1145. {
  1146. if (!(ourMaps[selected].players[i].canComputerPlay || ourMaps[selected].players[i].canComputerPlay))
  1147. 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)
  1148. StartInfo::PlayerSettings pset;
  1149. pset.color=(Ecolor)i;
  1150. pset.serial = serialC;
  1151. serialC++;
  1152. pset.bonus=brandom;
  1153. pset.castle=-2;
  1154. if (ourMaps[which].players[i].canHumanPlay && wasntpl)
  1155. {
  1156. pset.name=CGI->generaltexth->allTexts[434]; //Player
  1157. pset.human = true;
  1158. CPG->playerColor = i;
  1159. wasntpl = false;
  1160. }
  1161. else
  1162. {
  1163. pset.name=CGI->generaltexth->allTexts[468];//Computer
  1164. pset.human = false;
  1165. }
  1166. for (int j=0;j<F_NUMBER;j++)
  1167. {
  1168. if (((int)pow((double)2,j))&ourMaps[selected].players[i].allowedFactions)
  1169. {
  1170. if (pset.castle>=0)
  1171. pset.castle=-1;
  1172. if (pset.castle==-2)
  1173. pset.castle=j;
  1174. }
  1175. }
  1176. pset.heroPortrait=-1;
  1177. if (!((ourMaps[which].players[i].generateHeroAtMainTown && ourMaps[which].players[i].hasMainTown) || ourMaps[which].players[i].p8))
  1178. pset.hero=-2;
  1179. else
  1180. pset.hero=-1;
  1181. if(ourMaps[which].players[i].mainHeroName.length())
  1182. {
  1183. pset.heroName = ourMaps[which].players[i].mainHeroName;
  1184. if((pset.heroPortrait = ourMaps[which].players[i].mainHeroPortrait)==255)
  1185. pset.heroPortrait = ourMaps[which].players[i].p9;
  1186. }
  1187. pset.handicap=0;
  1188. CPG->ret.playerInfos.push_back(pset);
  1189. }
  1190. }
  1191. printSelectedInfo();
  1192. }
  1193. MapSel::MapSel():selected(0),sizeFilter(0)
  1194. {
  1195. }
  1196. void MapSel::printSelectedInfo()
  1197. {
  1198. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),screen,&genRect(399,337,413,29));
  1199. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),screen,&genRect(50,91,414,453));
  1200. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bScens.pos);
  1201. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bOptions.pos);
  1202. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bRandom.pos);
  1203. //blit texts
  1204. CSDL_Ext::printAt(CGI->generaltexth->zelp[21].second,420,25,GEOR13);
  1205. CSDL_Ext::printAt(CGI->generaltexth->allTexts[496],420,135,GEOR13);
  1206. CSDL_Ext::printAt(CGI->generaltexth->allTexts[497],420,285,GEOR13);
  1207. CSDL_Ext::printAt(CGI->generaltexth->allTexts[498],420,340,GEOR13);
  1208. CSDL_Ext::printAt(CGI->generaltexth->allTexts[390],420,406,GEOR13,zwykly);
  1209. CSDL_Ext::printAt(CGI->generaltexth->allTexts[391],585,406,GEOR13,zwykly);
  1210. int temp = ourMaps[selected].victoryCondition+1;
  1211. if (temp>20) temp=0;
  1212. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1213. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1214. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1215. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  1216. if (temp>20) temp=0;
  1217. sss = CGI->generaltexth->lossCondtions[temp];
  1218. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1219. //blit descrption
  1220. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  1221. for (int i=0;i<desc.size();i++)
  1222. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1223. if ((selected < 0) || (selected >= ourMaps.size()))
  1224. return;
  1225. if (ourMaps[selected].name.length())
  1226. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  1227. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1228. std::string diff;
  1229. switch (ourMaps[selected].difficulty)
  1230. {
  1231. case 0:
  1232. diff=gdiff(CGI->generaltexth->zelp[24].second);
  1233. break;
  1234. case 1:
  1235. diff=gdiff(CGI->generaltexth->zelp[25].second);
  1236. break;
  1237. case 2:
  1238. diff=gdiff(CGI->generaltexth->zelp[26].second);
  1239. break;
  1240. case 3:
  1241. diff=gdiff(CGI->generaltexth->zelp[27].second);
  1242. break;
  1243. case 4:
  1244. diff=gdiff(CGI->generaltexth->zelp[28].second);
  1245. break;
  1246. }
  1247. temp=-1;
  1248. switch (ourMaps[selected].width)
  1249. {
  1250. case 36:
  1251. temp=0;
  1252. break;
  1253. case 72:
  1254. temp=1;
  1255. break;
  1256. case 108:
  1257. temp=2;
  1258. break;
  1259. case 144:
  1260. temp=3;
  1261. break;
  1262. default:
  1263. temp=4;
  1264. break;
  1265. }
  1266. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1267. temp=ourMaps[selected].victoryCondition;
  1268. if (temp>12) temp=11;
  1269. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1270. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  1271. if (temp>12) temp=3;
  1272. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1273. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1274. //SDL_Flip(screen);
  1275. printFlags();
  1276. CSDL_Ext::update(screen);
  1277. }
  1278. void MapSel::printFlags()
  1279. {
  1280. int hy=405, fx=460, ex=640, myT;
  1281. if (ourMaps[selected].howManyTeams)
  1282. myT = ourMaps[selected].players[CPG->playerColor].team;
  1283. else myT = -1;
  1284. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1285. {
  1286. if (myT>=0)
  1287. {
  1288. if(ourMaps[selected].players[CPG->ret.playerInfos[i].color].team==myT)
  1289. {
  1290. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1291. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1292. }
  1293. else
  1294. {
  1295. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1296. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1297. }
  1298. }
  1299. else
  1300. {
  1301. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1302. {
  1303. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1304. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1305. }
  1306. else
  1307. {
  1308. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1309. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. std::string MapSel::gdiff(std::string ss)
  1315. {
  1316. std::string ret;
  1317. for (int i=2;i<ss.length();i++)
  1318. {
  1319. if (ss[i]==' ')
  1320. break;
  1321. ret+=ss[i];
  1322. }
  1323. return ret;
  1324. }
  1325. void CPreGame::printRating()
  1326. {
  1327. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(47,83,271,449),screen,&genRect(47,83,666,455));
  1328. updateRect(&genRect(47,83,666,455));
  1329. std::string tob;
  1330. switch (ourScenSel->selectedDiff)
  1331. {
  1332. case 0:
  1333. tob="80%";
  1334. break;
  1335. case 1:
  1336. tob="100%";
  1337. break;
  1338. case 2:
  1339. tob="130%";
  1340. break;
  1341. case 3:
  1342. tob="160%";
  1343. break;
  1344. case 4:
  1345. tob="200%";
  1346. break;
  1347. }
  1348. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1349. }
  1350. void CPreGame::printMapsFrom(int from)
  1351. {
  1352. ourScenSel->mapsel.printMaps(from);
  1353. }
  1354. void CPreGame::showScenList()
  1355. {
  1356. if (currentTab!=&ourScenSel->mapsel)
  1357. {
  1358. ourScenSel->listShowed=true;
  1359. ourScenSel->mapsel.show();
  1360. }
  1361. else
  1362. {
  1363. currentTab->hide();
  1364. showScenSel();
  1365. }
  1366. }
  1367. CPreGame::CPreGame()
  1368. {
  1369. CPG=this;
  1370. highlighted=NULL;
  1371. currentTab=NULL;
  1372. run=true;
  1373. timeHandler tmh;tmh.getDif();
  1374. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1375. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1376. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1377. currentMessage=NULL;
  1378. behindCurMes=NULL;
  1379. initMainMenu();
  1380. tlog0<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1381. initNewMenu();
  1382. tlog0<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1383. initLoadMenu();
  1384. tlog0<<"\tCPreGame: loadgame menu initialization: "<<tmh.getDif()<<std::endl;
  1385. initScenSel();
  1386. tlog0<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1387. initOptions();
  1388. tlog0<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1389. showMainMenu();
  1390. tlog0<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1391. playerName="Player";
  1392. }
  1393. void CPreGame::initOptions()
  1394. {
  1395. ourOptions = new Options();
  1396. ourOptions->init();
  1397. }
  1398. void CPreGame::initScenSel()
  1399. {
  1400. ourScenSel = new ScenSel();
  1401. tlog5 << "\t\tLoaded graphics\n";
  1402. ourScenSel->mapsel.init();
  1403. tlog5 << "\t\tLoaded maps\n";
  1404. }
  1405. void CPreGame::showScenSel()
  1406. {
  1407. state=ScenarioList;
  1408. SDL_BlitSurface(ourScenSel->background,NULL,screen,NULL);
  1409. SDL_BlitSurface(ourScenSel->scenInf,NULL,screen,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1410. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1411. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1412. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1413. //blit buttons
  1414. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bEasy.pos);
  1415. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bNormal.pos);
  1416. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bHard.pos);
  1417. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bExpert.pos);
  1418. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bImpossible.pos);
  1419. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBegin.pos);
  1420. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBack.pos);
  1421. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1422. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1423. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1424. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1425. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1426. //add buttons info
  1427. if(first)
  1428. {
  1429. btns.push_back(&ourScenSel->bEasy);
  1430. btns.push_back(&ourScenSel->bNormal);
  1431. btns.push_back(&ourScenSel->bHard);
  1432. btns.push_back(&ourScenSel->bExpert);
  1433. btns.push_back(&ourScenSel->bImpossible);
  1434. btns.push_back(&ourScenSel->bScens);
  1435. btns.push_back(&ourScenSel->bRandom);
  1436. btns.push_back(&ourScenSel->bOptions);
  1437. btns.push_back(&ourScenSel->bBegin);
  1438. btns.push_back(&ourScenSel->bBack);
  1439. ourScenSel->selectedDiff=1;
  1440. ourScenSel->bNormal.select(true);
  1441. handleOther = &CPreGame::scenHandleEv;
  1442. ourScenSel->mapsel.select(0,false);
  1443. for (size_t i=0; i < btns.size(); ++i)
  1444. {
  1445. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1446. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1447. btns[i]->ID=10;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. ourScenSel->mapsel.select(ourScenSel->mapsel.selected,false);
  1453. switch (ourScenSel->selectedDiff)
  1454. {
  1455. case 0:
  1456. ourScenSel->bEasy.select(true);
  1457. break;
  1458. case 1:
  1459. ourScenSel->bNormal.select(true);
  1460. break;
  1461. case 2:
  1462. ourScenSel->bHard.select(true);
  1463. break;
  1464. case 3:
  1465. ourScenSel->bExpert.select(true);
  1466. break;
  1467. case 4:
  1468. ourScenSel->bImpossible.select(true);
  1469. break;
  1470. }
  1471. }
  1472. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1473. //SDL_Flip(screen);
  1474. CSDL_Ext::update(screen);
  1475. first = false;
  1476. }
  1477. void CPreGame::showOptions()
  1478. {
  1479. ourOptions->show();
  1480. }
  1481. void CPreGame::initNewMenu()
  1482. {
  1483. ourNewMenu = new menuItems();
  1484. ourNewMenu->bgAd = BitmapHandler::loadBitmap("ZNEWGAM.bmp");
  1485. ourNewMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  1486. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1487. //loading menu buttons
  1488. ourNewMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  1489. ourNewMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  1490. ourNewMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  1491. ourNewMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  1492. ourNewMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  1493. // single scenario
  1494. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1495. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1496. ourNewMenu->lNewGame.x=545;
  1497. ourNewMenu->lNewGame.y=4;
  1498. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1499. //multiplayer
  1500. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1501. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1502. ourNewMenu->lLoadGame.x=568;
  1503. ourNewMenu->lLoadGame.y=120;
  1504. //campaign
  1505. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1506. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1507. ourNewMenu->lHighScores.x=541;
  1508. ourNewMenu->lHighScores.y=233;
  1509. //tutorial
  1510. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1511. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1512. ourNewMenu->lCredits.x=545;
  1513. ourNewMenu->lCredits.y=358;
  1514. //back
  1515. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1516. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1517. ourNewMenu->lQuit.x=582;
  1518. ourNewMenu->lQuit.y=464;
  1519. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1520. ourNewMenu->highlighted=0;
  1521. }
  1522. void CPreGame::showNewMenu()
  1523. {
  1524. if (currentTab/*==&ourScenSel->mapsel*/)
  1525. currentTab->hide();
  1526. btns.clear();
  1527. interested.clear();
  1528. handleOther=NULL;
  1529. state = newGame;
  1530. SDL_BlitSurface(ourNewMenu->background,NULL,screen,NULL);
  1531. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lNewGame);
  1532. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lLoadGame);
  1533. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lHighScores);
  1534. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lCredits);
  1535. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lQuit);
  1536. //SDL_Flip(screen);
  1537. CSDL_Ext::update(screen);
  1538. first = true;
  1539. }
  1540. void CPreGame::initMainMenu()
  1541. {
  1542. ourMainMenu = new menuItems();
  1543. ourMainMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1544. //loading menu buttons
  1545. ourMainMenu->newGame = CDefHandler::giveDef("ZMENUNG.DEF");
  1546. ourMainMenu->loadGame = CDefHandler::giveDef("ZMENULG.DEF");
  1547. ourMainMenu->highScores = CDefHandler::giveDef("ZMENUHS.DEF");
  1548. ourMainMenu->credits = CDefHandler::giveDef("ZMENUCR.DEF");
  1549. ourMainMenu->quit = CDefHandler::giveDef("ZMENUQT.DEF");
  1550. ok = CDefHandler::giveDef("IOKAY.DEF");
  1551. cancel = CDefHandler::giveDef("ICANCEL.DEF");
  1552. // new game button location
  1553. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1554. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1555. ourMainMenu->lNewGame.x=540;
  1556. ourMainMenu->lNewGame.y=10;
  1557. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1558. //load game location
  1559. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1560. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1561. ourMainMenu->lLoadGame.x=532;
  1562. ourMainMenu->lLoadGame.y=132;
  1563. ourMainMenu->fLoadGame=&CPreGame::showLoadMenu;
  1564. //high scores
  1565. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1566. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1567. ourMainMenu->lHighScores.x=524;
  1568. ourMainMenu->lHighScores.y=251;
  1569. //credits
  1570. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1571. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1572. ourMainMenu->lCredits.x=557;
  1573. ourMainMenu->lCredits.y=359;
  1574. //quit
  1575. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1576. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1577. ourMainMenu->lQuit.x=586;
  1578. ourMainMenu->lQuit.y=468;
  1579. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1580. ourMainMenu->highlighted=0;
  1581. }
  1582. void CPreGame::showMainMenu()
  1583. {
  1584. state = mainMenu;
  1585. SDL_BlitSurface(ourMainMenu->background,NULL,screen,NULL);
  1586. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lNewGame);
  1587. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lLoadGame);
  1588. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lHighScores);
  1589. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lCredits);
  1590. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lQuit);
  1591. //SDL_Flip(screen);
  1592. CSDL_Ext::update(screen);
  1593. }
  1594. void CPreGame::highlightButton(int which, int on)
  1595. {
  1596. menuItems * current = currentItems();
  1597. switch (which)
  1598. {
  1599. case 1:
  1600. {
  1601. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,screen,&current->lNewGame);
  1602. break;
  1603. }
  1604. case 2:
  1605. {
  1606. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,screen,&current->lLoadGame);
  1607. break;
  1608. }
  1609. case 3:
  1610. {
  1611. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,screen,&current->lHighScores);
  1612. break;
  1613. }
  1614. case 4:
  1615. {
  1616. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,screen,&current->lCredits);
  1617. break;
  1618. }
  1619. case 5:
  1620. {
  1621. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,screen,&current->lQuit);
  1622. break;
  1623. }
  1624. }
  1625. //SDL_Flip(screen);
  1626. CSDL_Ext::update(screen);
  1627. }
  1628. void CPreGame::showCenBox (std::string data)
  1629. {
  1630. CMessage * cmh = new CMessage();
  1631. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data));
  1632. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1633. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1634. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1635. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1636. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1637. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1638. SDL_FreeSurface(infoBox);
  1639. currentMessage = new SDL_Rect(pos);
  1640. delete cmh;
  1641. }
  1642. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1643. {
  1644. CMessage * cmh = new CMessage();
  1645. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1646. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1647. przyciski->push_back(ok);
  1648. przyciski->push_back(cancel);
  1649. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data), yesOrNO, przyciski, btnspos);
  1650. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1651. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1652. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1653. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1654. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1655. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1656. SDL_FreeSurface(infoBox);
  1657. currentMessage = new SDL_Rect(pos);
  1658. (*btnspos)[0].x+=pos.x;
  1659. (*btnspos)[0].y+=pos.y;
  1660. (*btnspos)[1].x+=pos.x;
  1661. (*btnspos)[1].y+=pos.y;
  1662. btns.push_back(new Button((*btnspos)[0],boost::bind(&CPreGame::quit,this),ok,false, NULL,2));
  1663. btns.push_back(new Button((*btnspos)[1],boost::bind(&CPreGame::hideBox,this),cancel,false, NULL,2));
  1664. delete cmh;
  1665. delete przyciski;
  1666. delete btnspos;
  1667. }
  1668. void CPreGame::hideBox ()
  1669. {
  1670. SDL_BlitSurface(behindCurMes,NULL,screen,currentMessage);
  1671. SDL_UpdateRect
  1672. (screen,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1673. for (size_t i=0; i < btns.size(); ++i)
  1674. {
  1675. if (btns[i]->ID==2)
  1676. {
  1677. delete btns[i];
  1678. btns.erase(btns.begin()+i);
  1679. i--;
  1680. }
  1681. }
  1682. SDL_FreeSurface(behindCurMes);
  1683. delete currentMessage;
  1684. currentMessage = NULL;
  1685. behindCurMes=NULL;
  1686. }
  1687. CPreGame::menuItems * CPreGame::currentItems()
  1688. {
  1689. switch (state)
  1690. {
  1691. case mainMenu:
  1692. return ourMainMenu;
  1693. case newGame:
  1694. return ourNewMenu;
  1695. case loadGame:
  1696. return ourLoadMenu;
  1697. default:
  1698. return NULL;
  1699. }
  1700. }
  1701. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1702. {
  1703. if(sEvent.type == SDL_MOUSEMOTION)
  1704. {
  1705. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1706. }
  1707. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1708. {
  1709. for (size_t i=0; i < btns.size(); ++i)
  1710. {
  1711. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1712. {
  1713. btns[i]->press(true);
  1714. ourScenSel->pressed=(Button*)btns[i];
  1715. }
  1716. }
  1717. if ((currentTab==&ourScenSel->mapsel) && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1718. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1719. {
  1720. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1721. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1722. }
  1723. }
  1724. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1725. {
  1726. Button * prnr=ourScenSel->pressed;
  1727. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1728. {
  1729. ourScenSel->pressed->press(false);
  1730. ourScenSel->pressed=NULL;
  1731. }
  1732. for (size_t i=0; i < btns.size(); ++i)
  1733. {
  1734. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1735. {
  1736. if (btns[i]->selectable)
  1737. btns[i]->select(true);
  1738. if (btns[i]->type==1 && ((Button*)btns[i])->fun)
  1739. ((Button*)btns[i])->fun();
  1740. int zz = btns.size();
  1741. if (i>=zz)
  1742. break;
  1743. if (btns[i]==prnr && btns[i]->type==2)
  1744. {
  1745. ((IntBut*)(btns[i]))->set();
  1746. ourScenSel->mapsel.slid->whereAreWe=0;
  1747. ourScenSel->mapsel.slid->updateSlid();
  1748. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1749. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0));
  1750. ourScenSel->mapsel.printMaps(0);
  1751. }
  1752. }
  1753. }
  1754. }
  1755. else if (sEvent.type==SDL_MOUSEMOTION)
  1756. {
  1757. if (highlighted)
  1758. {
  1759. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1760. return;
  1761. else
  1762. {
  1763. highlighted->hover(false);
  1764. highlighted = NULL;
  1765. }
  1766. }
  1767. for (size_t i=0; i < btns.size(); ++i)
  1768. {
  1769. if (!btns[i]->highlightable)
  1770. continue;
  1771. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1772. {
  1773. btns[i]->hover(true);
  1774. highlighted=btns[i];
  1775. return;
  1776. }
  1777. else if (btns[i]->highlighted)
  1778. btns[i]->hover(false);
  1779. }
  1780. }
  1781. }
  1782. StartInfo CPreGame::runLoop()
  1783. {
  1784. SDL_Event sEvent;
  1785. while(run)
  1786. {
  1787. try
  1788. {
  1789. if(SDL_PollEvent(&sEvent)) //wait for event...
  1790. {
  1791. menuItems * current = currentItems();
  1792. if(sEvent.type==SDL_QUIT)
  1793. {
  1794. exit(EXIT_SUCCESS);
  1795. return ret;
  1796. }
  1797. for (size_t i=0; i < interested.size(); ++i) {
  1798. interested[i]->handleIt(sEvent);
  1799. }
  1800. if (!current)
  1801. {
  1802. (this->*handleOther)(sEvent);
  1803. }
  1804. else if (sEvent.type==SDL_KEYDOWN)
  1805. {
  1806. if (sEvent.key.keysym.sym==SDLK_q)
  1807. {
  1808. exit(EXIT_SUCCESS);
  1809. }
  1810. if(sEvent.key.keysym.sym==SDLK_F4 && (sEvent.key.keysym.mod & KMOD_LALT)) //Alt+F4
  1811. {
  1812. exit(EXIT_SUCCESS);
  1813. }
  1814. /*if (state==EState::newGame)
  1815. {
  1816. switch (sEvent.key.keysym.sym)
  1817. {
  1818. case SDLK_LEFT:
  1819. {
  1820. if(currentItems()->lNewGame.x>0)
  1821. currentItems()->lNewGame.x--;
  1822. break;
  1823. }
  1824. case (SDLK_RIGHT):
  1825. {
  1826. currentItems()->lNewGame.x++;
  1827. break;
  1828. }
  1829. case (SDLK_UP):
  1830. {
  1831. if(currentItems()->lNewGame.y>0)
  1832. currentItems()->lNewGame.y--;
  1833. break;
  1834. }
  1835. case (SDLK_DOWN):
  1836. {
  1837. currentItems()->lNewGame.y++;
  1838. break;
  1839. }
  1840. }
  1841. showNewMenu();
  1842. }*/
  1843. }
  1844. else if (sEvent.type==SDL_MOUSEMOTION)
  1845. {
  1846. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  1847. if (currentMessage) continue;
  1848. if (current->highlighted)
  1849. {
  1850. switch (current->highlighted)
  1851. {
  1852. case 1:
  1853. {
  1854. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1855. continue;
  1856. else
  1857. {
  1858. current->highlighted=0;
  1859. highlightButton(1,0);
  1860. }
  1861. break;
  1862. }
  1863. case 2:
  1864. {
  1865. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1866. continue;
  1867. else
  1868. {
  1869. current->highlighted=0;
  1870. highlightButton(2,0);
  1871. }
  1872. break;
  1873. }
  1874. case 3:
  1875. {
  1876. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1877. continue;
  1878. else
  1879. {
  1880. current->highlighted=0;
  1881. highlightButton(3,0);
  1882. }
  1883. break;
  1884. }
  1885. case 4:
  1886. {
  1887. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1888. continue;
  1889. else
  1890. {
  1891. current->highlighted=0;
  1892. highlightButton(4,0);
  1893. }
  1894. break;
  1895. }
  1896. case 5:
  1897. {
  1898. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1899. continue;
  1900. else
  1901. {
  1902. current->highlighted=0;
  1903. highlightButton(5,0);
  1904. }
  1905. break;
  1906. }
  1907. } //switch (current->highlighted)
  1908. } // if (current->highlighted)
  1909. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1910. {
  1911. highlightButton(1,2);
  1912. current->highlighted=1;
  1913. }
  1914. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1915. {
  1916. highlightButton(2,2);
  1917. current->highlighted=2;
  1918. }
  1919. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1920. {
  1921. highlightButton(3,2);
  1922. current->highlighted=3;
  1923. }
  1924. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1925. {
  1926. highlightButton(4,2);
  1927. current->highlighted=4;
  1928. }
  1929. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1930. {
  1931. highlightButton(5,2);
  1932. current->highlighted=5;
  1933. }
  1934. }
  1935. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1936. {
  1937. mush->playClick();
  1938. for (size_t i=0; i < btns.size(); ++i)
  1939. {
  1940. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1941. {
  1942. btns[i]->press(true);
  1943. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,screen,&btns[i].pos);
  1944. //updateRect(&btns[i].pos);
  1945. }
  1946. }
  1947. if (currentMessage) continue;
  1948. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1949. {
  1950. highlightButton(1,1);
  1951. current->highlighted=1;
  1952. }
  1953. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1954. {
  1955. highlightButton(2,1);
  1956. current->highlighted=2;
  1957. }
  1958. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1959. {
  1960. highlightButton(3,1);
  1961. current->highlighted=3;
  1962. }
  1963. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1964. {
  1965. highlightButton(4,1);
  1966. current->highlighted=4;
  1967. }
  1968. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1969. {
  1970. highlightButton(5,1);
  1971. current->highlighted=5;
  1972. }
  1973. }
  1974. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1975. {
  1976. for (size_t i=0; i < btns.size(); ++i)
  1977. {
  1978. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1979. ((Button*)btns[i])->fun();
  1980. else
  1981. {
  1982. btns[i]->press(false);
  1983. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,screen,&btns[i].pos);
  1984. //updateRect(&btns[i].pos);
  1985. }
  1986. }
  1987. if (currentMessage) continue;
  1988. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1989. {
  1990. highlightButton(1,2);
  1991. current->highlighted=1;
  1992. (this->*(current->fNewGame))();
  1993. }
  1994. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1995. {
  1996. highlightButton(2,2);
  1997. current->highlighted=2;
  1998. (this->*(current->fLoadGame))();
  1999. }
  2000. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2001. {
  2002. highlightButton(3,2);
  2003. current->highlighted=3;
  2004. }
  2005. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2006. {
  2007. highlightButton(4,2);
  2008. current->highlighted=4;
  2009. }
  2010. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2011. {
  2012. highlightButton(5,2);
  2013. current->highlighted=5;
  2014. (this->*(current->fQuit))();
  2015. }
  2016. }
  2017. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  2018. {
  2019. if (currentMessage) continue;
  2020. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2021. {
  2022. showCenBox(buttonText(0));
  2023. }
  2024. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2025. {
  2026. showCenBox(buttonText(1));
  2027. }
  2028. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2029. {
  2030. showCenBox(buttonText(2));
  2031. }
  2032. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2033. {
  2034. showCenBox(buttonText(3));
  2035. }
  2036. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  2037. {
  2038. showCenBox(buttonText(4));
  2039. }
  2040. }
  2041. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  2042. {
  2043. hideBox();
  2044. }
  2045. }
  2046. }
  2047. catch(...)
  2048. { continue; }
  2049. CGI->curh->draw1();
  2050. SDL_Flip(screen);
  2051. CGI->curh->draw2();
  2052. SDL_Delay(20); //give time for other apps
  2053. }
  2054. return ret;
  2055. }
  2056. std::string CPreGame::buttonText(int which)
  2057. {
  2058. if (state==mainMenu)
  2059. {
  2060. switch (which)
  2061. {
  2062. case 0:
  2063. return CGI->generaltexth->zelp[3].second;
  2064. case 1:
  2065. return CGI->generaltexth->zelp[4].second;
  2066. case 2:
  2067. return CGI->generaltexth->zelp[5].second;
  2068. case 3:
  2069. return CGI->generaltexth->zelp[6].second;
  2070. case 4:
  2071. return CGI->generaltexth->zelp[7].second;
  2072. }
  2073. }
  2074. else if (state==newGame || state==loadGame)
  2075. {
  2076. switch (which)
  2077. {
  2078. case 0:
  2079. return CGI->generaltexth->zelp[10].second;
  2080. case 1:
  2081. return CGI->generaltexth->zelp[11].second;
  2082. case 2:
  2083. return CGI->generaltexth->zelp[12].second;
  2084. case 3:
  2085. return CGI->generaltexth->zelp[13].second;
  2086. case 4:
  2087. return CGI->generaltexth->zelp[14].second;
  2088. }
  2089. }
  2090. return std::string();
  2091. }
  2092. void CPreGame::quitAskBox()
  2093. {
  2094. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  2095. }
  2096. void CPreGame::sortMaps()
  2097. {
  2098. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(_name));
  2099. if(ourScenSel->mapsel.sortBy != _name)
  2100. std::stable_sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  2101. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0),false,true);
  2102. ourScenSel->mapsel.slid->whereAreWe=0;
  2103. ourScenSel->mapsel.slid->updateSlid();
  2104. printMapsFrom(0);
  2105. }
  2106. void CPreGame::setTurnLength(int on)
  2107. {
  2108. int min;
  2109. switch (on)
  2110. {
  2111. case 0:
  2112. min=1;
  2113. break;
  2114. case 1:
  2115. min=2;
  2116. break;
  2117. case 2:
  2118. min=4;
  2119. break;
  2120. case 3:
  2121. min=6;
  2122. break;
  2123. case 4:
  2124. min=8;
  2125. break;
  2126. case 5:
  2127. min=10;
  2128. break;
  2129. case 6:
  2130. min=15;
  2131. break;
  2132. case 7:
  2133. min=20;
  2134. break;
  2135. case 8:
  2136. min=25;
  2137. break;
  2138. case 9:
  2139. min=30;
  2140. break;
  2141. case 10:
  2142. min=0;
  2143. break;
  2144. default:
  2145. min=0;
  2146. break;
  2147. }
  2148. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),screen,&genRect(23,134,258,553));
  2149. updateRect(&genRect(23,134,258,553));
  2150. if (min)
  2151. {
  2152. std::ostringstream os;
  2153. os<<min<<" Minutes";
  2154. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  2155. }
  2156. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  2157. }
  2158. void CPreGame::showLoadMenu()
  2159. {
  2160. if (currentTab/*==&ourScenSel->mapsel*/)
  2161. currentTab->hide();
  2162. btns.clear();
  2163. interested.clear();
  2164. handleOther=NULL;
  2165. state = loadGame;
  2166. SDL_BlitSurface(ourLoadMenu->background,NULL,screen,NULL);
  2167. SDL_BlitSurface(ourLoadMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lNewGame);
  2168. SDL_BlitSurface(ourLoadMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lLoadGame);
  2169. SDL_BlitSurface(ourLoadMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lHighScores);
  2170. SDL_BlitSurface(ourLoadMenu->credits->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lCredits);
  2171. SDL_BlitSurface(ourLoadMenu->quit->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lQuit);
  2172. //SDL_Flip(screen);
  2173. CSDL_Ext::update(screen);
  2174. first = true;
  2175. }
  2176. void CPreGame::initLoadMenu()
  2177. {
  2178. ourLoadMenu = new menuItems();
  2179. ourLoadMenu->bgAd = BitmapHandler::loadBitmap("ZLOADGAM.bmp");
  2180. ourLoadMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  2181. blitAt(ourLoadMenu->bgAd,114,312,ourLoadMenu->background);
  2182. //loading menu buttons
  2183. ourLoadMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  2184. ourLoadMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  2185. ourLoadMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  2186. ourLoadMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  2187. ourLoadMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  2188. // single scenario
  2189. ourLoadMenu->lNewGame.h=ourLoadMenu->newGame->ourImages[0].bitmap->h;
  2190. ourLoadMenu->lNewGame.w=ourLoadMenu->newGame->ourImages[0].bitmap->w;
  2191. ourLoadMenu->lNewGame.x=545;
  2192. ourLoadMenu->lNewGame.y=4;
  2193. ourLoadMenu->fNewGame=&CPreGame::showScenSel;
  2194. //multiplayer
  2195. ourLoadMenu->lLoadGame.h=ourLoadMenu->loadGame->ourImages[0].bitmap->h;
  2196. ourLoadMenu->lLoadGame.w=ourLoadMenu->loadGame->ourImages[0].bitmap->w;
  2197. ourLoadMenu->lLoadGame.x=568;
  2198. ourLoadMenu->lLoadGame.y=120;
  2199. //campaign
  2200. ourLoadMenu->lHighScores.h=ourLoadMenu->highScores->ourImages[0].bitmap->h;
  2201. ourLoadMenu->lHighScores.w=ourLoadMenu->highScores->ourImages[0].bitmap->w;
  2202. ourLoadMenu->lHighScores.x=541;
  2203. ourLoadMenu->lHighScores.y=233;
  2204. //tutorial
  2205. ourLoadMenu->lCredits.h=ourLoadMenu->credits->ourImages[0].bitmap->h;
  2206. ourLoadMenu->lCredits.w=ourLoadMenu->credits->ourImages[0].bitmap->w;
  2207. ourLoadMenu->lCredits.x=545;
  2208. ourLoadMenu->lCredits.y=358;
  2209. //back
  2210. ourLoadMenu->lQuit.h=ourLoadMenu->quit->ourImages[0].bitmap->h;
  2211. ourLoadMenu->lQuit.w=ourLoadMenu->quit->ourImages[0].bitmap->w;
  2212. ourLoadMenu->lQuit.x=582;
  2213. ourLoadMenu->lQuit.y=464;
  2214. ourLoadMenu->fQuit=&CPreGame::showMainMenu;
  2215. ourLoadMenu->highlighted=0;
  2216. }
  2217. CPreGame::~CPreGame()
  2218. {
  2219. delete ourMainMenu;
  2220. delete ourNewMenu;
  2221. delete ourLoadMenu;
  2222. delete ok;
  2223. delete cancel;
  2224. }
  2225. CPreGame::menuItems::menuItems()
  2226. {
  2227. }
  2228. CPreGame::menuItems::~menuItems()
  2229. {
  2230. delete this->newGame;
  2231. delete this->loadGame;
  2232. delete this->highScores;
  2233. delete this->credits;
  2234. delete this->quit;
  2235. SDL_FreeSurface(bgAd);
  2236. SDL_FreeSurface(background);
  2237. }
  2238. ScenSel::ScenSel()
  2239. :
  2240. difficulty(new CPoinGroup()),
  2241. bEasy(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,difficulty,0),
  2242. bNormal(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,difficulty,1),
  2243. bHard(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,difficulty,2),
  2244. bExpert(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,difficulty,3),
  2245. bImpossible(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,difficulty,4),
  2246. bBack(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,CPG),CDefHandler::giveDef("SCNRBACK.DEF")),
  2247. bBegin(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRBEG.DEF")),
  2248. bScens(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2249. bRandom(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2250. bOptions(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,CPG),CDefHandler::giveDef("GSPBUTT.DEF"))
  2251. {
  2252. pressed=NULL;
  2253. listShowed=false;
  2254. if (rand()%2)
  2255. background = BitmapHandler::loadBitmap("ZPIC1000.bmp");
  2256. else
  2257. background = BitmapHandler::loadBitmap("ZPIC1001.bmp");
  2258. scenInf = BitmapHandler::loadBitmap("GSELPOP1.bmp");
  2259. randMap = BitmapHandler::loadBitmap("RANMAPBK.bmp");
  2260. options = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  2261. SDL_SetColorKey(scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(scenInf->format,0,255,255));
  2262. SDL_SetColorKey(randMap,SDL_SRCCOLORKEY,SDL_MapRGB(randMap->format,0,255,255));
  2263. SDL_SetColorKey(options,SDL_SRCCOLORKEY,SDL_MapRGB(options->format,0,255,255));
  2264. difficulty->type=1;
  2265. selectedDiff=-77;
  2266. difficulty->gdzie = &selectedDiff;
  2267. for (size_t i=0; i < bScens.imgs->ourImages.size(); ++i) {
  2268. CSDL_Ext::printAt(
  2269. CGI->generaltexth->allTexts[500],
  2270. 25+i,
  2271. 2+i,
  2272. GEOR13,
  2273. zwykly,
  2274. bScens.imgs->ourImages[i].bitmap
  2275. ); //"Show Available Scenarios"
  2276. }
  2277. for (size_t i=0; i < bRandom.imgs->ourImages.size(); ++i) {
  2278. CSDL_Ext::printAt(
  2279. CGI->generaltexth->allTexts[740],
  2280. 25+i,
  2281. 2+i,
  2282. GEOR13,
  2283. zwykly,
  2284. bRandom.imgs->ourImages[i].bitmap
  2285. );
  2286. }
  2287. for (size_t i=0; i < bOptions.imgs->ourImages.size(); ++i) {
  2288. CSDL_Ext::printAt(
  2289. CGI->generaltexth->allTexts[501],
  2290. 25+i,
  2291. 2+i,
  2292. GEOR13,
  2293. zwykly,
  2294. bOptions.imgs->ourImages[i].bitmap
  2295. ); //"Show Advanced Options"
  2296. }
  2297. }
  2298. ScenSel::~ScenSel()
  2299. {
  2300. delete difficulty;
  2301. SDL_FreeSurface(scenInf);
  2302. SDL_FreeSurface(randMap);
  2303. SDL_FreeSurface(background);
  2304. SDL_FreeSurface(options);
  2305. }