CGameInterface.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "stdafx.h"
  2. #include "CGameInterface.h"
  3. #include "CAdvMapInterface.h"
  4. #include "CMessage.h"
  5. #include "SDL_Extensions.h"
  6. #include "SDL_framerate.h"
  7. using namespace CSDL_Ext;
  8. CButtonBase::CButtonBase()
  9. {
  10. type=-1;
  11. abs=false;
  12. active=false;
  13. ourObj=NULL;
  14. state=0;
  15. }
  16. void CButtonBase::show()
  17. {
  18. if (!abs)
  19. {
  20. blitAt(imgs[state],pos.x,pos.y);
  21. updateRect(&pos);
  22. }
  23. else
  24. {
  25. blitAt(imgs[state],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y);
  26. updateRect(&genRect(pos.h,pos.w,pos.x+ourObj->pos.x,pos.y+ourObj->pos.y));
  27. }
  28. }
  29. void ClickableL::activate()
  30. {
  31. CURPLINT->lclickable.push_back(this);
  32. }
  33. void ClickableL::deactivate()
  34. {
  35. CURPLINT->lclickable.erase
  36. (std::find(CURPLINT->lclickable.begin(),CURPLINT->lclickable.end(),this));
  37. }
  38. void ClickableR::activate()
  39. {
  40. CURPLINT->rclickable.push_back(this);
  41. }
  42. void ClickableR::deactivate()
  43. {
  44. CURPLINT->rclickable.erase(std::find(CURPLINT->rclickable.begin(),CURPLINT->rclickable.end(),this));
  45. }
  46. void Hoverable::activate()
  47. {
  48. CURPLINT->hoverable.push_back(this);
  49. }
  50. void Hoverable::deactivate()
  51. {
  52. CURPLINT->hoverable.erase(std::find(CURPLINT->hoverable.begin(),CURPLINT->hoverable.end(),this));
  53. }
  54. void KeyInterested::activate()
  55. {
  56. CURPLINT->keyinterested.push_back(this);
  57. }
  58. void KeyInterested::deactivate()
  59. {
  60. CURPLINT->
  61. keyinterested.erase(std::find(CURPLINT->keyinterested.begin(),CURPLINT->keyinterested.end(),this));
  62. }
  63. CPlayerInterface::CPlayerInterface(int Player)
  64. {
  65. playerID=Player;
  66. human=true;
  67. adventureInt = new CAdvMapInt(Player);
  68. }
  69. void CPlayerInterface::yourTurn()
  70. {
  71. unsigned char & animVal = CURPLINT->adventureInt->anim; //for animations handling
  72. adventureInt->show();
  73. //show rest of things
  74. //initializing framerate keeper
  75. FPSmanager * mainLoopFramerateKeeper = new FPSmanager;
  76. SDL_initFramerate(mainLoopFramerateKeeper);
  77. SDL_setFramerate(mainLoopFramerateKeeper, 30);
  78. SDL_Event sEvent;
  79. //framerate keeper initialized
  80. for(;;) // main loop
  81. {
  82. CURPLINT->adventureInt->updateScreen = false;
  83. if(SDL_PollEvent(&sEvent)) //wait for event...
  84. {
  85. handleEvent(&sEvent);
  86. }
  87. ++CURPLINT->adventureInt->animValHitCount; //for animations
  88. if(CURPLINT->adventureInt->animValHitCount == 2)
  89. {
  90. CURPLINT->adventureInt->animValHitCount = 0;
  91. ++animVal;
  92. CURPLINT->adventureInt->updateScreen = true;
  93. }
  94. if(CURPLINT->adventureInt->scrollingLeft)
  95. {
  96. if(CURPLINT->adventureInt->position.x>0)
  97. {
  98. CURPLINT->adventureInt->position.x--;
  99. CURPLINT->adventureInt->updateScreen = true;
  100. }
  101. }
  102. if(CURPLINT->adventureInt->scrollingRight)
  103. {
  104. if(CURPLINT->adventureInt->position.x<CGI->ac->map.width-19+8)
  105. {
  106. CURPLINT->adventureInt->position.x++;
  107. CURPLINT->adventureInt->updateScreen = true;
  108. }
  109. }
  110. if(CURPLINT->adventureInt->scrollingUp)
  111. {
  112. if(CURPLINT->adventureInt->position.y>0)
  113. {
  114. CURPLINT->adventureInt->position.y--;
  115. CURPLINT->adventureInt->updateScreen = true;
  116. }
  117. }
  118. if(CURPLINT->adventureInt->scrollingDown)
  119. {
  120. if(CURPLINT->adventureInt->position.y<CGI->ac->map.height-18+8)
  121. {
  122. CURPLINT->adventureInt->position.y++;
  123. CURPLINT->adventureInt->updateScreen = true;
  124. }
  125. }
  126. if(CURPLINT->adventureInt->updateScreen)
  127. {
  128. adventureInt->update();
  129. CURPLINT->adventureInt->updateScreen=false;
  130. }
  131. SDL_Delay(5); //give time for other apps
  132. SDL_framerateDelay(mainLoopFramerateKeeper);
  133. }
  134. }
  135. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  136. {
  137. if(sEvent->type==SDL_QUIT)
  138. exit(0);
  139. else if (sEvent->type==SDL_KEYDOWN)
  140. {
  141. switch (sEvent->key.keysym.sym)
  142. {
  143. case SDLK_LEFT:
  144. {
  145. CURPLINT->adventureInt->scrollingLeft = true;
  146. break;
  147. }
  148. case (SDLK_RIGHT):
  149. {
  150. CURPLINT->adventureInt->scrollingRight = true;
  151. break;
  152. }
  153. case (SDLK_UP):
  154. {
  155. CURPLINT->adventureInt->scrollingUp = true;
  156. break;
  157. }
  158. case (SDLK_DOWN):
  159. {
  160. CURPLINT->adventureInt->scrollingDown = true;
  161. break;
  162. }
  163. case (SDLK_q):
  164. {
  165. exit(0);
  166. break;
  167. }
  168. case (SDLK_u):
  169. {
  170. if(!CGI->ac->map.twoLevel)
  171. break;
  172. if (adventureInt->position.z)
  173. adventureInt->position.z--;
  174. else adventureInt->position.z++;
  175. CURPLINT->adventureInt->updateScreen = true;
  176. break;
  177. }
  178. }
  179. } //keydown end
  180. else if(sEvent->type==SDL_KEYUP)
  181. {
  182. switch (sEvent->key.keysym.sym)
  183. {
  184. case SDLK_LEFT:
  185. {
  186. CURPLINT->adventureInt->scrollingLeft = false;
  187. break;
  188. }
  189. case (SDLK_RIGHT):
  190. {
  191. CURPLINT->adventureInt->scrollingRight = false;
  192. break;
  193. }
  194. case (SDLK_UP):
  195. {
  196. CURPLINT->adventureInt->scrollingUp = false;
  197. break;
  198. }
  199. case (SDLK_DOWN):
  200. {
  201. CURPLINT->adventureInt->scrollingDown = false;
  202. break;
  203. }
  204. }
  205. }//keyup end
  206. else if(sEvent->type==SDL_MOUSEMOTION)
  207. {
  208. if(sEvent->motion.x<15)
  209. {
  210. CURPLINT->adventureInt->scrollingLeft = true;
  211. }
  212. else
  213. {
  214. CURPLINT->adventureInt->scrollingLeft = false;
  215. }
  216. if(sEvent->motion.x>ekran->w-15)
  217. {
  218. CURPLINT->adventureInt->scrollingRight = true;
  219. }
  220. else
  221. {
  222. CURPLINT->adventureInt->scrollingRight = false;
  223. }
  224. if(sEvent->motion.y<15)
  225. {
  226. CURPLINT->adventureInt->scrollingUp = true;
  227. }
  228. else
  229. {
  230. CURPLINT->adventureInt->scrollingUp = false;
  231. }
  232. if(sEvent->motion.y>ekran->h-15)
  233. {
  234. CURPLINT->adventureInt->scrollingDown = true;
  235. }
  236. else
  237. {
  238. CURPLINT->adventureInt->scrollingDown = false;
  239. }
  240. }
  241. } //event end