lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
lmgl::core::Engine Class Reference

A singleton class that manages the main application loop using GLFW. More...

#include <engine.hpp>

Public Member Functions

bool init (int w, int h, std::string title, bool resizable=true, bool vsync=true)
 Initialize the engine with the specified window dimensions and title.
 
void run (std::function< void(float)> update_callback)
 Run the main application loop.
 
void free ()
 Free resources and clean up the engine.
 
bool is_running () const
 Check if the engine is still running.
 
void shutdown ()
 Shuts the engine down and closes the window.
 
void clear (float r, float g, float b, float a=1.0f)
 Clear the screen.
 
void set_vsync (VSyncMode mode)
 Set VSync mode.
 
int get_width () const
 Retrieves the window's width.
 
int get_height () const
 Retrieves the window's height.
 
float get_aspect_ratio () const
 Returns the aspect ratio of the window (width/height).
 
void set_size (int width, int height)
 Sets the window to a new size.
 
void set_title (const std::string &title)
 Sets the window title to a new string.
 
void set_fullscreen (bool fullscreen)
 Toggles fullscreen mode.
 
bool is_fullscreen () const
 Checks if the window is in fullscreen mode.
 
void maximize ()
 Maximizes the window to occupy the entirety of the screen.
 
void minimize ()
 Minimizes the window.
 
void restore ()
 Restores the window from minimized/maximized state.
 
bool is_key_pressed (Key key) const
 Checks if a key is currently pressed.
 
bool is_key_just_pressed (Key key) const
 Checks if a key was just pressed this frame.
 
bool is_key_just_released (Key key) const
 Checks if a key was just released this frame.
 
bool is_mouse_button_pressed (MouseButton button) const
 Checks if a mouse button is pressed.
 
double get_mouse_x () const
 Retrieves the x position of the mouse in the window.
 
double get_mouse_y () const
 Retrieves the y position of the mouse in the window.
 
void get_mouse_delta (double &dx, double &dy) const
 Retrieves the mouse position change (delta) between the last frame and the current frame.
 
void set_mouse_position (double x, double y)
 Sets the mouse position to a (x,y) coordinate.
 
void set_cursor_mode (CursorMode mode)
 Sets a new cursor mode to the engine.
 
void get_scroll_offset (double &xoffs, double &yoffs) const
 Retrieves the offset of scroll on x and y axes since the last frame.
 
GLFWwindow * get_window () const
 Get the GLFW window associated with the engine.
 
void set_resize_callback (std::function< void(int, int)> callback)
 Sets the callback responsible for window resizing.
 
float get_delta_time () const
 Get the delta time between the current and last frame.
 
float get_time () const
 Retrieves the total time elapsed since the engine's start.
 
float get_fps () const
 Retrieves the current FPS count.
 

Static Public Member Functions

static Engineget_instance ()
 Get the singleton instance of the Engine.
 

Detailed Description

A singleton class that manages the main application loop using GLFW.

This class provides methods to initialize the engine, run the main loop, and clean up resources. It also tracks the delta time between frames. The engine is designed to be used in applications that require a rendering loop, such as games or graphical simulations.

Note
This class is implemented as a singleton to ensure that only one instance of the engine exists throughout the application lifecycle.

Member Function Documentation

◆ clear()

void lmgl::core::Engine::clear ( float r,
float g,
float b,
float a = 1.0f )

Clear the screen.

This method can be used to clear the screen before rendering a new frame.

Parameters
rRed component of the clear color (0.0 to 1.0).
gGreen component of the clear color (0.0 to 1.0).
bBlue component of the clear color (0.0 to 1.0).
aAlpha component of the clear color (0.0 to 1.0), default is 1.0.

◆ free()

void lmgl::core::Engine::free ( )

Free resources and clean up the engine.

Destroys the GLFW window and terminates GLFW.

◆ get_aspect_ratio()

float lmgl::core::Engine::get_aspect_ratio ( ) const

Returns the aspect ratio of the window (width/height).

Returns
the aspect ratio of the window.

◆ get_delta_time()

float lmgl::core::Engine::get_delta_time ( ) const
inline

Get the delta time between the current and last frame.

This value is useful for frame-independent movement and animations.

@returnDelta time in seconds.

◆ get_fps()

float lmgl::core::Engine::get_fps ( ) const
inline

Retrieves the current FPS count.

Returns
The current FPS count.

◆ get_height()

int lmgl::core::Engine::get_height ( ) const
inline

Retrieves the window's height.

Returns
the window height.

◆ get_instance()

Engine & lmgl::core::Engine::get_instance ( )
static

Get the singleton instance of the Engine.

Maintains a static reference to the single Engine instance. If the instance does not exist, it is created.

Returns
Reference to the singleton Engine instance.

◆ get_mouse_delta()

void lmgl::core::Engine::get_mouse_delta ( double & dx,
double & dy ) const

Retrieves the mouse position change (delta) between the last frame and the current frame.

Parameters
dxreturn parameter for the mouse position delta on the x axis
dyreturn parameter for the mouse position delta on the y axis

◆ get_mouse_x()

double lmgl::core::Engine::get_mouse_x ( ) const
inline

Retrieves the x position of the mouse in the window.

Returns
The x position of the mouse in the window.

◆ get_mouse_y()

double lmgl::core::Engine::get_mouse_y ( ) const
inline

Retrieves the y position of the mouse in the window.

Returns
The y position of the mouse in the window.

◆ get_scroll_offset()

void lmgl::core::Engine::get_scroll_offset ( double & xoffs,
double & yoffs ) const

Retrieves the offset of scroll on x and y axes since the last frame.

Parameters
xoffsthe offset of scroll on the x axis since the last frame
yoffsthe offset of scroll on the y axis since the last frame

◆ get_time()

float lmgl::core::Engine::get_time ( ) const
inline

Retrieves the total time elapsed since the engine's start.

Returns
The total time elapsed since the engine's start.

◆ get_width()

int lmgl::core::Engine::get_width ( ) const
inline

Retrieves the window's width.

Returns
the window width.

◆ get_window()

GLFWwindow * lmgl::core::Engine::get_window ( ) const
inline

Get the GLFW window associated with the engine.

The returned pointer can be used for further GLFW operations.

Returns
Pointer to the GLFWwindow.

◆ init()

bool lmgl::core::Engine::init ( int w,
int h,
std::string title,
bool resizable = true,
bool vsync = true )

Initialize the engine with the specified window dimensions and title.

Initializes GLFW, creates a window, and sets up the necessary context for rendering. Returns true if initialization is successful, false otherwise.

Parameters
wWidth of the window.
hHeight of the window.
titleTitle of the window.
Returns
True if initialization was successful, false otherwise.

◆ is_fullscreen()

bool lmgl::core::Engine::is_fullscreen ( ) const
inline

Checks if the window is in fullscreen mode.

Returns
the fullsscreen state (true/false).

◆ is_key_just_pressed()

bool lmgl::core::Engine::is_key_just_pressed ( Key key) const

Checks if a key was just pressed this frame.

Parameters
keythe key to check
Returns
whether the key was just pressed this frame or not.

◆ is_key_just_released()

bool lmgl::core::Engine::is_key_just_released ( Key key) const

Checks if a key was just released this frame.

Parameters
keythe key to check
Returns
whether the key was just released this frame or not.

◆ is_key_pressed()

bool lmgl::core::Engine::is_key_pressed ( Key key) const

Checks if a key is currently pressed.

Parameters
keythe key to check
Returns
whether the key is currently pressed or not.

◆ is_mouse_button_pressed()

bool lmgl::core::Engine::is_mouse_button_pressed ( MouseButton button) const

Checks if a mouse button is pressed.

Parameters
buttonthe mouse button to check
Returns
whether the mouse button is pressed or not.

◆ is_running()

bool lmgl::core::Engine::is_running ( ) const

Check if the engine is still running.

The engine is considered running as long as the window has not been closed.

Returns
True if the window is open, false otherwise.

◆ run()

void lmgl::core::Engine::run ( std::function< void(float)> update_callback)

Run the main application loop.

The loop continues until the window is closed. The provided update callback is called each frame with the delta time as an argument.

Parameters
update_callbackA function to be called each frame with delta time.

◆ set_cursor_mode()

void lmgl::core::Engine::set_cursor_mode ( CursorMode mode)

Sets a new cursor mode to the engine.

Parameters
modethe new cursor mode to set

◆ set_fullscreen()

void lmgl::core::Engine::set_fullscreen ( bool fullscreen)

Toggles fullscreen mode.

Fullscreen mode forces the window to render as fullscreen, overriding system controls.

Parameters
fullscreenthe new fullscreen state (true/false)

◆ set_mouse_position()

void lmgl::core::Engine::set_mouse_position ( double x,
double y )

Sets the mouse position to a (x,y) coordinate.

Parameters
xthe new x coordinate component of the mouse position
ythe new y coordinate component of the mouse position

◆ set_size()

void lmgl::core::Engine::set_size ( int width,
int height )

Sets the window to a new size.

Parameters
widththe new window width
heightthe new window height

◆ set_title()

void lmgl::core::Engine::set_title ( const std::string & title)

Sets the window title to a new string.

Parameters
titlethe new title to set.

◆ set_vsync()

void lmgl::core::Engine::set_vsync ( VSyncMode mode)

Set VSync mode.

Parameters
modeThe VSync mode to set

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