CGuiHandler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 currentEvent = SDLEventsQueue.front();
  168. cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y);
  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 ((currentEvent.type == SDL_MOUSEMOTION) && !SDLEventsQueue.empty() && (SDLEventsQueue.front().type == SDL_MOUSEMOTION))
  175. continue;
  176. handleCurrentEvent(currentEvent);
  177. }
  178. }
  179. void CGuiHandler::handleCurrentEvent(const SDL_Event & current )
  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(current);
  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 == getCursorPosition() && (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. } //event end
  322. void CGuiHandler::handleMouseButtonClick(CIntObjectList & interestedObjs, EIntObjMouseBtnType btn, bool isPressed)
  323. {
  324. auto hlp = interestedObjs;
  325. for(auto i = hlp.begin(); i != hlp.end() && continueEventHandling; i++)
  326. {
  327. if(!vstd::contains(interestedObjs, *i)) continue;
  328. auto prev = (*i)->mouseState(btn);
  329. if(!isPressed)
  330. (*i)->updateMouseState(btn, isPressed);
  331. if((*i)->pos.isInside(getCursorPosition()))
  332. {
  333. if(isPressed)
  334. (*i)->updateMouseState(btn, isPressed);
  335. (*i)->click(btn, isPressed, prev);
  336. }
  337. else if(!isPressed)
  338. (*i)->click(btn, boost::logic::indeterminate, prev);
  339. }
  340. }
  341. void CGuiHandler::handleMouseMotion(const SDL_Event & current)
  342. {
  343. //sending active, hovered hoverable objects hover() call
  344. std::vector<CIntObject*> hlp;
  345. for(auto & elem : hoverable)
  346. {
  347. if(elem->pos.isInside(getCursorPosition()))
  348. {
  349. if (!(elem)->hovered)
  350. hlp.push_back((elem));
  351. }
  352. else if ((elem)->hovered)
  353. {
  354. (elem)->hover(false);
  355. (elem)->hovered = false;
  356. }
  357. }
  358. for(auto & elem : hlp)
  359. {
  360. elem->hover(true);
  361. elem->hovered = true;
  362. }
  363. handleMoveInterested(current.motion);
  364. }
  365. void CGuiHandler::simpleRedraw()
  366. {
  367. //update only top interface and draw background
  368. if(objsToBlit.size() > 1)
  369. CSDL_Ext::blitAt(screen2,0,0,screen); //blit background
  370. if(!objsToBlit.empty())
  371. objsToBlit.back()->show(screen); //blit active interface/window
  372. }
  373. void CGuiHandler::handleMoveInterested(const SDL_MouseMotionEvent & motion)
  374. {
  375. //sending active, MotionInterested objects mouseMoved() call
  376. std::list<CIntObject*> miCopy = motioninterested;
  377. for(auto & elem : miCopy)
  378. {
  379. if(elem->strongInterest || Rect::createAround(elem->pos, 1).isInside( motion.x, motion.y)) //checking bounds including border fixes bug #2476
  380. {
  381. (elem)->mouseMoved(motion);
  382. }
  383. }
  384. }
  385. void CGuiHandler::fakeMouseMove()
  386. {
  387. SDL_Event event;
  388. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
  389. int x, y;
  390. sme.state = SDL_GetMouseState(&x, &y);
  391. sme.x = x;
  392. sme.y = y;
  393. event.motion = sme;
  394. SDL_PushEvent(&event);
  395. }
  396. void CGuiHandler::renderFrame()
  397. {
  398. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  399. // During game:
  400. // When ending the game, the pim mutex might be hold by other thread,
  401. // that will notify us about the ending game by setting terminate_cond flag.
  402. //in PreGame terminate_cond stay false
  403. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  404. while(!terminate_cond->get() && !(acquiredTheLockOnPim = CPlayerInterface::pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  405. boost::this_thread::sleep(boost::posix_time::milliseconds(1));
  406. if(acquiredTheLockOnPim)
  407. {
  408. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  409. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim, boost::adopt_lock);
  410. if(nullptr != curInt)
  411. curInt->update();
  412. if(settings["general"]["showfps"].Bool())
  413. drawFPSCounter();
  414. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  415. SDL_RenderClear(mainRenderer);
  416. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  417. CCS->curh->render();
  418. SDL_RenderPresent(mainRenderer);
  419. disposed.clear();
  420. }
  421. mainFPSmng->framerateDelay(); // holds a constant FPS
  422. }
  423. CGuiHandler::CGuiHandler()
  424. : lastClick(-500, -500),lastClickTime(0), defActionsDef(0), captureChildren(false)
  425. {
  426. continueEventHandling = true;
  427. curInt = nullptr;
  428. statusbar = nullptr;
  429. // Creates the FPS manager and sets the framerate to 48 which is doubled the value of the original Heroes 3 FPS rate
  430. mainFPSmng = new CFramerateManager(60);
  431. //do not init CFramerateManager here --AVS
  432. terminate_cond = new CondSh<bool>(false);
  433. }
  434. CGuiHandler::~CGuiHandler()
  435. {
  436. delete mainFPSmng;
  437. delete terminate_cond;
  438. }
  439. void CGuiHandler::breakEventHandling()
  440. {
  441. continueEventHandling = false;
  442. }
  443. const Point & CGuiHandler::getCursorPosition() const
  444. {
  445. return cursorPosition;
  446. }
  447. void CGuiHandler::drawFPSCounter()
  448. {
  449. const static SDL_Color yellow = {255, 255, 0, 0};
  450. static SDL_Rect overlay = { 0, 0, 64, 32};
  451. Uint32 black = SDL_MapRGB(screen->format, 10, 10, 10);
  452. SDL_FillRect(screen, &overlay, black);
  453. std::string fps = boost::lexical_cast<std::string>(mainFPSmng->fps);
  454. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, yellow, Point(10, 10));
  455. }
  456. SDL_Keycode CGuiHandler::arrowToNum(SDL_Keycode key)
  457. {
  458. switch(key)
  459. {
  460. case SDLK_DOWN:
  461. return SDLK_KP_2;
  462. case SDLK_UP:
  463. return SDLK_KP_8;
  464. case SDLK_LEFT:
  465. return SDLK_KP_4;
  466. case SDLK_RIGHT:
  467. return SDLK_KP_6;
  468. default:
  469. throw std::runtime_error("Wrong key!");
  470. }
  471. }
  472. SDL_Keycode CGuiHandler::numToDigit(SDL_Keycode key)
  473. {
  474. #define REMOVE_KP(keyName) case SDLK_KP_ ## keyName : return SDLK_ ## keyName;
  475. switch(key)
  476. {
  477. REMOVE_KP(0)
  478. REMOVE_KP(1)
  479. REMOVE_KP(2)
  480. REMOVE_KP(3)
  481. REMOVE_KP(4)
  482. REMOVE_KP(5)
  483. REMOVE_KP(6)
  484. REMOVE_KP(7)
  485. REMOVE_KP(8)
  486. REMOVE_KP(9)
  487. REMOVE_KP(PERIOD)
  488. REMOVE_KP(MINUS)
  489. REMOVE_KP(PLUS)
  490. REMOVE_KP(EQUALS)
  491. case SDLK_KP_MULTIPLY:
  492. return SDLK_ASTERISK;
  493. case SDLK_KP_DIVIDE:
  494. return SDLK_SLASH;
  495. case SDLK_KP_ENTER:
  496. return SDLK_RETURN;
  497. default:
  498. return SDLK_UNKNOWN;
  499. }
  500. #undef REMOVE_KP
  501. }
  502. bool CGuiHandler::isNumKey(SDL_Keycode key, bool number)
  503. {
  504. if(number)
  505. return key >= SDLK_KP_1 && key <= SDLK_KP_0;
  506. else
  507. return (key >= SDLK_KP_1 && key <= SDLK_KP_0) || key == SDLK_KP_MINUS || key == SDLK_KP_PLUS || key == SDLK_KP_EQUALS;
  508. }
  509. bool CGuiHandler::isArrowKey(SDL_Keycode key)
  510. {
  511. return key == SDLK_UP || key == SDLK_DOWN || key == SDLK_LEFT || key == SDLK_RIGHT;
  512. }
  513. bool CGuiHandler::amIGuiThread()
  514. {
  515. return inGuiThread.get() && *inGuiThread;
  516. }
  517. void CGuiHandler::pushSDLEvent(int type, int usercode)
  518. {
  519. SDL_Event event;
  520. event.type = type;
  521. event.user.code = usercode; // not necessarily used
  522. SDL_PushEvent(&event);
  523. }
  524. CFramerateManager::CFramerateManager(int rate)
  525. {
  526. this->rate = rate;
  527. this->rateticks = (1000.0 / rate);
  528. this->fps = 0;
  529. this->accumulatedFrames = 0;
  530. this->accumulatedTime = 0;
  531. this->lastticks = 0;
  532. this->timeElapsed = 0;
  533. }
  534. void CFramerateManager::init()
  535. {
  536. this->lastticks = SDL_GetTicks();
  537. }
  538. void CFramerateManager::framerateDelay()
  539. {
  540. ui32 currentTicks = SDL_GetTicks();
  541. timeElapsed = currentTicks - lastticks;
  542. accumulatedFrames++;
  543. // FPS is higher than it should be, then wait some time
  544. if(timeElapsed < rateticks)
  545. {
  546. SDL_Delay((Uint32)ceil(this->rateticks) - timeElapsed);
  547. }
  548. currentTicks = SDL_GetTicks();
  549. // recalculate timeElapsed for external calls via getElapsed()
  550. // limit it to 100 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
  551. timeElapsed = std::min<ui32>(currentTicks - lastticks, 100);
  552. lastticks = SDL_GetTicks();
  553. accumulatedTime += timeElapsed;
  554. if(accumulatedFrames >= 100)
  555. {
  556. //about 2 second should be passed
  557. fps = static_cast<int>(ceil(1000.0 / (accumulatedTime / accumulatedFrames)));
  558. accumulatedTime = 0;
  559. accumulatedFrames = 0;
  560. }
  561. }