GameEngine.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 GameEngine
  32. {
  33. private:
  34. /// Fake no-op version status bar, for use in windows that have no status bar
  35. std::shared_ptr<IStatusBar> fakeStatusBar;
  36. /// Status bar of current window, if any. Uses weak_ptr to allow potential hanging reference after owned window has been deleted
  37. std::weak_ptr<IStatusBar> currentStatusBar;
  38. std::unique_ptr<ShortcutHandler> shortcutsHandlerInstance;
  39. std::unique_ptr<WindowHandler> windowHandlerInstance;
  40. std::unique_ptr<IScreenHandler> screenHandlerInstance;
  41. std::unique_ptr<IRenderHandler> renderHandlerInstance;
  42. std::unique_ptr<FramerateManager> framerateManagerInstance;
  43. std::unique_ptr<EventDispatcher> eventDispatcherInstance;
  44. std::unique_ptr<InputHandler> inputHandlerInstance;
  45. std::unique_ptr<ISoundPlayer> soundPlayerInstance;
  46. std::unique_ptr<IMusicPlayer> musicPlayerInstance;
  47. std::unique_ptr<CursorHandler> cursorHandlerInstance;
  48. std::unique_ptr<IVideoPlayer> videoPlayerInstance;
  49. std::unique_ptr<AsyncRunner> asyncTasks;
  50. IGameEngineUser *engineUser = nullptr;
  51. void updateFrame();
  52. public:
  53. std::mutex interfaceMutex;
  54. /// returns current position of mouse cursor, relative to vcmi window
  55. const Point & getCursorPosition() const;
  56. ShortcutHandler & shortcuts();
  57. FramerateManager & framerate();
  58. EventDispatcher & events();
  59. InputHandler & input();
  60. AsyncRunner & async() { return *asyncTasks; }
  61. IGameEngineUser & user() { return *engineUser; }
  62. ISoundPlayer & sound() { return *soundPlayerInstance; }
  63. IMusicPlayer & music() { return *musicPlayerInstance; }
  64. CursorHandler & cursor() { return *cursorHandlerInstance; }
  65. IVideoPlayer & video() { return *videoPlayerInstance; }
  66. /// Returns current logical screen dimensions
  67. /// May not match size of window if user has UI scaling different from 100%
  68. Point screenDimensions() const;
  69. /// returns true if Alt is currently pressed down
  70. bool isKeyboardAltDown() const;
  71. /// returns true if Ctrl is currently pressed down
  72. /// on Apple system, this also tests for Cmd key
  73. /// For use with keyboard-based events
  74. bool isKeyboardCtrlDown() const;
  75. /// on Apple systems, returns true if Cmd key is pressed
  76. /// on other systems, returns true if Ctrl is pressed
  77. /// /// For use with mouse-based events
  78. bool isKeyboardCmdDown() const;
  79. /// returns true if Shift is currently pressed down
  80. bool isKeyboardShiftDown() const;
  81. IScreenHandler & screenHandler();
  82. IRenderHandler & renderHandler();
  83. WindowHandler & windows();
  84. /// Returns currently active status bar. Guaranteed to be non-null
  85. std::shared_ptr<IStatusBar> statusbar();
  86. /// Set currently active status bar
  87. void setStatusbar(std::shared_ptr<IStatusBar>);
  88. void setEngineUser(IGameEngineUser * user);
  89. bool captureChildren; //all newly created objects will get their parents from stack and will be added to parents children list
  90. std::list<CIntObject *> createdObj; //stack of objs being created
  91. GameEngine();
  92. ~GameEngine();
  93. [[noreturn]] void mainLoop();
  94. /// called whenever SDL_WINDOWEVENT_RESTORED is reported or the user selects a different resolution, requiring to center/resize all windows
  95. void onScreenResize(bool resolutionChanged);
  96. void handleEvents(); //takes events from queue and calls interested objects
  97. void fakeMouseMove();
  98. void drawFPSCounter(); // draws the FPS to the upper left corner of the screen
  99. bool amIGuiThread();
  100. /// Calls provided functor in main thread on next execution frame
  101. void dispatchMainThread(const std::function<void()> & functor);
  102. };
  103. extern std::unique_ptr<GameEngine> ENGINE; //global gui handler