CPreGame.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef CPREGAME_H
  2. #define CPREGAME_H
  3. #include "SDL.h"
  4. #include "CSemiDefHandler.h"
  5. #include "CSemiLodHandler.h"
  6. #include "CPreGameTextHandler.h"
  7. #include "CMessage.h"
  8. #include "map.h"
  9. #include "CMusicHandler.h"
  10. class CPreGame;
  11. extern CPreGame * CPG;
  12. enum Ebonus {brandom=-1,bartifact, bgold, bresource};
  13. typedef void(CPreGame::*ttt)();
  14. template <class T=ttt> class CGroup;
  15. template <class T=ttt> class CPoinGroup ;
  16. struct HighButton
  17. {
  18. int ID;
  19. int type;
  20. SDL_Rect pos;
  21. CDefHandler* imgs;
  22. int state;
  23. HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel=false, int id=-1)
  24. {type=0;imgs=Imgs;selectable=Sel;selected=false;state=0;pos=Pos;ID=id;highlightable=false;};
  25. HighButton(){state=0;}
  26. bool selectable, selected;
  27. bool highlightable, highlighted;
  28. virtual void show();
  29. virtual void press(bool down=true);
  30. virtual void hover(bool on=true)=0;
  31. virtual void select(bool on=true)=0;
  32. };
  33. template <class T=ttt> struct Button: public HighButton
  34. {
  35. CGroup<T> * ourGroup;
  36. Button( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CGroup<T>* gr=NULL, int id=-1)
  37. :HighButton(Pos,Imgs,Sel,id),ourGroup(gr),fun(Fun){type=1;};
  38. Button(){ourGroup=NULL;type=1;};
  39. T fun;
  40. virtual void hover(bool on=true);
  41. virtual void select(bool on=true);
  42. };
  43. template <class T=ttt> struct SetrButton: public Button<T>
  44. {
  45. int key, * poin;
  46. virtual void press(bool down=true);
  47. SetrButton(){type=1;selectable=selected=false;state=0;highlightable=false;}
  48. };
  49. template<class T=CPreGame> class Slider
  50. { //
  51. public:
  52. bool vertical; // false means horizontal
  53. SDL_Rect pos; // position
  54. Button<void(Slider::*)()> up, down, //or left/right
  55. slider;
  56. int positionsAmnt, capacity;// capacity - amount of positions dispplayed at once
  57. int whereAreWe; // first displayed thing
  58. bool moving;
  59. void(T::*fun)(int);
  60. void clickDown(int x, int y, bool bzgl=true);
  61. void clickUp(int x, int y, bool bzgl=true);
  62. void mMove(int x, int y, bool bzgl=true);
  63. void moveUp();
  64. void moveDown();
  65. void deactivate();
  66. void activate();
  67. Slider(int x, int y, int h, int amnt, int cap, bool ver);
  68. void updateSlid();
  69. void handleIt(SDL_Event sev);
  70. };
  71. //template<class T=void(CPreGame::*)(int)>
  72. template<class T=ttt> struct IntBut: public Button<T>
  73. {
  74. public:
  75. int key;
  76. int * what;
  77. IntBut(){type=2;fun=NULL;highlightable=false;};
  78. void set(){*what=key;};
  79. };
  80. template<class T=ttt> struct IntSelBut: public Button<T>
  81. {
  82. public:
  83. CPoinGroup<T> * ourPoinGroup;
  84. int key;
  85. IntSelBut(){};
  86. IntSelBut( SDL_Rect Pos, T Fun,CDefHandler* Imgs, bool Sel=false, CPoinGroup<T>* gr=NULL, int My=-1)
  87. : Button(Pos,Fun,Imgs,Sel,gr),key(My){ourPoinGroup=gr;};
  88. void select(bool on=true) {(*this).Button::select(on);ourPoinGroup->setYour(this);}
  89. };
  90. template <class T=ttt> class CPoinGroup :public CGroup<T>
  91. {
  92. public:
  93. int * gdzie; //where (po polsku, bo by by³o s³owo kluczowe :/)
  94. void setYour(IntSelBut<T> * your){*gdzie=your->key;};
  95. };
  96. template <class T=ttt> class CGroup
  97. {
  98. public:
  99. Button<T> * selected;
  100. int type; // 1=sinsel
  101. CGroup():selected(NULL),type(0){};
  102. };
  103. struct StartInfo
  104. {
  105. struct PlayerSettings
  106. {
  107. int castle, hero, heroPortrait; //ID, if -1 then random, if -2 then none
  108. std::string heroName;
  109. Ebonus bonus;
  110. Ecolor color; //from 0 -
  111. int handicap;//0-no, 1-mild, 2-severe
  112. std::string name;
  113. };
  114. std::vector<PlayerSettings> playerInfos;
  115. int turnTime; //in minutes, 0=unlimited
  116. };
  117. class PreGameTab
  118. {
  119. public:
  120. bool showed;
  121. virtual void init()=0;
  122. virtual void show()=0;
  123. virtual void hide()=0;
  124. PreGameTab();
  125. };
  126. class RanSel : public PreGameTab
  127. {
  128. Button<> horcpl[9], horcte[9], conpl[9], conte[8], water[4], monster[4], //last is random
  129. size[4], twoLevel, showRand;
  130. CGroup<> *Ghorcpl, *Ghorcte, *Gconpl, *Gconte, *Gwater, *Gmonster, *Gsize;
  131. };
  132. class Options : public PreGameTab
  133. {
  134. bool inited;
  135. struct OptionSwitch:public HighButton
  136. {
  137. void hover(bool on=true){};
  138. void select(bool on=true){};
  139. OptionSwitch( SDL_Rect Pos, CDefHandler* Imgs, bool Left, int Which)
  140. :HighButton(Pos,Imgs,false,7),left(Left),which(Which)
  141. {selectable=false;highlightable=false;}
  142. void press(bool down=true);
  143. bool left;
  144. int playerID;
  145. int serialID;
  146. int which; //-1=castle;0=hero;1=bonus
  147. };
  148. struct PlayerFlag:public HighButton
  149. {
  150. int color;
  151. PlayerFlag(SDL_Rect Pos, CDefHandler* Imgs, int Color)
  152. :HighButton(Pos,Imgs,false,7),color(Color)
  153. {selectable=false;highlightable=true;}
  154. void hover(bool on=true);
  155. void press(bool down=true);
  156. void select(bool on=true){};
  157. };
  158. struct PlayerOptions
  159. {
  160. PlayerOptions(int serial, int player);
  161. Ecolor color;
  162. PlayerFlag flag;
  163. //SDL_Surface * bg;
  164. OptionSwitch Cleft, Cright, Hleft, Hright, Bleft, Bright;
  165. int nr;
  166. };
  167. public:
  168. Slider<> * turnLength;
  169. SDL_Surface * bg,
  170. * rHero, * rCastle, * nHero, * nCastle;
  171. std::vector<SDL_Surface*> bgs;
  172. std::vector<CDefHandler*> flags;
  173. CDefHandler //* castles, * heroes, * bonus,
  174. * left, * right,
  175. * bonuses;
  176. std::vector<PlayerOptions*> poptions;
  177. void show();
  178. void hide();
  179. void init();
  180. void showIcon (int what, int nr, bool abs); //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  181. Options(){inited=showed=false;};
  182. ~Options();
  183. };
  184. class MapSel : public PreGameTab
  185. {
  186. public:
  187. ESortBy sortBy;
  188. SDL_Surface * bg;
  189. int selected;
  190. CDefHandler * Dtypes, * Dvic;
  191. CDefHandler *Dsizes, * Dloss;
  192. std::vector<Mapa*> scenList;
  193. std::vector<SDL_Surface*> scenImgs;
  194. int current;
  195. std::vector<CMapInfo> ourMaps;
  196. IntBut<> small, medium, large, xlarge, all;
  197. SetrButton<> nrplayer, mapsize, type, name, viccon, loscon;
  198. Slider<> *slid, *descslid;
  199. int sizeFilter;
  200. int whichWL(int nr);
  201. int countWL();
  202. void show();
  203. void hide();
  204. void init();
  205. std::string gdiff(std::string ss);
  206. void printMaps(int from,int to=18, int at=0, bool abs=false);
  207. void select(int which, bool updateMapsList=true);
  208. void moveByOne(bool up);
  209. void printSelectedInfo();
  210. MapSel();
  211. ~MapSel();
  212. };
  213. class ScenSel
  214. {
  215. public:
  216. bool listShowed;
  217. //RanSel ransel;
  218. MapSel mapsel;
  219. SDL_Surface * background, *scenInf, *scenList, *randMap, *options ;
  220. Button<> bScens, bOptions, bRandom, bBegin, bBack;
  221. IntSelBut<> bEasy, bNormal, bHard, bExpert, bImpossible;
  222. Button<> * pressed;
  223. CPoinGroup<> * difficulty;
  224. std::vector<Mapa> maps;
  225. int selectedDiff;
  226. void initRanSel();
  227. void showRanSel();
  228. void hideRanSel();
  229. void genScenList();
  230. ~ScenSel(){delete difficulty;};
  231. } ;
  232. class CPreGame
  233. {
  234. public:
  235. HighButton * highlighted;
  236. PreGameTab* currentTab;
  237. StartInfo ret;
  238. bool run;
  239. std::vector<Slider<> *> interested;
  240. CMusicHandler * mush;
  241. CSemiLodHandler * slh ;
  242. std::vector<HighButton *> btns;
  243. CPreGameTextHandler * preth ;
  244. SDL_Rect * currentMessage;
  245. SDL_Surface * behindCurMes;
  246. CDefHandler *ok, *cancel;
  247. enum EState { //where are we?
  248. mainMenu, newGame, loadGame, ScenarioList
  249. } state;
  250. struct menuItems {
  251. SDL_Surface * background, *bgAd;
  252. CDefHandler *newGame, *loadGame, *highScores,*credits, *quit;
  253. SDL_Rect lNewGame, lLoadGame, lHighScores, lCredits, lQuit;
  254. ttt fNewGame, fLoadGame, fHighScores, fCredits, fQuit;
  255. int highlighted;//0=none; 1=new game; 2=load game; 3=high score; 4=credits; 5=quit
  256. } * ourMainMenu, * ourNewMenu;
  257. ScenSel * ourScenSel;
  258. Options * ourOptions;
  259. std::string map; //selected map
  260. std::vector<CSemiLodHandler *> handledLods;
  261. CPreGame(); //c-tor
  262. std::string buttonText(int which);
  263. menuItems * currentItems();
  264. void(CPreGame::*handleOther)(SDL_Event&);
  265. void scenHandleEv(SDL_Event& sEvent);
  266. void begin(){run=false;};
  267. void quitAskBox();
  268. void quit(){exit(0);};
  269. void initScenSel();
  270. void showScenSel();
  271. void showScenList();
  272. void initOptions();
  273. void showOptions();
  274. void initNewMenu();
  275. void showNewMenu();
  276. void showMainMenu();
  277. StartInfo runLoop(); // runs mainloop of PreGame
  278. void initMainMenu(); //loads components for main menu
  279. void highlightButton(int which, int on); //highlights one from 5 main menu buttons
  280. void showCenBox (std::string data); //
  281. void showAskBox (std::string data, void(*f1)(),void(*f2)());
  282. void hideBox ();
  283. void printMapsFrom(int from);
  284. void setTurnLength(int on);
  285. void sortMaps();
  286. };
  287. #endif //CPREGAME_H