CGameInterface.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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(CCallback * CB)
  103. {
  104. cb = CB;
  105. CGI->localPlayer = serialID;
  106. adventureInt = new CAdvMapInt(playerID);
  107. }
  108. void CPlayerInterface::yourTurn()
  109. {
  110. CGI->localPlayer = serialID;
  111. unsigned char & animVal = LOCPLINT->adventureInt->anim; //for animations handling
  112. adventureInt->show();
  113. //show rest of things
  114. //initializing framerate keeper
  115. mainFPSmng = new FPSmanager;
  116. SDL_initFramerate(mainFPSmng);
  117. SDL_setFramerate(mainFPSmng, 24);
  118. SDL_Event sEvent;
  119. //framerate keeper initialized
  120. for(;;) // main loop
  121. {
  122. CGI->screenh->updateScreen();
  123. LOCPLINT->adventureInt->updateScreen = false;
  124. while (SDL_PollEvent(&sEvent)) //wait for event...
  125. {
  126. handleEvent(&sEvent);
  127. }
  128. ++LOCPLINT->adventureInt->animValHitCount; //for animations
  129. if(LOCPLINT->adventureInt->animValHitCount == 2)
  130. {
  131. LOCPLINT->adventureInt->animValHitCount = 0;
  132. ++animVal;
  133. LOCPLINT->adventureInt->updateScreen = true;
  134. }
  135. if(LOCPLINT->adventureInt->scrollingLeft)
  136. {
  137. if(LOCPLINT->adventureInt->position.x>-Woff)
  138. {
  139. LOCPLINT->adventureInt->position.x--;
  140. LOCPLINT->adventureInt->updateScreen = true;
  141. }
  142. }
  143. if(LOCPLINT->adventureInt->scrollingRight)
  144. {
  145. if(LOCPLINT->adventureInt->position.x<CGI->ac->map.width-19+4)
  146. {
  147. LOCPLINT->adventureInt->position.x++;
  148. LOCPLINT->adventureInt->updateScreen = true;
  149. }
  150. }
  151. if(LOCPLINT->adventureInt->scrollingUp)
  152. {
  153. if(LOCPLINT->adventureInt->position.y>-Hoff)
  154. {
  155. LOCPLINT->adventureInt->position.y--;
  156. LOCPLINT->adventureInt->updateScreen = true;
  157. }
  158. }
  159. if(LOCPLINT->adventureInt->scrollingDown)
  160. {
  161. if(LOCPLINT->adventureInt->position.y<CGI->ac->map.height-18+4)
  162. {
  163. LOCPLINT->adventureInt->position.y++;
  164. LOCPLINT->adventureInt->updateScreen = true;
  165. }
  166. }
  167. if(LOCPLINT->adventureInt->updateScreen)
  168. {
  169. adventureInt->update();
  170. LOCPLINT->adventureInt->updateScreen=false;
  171. }
  172. SDL_Delay(5); //give time for other apps
  173. SDL_framerateDelay(mainFPSmng);
  174. }
  175. }
  176. void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
  177. {
  178. //initializing objects and performing first step of move
  179. CObjectInstance * ho = CGI->heroh->heroInstances[details.heroID]->ourObject; //object representing this hero
  180. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  181. {
  182. }
  183. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  184. {
  185. }
  186. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  187. {
  188. }
  189. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  190. {
  191. }
  192. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  193. {
  194. }
  195. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  196. {
  197. }
  198. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  199. {
  200. }
  201. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  202. {
  203. }
  204. for(int i=0; i<32; ++i)
  205. {
  206. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  207. {
  208. }
  209. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  210. {
  211. }
  212. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  213. {
  214. }
  215. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  216. {
  217. }
  218. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  219. {
  220. }
  221. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  222. {
  223. }
  224. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  225. {
  226. }
  227. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  228. {
  229. }
  230. }
  231. }
  232. void CPlayerInterface::heroKilled(const CHeroInstance * hero)
  233. {
  234. }
  235. void CPlayerInterface::heroCreated(const CHeroInstance * hero)
  236. {
  237. }
  238. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  239. {
  240. current = sEvent;
  241. if(sEvent->type == SDL_MOUSEMOTION)
  242. {
  243. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  244. }
  245. if(sEvent->type==SDL_QUIT)
  246. exit(0);
  247. else if (sEvent->type==SDL_KEYDOWN)
  248. {
  249. switch (sEvent->key.keysym.sym)
  250. {
  251. case SDLK_LEFT:
  252. {
  253. LOCPLINT->adventureInt->scrollingLeft = true;
  254. break;
  255. }
  256. case (SDLK_RIGHT):
  257. {
  258. LOCPLINT->adventureInt->scrollingRight = true;
  259. break;
  260. }
  261. case (SDLK_UP):
  262. {
  263. LOCPLINT->adventureInt->scrollingUp = true;
  264. break;
  265. }
  266. case (SDLK_DOWN):
  267. {
  268. LOCPLINT->adventureInt->scrollingDown = true;
  269. break;
  270. }
  271. case (SDLK_q):
  272. {
  273. exit(0);
  274. break;
  275. }
  276. case (SDLK_u):
  277. {
  278. adventureInt->underground.clickLeft(true);
  279. }
  280. }
  281. } //keydown end
  282. else if(sEvent->type==SDL_KEYUP)
  283. {
  284. switch (sEvent->key.keysym.sym)
  285. {
  286. case SDLK_LEFT:
  287. {
  288. LOCPLINT->adventureInt->scrollingLeft = false;
  289. break;
  290. }
  291. case (SDLK_RIGHT):
  292. {
  293. LOCPLINT->adventureInt->scrollingRight = false;
  294. break;
  295. }
  296. case (SDLK_UP):
  297. {
  298. LOCPLINT->adventureInt->scrollingUp = false;
  299. break;
  300. }
  301. case (SDLK_DOWN):
  302. {
  303. LOCPLINT->adventureInt->scrollingDown = false;
  304. break;
  305. }
  306. case (SDLK_u):
  307. {
  308. adventureInt->underground.clickLeft(false);
  309. }
  310. }
  311. }//keyup end
  312. else if(sEvent->type==SDL_MOUSEMOTION)
  313. {
  314. for (int i=0; i<hoverable.size();i++)
  315. {
  316. if (isItIn(&hoverable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  317. {
  318. if (!hoverable[i]->hovered)
  319. hoverable[i]->hover(true);
  320. }
  321. else if (hoverable[i]->hovered)
  322. {
  323. hoverable[i]->hover(false);
  324. }
  325. }
  326. for(int i=0; i<motioninterested.size();i++)
  327. {
  328. if (isItIn(&motioninterested[i]->pos,sEvent->motion.x,sEvent->motion.y))
  329. {
  330. motioninterested[i]->mouseMoved(sEvent->motion);
  331. }
  332. }
  333. if(sEvent->motion.x<15)
  334. {
  335. LOCPLINT->adventureInt->scrollingLeft = true;
  336. }
  337. else
  338. {
  339. LOCPLINT->adventureInt->scrollingLeft = false;
  340. }
  341. if(sEvent->motion.x>ekran->w-15)
  342. {
  343. LOCPLINT->adventureInt->scrollingRight = true;
  344. }
  345. else
  346. {
  347. LOCPLINT->adventureInt->scrollingRight = false;
  348. }
  349. if(sEvent->motion.y<15)
  350. {
  351. LOCPLINT->adventureInt->scrollingUp = true;
  352. }
  353. else
  354. {
  355. LOCPLINT->adventureInt->scrollingUp = false;
  356. }
  357. if(sEvent->motion.y>ekran->h-15)
  358. {
  359. LOCPLINT->adventureInt->scrollingDown = true;
  360. }
  361. else
  362. {
  363. LOCPLINT->adventureInt->scrollingDown = false;
  364. }
  365. } //mousemotion end
  366. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  367. {
  368. for(int i=0; i<lclickable.size();i++)
  369. {
  370. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  371. {
  372. lclickable[i]->clickLeft(true);
  373. }
  374. }
  375. }
  376. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  377. {
  378. for(int i=0; i<lclickable.size();i++)
  379. {
  380. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  381. {
  382. lclickable[i]->clickLeft(false);
  383. }
  384. else
  385. lclickable[i]->clickLeft(boost::logic::indeterminate);
  386. }
  387. }
  388. current = NULL;
  389. } //event end