CGameInterface.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #include "CScreenHandler.h"
  8. #include "CCursorHandler.h"
  9. #include "CCallback.h"
  10. using namespace CSDL_Ext;
  11. CButtonBase::CButtonBase()
  12. {
  13. curimg=0;
  14. type=-1;
  15. abs=false;
  16. active=false;
  17. ourObj=NULL;
  18. state=0;
  19. }
  20. void CButtonBase::show()
  21. {
  22. if (abs)
  23. {
  24. blitAt(imgs[curimg][state],pos.x,pos.y);
  25. updateRect(&pos);
  26. }
  27. else
  28. {
  29. blitAt(imgs[curimg][state],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y);
  30. updateRect(&genRect(pos.h,pos.w,pos.x+ourObj->pos.x,pos.y+ourObj->pos.y));
  31. }
  32. }
  33. ClickableL::ClickableL()
  34. {
  35. pressedL=false;
  36. }
  37. void ClickableL::clickLeft(tribool down)
  38. {
  39. if (down)
  40. pressedL=true;
  41. else
  42. pressedL=false;
  43. }
  44. void ClickableL::activate()
  45. {
  46. LOCPLINT->lclickable.push_back(this);
  47. }
  48. void ClickableL::deactivate()
  49. {
  50. LOCPLINT->lclickable.erase
  51. (std::find(LOCPLINT->lclickable.begin(),LOCPLINT->lclickable.end(),this));
  52. }
  53. ClickableR::ClickableR()
  54. {
  55. pressedR=false;
  56. }
  57. void ClickableR::activate()
  58. {
  59. LOCPLINT->rclickable.push_back(this);
  60. }
  61. void ClickableR::deactivate()
  62. {
  63. LOCPLINT->rclickable.erase(std::find(LOCPLINT->rclickable.begin(),LOCPLINT->rclickable.end(),this));
  64. }
  65. void Hoverable::activate()
  66. {
  67. LOCPLINT->hoverable.push_back(this);
  68. }
  69. void Hoverable::deactivate()
  70. {
  71. LOCPLINT->hoverable.erase(std::find(LOCPLINT->hoverable.begin(),LOCPLINT->hoverable.end(),this));
  72. }
  73. void Hoverable::hover(bool on)
  74. {
  75. hovered=on;
  76. }
  77. void KeyInterested::activate()
  78. {
  79. LOCPLINT->keyinterested.push_back(this);
  80. }
  81. void KeyInterested::deactivate()
  82. {
  83. LOCPLINT->
  84. keyinterested.erase(std::find(LOCPLINT->keyinterested.begin(),LOCPLINT->keyinterested.end(),this));
  85. }
  86. void MotionInterested::activate()
  87. {
  88. LOCPLINT->motioninterested.push_back(this);
  89. }
  90. void MotionInterested::deactivate()
  91. {
  92. LOCPLINT->
  93. motioninterested.erase(std::find(LOCPLINT->motioninterested.begin(),LOCPLINT->motioninterested.end(),this));
  94. }
  95. CPlayerInterface::CPlayerInterface(int Player, int serial)
  96. {
  97. playerID=Player;
  98. serialID=serial;
  99. CGI->localPlayer = playerID;
  100. human=true;
  101. }
  102. void CPlayerInterface::init()
  103. {
  104. CGI->localPlayer = serialID;
  105. adventureInt = new CAdvMapInt(playerID);
  106. }
  107. void CPlayerInterface::yourTurn()
  108. {
  109. CGI->localPlayer = serialID;
  110. unsigned char & animVal = LOCPLINT->adventureInt->anim; //for animations handling
  111. adventureInt->show();
  112. //show rest of things
  113. //initializing framerate keeper
  114. mainFPSmng = new FPSmanager;
  115. SDL_initFramerate(mainFPSmng);
  116. SDL_setFramerate(mainFPSmng, 24);
  117. SDL_Event sEvent;
  118. //framerate keeper initialized
  119. for(;;) // main loop
  120. {
  121. CGI->screenh->updateScreen();
  122. LOCPLINT->adventureInt->updateScreen = false;
  123. while (SDL_PollEvent(&sEvent)) //wait for event...
  124. {
  125. handleEvent(&sEvent);
  126. }
  127. ++LOCPLINT->adventureInt->animValHitCount; //for animations
  128. if(LOCPLINT->adventureInt->animValHitCount == 2)
  129. {
  130. LOCPLINT->adventureInt->animValHitCount = 0;
  131. ++animVal;
  132. LOCPLINT->adventureInt->updateScreen = true;
  133. }
  134. if(LOCPLINT->adventureInt->scrollingLeft)
  135. {
  136. if(LOCPLINT->adventureInt->position.x>-Woff)
  137. {
  138. LOCPLINT->adventureInt->position.x--;
  139. LOCPLINT->adventureInt->updateScreen = true;
  140. }
  141. }
  142. if(LOCPLINT->adventureInt->scrollingRight)
  143. {
  144. if(LOCPLINT->adventureInt->position.x<CGI->ac->map.width-19+4)
  145. {
  146. LOCPLINT->adventureInt->position.x++;
  147. LOCPLINT->adventureInt->updateScreen = true;
  148. }
  149. }
  150. if(LOCPLINT->adventureInt->scrollingUp)
  151. {
  152. if(LOCPLINT->adventureInt->position.y>-Hoff)
  153. {
  154. LOCPLINT->adventureInt->position.y--;
  155. LOCPLINT->adventureInt->updateScreen = true;
  156. }
  157. }
  158. if(LOCPLINT->adventureInt->scrollingDown)
  159. {
  160. if(LOCPLINT->adventureInt->position.y<CGI->ac->map.height-18+4)
  161. {
  162. LOCPLINT->adventureInt->position.y++;
  163. LOCPLINT->adventureInt->updateScreen = true;
  164. }
  165. }
  166. if(LOCPLINT->adventureInt->updateScreen)
  167. {
  168. adventureInt->update();
  169. LOCPLINT->adventureInt->updateScreen=false;
  170. }
  171. SDL_Delay(5); //give time for other apps
  172. SDL_framerateDelay(mainFPSmng);
  173. }
  174. }
  175. void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
  176. {
  177. //initializing objects and performing first step of move
  178. CObjectInstance * ho = CGI->heroh->heroInstances[details.heroID]->ourObject; //object representing this hero
  179. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  180. {
  181. }
  182. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  183. {
  184. }
  185. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  186. {
  187. }
  188. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  189. {
  190. }
  191. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  192. {
  193. }
  194. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  195. {
  196. }
  197. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  198. {
  199. }
  200. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  201. {
  202. }
  203. for(int i=0; i<32; ++i)
  204. {
  205. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  206. {
  207. }
  208. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  209. {
  210. }
  211. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  212. {
  213. }
  214. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  215. {
  216. }
  217. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  218. {
  219. }
  220. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  221. {
  222. }
  223. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  224. {
  225. }
  226. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  227. {
  228. }
  229. }
  230. }
  231. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  232. {
  233. current = sEvent;
  234. if(sEvent->type == SDL_MOUSEMOTION)
  235. {
  236. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  237. }
  238. if(sEvent->type==SDL_QUIT)
  239. exit(0);
  240. else if (sEvent->type==SDL_KEYDOWN)
  241. {
  242. switch (sEvent->key.keysym.sym)
  243. {
  244. case SDLK_LEFT:
  245. {
  246. LOCPLINT->adventureInt->scrollingLeft = true;
  247. break;
  248. }
  249. case (SDLK_RIGHT):
  250. {
  251. LOCPLINT->adventureInt->scrollingRight = true;
  252. break;
  253. }
  254. case (SDLK_UP):
  255. {
  256. LOCPLINT->adventureInt->scrollingUp = true;
  257. break;
  258. }
  259. case (SDLK_DOWN):
  260. {
  261. LOCPLINT->adventureInt->scrollingDown = true;
  262. break;
  263. }
  264. case (SDLK_q):
  265. {
  266. exit(0);
  267. break;
  268. }
  269. case (SDLK_u):
  270. {
  271. adventureInt->underground.clickLeft(true);
  272. }
  273. }
  274. } //keydown end
  275. else if(sEvent->type==SDL_KEYUP)
  276. {
  277. switch (sEvent->key.keysym.sym)
  278. {
  279. case SDLK_LEFT:
  280. {
  281. LOCPLINT->adventureInt->scrollingLeft = false;
  282. break;
  283. }
  284. case (SDLK_RIGHT):
  285. {
  286. LOCPLINT->adventureInt->scrollingRight = false;
  287. break;
  288. }
  289. case (SDLK_UP):
  290. {
  291. LOCPLINT->adventureInt->scrollingUp = false;
  292. break;
  293. }
  294. case (SDLK_DOWN):
  295. {
  296. LOCPLINT->adventureInt->scrollingDown = false;
  297. break;
  298. }
  299. case (SDLK_u):
  300. {
  301. adventureInt->underground.clickLeft(false);
  302. }
  303. }
  304. }//keyup end
  305. else if(sEvent->type==SDL_MOUSEMOTION)
  306. {
  307. for (int i=0; i<hoverable.size();i++)
  308. {
  309. if (isItIn(&hoverable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  310. {
  311. if (!hoverable[i]->hovered)
  312. hoverable[i]->hover(true);
  313. }
  314. else if (hoverable[i]->hovered)
  315. {
  316. hoverable[i]->hover(false);
  317. }
  318. }
  319. for(int i=0; i<motioninterested.size();i++)
  320. {
  321. if (isItIn(&motioninterested[i]->pos,sEvent->motion.x,sEvent->motion.y))
  322. {
  323. motioninterested[i]->mouseMoved(sEvent->motion);
  324. }
  325. }
  326. if(sEvent->motion.x<15)
  327. {
  328. LOCPLINT->adventureInt->scrollingLeft = true;
  329. }
  330. else
  331. {
  332. LOCPLINT->adventureInt->scrollingLeft = false;
  333. }
  334. if(sEvent->motion.x>ekran->w-15)
  335. {
  336. LOCPLINT->adventureInt->scrollingRight = true;
  337. }
  338. else
  339. {
  340. LOCPLINT->adventureInt->scrollingRight = false;
  341. }
  342. if(sEvent->motion.y<15)
  343. {
  344. LOCPLINT->adventureInt->scrollingUp = true;
  345. }
  346. else
  347. {
  348. LOCPLINT->adventureInt->scrollingUp = false;
  349. }
  350. if(sEvent->motion.y>ekran->h-15)
  351. {
  352. LOCPLINT->adventureInt->scrollingDown = true;
  353. }
  354. else
  355. {
  356. LOCPLINT->adventureInt->scrollingDown = false;
  357. }
  358. }
  359. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  360. {
  361. for(int i=0; i<lclickable.size();i++)
  362. {
  363. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  364. {
  365. lclickable[i]->clickLeft(true);
  366. }
  367. }
  368. }
  369. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  370. {
  371. for(int i=0; i<lclickable.size();i++)
  372. {
  373. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  374. {
  375. lclickable[i]->clickLeft(false);
  376. }
  377. else
  378. lclickable[i]->clickLeft(boost::logic::indeterminate);
  379. }
  380. }
  381. current = NULL;
  382. } //event end