Nameless Engine
Loading...
Searching...
No Matches
ne::Window Class Reference

#include <Window.h>

Public Member Functions

 Window (const Window &)=delete
 
Windowoperator= (const Window &)=delete
 
void setPreferredRenderer (const std::optional< RendererType > &preferredRenderer)
 
template<typename MyGameInstance >
requires std::derived_from<MyGameInstance, GameInstance>
void processEvents ()
 
void setOpacity (float opacity) const
 
void setTitle (const std::string &sNewTitle)
 
std::optional< ErrorsetIcon (const std::filesystem::path &pathToIcon) const
 
std::variant< WindowCursor *, ErrorcreateCursor (const std::filesystem::path &pathToIcon)
 
void setCursor (WindowCursor *pCursor)
 
void setDefaultCursor ()
 
void setCursorVisibility (bool bIsVisible) const
 
void minimize () const
 
void maximize () const
 
void restore () const
 
void hide () const
 
void show () const
 
void close () const
 
std::pair< int, int > getSize () const
 
std::pair< double, double > getCursorPosition () const
 
std::string getTitle () const
 
float getOpacity () const
 
RenderergetRenderer () const
 
GLFWwindow * getGlfwWindow () const
 
void onKeyboardInput (KeyboardKey key, KeyboardModifiers modifiers, bool bIsPressedDown) const
 
void onMouseInput (MouseButton button, KeyboardModifiers modifiers, bool bIsPressedDown) const
 
void onMouseScrollMove (int iOffset) const
 

Static Public Member Functions

static WindowBuilder getBuilder ()
 

Private Member Functions

void bindToWindowEvents ()
 
void unbindFromWindowEvents ()
 
void showErrorIfNotOnMainThread () const
 
void onMouseMove (double xPos, double yPos)
 
void onWindowFocusChanged (bool bIsFocused) const
 
void onFramebufferSizeChanged (int iWidth, int iHeight) const
 
 Window (GLFWwindow *pGlfwWindow, const std::string &sWindowTitle)
 

Static Private Member Functions

static void glfwWindowKeyboardCallback (GLFWwindow *pGlfwWindow, int iKey, int iScancode, int iAction, int iMods)
 
static void glfwWindowMouseCallback (GLFWwindow *pGlfwWindow, int iButton, int iAction, int iMods)
 
static void glfwWindowFocusCallback (GLFWwindow *pGlfwWindow, int iFocused)
 
static void glfwWindowMouseCursorPosCallback (GLFWwindow *pGlfwWindow, double xPos, double yPos)
 
static void glfwWindowMouseScrollCallback (GLFWwindow *pGlfwWindow, double xOffset, double yOffset)
 
static void glfwFramebufferResizeCallback (GLFWwindow *pGlfwWindow, int iWidth, int iHeight)
 
static std::variant< std::unique_ptr< Window >, Errorcreate (WindowBuilderParameters &params)
 

Private Attributes

std::unique_ptr< GameManagerpGameManager
 
GLFWwindow * pGlfwWindow = nullptr
 
std::vector< std::unique_ptr< WindowCursor > > vCreatedCursors
 
std::thread::id mainThreadId
 
std::string sWindowTitle
 
std::optional< RendererType > preferredRenderer
 
double lastMouseXPos = 0.0
 
double lastMouseYPos = 0.0
 

Friends

class WindowBuilder
 

Detailed Description

Describes a window.

Constructor & Destructor Documentation

◆ Window()

ne::Window::Window ( GLFWwindow *  pGlfwWindow,
const std::string &  sWindowTitle 
)
private

Default constructor.

Parameters
pGlfwWindowCreated GLFW window.
sWindowTitleTitle of this window.

Member Function Documentation

◆ bindToWindowEvents()

void ne::Window::bindToWindowEvents ( )
private

Binds to various window events such as user input events.

Remarks
Expects game instance to be created at this point.

◆ close()

void ne::Window::close ( ) const

Closes this window causing game instance, audio engine and etc to be destroyed.

◆ create()

std::variant< std::unique_ptr< Window >, Error > ne::Window::create ( WindowBuilderParameters params)
staticprivate

Creates a new window.

Parameters
paramsParameters that define initial window state.
Returns
Returns error if something went wrong or created window otherwise.

◆ createCursor()

std::variant< WindowCursor *, Error > ne::Window::createCursor ( const std::filesystem::path &  pathToIcon)

Loads the image and creates a new cursor, note that in order for this new cursor to be visible you have to call setCursor.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.
Parameters
pathToIconPath to the image (.png). The image data should be 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits per channel with the red channel first.
Returns
Error if something went wrong, otherwise a non-owning pointer to the created cursor that is managed by the window. Do not delete returned pointer, the window owns this cursor and will automatically delete it when the window is closed.

◆ getBuilder()

WindowBuilder ne::Window::getBuilder ( )
static

Returns a builder for a new window. Use can use this static function to receive a WindowBuilder or create an instance of WindowBuilder manually.

Returns
Builder for a new window.

◆ getCursorPosition()

std::pair< double, double > ne::Window::getCursorPosition ( ) const

Returns the current cursor position on window.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.
Returns
A pair of X and Y coordinates in range [0.0; 1.0] relative to the upper-left corner of the window.

◆ getGlfwWindow()

GLFWwindow * ne::Window::getGlfwWindow ( ) const

Returns internal GLFW window pointer.

Remarks
Do not delete (free) returned pointer.
Returns
GLFW window.

◆ getOpacity()

float ne::Window::getOpacity ( ) const

Returns window opacity.

Returns
Window opacity.

◆ getRenderer()

Renderer * ne::Window::getRenderer ( ) const

Returns renderer used for this window.

Returns
nullptr if renderer was not created yet, valid pointer otherwise.

◆ getSize()

std::pair< int, int > ne::Window::getSize ( ) const

Returns the current window size in pixels.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.
Returns
A pair of width and height in pixels.

◆ getTitle()

std::string ne::Window::getTitle ( ) const

Returns the title of this window.

Returns
Title of the window.

◆ glfwFramebufferResizeCallback()

void ne::Window::glfwFramebufferResizeCallback ( GLFWwindow *  pGlfwWindow,
int  iWidth,
int  iHeight 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowWindow.
iWidthNew width of the framebuffer (in pixels).
iHeightNew height of the framebuffer (in pixels).

◆ glfwWindowFocusCallback()

void ne::Window::glfwWindowFocusCallback ( GLFWwindow *  pGlfwWindow,
int  iFocused 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowThe window that received the event.
iFocusedGLFW_TRUE if the window was given input focus, or GLFW_FALSE if it lost it.

◆ glfwWindowKeyboardCallback()

void ne::Window::glfwWindowKeyboardCallback ( GLFWwindow *  pGlfwWindow,
int  iKey,
int  iScancode,
int  iAction,
int  iMods 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowThe window that received the event.
iKeyThe keyboard key that was pressed or released.
iScancodeThe system-specific scancode of the key.
iActionGLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT.
iModsBit field describing which modifier keys were held down.

◆ glfwWindowMouseCallback()

void ne::Window::glfwWindowMouseCallback ( GLFWwindow *  pGlfwWindow,
int  iButton,
int  iAction,
int  iMods 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowThe window that received the event.
iButtonThe mouse button that was pressed or released.
iActionOne of GLFW_PRESS or GLFW_RELEASE.
iModsBit field describing which modifier keys were held down.

◆ glfwWindowMouseCursorPosCallback()

void ne::Window::glfwWindowMouseCursorPosCallback ( GLFWwindow *  pGlfwWindow,
double  xPos,
double  yPos 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowThe window that received the event.
xPosThe new x-coordinate, in screen coordinates, of the cursor.
yPosThe new y-coordinate, in screen coordinates, of the cursor.

◆ glfwWindowMouseScrollCallback()

void ne::Window::glfwWindowMouseScrollCallback ( GLFWwindow *  pGlfwWindow,
double  xOffset,
double  yOffset 
)
staticprivate

GLFW callback.

Parameters
pGlfwWindowThe window that received the event.
xOffsetThe scroll offset along the x-axis.
yOffsetThe scroll offset along the y-axis.

◆ hide()

void ne::Window::hide ( ) const

Hides the windows (makes it invisible). Does nothing for fullscreen windows.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.

◆ maximize()

void ne::Window::maximize ( ) const

Maximizes the window.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.

◆ minimize()

void ne::Window::minimize ( ) const

Minimizes the window.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.

◆ onFramebufferSizeChanged()

void ne::Window::onFramebufferSizeChanged ( int  iWidth,
int  iHeight 
) const
private

Called when the framebuffer size was changed.

Parameters
iWidthNew width of the framebuffer (in pixels).
iHeightNew height of the framebuffer (in pixels).

◆ onKeyboardInput()

void ne::Window::onKeyboardInput ( KeyboardKey  key,
KeyboardModifiers  modifiers,
bool  bIsPressedDown 
) const

Called when the window receives keyboard input.

Remarks
Made public so you can simulate input in your tests.
Parameters
keyKeyboard key.
modifiersKeyboard modifier keys.
bIsPressedDownWhether the key down event occurred or key up.

◆ onMouseInput()

void ne::Window::onMouseInput ( MouseButton  button,
KeyboardModifiers  modifiers,
bool  bIsPressedDown 
) const

Called when the window receives mouse input.

Remarks
Made public so you can simulate input in your tests.
Parameters
buttonMouse button.
modifiersKeyboard modifier keys.
bIsPressedDownWhether the button down event occurred or button up.

◆ onMouseMove()

void ne::Window::onMouseMove ( double  xPos,
double  yPos 
)
private

Called when the window receives mouse movement.

Parameters
xPosMouse X position in pixels.
yPosMouse Y position in pixels.

◆ onMouseScrollMove()

void ne::Window::onMouseScrollMove ( int  iOffset) const

Called when the window receives mouse scroll movement.

Remarks
Made public so you can simulate input in your tests.
Parameters
iOffsetMovement offset.

◆ onWindowFocusChanged()

void ne::Window::onWindowFocusChanged ( bool  bIsFocused) const
private

Called when the window focus was changed.

Parameters
bIsFocusedWhether the window has gained or lost the focus.

◆ processEvents()

template<typename MyGameInstance >
requires std::derived_from<MyGameInstance, GameInstance>
void ne::Window::processEvents

Starts the message queue, rendering and game logic. Set GameInstance derived class to react to user inputs, window events and etc.

Will return control after the window was closed.

◆ restore()

void ne::Window::restore ( ) const

Restores the window (makes it visible with normal size). Does nothing for fullscreen windows.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.

◆ setCursor()

void ne::Window::setCursor ( WindowCursor pCursor)

Changes the cursor.

Use createCursor to create a new cursor.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.
Parameters
pCursorCursor to use.

◆ setCursorVisibility()

void ne::Window::setCursorVisibility ( bool  bIsVisible) const

Whether the cursor is visible or not (locked in this window).

Parameters
bIsVisible'true' shows cursor (normal behavior), 'false' will hide the cursor and lock it to the window.

◆ setDefaultCursor()

void ne::Window::setDefaultCursor ( )

Changes the cursor icon to be system default cursor icon.

This function can be used to revert changes made by setCursor function.

◆ setIcon()

std::optional< Error > ne::Window::setIcon ( const std::filesystem::path &  pathToIcon) const

Sets new window icon.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.
Parameters
pathToIconPath to the image (.png). The image data should be 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits per channel with the red channel first.
Returns
Error if file not found.

◆ setOpacity()

void ne::Window::setOpacity ( float  opacity) const

Sets the window opacity (1.0f for opaque, 0.0f for transparent). Does nothing if the OS does not support transparent windows.

Parameters
opacityOpacity value between 0.0 and 1.0.

◆ setPreferredRenderer()

void ne::Window::setPreferredRenderer ( const std::optional< RendererType > &  preferredRenderer)

Saves the type of the preferred renderer to use for the next game created using this window (when processEvents is called).

Remarks
Preferred renderer can also be specified using the renderer config file on the disk (for example this is how users of your game can change it without using your game) but if this function was called and some renderer type was specified (not empty) the setting from the renderer config file will be ignored and the specified here renderer type will be considered instead.
By default (no preferred renderer) on Windows DirectX renderer will be preferred (and Vulkan renderer will be used if DirectX renderer is not supported by the hardware), on other platforms Vulkan renderer will be picked.
Parameters
preferredRendererRenderer that should be preferred to be used although there is no guarantee that it will actually be used because of OS/hardware support. The specified renderer will be the first one to test OS/hardware support. Specify empty value to clear your previously specified preference.

◆ setTitle()

void ne::Window::setTitle ( const std::string &  sNewTitle)

Sets new window title.

Parameters
sNewTitleNew window title.

◆ show()

void ne::Window::show ( ) const

Shows the hidden window on screen.

Warning
This function must only be called from the main thread. If this function is called outside of the main thread an error will be shown.

◆ showErrorIfNotOnMainThread()

void ne::Window::showErrorIfNotOnMainThread ( ) const
private

Checks whether the current thread is the main thread or not and if not shows an error.

◆ unbindFromWindowEvents()

void ne::Window::unbindFromWindowEvents ( )
private

Unbinds from window events created in bindToWindowEvents.

Remarks
Expects game instance to be valid until this function is not finished.

Member Data Documentation

◆ lastMouseXPos

double ne::Window::lastMouseXPos = 0.0
private

Last mouse X position, used for calculating delta movement.

◆ lastMouseYPos

double ne::Window::lastMouseYPos = 0.0
private

Last mouse Y position, used for calculating delta movement.

◆ mainThreadId

std::thread::id ne::Window::mainThreadId
private

ID of the main thread.

◆ pGameManager

std::unique_ptr<GameManager> ne::Window::pGameManager
private

Holds main game objects.

◆ pGlfwWindow

GLFWwindow* ne::Window::pGlfwWindow = nullptr
private

GLFW window.

◆ preferredRenderer

std::optional<RendererType> ne::Window::preferredRenderer
private

Renderer that the developer wants to use.

◆ sWindowTitle

std::string ne::Window::sWindowTitle
private

Title of the window.

◆ vCreatedCursors

std::vector<std::unique_ptr<WindowCursor> > ne::Window::vCreatedCursors
private

Array of created cursors using createCursor. Should be used with the mutex.


The documentation for this class was generated from the following files: