CGuiHandler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * CGuiHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGuiHandler.h"
  12. #include "../lib/CondSh.h"
  13. #include <SDL_timer.h>
  14. #include "CIntObject.h"
  15. #include "CursorHandler.h"
  16. #include "SDL_Extensions.h"
  17. #include "../CGameInfo.h"
  18. #include "../../lib/CThreadHelper.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../CMT.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../battle/BattleInterface.h"
  23. extern std::queue<SDL_Event> SDLEventsQueue;
  24. extern boost::mutex eventsM;
  25. boost::thread_specific_ptr<bool> inGuiThread;
  26. SObjectConstruction::SObjectConstruction(CIntObject *obj)
  27. :myObj(obj)
  28. {
  29. GH.createdObj.push_front(obj);
  30. GH.captureChildren = true;
  31. }
  32. SObjectConstruction::~SObjectConstruction()
  33. {
  34. assert(GH.createdObj.size());
  35. assert(GH.createdObj.front() == myObj);
  36. GH.createdObj.pop_front();
  37. GH.captureChildren = GH.createdObj.size();
  38. }
  39. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  40. {
  41. previousCapture = GH.captureChildren;
  42. GH.captureChildren = false;
  43. prevActions = GH.defActionsDef;
  44. GH.defActionsDef = actions;
  45. }
  46. SSetCaptureState::~SSetCaptureState()
  47. {
  48. GH.captureChildren = previousCapture;
  49. GH.defActionsDef = prevActions;
  50. }
  51. static inline void
  52. processList(const ui16 mask, const ui16 flag, std::list<CIntObject*> *lst, std::function<void (std::list<CIntObject*> *)> cb)
  53. {
  54. if (mask & flag)
  55. cb(lst);
  56. }
  57. void CGuiHandler::processLists(const ui16 activityFlag, std::function<void (std::list<CIntObject*> *)> cb)
  58. {
  59. processList(CIntObject::LCLICK,activityFlag,&lclickable,cb);
  60. processList(CIntObject::RCLICK,activityFlag,&rclickable,cb);
  61. processList(CIntObject::MCLICK,activityFlag,&mclickable,cb);
  62. processList(CIntObject::HOVER,activityFlag,&hoverable,cb);
  63. processList(CIntObject::MOVE,activityFlag,&motioninterested,cb);
  64. processList(CIntObject::KEYBOARD,activityFlag,&keyinterested,cb);
  65. processList(CIntObject::TIME,activityFlag,&timeinterested,cb);
  66. processList(CIntObject::WHEEL,activityFlag,&wheelInterested,cb);
  67. processList(CIntObject::DOUBLECLICK,activityFlag,&doubleClickInterested,cb);
  68. processList(CIntObject::TEXTINPUT,activityFlag,&textInterested,cb);
  69. }
  70. void CGuiHandler::handleElementActivate(CIntObject * elem, ui16 activityFlag)
  71. {
  72. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  73. lst->push_front(elem);
  74. });
  75. elem->active_m |= activityFlag;
  76. }
  77. void CGuiHandler::handleElementDeActivate(CIntObject * elem, ui16 activityFlag)
  78. {
  79. processLists(activityFlag,[&](std::list<CIntObject*> * lst){
  80. auto hlp = std::find(lst->begin(),lst->end(),elem);
  81. assert(hlp != lst->end());
  82. lst->erase(hlp);
  83. });
  84. elem->active_m &= ~activityFlag;
  85. }
  86. void CGuiHandler::popInt(std::shared_ptr<IShowActivatable> top)
  87. {
  88. assert(listInt.front() == top);
  89. top->deactivate();
  90. disposed.push_back(top);
  91. listInt.pop_front();
  92. objsToBlit -= top;
  93. if(!listInt.empty())
  94. listInt.front()->activate();
  95. totalRedraw();
  96. pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
  97. }
  98. void CGuiHandler::pushInt(std::shared_ptr<IShowActivatable> newInt)
  99. {
  100. assert(newInt);
  101. assert(!vstd::contains(listInt, newInt)); // do not add same object twice
  102. //a new interface will be present, we'll need to use buffer surface (unless it's advmapint that will alter screenBuf on activate anyway)
  103. screenBuf = screen2;
  104. if(!listInt.empty())
  105. listInt.front()->deactivate();
  106. listInt.push_front(newInt);
  107. CCS->curh->set(Cursor::Map::POINTER);
  108. newInt->activate();
  109. objsToBlit.push_back(newInt);
  110. totalRedraw();
  111. pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
  112. }
  113. void CGuiHandler::popInts(int howMany)
  114. {
  115. if(!howMany) return; //senseless but who knows...
  116. assert(listInt.size() >= howMany);
  117. listInt.front()->deactivate();
  118. for(int i=0; i < howMany; i++)
  119. {
  120. objsToBlit -= listInt.front();
  121. disposed.push_back(listInt.front());
  122. listInt.pop_front();
  123. }
  124. if(!listInt.empty())
  125. {
  126. listInt.front()->activate();
  127. totalRedraw();
  128. }
  129. fakeMouseMove();
  130. pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
  131. }
  132. std::shared_ptr<IShowActivatable> CGuiHandler::topInt()
  133. {
  134. if(listInt.empty())
  135. return std::shared_ptr<IShowActivatable>();
  136. else
  137. return listInt.front();
  138. }
  139. void CGuiHandler::totalRedraw()
  140. {
  141. #ifdef VCMI_ANDROID
  142. SDL_FillRect(screen2, NULL, SDL_MapRGB(screen2->format, 0, 0, 0));
  143. #endif
  144. for(auto & elem : objsToBlit)
  145. elem->showAll(screen2);
  146. CSDL_Ext::blitAt(screen2,0,0,screen);
  147. }
  148. void CGuiHandler::updateTime()
  149. {
  150. int ms = mainFPSmng->getElapsedMilliseconds();
  151. std::list<CIntObject*> hlp = timeinterested;
  152. for (auto & elem : hlp)
  153. {
  154. if(!vstd::contains(timeinterested,elem)) continue;
  155. (elem)->onTimer(ms);
  156. }
  157. }
  158. void CGuiHandler::handleEvents()
  159. {
  160. //player interface may want special event handling
  161. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  162. return;
  163. boost::unique_lock<boost::mutex> lock(eventsM);
  164. while(!SDLEventsQueue.empty())
  165. {
  166. continueEventHandling = true;
  167. SDL_Event ev = SDLEventsQueue.front();
  168. current = &ev;
  169. SDLEventsQueue.pop();
  170. // In a sequence of mouse motion events, skip all but the last one.
  171. // This prevents freezes when every motion event takes longer to handle than interval at which
  172. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  173. // so that the events would start piling up faster than they can be processed.
  174. if ((ev.type == SDL_MOUSEMOTION) && !SDLEventsQueue.empty() && (SDLEventsQueue.front().type == SDL_MOUSEMOTION))
  175. continue;
  176. handleCurrentEvent();
  177. }
  178. }
  179. void CGuiHandler::handleCurrentEvent()
  180. {
  181. if(current->type == SDL_KEYDOWN || current->type == SDL_KEYUP)
  182. {
  183. SDL_KeyboardEvent key = current->key;
  184. if(current->type == SDL_KEYDOWN && key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  185. {
  186. //TODO: we need some central place for all interface-independent hotkeys
  187. Settings s = settings.write["session"];
  188. switch(key.keysym.sym)
  189. {
  190. case SDLK_F5:
  191. if(settings["session"]["spectate-locked-pim"].Bool())
  192. LOCPLINT->pim->unlock();
  193. else
  194. LOCPLINT->pim->lock();
  195. s["spectate-locked-pim"].Bool() = !settings["session"]["spectate-locked-pim"].Bool();
  196. break;
  197. case SDLK_F6:
  198. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  199. break;
  200. case SDLK_F7:
  201. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  202. break;
  203. case SDLK_F8:
  204. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  205. break;
  206. case SDLK_F9:
  207. //not working yet since CClient::run remain locked after BattleInterface removal
  208. // if(LOCPLINT->battleInt)
  209. // {
  210. // GH.popInts(1);
  211. // vstd::clear_pointer(LOCPLINT->battleInt);
  212. // }
  213. break;
  214. default:
  215. break;
  216. }
  217. return;
  218. }
  219. //translate numpad keys
  220. if(key.keysym.sym == SDLK_KP_ENTER)
  221. {
  222. key.keysym.sym = SDLK_RETURN;
  223. key.keysym.scancode = SDL_SCANCODE_RETURN;
  224. }
  225. bool keysCaptured = false;
  226. for(auto i = keyinterested.begin(); i != keyinterested.end() && continueEventHandling; i++)
  227. {
  228. if((*i)->captureThisEvent(key))
  229. {
  230. keysCaptured = true;
  231. break;
  232. }
  233. }
  234. std::list<CIntObject*> miCopy = keyinterested;
  235. for(auto i = miCopy.begin(); i != miCopy.end() && continueEventHandling; i++)
  236. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureThisEvent(key)))
  237. (**i).keyPressed(key);
  238. }
  239. else if(current->type == SDL_MOUSEMOTION)
  240. {
  241. handleMouseMotion();
  242. }
  243. else if(current->type == SDL_MOUSEBUTTONDOWN)
  244. {
  245. switch(current->button.button)
  246. {
  247. case SDL_BUTTON_LEFT:
  248. {
  249. auto doubleClicked = false;
  250. if(lastClick == current->motion && (SDL_GetTicks() - lastClickTime) < 300)
  251. {
  252. std::list<CIntObject*> hlp = doubleClickInterested;
  253. for(auto i = hlp.begin(); i != hlp.end() && continueEventHandling; i++)
  254. {
  255. if(!vstd::contains(doubleClickInterested, *i)) continue;
  256. if((*i)->pos.isInside(current->motion.x, current->motion.y))
  257. {
  258. (*i)->onDoubleClick();
  259. doubleClicked = true;
  260. }
  261. }
  262. }
  263. lastClick = current->motion;
  264. lastClickTime = SDL_GetTicks();
  265. if(!doubleClicked)
  266. handleMouseButtonClick(lclickable, EIntObjMouseBtnType::LEFT, true);
  267. break;
  268. }
  269. case SDL_BUTTON_RIGHT:
  270. handleMouseButtonClick(rclickable, EIntObjMouseBtnType::RIGHT, true);
  271. break;
  272. case SDL_BUTTON_MIDDLE:
  273. handleMouseButtonClick(mclickable, EIntObjMouseBtnType::MIDDLE, true);
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. else if(current->type == SDL_MOUSEWHEEL)
  280. {
  281. std::list<CIntObject*> hlp = wheelInterested;
  282. for(auto i = hlp.begin(); i != hlp.end() && continueEventHandling; i++)
  283. {
  284. if(!vstd::contains(wheelInterested,*i)) continue;
  285. // SDL doesn't have the proper values for mouse positions on SDL_MOUSEWHEEL, refetch them
  286. int x = 0, y = 0;
  287. SDL_GetMouseState(&x, &y);
  288. (*i)->wheelScrolled(current->wheel.y < 0, (*i)->pos.isInside(x, y));
  289. }
  290. }
  291. else if(current->type == SDL_TEXTINPUT)
  292. {
  293. for(auto it : textInterested)
  294. {
  295. it->textInputed(current->text);
  296. }
  297. }
  298. else if(current->type == SDL_TEXTEDITING)
  299. {
  300. for(auto it : textInterested)
  301. {
  302. it->textEdited(current->edit);
  303. }
  304. }
  305. //todo: muiltitouch
  306. else if(current->type == SDL_MOUSEBUTTONUP)
  307. {
  308. switch(current->button.button)
  309. {
  310. case SDL_BUTTON_LEFT:
  311. handleMouseButtonClick(lclickable, EIntObjMouseBtnType::LEFT, false);
  312. break;
  313. case SDL_BUTTON_RIGHT:
  314. handleMouseButtonClick(rclickable, EIntObjMouseBtnType::RIGHT, false);
  315. break;
  316. case SDL_BUTTON_MIDDLE:
  317. handleMouseButtonClick(mclickable, EIntObjMouseBtnType::MIDDLE, false);
  318. break;
  319. }
  320. }
  321. current = nullptr;
  322. } //event end
  323. void CGuiHandler::handleMouseButtonClick(CIntObjectList & interestedObjs, EIntObjMouseBtnType btn, bool isPressed)
  324. {
  325. auto hlp = interestedObjs;
  326. for(auto i = hlp.begin(); i != hlp.end() && continueEventHandling; i++)
  327. {
  328. if(!vstd::contains(interestedObjs, *i)) continue;
  329. auto prev = (*i)->mouseState(btn);
  330. if(!isPressed)
  331. (*i)->updateMouseState(btn, isPressed);
  332. if((*i)->pos.isInside(current->motion.x, current->motion.y))
  333. {
  334. if(isPressed)
  335. (*i)->updateMouseState(btn, isPressed);
  336. (*i)->click(btn, isPressed, prev);
  337. }
  338. else if(!isPressed)
  339. (*i)->click(btn, boost::logic::indeterminate, prev);
  340. }
  341. }
  342. void CGuiHandler::handleMouseMotion()
  343. {
  344. //sending active, hovered hoverable objects hover() call
  345. std::vector<CIntObject*> hlp;
  346. for(auto & elem : hoverable)
  347. {
  348. if(elem->pos.isInside(current->motion.x, current->motion.y))
  349. {
  350. if (!(elem)->hovered)
  351. hlp.push_back((elem));
  352. }
  353. else if ((elem)->hovered)
  354. {
  355. (elem)->hover(false);
  356. (elem)->hovered = false;
  357. }
  358. }
  359. for(auto & elem : hlp)
  360. {
  361. elem->hover(true);
  362. elem->hovered = true;
  363. }
  364. handleMoveInterested(current->motion);
  365. }
  366. void CGuiHandler::simpleRedraw()
  367. {
  368. //update only top interface and draw background
  369. if(objsToBlit.size() > 1)
  370. CSDL_Ext::blitAt(screen2,0,0,screen); //blit background
  371. if(!objsToBlit.empty())
  372. objsToBlit.back()->show(screen); //blit active interface/window
  373. }
  374. void CGuiHandler::handleMoveInterested(const SDL_MouseMotionEvent & motion)
  375. {
  376. //sending active, MotionInterested objects mouseMoved() call
  377. std::list<CIntObject*> miCopy = motioninterested;
  378. for(auto & elem : miCopy)
  379. {
  380. if(elem->strongInterest || Rect::createAround(elem->pos, 1).isInside( motion.x, motion.y)) //checking bounds including border fixes bug #2476
  381. {
  382. (elem)->mouseMoved(motion);
  383. }
  384. }
  385. }
  386. void CGuiHandler::fakeMouseMove()
  387. {
  388. SDL_Event event;
  389. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
  390. int x, y;
  391. sme.state = SDL_GetMouseState(&x, &y);
  392. sme.x = x;
  393. sme.y = y;
  394. event.motion = sme;
  395. SDL_PushEvent(&event);
  396. }
  397. void CGuiHandler::renderFrame()
  398. {
  399. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  400. // During game:
  401. // When ending the game, the pim mutex might be hold by other thread,
  402. // that will notify us about the ending game by setting terminate_cond flag.
  403. //in PreGame terminate_cond stay false
  404. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  405. while(!terminate_cond->get() && !(acquiredTheLockOnPim = CPlayerInterface::pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  406. boost::this_thread::sleep(boost::posix_time::milliseconds(1));
  407. if(acquiredTheLockOnPim)
  408. {
  409. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  410. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim, boost::adopt_lock);
  411. if(nullptr != curInt)
  412. curInt->update();
  413. if(settings["general"]["showfps"].Bool())
  414. drawFPSCounter();
  415. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  416. SDL_RenderClear(mainRenderer);
  417. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  418. CCS->curh->render();
  419. SDL_RenderPresent(mainRenderer);
  420. disposed.clear();
  421. }
  422. mainFPSmng->framerateDelay(); // holds a constant FPS
  423. }
  424. CGuiHandler::CGuiHandler()
  425. : lastClick(-500, -500),lastClickTime(0), defActionsDef(0), captureChildren(false)
  426. {
  427. continueEventHandling = true;
  428. curInt = nullptr;
  429. current = nullptr;
  430. statusbar = nullptr;
  431. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  432. mainFPSmng = new CFramerateManager(60);
  433. //do not init CFramerateManager here --AVS
  434. terminate_cond = new CondSh<bool>(false);
  435. }
  436. CGuiHandler::~CGuiHandler()
  437. {
  438. delete mainFPSmng;
  439. delete terminate_cond;
  440. }
  441. void CGuiHandler::breakEventHandling()
  442. {
  443. continueEventHandling = false;
  444. }
  445. void CGuiHandler::drawFPSCounter()
  446. {
  447. const static SDL_Color yellow = {255, 255, 0, 0};
  448. static SDL_Rect overlay = { 0, 0, 64, 32};
  449. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  450. SDL_FillRect(screen, &overlay, black);
  451. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  452. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, yellow, Point(10, 10));
  453. }
  454. SDL_Keycode CGuiHandler::arrowToNum(SDL_Keycode key)
  455. {
  456. switch(key)
  457. {
  458. case SDLK_DOWN:
  459. return SDLK_KP_2;
  460. case SDLK_UP:
  461. return SDLK_KP_8;
  462. case SDLK_LEFT:
  463. return SDLK_KP_4;
  464. case SDLK_RIGHT:
  465. return SDLK_KP_6;
  466. default:
  467. throw std::runtime_error("Wrong key!");
  468. }
  469. }
  470. SDL_Keycode CGuiHandler::numToDigit(SDL_Keycode key)
  471. {
  472. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  473. switch(key)
  474. {
  475. REMOVE_KP(0)
  476. REMOVE_KP(1)
  477. REMOVE_KP(2)
  478. REMOVE_KP(3)
  479. REMOVE_KP(4)
  480. REMOVE_KP(5)
  481. REMOVE_KP(6)
  482. REMOVE_KP(7)
  483. REMOVE_KP(8)
  484. REMOVE_KP(9)
  485. REMOVE_KP(PERIOD)
  486. REMOVE_KP(MINUS)
  487. REMOVE_KP(PLUS)
  488. REMOVE_KP(EQUALS)
  489. case SDLK_KP_MULTIPLY:
  490. return SDLK_ASTERISK;
  491. case SDLK_KP_DIVIDE:
  492. return SDLK_SLASH;
  493. case SDLK_KP_ENTER:
  494. return SDLK_RETURN;
  495. default:
  496. return SDLK_UNKNOWN;
  497. }
  498. #undef REMOVE_KP
  499. }
  500. bool CGuiHandler::isNumKey(SDL_Keycode key, bool number)
  501. {
  502. if(number)
  503. return key >= SDLK_KP_1 && key <= SDLK_KP_0;
  504. else
  505. return (key >= SDLK_KP_1 && key <= SDLK_KP_0) || key == SDLK_KP_MINUS || key == SDLK_KP_PLUS || key == SDLK_KP_EQUALS;
  506. }
  507. bool CGuiHandler::isArrowKey(SDL_Keycode key)
  508. {
  509. return key == SDLK_UP || key == SDLK_DOWN || key == SDLK_LEFT || key == SDLK_RIGHT;
  510. }
  511. bool CGuiHandler::amIGuiThread()
  512. {
  513. return inGuiThread.get() && *inGuiThread;
  514. }
  515. void CGuiHandler::pushSDLEvent(int type, int usercode)
  516. {
  517. SDL_Event event;
  518. event.type = type;
  519. event.user.code = usercode; // not necessarily used
  520. SDL_PushEvent(&event);
  521. }
  522. CFramerateManager::CFramerateManager(int rate)
  523. {
  524. this->rate = rate;
  525. this->rateticks = (1000.0 / rate);
  526. this->fps = 0;
  527. this->accumulatedFrames = 0;
  528. this->accumulatedTime = 0;
  529. this->lastticks = 0;
  530. this->timeElapsed = 0;
  531. }
  532. void CFramerateManager::init()
  533. {
  534. this->lastticks = SDL_GetTicks();
  535. }
  536. void CFramerateManager::framerateDelay()
  537. {
  538. ui32 currentTicks = SDL_GetTicks();
  539. timeElapsed = currentTicks - lastticks;
  540. accumulatedFrames++;
  541. // FPS is higher than it should be, then wait some time
  542. if(timeElapsed < rateticks)
  543. {
  544. SDL_Delay((Uint32)ceil(this->rateticks) - timeElapsed);
  545. }
  546. currentTicks = SDL_GetTicks();
  547. // recalculate timeElapsed for external calls via getElapsed()
  548. // limit it to 100 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
  549. timeElapsed = std::min<ui32>(currentTicks - lastticks, 100);
  550. lastticks = SDL_GetTicks();
  551. accumulatedTime += timeElapsed;
  552. if(accumulatedFrames >= 100)
  553. {
  554. //about 2 second should be passed
  555. fps = static_cast<int>(ceil(1000.0 / (accumulatedTime / accumulatedFrames)));
  556. accumulatedTime = 0;
  557. accumulatedFrames = 0;
  558. }
  559. }