GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Window.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_WINDOW_HPP
5#define GREM_GRAPHICS_WINDOW_HPP
6
8
11#include <GREM/core/extents.hpp>
14
15namespace grem::resource {
16class ImageView; // Forward declaration, to avoid including Image.hpp.
17} // namespace grem::resource
18
19namespace grem::graphics {
20
28 String title = "Application";
29
36
43
49 Extent2D size{.width = 800, .height = 600};
50
64 uint32_t multisampleCount = 1;
65
69 float opacity = 1.0f;
70
75 bool highPixelDensity = false;
76
80 bool hidden = false;
81
85 bool focus = true;
86
90 bool minimized = false;
91
95 bool resizable = true;
96
100 bool fullscreen = false;
101
105 bool borderless = false;
106
111 bool hideFromTaskbar = false;
112
116 bool alwaysOnTop = false;
117
125 bool relativeMouseMode = false;
126};
127
131class Window {
132public:
139 [[nodiscard]] GREM_API(graphics) static Window* getFocused();
140
149 [[nodiscard]] GREM_API(graphics) static Window* findByID(uint32_t id);
150
161 GREM_API(graphics) explicit Window(const WindowOptions& options);
162
174 GREM_API(graphics) Window(Window& parent, const WindowOptions& options);
175
177 ~Window() = default;
178
180 Window(const Window&) = delete;
181
183 Window(Window&&) = delete;
184
186 Window& operator=(const Window&) = delete;
187
189 Window& operator=(Window&&) = delete;
190
204 GREM_API(graphics) void setParent(Window* newParent);
205
216 GREM_API(graphics) void setIcon(const resource::ImageView& newIconImage);
217
229 GREM_API(graphics) void setTitle(CStringView newTitle);
230
241 GREM_API(graphics) void setPosition(Offset2D newPosition);
242
255 GREM_API(graphics) void setSize(Extent2D newSize);
256
269 GREM_API(graphics) void setOpacity(float newOpacity);
270
283 GREM_API(graphics) void show(bool activate = true);
284
294 GREM_API(graphics) void hide();
295
305 GREM_API(graphics) void raise();
306
316 GREM_API(graphics) void minimize();
317
329 GREM_API(graphics) void setResizable(bool newResizable);
330
348 GREM_API(graphics) void setFullscreen(bool newFullscreen);
349
367
383 GREM_API(graphics) void setRelativeMouseMode(bool newRelativeMouseMode);
384
399 GREM_API(graphics) void setTextInputArea(const Region2D& area, int32_t cursorOffset);
400
411 GREM_API(graphics) void startTextInput();
412
423 GREM_API(graphics) void stopTextInput();
424
434 [[nodiscard]] GREM_API(graphics) bool isTextInputActive() const;
435
443 [[nodiscard]] GREM_API(graphics) bool hasScreenKeyboardSupport() const;
444
452 [[nodiscard]] GREM_API(graphics) bool isScreenKeyboardShown() const;
453
462 [[nodiscard]] GREM_API(graphics) bool isHighPixelDensity() const;
463
472 [[nodiscard]] GREM_API(graphics) bool hasFocus() const;
473
481 [[nodiscard]] GREM_API(graphics) bool isMinimized() const;
482
490 [[nodiscard]] GREM_API(graphics) bool isResizable() const;
491
500 [[nodiscard]] GREM_API(graphics) bool isFullscreen() const;
501
510 [[nodiscard]] GREM_API(graphics) Window* getParent() const;
511
520 [[nodiscard]] GREM_API(graphics) Offset2D getPosition() const;
521
533 [[nodiscard]] GREM_API(graphics) Extent2D getSize() const;
534
545 [[nodiscard]] GREM_API(graphics) Extent2D getDrawableSize() const;
546
553 [[nodiscard]] uint32_t getMultisampleCount() const {
554 return implementation.multisampleCount;
555 }
556
565 [[nodiscard]] GREM_API(graphics) float getDisplayScale() const;
566
578 [[nodiscard]] GREM_API(graphics) float getOpacity() const;
579
588 [[nodiscard]] GREM_API(graphics) Display getDisplay() const;
589
603
612 [[nodiscard]] GREM_API(graphics) uint32_t getID() const;
613
629 [[nodiscard]] GREM_API(graphics) void* getNativeHandle();
630
646 [[nodiscard]] const void* getNativeHandle() const {
647 return const_cast<Window*>(this)->getNativeHandle();
648 }
649
660 [[nodiscard]] void* get() noexcept { // NOLINT(readability-make-member-function-const)
661 return implementation.window;
662 }
663
674 [[nodiscard]] const void* get() const noexcept {
675 return implementation.window;
676 }
677
689 [[nodiscard]] void* getSurface() noexcept { // NOLINT(readability-make-member-function-const)
690 return implementation.surface;
691 }
692
704 [[nodiscard]] const void* getSurface() const noexcept {
705 return implementation.surface;
706 }
707
708private:
709 struct Implementation {
710 void* window = nullptr; // SDL_Window*
711 void* surface = nullptr; // VkSurfaceKHR on Vulkan, SDL_GLContext on OpenGL.
712 uint32_t multisampleCount;
713
714 GREM_API(graphics) Implementation(Window* parent, const WindowOptions& options);
715 GREM_API(graphics) ~Implementation();
716
717 Implementation(const Implementation&) = delete;
718 Implementation(Implementation&&) = delete;
719 Implementation& operator=(const Implementation&) = delete;
720 Implementation& operator=(Implementation&&) = delete;
721 };
722
723 Implementation implementation;
724};
725
726} // namespace grem::graphics
727
728#endif
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Information about a display on the host system.
Definition Display.hpp:95
Graphical window that can be rendered to.
Definition Window.hpp:131
void setTitle(CStringView newTitle)
Set the displayed title of the window.
bool isResizable() const
Check if the window is currently resizable.
bool isHighPixelDensity() const
Check if the window's backbuffer is high pixel density.
bool isTextInputActive() const
Check if text input events are currently being accepted.
void setResizable(bool newResizable)
Set whether to allow the window to be resized by the user or not.
void raise()
Request the window to be raised above other windows and gain focus.
~Window()=default
Destructor.
Offset2D getPosition() const
Get the position of the window as reported by the windowing system.
static Window * findByID(uint32_t id)
Get the window with a specific identifier.
bool hasFocus() const
Check if the window currently has input focus.
static Window * getFocused()
Get the window that currently has keyboard focus.
bool isMinimized() const
Check if the window is currently minimized.
void setOpacity(float newOpacity)
Set the opacity of the window.
Window & operator=(Window &&)=delete
Moving a window is not allowed, since it manages global state.
void setParent(Window *newParent)
Set the parent window of the window.
void stopTextInput()
Stop accepting text input events.
void setTextInputArea(const Region2D &area, int32_t cursorOffset)
Set the window's input area for text input.
Optional< DisplayMode > getFullscreenDisplayMode() const
Get the fullscreen display mode of the window.
void setFullscreen(bool newFullscreen)
Set the fullscreen state of the window.
Window(Window &parent, const WindowOptions &options)
Create a new child window.
void hide()
Hide the window.
float getOpacity() const
Get the opacity of the window.
void setSize(Extent2D newSize)
Set the size of the window.
void startTextInput()
Start accepting text input events in the current text input rectangle.
Window * getParent() const
Get the parent of the window.
Display getDisplay() const
Get the display that this window is associated with.
bool hasScreenKeyboardSupport() const
Check if the application supports a screen keyboard.
Window(const WindowOptions &options)
Create a new standalone window.
bool isScreenKeyboardShown() const
Check if the screen keyboard is currently open.
Window & operator=(const Window &)=delete
Copying a window is not allowed, since it manages global state.
void show(bool activate=true)
Show the window.
Window(Window &&)=delete
Moving a window is not allowed, since it manages global state.
void minimize()
Request the window to be minimized to the taskbar.
const void * getSurface() const noexcept
Get an opaque pointer to the internal representation of the window surface.
Definition Window.hpp:704
Window(const Window &)=delete
Copying a window is not allowed, since it manages global state.
const void * getNativeHandle() const
Get a pointer to the native handle of the window, if available for the current platform.
Definition Window.hpp:646
void * getNativeHandle()
Get a pointer to the native handle of the window, if available for the current platform.
void setIcon(const resource::ImageView &newIconImage)
Set the icon of the window.
float getDisplayScale() const
Get the content display scale relative to the window's pixel size.
uint32_t getID() const
Get a unique identifier for this window.
uint32_t getMultisampleCount() const
Get the number of samples used for multisample anti-aliasing (MSAA) when rendering to the window.
Definition Window.hpp:553
bool isFullscreen() const
Check if the window is currently in fullscreen mode.
void * getSurface() noexcept
Get an opaque pointer to the internal representation of the window surface.
Definition Window.hpp:689
Extent2D getSize() const
Get the size of the window.
void * get() noexcept
Get an opaque pointer to the internal representation of the window.
Definition Window.hpp:660
void setFullscreenDisplayMode(Optional< DisplayMode > newDisplayMode)
Set the display mode that will be used in fullscreen for this window if the window is not resizable.
void setRelativeMouseMode(bool newRelativeMouseMode)
Enable or disable relative mouse mode for this window.
Extent2D getDrawableSize() const
Get the drawable size of the window.
const void * get() const noexcept
Get an opaque pointer to the internal representation of the window.
Definition Window.hpp:674
void setPosition(Offset2D newPosition)
Set the position of the window in the windowing system.
Read-only non-owning view over a 2D image.
Definition Image.hpp:108
Definition buffer_layouts.hpp:20
Definition Window.hpp:15
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
Region< 2 > Region2D
Rectangular region of 2D data.
Definition extents.hpp:819
Offset< 2 > Offset2D
Offset in a 2D data region.
Definition extents.hpp:21
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Configuration options for a Window.
Definition Window.hpp:24
Optional< int32_t > positionY
The desired vertical position of the window, in screen coordinates (typically pixels),...
Definition Window.hpp:42
String title
Null-terminated UTF-8 string of the displayed title of the window.
Definition Window.hpp:28
bool minimized
Whether the window should start minimized or not.
Definition Window.hpp:90
bool hideFromTaskbar
Whether the window should be hidden from the taskbar and window list and be treated as a utility wind...
Definition Window.hpp:111
Optional< int32_t > positionX
The desired horizontal position of the window, in screen coordinates (typically pixels),...
Definition Window.hpp:35
bool fullscreen
Whether the window should start in fullscreen mode or not.
Definition Window.hpp:100
Extent2D size
The desired size of the window, in screen coordinates (typically pixels).
Definition Window.hpp:49
bool hidden
Whether the window should start hidden or not.
Definition Window.hpp:80
float opacity
Opacity of the window, from 0 (transparent) to 1 (opaque).
Definition Window.hpp:69
bool resizable
Whether the user should be allowed to resize the window or not.
Definition Window.hpp:95
bool relativeMouseMode
Whether the window should start with relative mouse mode enabled or not.
Definition Window.hpp:125
bool highPixelDensity
Whether the window's backbuffer should have high pixel density (if possible) or not.
Definition Window.hpp:75
bool focus
Whether the window should start with input focus or not.
Definition Window.hpp:85
uint32_t multisampleCount
Number of samples used for multisample anti-aliasing (MSAA) when rendering directly to the window.
Definition Window.hpp:64
bool borderless
Whether the window should be borderless or not.
Definition Window.hpp:105
bool alwaysOnTop
Whether the window should always be on top of other windows or not.
Definition Window.hpp:116