cangine
Loading...
Searching...
No Matches
window.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <algorithm>
5#include <unordered_set>
6
7#include "../util/position.hpp"
8#include "../types/types.hpp"
9#include "../util/color.hpp"
10
11#include "../engine/exception.hpp"
12
13#include "key.hpp"
14#include "mouse.hpp"
15
16struct GLFWwindow;
17
18namespace cangine {
25 void resizeCallback(GLFWwindow* window, int w, int h);
26
34 class Window {
35 private:
36 double lastTime;
38 int fps;
40 double lastFrameTime = 0.0;
41 float deltaTime = 0.0f;
42 GLFWwindow* main;
43 static Window* instance;
45 static void _mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
47 static void _mouseEnterCallback(GLFWwindow *window, int e);
49 static void _mouseMoveCallback(GLFWwindow *window, double _x, double _y);
50
51 public:
57 static void _characterCallback(GLFWwindow* window, unsigned int codepoint);
58 static std::unordered_set<Key> keysPressed;
59 static std::unordered_set<Key> prevKeysPressed;
68 Window(int width, int height, std::string title, bool fullscreen = false);
70 ~Window();
75 bool shouldClose();
77 static void _processInput(GLFWwindow* win, int key, int scanCode, int action, int mods);
79 static void _scrollCallback(GLFWwindow *window, double xOffset, double yOffset);
81 void swapBuffers();
83 void pollEvents();
85 void loadGLAD();
91 void onResize(int w, int h);
96 void setTitle(std::string title);
101 void fill(const Color &c);
103 void beginFrame();
105 void endFrame();
110 int getFPS();
115 int getInstantFPS();
120 double getDeltaTime();
125 static Window* getInstance();
126 };
127}
Owns the application window, OpenGL context and the frame loop.
Definition window.hpp:34
bool shouldClose()
Reports whether the window has been asked to close.
Definition window.cpp:135
static void _characterCallback(GLFWwindow *window, unsigned int codepoint)
Raw GLFW character input callback, forwarded to Input.
Definition window.cpp:50
float deltaTime
Seconds elapsed since the previous frame.
Definition window.hpp:41
void swapBuffers()
Swaps the front and back framebuffers.
Definition window.cpp:147
static std::unordered_set< Key > prevKeysPressed
Keys held the previous frame (for edge detection).
Definition window.hpp:59
void onResize(int w, int h)
Updates cached state in response to a resize.
Definition window.cpp:152
void setTitle(std::string title)
Sets the window title.
Definition window.cpp:159
void endFrame()
Ends a frame: swaps buffers and polls events.
Definition window.cpp:196
int instantFps
FPS estimated from the latest frame's delta time.
Definition window.hpp:39
int frameCount
Frames counted in the current second.
Definition window.hpp:37
static void _mouseEnterCallback(GLFWwindow *window, int e)
Raw GLFW cursor enter/leave callback, forwarded to Mouse.
Definition window.cpp:42
int getInstantFPS()
Returns the instantaneous FPS from the latest frame.
Definition window.cpp:209
void beginFrame()
Begins a frame: updates timing and prepares for rendering.
Definition window.cpp:167
static void _processInput(GLFWwindow *win, int key, int scanCode, int action, int mods)
Raw GLFW keyboard callback, forwarded to Input.
Definition window.cpp:26
void fill(const Color &c)
Clears the framebuffer to a solid color.
Definition window.cpp:163
static void _mouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
Raw GLFW mouse button callback, forwarded to Mouse.
Definition window.cpp:38
double lastTime
Timestamp used for the per-second FPS counter.
Definition window.hpp:36
static Window * getInstance()
Returns the singleton instance.
Definition window.cpp:218
static std::unordered_set< Key > keysPressed
Keys held this frame.
Definition window.hpp:58
~Window()
Destroys the window and releases GLFW resources.
Definition window.cpp:129
static Window * instance
Definition window.hpp:43
double getDeltaTime()
Returns the time elapsed since the previous frame.
Definition window.cpp:214
double lastFrameTime
Timestamp of the previous frame.
Definition window.hpp:40
GLFWwindow * main
Underlying GLFW window handle.
Definition window.hpp:42
static void _scrollCallback(GLFWwindow *window, double xOffset, double yOffset)
Raw GLFW scroll callback, forwarded to Mouse.
Definition window.cpp:72
void pollEvents()
Processes pending window and input events.
Definition window.cpp:139
int fps
Frames rendered in the last full second.
Definition window.hpp:38
Dimensions dimensions
Current window size in pixels.
Definition window.hpp:60
static void _mouseMoveCallback(GLFWwindow *window, double _x, double _y)
Raw GLFW cursor move callback, forwarded to Mouse.
Definition window.cpp:46
int getFPS()
Returns the frames-per-second over the last full second.
Definition window.cpp:205
void loadGLAD()
Loads OpenGL function pointers via GLAD.
Definition window.cpp:143
Definition audiomanager.cpp:4
void resizeCallback(GLFWwindow *window, int w, int h)
GLFW framebuffer resize callback.
Definition window.cpp:16
RGBA color stored as normalized floating-point components.
Definition color.hpp:12
Holds an integer width/height pair.
Definition dimensions.hpp:9