GameEngine.h 4.0 KB

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