GameEngine.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * GameEngine.h, 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. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. class Point;
  13. class AsyncRunner;
  14. class Rect;
  15. VCMI_LIB_NAMESPACE_END
  16. enum class MouseButton;
  17. class ShortcutHandler;
  18. class FramerateManager;
  19. class IStatusBar;
  20. class CIntObject;
  21. class IGameEngineUser;
  22. class IRenderHandler;
  23. class IScreenHandler;
  24. class WindowHandler;
  25. class EventDispatcher;
  26. class InputHandler;
  27. class ISoundPlayer;
  28. class IMusicPlayer;
  29. class CursorHandler;
  30. class IVideoPlayer;
  31. class Discord;
  32. class GameEngine
  33. {
  34. private:
  35. /// Fake no-op version status bar, for use in windows that have no status bar
  36. std::shared_ptr<IStatusBar> fakeStatusBar;
  37. /// Status bar of current window, if any. Uses weak_ptr to allow potential hanging reference after owned window has been deleted
  38. std::weak_ptr<IStatusBar> currentStatusBar;
  39. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  40. std::unique_ptr<WindowHandler> windowHandlerInstance;
  41. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  42. std::unique_ptr<IRenderHandler> renderHandlerInstance;
  43. std::unique_ptr<FramerateManager> framerateManagerInstance;
  44. std::unique_ptr<EventDispatcher> eventDispatcherInstance;
  45. std::unique_ptr<InputHandler> inputHandlerInstance;
  46. std::unique_ptr<ISoundPlayer> soundPlayerInstance;
  47. std::unique_ptr<IMusicPlayer> musicPlayerInstance;
  48. std::unique_ptr<CursorHandler> cursorHandlerInstance;
  49. std::unique_ptr<IVideoPlayer> videoPlayerInstance;
  50. std::unique_ptr<AsyncRunner> asyncTasks;
  51. std::unique_ptr<Discord> discordInstance;
  52. IGameEngineUser *engineUser = nullptr;
  53. int maxPerformanceOverlayTextWidth = 0;
  54. void updateFrame();
  55. void handleEvents(); //takes events from queue and calls interested objects
  56. void drawPerformanceOverlay(); // draws box with additional infos (e.g. fps)
  57. public:
  58. std::mutex interfaceMutex;
  59. /// returns current position of mouse cursor, relative to vcmi window
  60. const Point & getCursorPosition() const;
  61. ShortcutHandler & shortcuts();
  62. FramerateManager & framerate();
  63. EventDispatcher & events();
  64. InputHandler & input();
  65. AsyncRunner & async() { return *asyncTasks; }
  66. IGameEngineUser & user() { return *engineUser; }
  67. ISoundPlayer & sound() { return *soundPlayerInstance; }
  68. IMusicPlayer & music() { return *musicPlayerInstance; }
  69. CursorHandler & cursor() { return *cursorHandlerInstance; }
  70. IVideoPlayer & video() { return *videoPlayerInstance; }
  71. Discord & discord() { return *discordInstance; }
  72. /// Returns current logical screen dimensions
  73. /// May not match size of window if user has UI scaling different from 100%
  74. Point screenDimensions() const;
  75. /// returns true if Alt is currently pressed down
  76. bool isKeyboardAltDown() const;
  77. /// returns true if Ctrl is currently pressed down
  78. /// on Apple system, this also tests for Cmd key
  79. /// For use with keyboard-based events
  80. bool isKeyboardCtrlDown() const;
  81. /// on Apple systems, returns true if Cmd key is pressed
  82. /// on other systems, returns true if Ctrl is pressed
  83. /// /// For use with mouse-based events
  84. bool isKeyboardCmdDown() const;
  85. /// returns true if Shift is currently pressed down
  86. bool isKeyboardShiftDown() const;
  87. IScreenHandler & screenHandler();
  88. IRenderHandler & renderHandler();
  89. WindowHandler & windows();
  90. /// Returns currently active status bar. Guaranteed to be non-null
  91. std::shared_ptr<IStatusBar> statusbar();
  92. /// Set currently active status bar
  93. void setStatusbar(const std::shared_ptr<IStatusBar> &);
  94. /// Sets engine user that is used as target of callback for events received by engine
  95. void setEngineUser(IGameEngineUser * user);
  96. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  97. std::list<CIntObject *> createdObj; //stack of objs being created
  98. GameEngine();
  99. ~GameEngine();
  100. /// Performs main game loop till game shutdown
  101. /// This method never returns, to abort main loop throw GameShutdownException
  102. [[noreturn]] void mainLoop();
  103. /// called whenever SDL_WINDOWEVENT_RESTORED is reported or the user selects a different resolution, requiring to center/resize all windows
  104. void onScreenResize(bool resolutionChanged, bool windowResized);
  105. /// Simulate mouse movement to force refresh UI state that updates on mouse move
  106. void fakeMouseMove();
  107. /// Returns true for calls made from main (GUI) thread, false othervice
  108. bool amIGuiThread();
  109. /// Calls provided functor in main thread on next execution frame
  110. void dispatchMainThread(const std::function<void()> & functor);
  111. };
  112. extern std::unique_ptr<GameEngine> ENGINE; //global gui handler