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