BsRenderWindow.h 11 KB

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