CPreGame.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "stdafx.h"
  2. #include "CPreGame.h"
  3. #include "SDL.h"
  4. extern SDL_Surface * ekran;
  5. bool isItIn(const SDL_Rect * rect, int x, int y)
  6. {
  7. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  8. return true;
  9. else return false;
  10. }
  11. CPreGame::CPreGame()
  12. {
  13. initMainMenu();
  14. showMainMenu();
  15. }
  16. void CPreGame::initMainMenu()
  17. {
  18. ourMainMenu = new menuItems();
  19. ourMainMenu->background = SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  20. CSemiLodHandler * slh = new CSemiLodHandler();
  21. slh->openLod("H3sprite.lod");
  22. //loading menu buttons
  23. ourMainMenu->newGame = slh->giveDef("ZMENUNG.DEF");
  24. ourMainMenu->loadGame = slh->giveDef("ZMENULG.DEF");
  25. ourMainMenu->highScores = slh->giveDef("ZMENUHS.DEF");
  26. ourMainMenu->credits = slh->giveDef("ZMENUCR.DEF");
  27. ourMainMenu->quit = slh->giveDef("ZMENUQT.DEF");
  28. // new game button location
  29. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  30. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  31. ourMainMenu->lNewGame.x=540;
  32. ourMainMenu->lNewGame.y=10;
  33. //load game location
  34. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  35. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  36. ourMainMenu->lLoadGame.x=532;
  37. ourMainMenu->lLoadGame.y=132;
  38. //high scores
  39. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  40. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  41. ourMainMenu->lHighScores.x=524;
  42. ourMainMenu->lHighScores.y=251;
  43. //credits
  44. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  45. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  46. ourMainMenu->lCredits.x=557;
  47. ourMainMenu->lCredits.y=359;
  48. //quit
  49. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  50. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  51. ourMainMenu->lQuit.x=586;
  52. ourMainMenu->lQuit.y=469;
  53. ourMainMenu->highlighted=0;
  54. handledLods.push_back(slh);
  55. delete slh;
  56. }
  57. void CPreGame::showMainMenu()
  58. {
  59. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  60. SDL_Flip(ekran);
  61. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  62. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  63. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  64. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  65. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  66. SDL_Flip(ekran);
  67. }
  68. void CPreGame::highlightButton(int which, int on)
  69. {
  70. switch (which)
  71. {
  72. case 1:
  73. {
  74. SDL_BlitSurface(ourMainMenu->newGame->ourImages[on].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  75. break;
  76. }
  77. case 2:
  78. {
  79. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[on].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  80. break;
  81. }
  82. case 3:
  83. {
  84. SDL_BlitSurface(ourMainMenu->highScores->ourImages[on].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  85. break;
  86. }
  87. case 4:
  88. {
  89. SDL_BlitSurface(ourMainMenu->credits->ourImages[on].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  90. break;
  91. }
  92. case 5:
  93. {
  94. SDL_BlitSurface(ourMainMenu->quit->ourImages[on].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  95. break;
  96. }
  97. }
  98. SDL_Flip(ekran);
  99. }
  100. void CPreGame::runLoop()
  101. {
  102. SDL_Event sEvent;
  103. while(true)
  104. {
  105. try
  106. {
  107. if(SDL_PollEvent(&sEvent)) //wait for event...
  108. {
  109. if(sEvent.type==SDL_QUIT)
  110. return ;
  111. else if (sEvent.type==SDL_MOUSEMOTION)
  112. {
  113. if (ourMainMenu->highlighted)
  114. {
  115. switch (ourMainMenu->highlighted)
  116. {
  117. case 1:
  118. {
  119. if(isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y))
  120. continue;
  121. else
  122. {
  123. ourMainMenu->highlighted=0;
  124. highlightButton(1,0);
  125. }
  126. break;
  127. }
  128. case 2:
  129. {
  130. if(isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  131. continue;
  132. else
  133. {
  134. ourMainMenu->highlighted=0;
  135. highlightButton(2,0);
  136. }
  137. break;
  138. }
  139. case 3:
  140. {
  141. if(isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y))
  142. continue;
  143. else
  144. {
  145. ourMainMenu->highlighted=0;
  146. highlightButton(3,0);
  147. }
  148. break;
  149. }
  150. case 4:
  151. {
  152. if(isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y))
  153. continue;
  154. else
  155. {
  156. ourMainMenu->highlighted=0;
  157. highlightButton(4,0);
  158. }
  159. break;
  160. }
  161. case 5:
  162. {
  163. if(isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  164. continue;
  165. else
  166. {
  167. ourMainMenu->highlighted=0;
  168. highlightButton(5,0);
  169. }
  170. break;
  171. }
  172. } //switch (ourMainMenu->highlighted)
  173. } // if (ourMainMenu->highlighted)
  174. if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y))
  175. {
  176. highlightButton(1,2);
  177. ourMainMenu->highlighted=1;
  178. }
  179. else if (isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  180. {
  181. highlightButton(2,2);
  182. ourMainMenu->highlighted=2;
  183. }
  184. else if (isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y))
  185. {
  186. highlightButton(3,2);
  187. ourMainMenu->highlighted=3;
  188. }
  189. else if (isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y))
  190. {
  191. highlightButton(4,2);
  192. ourMainMenu->highlighted=4;
  193. }
  194. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  195. {
  196. highlightButton(5,2);
  197. ourMainMenu->highlighted=5;
  198. }
  199. }
  200. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && SDL_BUTTON(1))
  201. {
  202. if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y))
  203. {
  204. highlightButton(1,1);
  205. ourMainMenu->highlighted=1;
  206. }
  207. else if (isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  208. {
  209. highlightButton(2,1);
  210. ourMainMenu->highlighted=2;
  211. }
  212. else if (isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y))
  213. {
  214. highlightButton(3,1);
  215. ourMainMenu->highlighted=3;
  216. }
  217. else if (isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y))
  218. {
  219. highlightButton(4,1);
  220. ourMainMenu->highlighted=4;
  221. }
  222. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  223. {
  224. highlightButton(5,1);
  225. ourMainMenu->highlighted=5;
  226. }
  227. }
  228. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && SDL_BUTTON(1))
  229. {
  230. if (isItIn(&ourMainMenu->lNewGame,sEvent.motion.x,sEvent.motion.y))
  231. {
  232. highlightButton(1,2);
  233. ourMainMenu->highlighted=1;
  234. }
  235. else if (isItIn(&ourMainMenu->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  236. {
  237. highlightButton(2,2);
  238. ourMainMenu->highlighted=2;
  239. }
  240. else if (isItIn(&ourMainMenu->lHighScores,sEvent.motion.x,sEvent.motion.y))
  241. {
  242. highlightButton(3,2);
  243. ourMainMenu->highlighted=3;
  244. }
  245. else if (isItIn(&ourMainMenu->lCredits,sEvent.motion.x,sEvent.motion.y))
  246. {
  247. highlightButton(4,2);
  248. ourMainMenu->highlighted=4;
  249. }
  250. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  251. {
  252. highlightButton(5,2);
  253. ourMainMenu->highlighted=5;
  254. }
  255. }
  256. else if (sEvent.type==SDL_KEYDOWN)
  257. {
  258. switch (sEvent.key.keysym.sym)
  259. {
  260. //case SDLK_LEFT:
  261. // {
  262. // if(ourMainMenu->lQuit.x>0)
  263. // ourMainMenu->lQuit.x--;
  264. // break;
  265. // }
  266. //case (SDLK_RIGHT):
  267. // {
  268. // ourMainMenu->lQuit.x++;
  269. // break;
  270. // }
  271. //case (SDLK_UP):
  272. // {
  273. // if(ourMainMenu->lQuit.y>0)
  274. // ourMainMenu->lQuit.y--;
  275. // break;
  276. // }
  277. //case (SDLK_DOWN):
  278. // {
  279. // ourMainMenu->lQuit.y++;
  280. // break;
  281. // }
  282. case (SDLK_q):
  283. {
  284. return ;
  285. break;
  286. }
  287. }
  288. showMainMenu();
  289. }
  290. }
  291. }
  292. catch(...)
  293. { continue; }
  294. SDL_Delay(30); //give time for other apps
  295. }
  296. }