lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
engine.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <glad/glad.h>
17#include <GLFW/glfw3.h>
18#include <functional>
19#include <string>
20
28namespace lmgl {
29
38namespace core {
39
43enum class Key {
44 // Numbers
45 Key0 = 48,
46 Key1 = 49,
47 Key2 = 50,
48 Key3 = 51,
49 Key4 = 52,
50 Key5 = 53,
51 Key6 = 54,
52 Key7 = 55,
53 Key8 = 56,
54 Key9 = 57,
55 // Letters
56 A = 65,
57 B = 66,
58 C = 67,
59 D = 68,
60 E = 69,
61 F = 70,
62 G = 71,
63 H = 72,
64 I = 73,
65 J = 74,
66 K = 75,
67 L = 76,
68 M = 77,
69 N = 78,
70 O = 79,
71 P = 80,
72 Q = 81,
73 R = 82,
74 S = 83,
75 T = 84,
76 U = 85,
77 V = 86,
78 W = 87,
79 X = 88,
80 Y = 89,
81 Z = 90,
82 // Specials
83 Space = 32,
84 Esc = 256,
85 Enter = 257,
86 Tab = 258,
87 Backspace = 259,
88 Right = 262,
89 Left = 263,
90 Down = 264,
91 Up = 265,
92 // Fn Keys
93 F1 = 290,
94 F2 = 291,
95 F3 = 292,
96 F4 = 293,
97 F5 = 294,
98 F6 = 295,
99 F7 = 296,
100 F8 = 297,
101 F9 = 298,
102 F10 = 299,
103 F11 = 300,
104 F12 = 301,
105 // Modifiers
106 LShift = 340,
107 LCtrl = 341,
108 LAlt = 342,
109 RShift = 344,
110 RCtrl = 345,
111 RAlt = 346
112};
113
117enum class MouseButton { Left = 0, Right = 1, Middle = 2 };
118
122enum class InputState { Released = 0, Pressed = 1, Repeat = 2 };
123
132
136enum class VSyncMode {
137 Off = 0,
138 On = 1,
140};
141
153class Engine {
154 public:
163 static Engine &get_instance();
164
176 bool init(int w, int h, std::string title, bool resizable = true, bool vsync = true);
177
186 void run(std::function<void(float)> update_callback);
187
193 void free();
194
202 bool is_running() const;
203
207 void shutdown();
208
219 void clear(float r, float g, float b, float a = 1.0f);
220
226 void set_vsync(VSyncMode mode);
227
233 inline int get_width() const { return m_width; };
234
240 inline int get_height() const { return m_height; };
241
247 float get_aspect_ratio() const;
248
255 void set_size(int width, int height);
256
262 void set_title(const std::string &title);
263
272 void set_fullscreen(bool fullscreen);
273
279 inline bool is_fullscreen() const { return m_fullscreen; };
280
284 void maximize();
285
289 void minimize();
290
294 void restore();
295
302 bool is_key_pressed(Key key) const;
303
310 bool is_key_just_pressed(Key key) const;
311
318 bool is_key_just_released(Key key) const;
319
326 bool is_mouse_button_pressed(MouseButton button) const;
327
333 inline double get_mouse_x() const { return m_mouse_x; };
334
340 inline double get_mouse_y() const { return m_mouse_y; };
341
348 void get_mouse_delta(double &dx, double &dy) const;
349
356 void set_mouse_position(double x, double y);
357
363 void set_cursor_mode(CursorMode mode);
364
371 void get_scroll_offset(double &xoffs, double &yoffs) const;
372
380 inline GLFWwindow *get_window() const { return m_window; };
381
385 inline void set_resize_callback(std::function<void(int, int)> callback) { m_resize_callback = callback; };
386
394 inline float get_delta_time() const { return m_delta_time; };
395
401 inline float get_time() const { return m_time; };
402
408 inline float get_fps() const { return m_fps; };
409
410 private:
412 Engine() = default;
413
415 ~Engine() = default;
416
418 Engine(const Engine &) = delete;
419
421 Engine &operator=(const Engine &) = delete;
422
432 static void fb_size_callback(GLFWwindow *window, int w, int h);
433
446 static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
447
459 static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
460
470 static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos);
471
481 static void scroll_callback(GLFWwindow *window, double xoffs, double yoffs);
482
486 void update_input_state();
487
489 GLFWwindow *m_window = nullptr;
490
492 int m_width = 0;
493
495 int m_height = 0;
496
498 bool m_fullscreen = false;
499
501 float m_delta_time = 0.0f;
502
504 float m_last_frame_time = 0.0f;
505
507 float m_time = 0.0f;
508
510 float m_fps = 0.0f;
511
513 int m_frame_count = 0;
514
516 float m_fps_timer = 0.0f;
517
519 bool m_key_states[512] = {false};
520
522 bool m_key_just_pressed[512] = {false};
523
525 bool m_key_just_released[512] = {false};
526
528 bool m_mouse_button_states[8] = {false};
529
531 double m_mouse_x = 0.0;
532
534 double m_mouse_y = 0.0;
535
537 double m_last_mouse_x = 0.0;
538
540 double m_last_mouse_y = 0.0;
541
543 double m_scroll_x = 0.0;
544
546 double m_scroll_y = 0.0;
547
549 std::function<void(int, int)> m_resize_callback;
550};
551
552} // namespace core
553
554} // namespace lmgl
A singleton class that manages the main application loop using GLFW.
Definition engine.hpp:153
int get_width() const
Retrieves the window's width.
Definition engine.hpp:233
void set_title(const std::string &title)
Sets the window title to a new string.
Definition engine.cpp:109
bool is_running() const
Check if the engine is still running.
Definition engine.cpp:90
void get_scroll_offset(double &xoffs, double &yoffs) const
Retrieves the offset of scroll on x and y axes since the last frame.
Definition engine.cpp:196
double get_mouse_x() const
Retrieves the x position of the mouse in the window.
Definition engine.hpp:333
GLFWwindow * get_window() const
Get the GLFW window associated with the engine.
Definition engine.hpp:380
float get_time() const
Retrieves the total time elapsed since the engine's start.
Definition engine.hpp:401
static Engine & get_instance()
Get the singleton instance of the Engine.
Definition engine.cpp:10
bool is_key_pressed(Key key) const
Checks if a key is currently pressed.
Definition engine.cpp:149
void get_mouse_delta(double &dx, double &dy) const
Retrieves the mouse position change (delta) between the last frame and the current frame.
Definition engine.cpp:169
bool is_key_just_released(Key key) const
Checks if a key was just released this frame.
Definition engine.cpp:159
void maximize()
Maximizes the window to occupy the entirety of the screen.
Definition engine.cpp:129
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.
Definition engine.cpp:15
void set_size(int width, int height)
Sets the window to a new size.
Definition engine.cpp:101
void minimize()
Minimizes the window.
Definition engine.cpp:135
void set_vsync(VSyncMode mode)
Set VSync mode.
Definition engine.cpp:147
void set_mouse_position(double x, double y)
Sets the mouse position to a (x,y) coordinate.
Definition engine.cpp:174
void free()
Free resources and clean up the engine.
Definition engine.cpp:77
float get_fps() const
Retrieves the current FPS count.
Definition engine.hpp:408
void set_cursor_mode(CursorMode mode)
Sets a new cursor mode to the engine.
Definition engine.cpp:180
void set_fullscreen(bool fullscreen)
Toggles fullscreen mode.
Definition engine.cpp:115
bool is_mouse_button_pressed(MouseButton button) const
Checks if a mouse button is pressed.
Definition engine.cpp:164
void shutdown()
Shuts the engine down and closes the window.
Definition engine.cpp:92
void run(std::function< void(float)> update_callback)
Run the main application loop.
Definition engine.cpp:53
int get_height() const
Retrieves the window's height.
Definition engine.hpp:240
void clear(float r, float g, float b, float a=1.0f)
Clear the screen.
Definition engine.cpp:85
void restore()
Restores the window from minimized/maximized state.
Definition engine.cpp:141
void set_resize_callback(std::function< void(int, int)> callback)
Sets the callback responsible for window resizing.
Definition engine.hpp:385
double get_mouse_y() const
Retrieves the y position of the mouse in the window.
Definition engine.hpp:340
float get_aspect_ratio() const
Returns the aspect ratio of the window (width/height).
Definition engine.cpp:98
bool is_key_just_pressed(Key key) const
Checks if a key was just pressed this frame.
Definition engine.cpp:154
float get_delta_time() const
Get the delta time between the current and last frame.
Definition engine.hpp:394
bool is_fullscreen() const
Checks if the window is in fullscreen mode.
Definition engine.hpp:279
InputState
Key/button states.
Definition engine.hpp:122
Key
Input key codes.
Definition engine.hpp:43
VSyncMode
VSync mode.
Definition engine.hpp:136
@ Adaptive
Adaptive VSync - Only when FPS exceeds refresh rate.
Definition engine.hpp:139
@ On
Standard VSync - Locks to monitor refresh rate.
Definition engine.hpp:138
@ Off
Disabled VSync - unlimited FPS.
Definition engine.hpp:137
MouseButton
Mouse button codes.
Definition engine.hpp:117
CursorMode
Cursor mode.
Definition engine.hpp:127
@ Hidden
Cursor is invisible but can still move freely.
Definition engine.hpp:129
@ Normal
Cursor is visible and behaves normally.
Definition engine.hpp:128
@ Disabled
Cursor is locked to window center (FPS mode)
Definition engine.hpp:130
Core components of the LMGL project.
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12