BsRenderWindow.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "RenderAPI/BsRenderTarget.h"
  6. #include "RenderAPI/BsVideoModeInfo.h"
  7. #include "Math/BsVector2I.h"
  8. namespace bs
  9. {
  10. class RenderWindowManager;
  11. /** @addtogroup RenderAPI-Internal
  12. * @{
  13. */
  14. /** Types of events that a RenderWindow can be notified of. */
  15. enum class WindowEventType
  16. {
  17. /** Triggered when window size changes. */
  18. Resized,
  19. /** Triggered when window position changes. */
  20. Moved,
  21. /** Triggered when window receives input focus. */
  22. FocusReceived,
  23. /** Triggered when window loses input focus. */
  24. FocusLost,
  25. /** Triggered when the window is minimized (iconified). */
  26. Minimized,
  27. /** Triggered when the window is expanded to cover the current screen. */
  28. Maximized,
  29. /** Triggered when the window leaves minimized or maximized state. */
  30. Restored,
  31. /** Triggered when the mouse pointer leaves the window area. */
  32. MouseLeft,
  33. /** Triggered when the user wants to close the window. */
  34. CloseRequested,
  35. };
  36. /** @} */
  37. /** @addtogroup RenderAPI
  38. * @{
  39. */
  40. /** Structure that is used for initializing a render window. */
  41. struct BS_CORE_EXPORT RENDER_WINDOW_DESC
  42. {
  43. RENDER_WINDOW_DESC()
  44. : fullscreen(false), vsync(false), vsyncInterval(1), hidden(false), depthBuffer(true)
  45. , multisampleCount(0), multisampleHint(""), gamma(false), left(-1), top(-1), title("")
  46. , showTitleBar(true), showBorder(true), allowResize(true), toolWindow (false), modal(false)
  47. , hideUntilSwap(false)
  48. { }
  49. VideoMode videoMode; /**< Output monitor, frame buffer resize and refresh rate. */
  50. bool fullscreen; /**< Should the window be opened in fullscreen mode. */
  51. bool vsync; /**< Should the window wait for vertical sync before swapping buffers. */
  52. UINT32 vsyncInterval; /**< Determines how many vsync intervals occur per frame. FPS = refreshRate/interval. Usually 1 when vsync active. */
  53. bool hidden; /**< Should the window be hidden initially. */
  54. bool depthBuffer; /**< Should the window be created with a depth/stencil buffer. */
  55. UINT32 multisampleCount; /**< If higher than 1, texture containing multiple samples per pixel is created. */
  56. String multisampleHint; /**< Hint about what kind of multisampling to use. Render system specific. */
  57. bool gamma; /**< Should the written color pixels be gamma corrected before write. */
  58. INT32 left; /**< Window origin on X axis in pixels. -1 == screen center. Relative to monitor provided in videoMode. */
  59. INT32 top; /**< Window origin on Y axis in pixels. -1 == screen center. Relative to monitor provided in videoMode. */
  60. String title; /**< Title of the window. */
  61. bool showTitleBar; /**< Determines if the title-bar should be shown or not. */
  62. bool showBorder; /**< Determines if the window border should be shown or not. */
  63. bool allowResize; /**< Determines if the user can resize the window by dragging on the window edges. */
  64. bool toolWindow; /**< Tool windows have a different look than normal windows and have no task bar entry. */
  65. bool modal; /**< When a modal window is open all other windows will be locked until modal window is closed. */
  66. bool hideUntilSwap; /**< Window will be created as hidden and only be shown when the first framebuffer swap happens. */
  67. NameValuePairList platformSpecific; /**< Platform-specific creation options. */
  68. };
  69. /** Contains various properties that describe a render window. */
  70. class BS_CORE_EXPORT RenderWindowProperties : public RenderTargetProperties
  71. {
  72. public:
  73. RenderWindowProperties(const RENDER_WINDOW_DESC& desc);
  74. virtual ~RenderWindowProperties() { }
  75. /** True if window is running in fullscreen mode. */
  76. bool isFullScreen = false;
  77. /** Horizontal origin of the window in pixels. */
  78. INT32 left = 0;
  79. /** Vertical origin of the window in pixels. */
  80. INT32 top = 0;
  81. /** Indicates whether the window currently has keyboard focus. */
  82. bool hasFocus = false;
  83. /** True if the window is hidden. */
  84. bool isHidden = false;
  85. /** True if the window is modal (blocks interaction with any non-modal window until closed). */
  86. bool isModal = false;
  87. /** True if the window is maximized. */
  88. bool isMaximized = false;
  89. };
  90. /**
  91. * Operating system window with a specific position, size and style. Each window serves as a surface that can be
  92. * rendered into by RenderAPI operations.
  93. */
  94. class BS_CORE_EXPORT RenderWindow : public RenderTarget
  95. {
  96. public:
  97. virtual ~RenderWindow() { }
  98. /** Converts screen position into window local position. */
  99. virtual Vector2I screenToWindowPos(const Vector2I& screenPos) const = 0;
  100. /** Converts window local position to screen position. */
  101. virtual Vector2I windowToScreenPos(const Vector2I& windowPos) const = 0;
  102. /**
  103. * Resize the window to specified width and height in pixels.
  104. *
  105. * @param[in] width Width of the window in pixels.
  106. * @param[in] height Height of the window in pixels.
  107. */
  108. virtual void resize(UINT32 width, UINT32 height);
  109. /**
  110. * Move the window to specified screen coordinates.
  111. *
  112. * @param[in] left Position of the left border of the window on the screen.
  113. * @param[in] top Position of the top border of the window on the screen.
  114. *
  115. * @note This is an @ref asyncMethod "asynchronous method".
  116. */
  117. virtual void move(INT32 left, INT32 top);
  118. /**
  119. * Hides the window.
  120. *
  121. * @note This is an @ref asyncMethod "asynchronous method".
  122. */
  123. virtual void hide();
  124. /**
  125. * Shows a previously hidden window.
  126. *
  127. * @note This is an @ref asyncMethod "asynchronous method".
  128. */
  129. virtual void show();
  130. /**
  131. * @copydoc ct::RenderWindow::minimize
  132. *
  133. * @note This is an @ref asyncMethod "asynchronous method".
  134. */
  135. virtual void minimize();
  136. /**
  137. * @copydoc ct::RenderWindow::maximize
  138. *
  139. * @note This is an @ref asyncMethod "asynchronous method".
  140. */
  141. virtual void maximize();
  142. /**
  143. * @copydoc ct::RenderWindow::restore
  144. *
  145. * @note This is an @ref asyncMethod "asynchronous method".
  146. */
  147. virtual void restore();
  148. /**
  149. * @copydoc ct::RenderWindow::setFullscreen(UINT32, UINT32, float, UINT32)
  150. *
  151. * @note This is an @ref asyncMethod "asynchronous method".
  152. */
  153. virtual void setFullscreen(UINT32 width, UINT32 height, float refreshRate = 60.0f, UINT32 monitorIdx = 0);
  154. /**
  155. * @copydoc ct::RenderWindow::setFullscreen(const VideoMode&)
  156. *
  157. * @note This is an @ref asyncMethod "asynchronous method".
  158. */
  159. virtual void setFullscreen(const VideoMode& videoMode);
  160. /**
  161. * @copydoc ct::RenderWindow::setWindowed
  162. *
  163. * @note This is an @ref asyncMethod "asynchronous method".
  164. */
  165. virtual void setWindowed(UINT32 width, UINT32 height);
  166. /** Retrieves a core implementation of a render window usable only from the core thread. */
  167. SPtr<ct::RenderWindow> getCore() const;
  168. /** Returns properties that describe the render window. */
  169. const RenderWindowProperties& getProperties() const;
  170. /** Closes and destroys the window. */
  171. void destroy() override;
  172. /**
  173. * Creates a new render window using the specified options. Optionally makes the created window a child of another
  174. * window.
  175. */
  176. static SPtr<RenderWindow> create(RENDER_WINDOW_DESC& desc, SPtr<RenderWindow> parentWindow = nullptr);
  177. /** Triggers when the OS requests that the window is closed (e.g. user clicks on the X button in the title bar). */
  178. Event<void()> onCloseRequested;
  179. /**
  180. * @name Internal
  181. */
  182. /** Notifies the window that a specific event occurred. Usually called by the platform specific main event loop. */
  183. void _notifyWindowEvent(WindowEventType type);
  184. /** Method that triggers whenever the window changes size or position. */
  185. virtual void _windowMovedOrResized() { }
  186. /** @} */
  187. protected:
  188. friend class RenderWindowManager;
  189. RenderWindow(const RENDER_WINDOW_DESC& desc, UINT32 windowId);
  190. /** Returns render window properties that may be edited. */
  191. RenderWindowProperties& getMutableProperties();
  192. /** Updates window properties from the synced property data. */
  193. virtual void syncProperties() = 0;
  194. protected:
  195. RENDER_WINDOW_DESC mDesc;
  196. UINT32 mWindowId;
  197. };
  198. /** @} */
  199. namespace ct
  200. {
  201. /** @addtogroup RenderAPI-Internal
  202. * @{
  203. */
  204. /** Core thread counterpart of bs::RenderWindow. */
  205. class BS_CORE_EXPORT RenderWindow : public RenderTarget
  206. {
  207. public:
  208. RenderWindow(const RENDER_WINDOW_DESC& desc);
  209. virtual ~RenderWindow();
  210. /**
  211. * Switches the window to fullscreen mode. Child windows cannot go into fullscreen mode.
  212. *
  213. * @param[in] width Width of the window frame buffer in pixels.
  214. * @param[in] height Height of the window frame buffer in pixels.
  215. * @param[in] refreshRate Refresh rate of the window in Hertz.
  216. * @param[in] monitorIdx Index of the monitor to go fullscreen on.
  217. *
  218. * @note If the exact provided mode isn't available, closest one is used instead.
  219. */
  220. virtual void setFullscreen(UINT32 width, UINT32 height, float refreshRate = 60.0f, UINT32 monitorIdx = 0) { }
  221. /**
  222. * Switches the window to fullscreen mode. Child windows cannot go into fullscreen mode.
  223. *
  224. * @param[in] videoMode Mode retrieved from VideoModeInfo in RenderAPI.
  225. */
  226. virtual void setFullscreen(const VideoMode& videoMode) { }
  227. /**
  228. * Switches the window to windowed mode.
  229. *
  230. * @param[in] width Window width in pixels.
  231. * @param[in] height Window height in pixels.
  232. */
  233. virtual void setWindowed(UINT32 width, UINT32 height) { }
  234. /** Hide or show the window. */
  235. virtual void setHidden(bool hidden);
  236. /**
  237. * Makes the render target active or inactive. (for example in the case of a window, it will hide or restore the
  238. * window).
  239. */
  240. virtual void setActive(bool state);
  241. /** Minimizes the window to the taskbar. */
  242. virtual void minimize() { }
  243. /** Maximizes the window over the entire current screen. */
  244. virtual void maximize() { }
  245. /** Restores the window to original position and size if it is minimized or maximized. */
  246. virtual void restore() { }
  247. /** Change the size of the window. */
  248. virtual void resize(UINT32 width, UINT32 height) = 0;
  249. /** Reposition the window. */
  250. virtual void move(INT32 left, INT32 top) = 0;
  251. /**
  252. * Enables or disables vertical synchronization. When enabled the system will wait for monitor refresh before
  253. * presenting the back buffer. This eliminates tearing but can result in increased input lag.
  254. *
  255. * @param enabled True to enable vsync, false to disable.
  256. * @param interval Interval at which to perform the sync. Value of one means the sync will be performed for
  257. * each monitor refresh, value of two means it will be performs for every second (half the
  258. * rate), and so on.
  259. */
  260. virtual void setVSync(bool enabled, UINT32 interval = 1) = 0;
  261. /** Returns properties that describe the render window. */
  262. const RenderWindowProperties& getProperties() const;
  263. /** Notifies the window that a specific event occurred. Usually called by the platform specific main event loop. */
  264. void _notifyWindowEvent(WindowEventType type);
  265. /** Method that triggers whenever the window changes size or position. */
  266. virtual void _windowMovedOrResized() { }
  267. protected:
  268. friend class bs::RenderWindow;
  269. friend class RenderWindowManager;
  270. friend class bs::RenderWindowManager;
  271. /**
  272. * Returns window properties that are always kept in sync between core and sim threads.
  273. *
  274. * @note Used for keeping up what are the most up to date settings.
  275. */
  276. virtual RenderWindowProperties& getSyncedProperties() = 0;
  277. /** Updates window properties from the synced property data. */
  278. virtual void syncProperties() = 0;
  279. RENDER_WINDOW_DESC mDesc;
  280. SpinLock mLock;
  281. UINT32 mWindowId;
  282. };
  283. /** @} */
  284. }
  285. }