CGuiHandler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "CIntObject.h"
  14. #include "CursorHandler.h"
  15. #include "ShortcutHandler.h"
  16. #include "FramerateManager.h"
  17. #include "WindowHandler.h"
  18. #include "EventDispatcher.h"
  19. #include "../eventsSDL/InputHandler.h"
  20. #include "../CGameInfo.h"
  21. #include "../render/Colors.h"
  22. #include "../renderSDL/ScreenHandler.h"
  23. #include "../CMT.h"
  24. #include "../CPlayerInterface.h"
  25. #include "../battle/BattleInterface.h"
  26. #include "../../lib/CThreadHelper.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include <SDL_render.h>
  29. CGuiHandler GH;
  30. boost::thread_specific_ptr<bool> inGuiThread;
  31. SObjectConstruction::SObjectConstruction(CIntObject *obj)
  32. :myObj(obj)
  33. {
  34. GH.createdObj.push_front(obj);
  35. GH.captureChildren = true;
  36. }
  37. SObjectConstruction::~SObjectConstruction()
  38. {
  39. assert(GH.createdObj.size());
  40. assert(GH.createdObj.front() == myObj);
  41. GH.createdObj.pop_front();
  42. GH.captureChildren = GH.createdObj.size();
  43. }
  44. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  45. {
  46. previousCapture = GH.captureChildren;
  47. GH.captureChildren = false;
  48. prevActions = GH.defActionsDef;
  49. GH.defActionsDef = actions;
  50. }
  51. SSetCaptureState::~SSetCaptureState()
  52. {
  53. GH.captureChildren = previousCapture;
  54. GH.defActionsDef = prevActions;
  55. }
  56. void CGuiHandler::init()
  57. {
  58. inputHandlerInstance = std::make_unique<InputHandler>();
  59. eventDispatcherInstance = std::make_unique<EventDispatcher>();
  60. windowHandlerInstance = std::make_unique<WindowHandler>();
  61. screenHandlerInstance = std::make_unique<ScreenHandler>();
  62. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  63. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  64. }
  65. void CGuiHandler::handleEvents()
  66. {
  67. events().dispatchTimer(framerate().getElapsedMilliseconds());
  68. //player interface may want special event handling
  69. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  70. return;
  71. input().processEvents();
  72. }
  73. void CGuiHandler::fakeMouseMove()
  74. {
  75. dispatchMainThread([](){
  76. assert(CPlayerInterface::pim);
  77. boost::unique_lock lock(*CPlayerInterface::pim);
  78. GH.events().dispatchMouseMoved(Point(0, 0), GH.getCursorPosition());
  79. });
  80. }
  81. void CGuiHandler::startTextInput(const Rect & whereInput)
  82. {
  83. input().startTextInput(whereInput);
  84. }
  85. void CGuiHandler::stopTextInput()
  86. {
  87. input().stopTextInput();
  88. }
  89. void CGuiHandler::renderFrame()
  90. {
  91. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  92. // During game:
  93. // When ending the game, the pim mutex might be hold by other thread,
  94. // that will notify us about the ending game by setting terminate_cond flag.
  95. //in PreGame terminate_cond stay false
  96. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  97. while(!terminate_cond->get() && !(acquiredTheLockOnPim = CPlayerInterface::pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  98. boost::this_thread::sleep(boost::posix_time::milliseconds(1));
  99. if(acquiredTheLockOnPim)
  100. {
  101. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  102. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim, boost::adopt_lock);
  103. if(nullptr != curInt)
  104. curInt->update();
  105. if(settings["video"]["showfps"].Bool())
  106. drawFPSCounter();
  107. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  108. SDL_RenderClear(mainRenderer);
  109. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  110. CCS->curh->render();
  111. SDL_RenderPresent(mainRenderer);
  112. windows().onFrameRendered();
  113. }
  114. framerate().framerateDelay(); // holds a constant FPS
  115. }
  116. CGuiHandler::CGuiHandler()
  117. : defActionsDef(0)
  118. , captureChildren(false)
  119. , curInt(nullptr)
  120. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  121. , terminate_cond (new CondSh<bool>(false))
  122. {
  123. }
  124. CGuiHandler::~CGuiHandler()
  125. {
  126. delete terminate_cond;
  127. }
  128. ShortcutHandler & CGuiHandler::shortcuts()
  129. {
  130. assert(shortcutsHandlerInstance);
  131. return *shortcutsHandlerInstance;
  132. }
  133. FramerateManager & CGuiHandler::framerate()
  134. {
  135. assert(framerateManagerInstance);
  136. return *framerateManagerInstance;
  137. }
  138. bool CGuiHandler::isKeyboardCtrlDown() const
  139. {
  140. return inputHandlerInstance->isKeyboardCtrlDown();
  141. }
  142. bool CGuiHandler::isKeyboardAltDown() const
  143. {
  144. return inputHandlerInstance->isKeyboardAltDown();
  145. }
  146. bool CGuiHandler::isKeyboardShiftDown() const
  147. {
  148. return inputHandlerInstance->isKeyboardShiftDown();
  149. }
  150. const Point & CGuiHandler::getCursorPosition() const
  151. {
  152. return inputHandlerInstance->getCursorPosition();
  153. }
  154. Point CGuiHandler::screenDimensions() const
  155. {
  156. return Point(screen->w, screen->h);
  157. }
  158. void CGuiHandler::drawFPSCounter()
  159. {
  160. static SDL_Rect overlay = { 0, 0, 64, 32};
  161. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  162. SDL_FillRect(screen, &overlay, black);
  163. std::string fps = std::to_string(framerate().getFramerate());
  164. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
  165. }
  166. bool CGuiHandler::amIGuiThread()
  167. {
  168. return inGuiThread.get() && *inGuiThread;
  169. }
  170. void CGuiHandler::dispatchMainThread(const std::function<void()> & functor)
  171. {
  172. inputHandlerInstance->dispatchMainThread(functor);
  173. }
  174. IScreenHandler & CGuiHandler::screenHandler()
  175. {
  176. return *screenHandlerInstance;
  177. }
  178. EventDispatcher & CGuiHandler::events()
  179. {
  180. return *eventDispatcherInstance;
  181. }
  182. InputHandler & CGuiHandler::input()
  183. {
  184. return *inputHandlerInstance;
  185. }
  186. WindowHandler & CGuiHandler::windows()
  187. {
  188. assert(windowHandlerInstance);
  189. return *windowHandlerInstance;
  190. }
  191. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  192. {
  193. auto locked = currentStatusBar.lock();
  194. if (!locked)
  195. return fakeStatusBar;
  196. return locked;
  197. }
  198. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  199. {
  200. currentStatusBar = newStatusBar;
  201. }
  202. void CGuiHandler::onScreenResize()
  203. {
  204. screenHandler().onScreenResize();
  205. windows().onScreenResize();
  206. }