CGVIS Utils
Loading...
Searching...
No Matches
GLEnv.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <chrono>
6
7#ifdef __EMSCRIPTEN__
8#include <emscripten/emscripten.h>
9#include <emscripten/html5.h>
10#include <GLES3/gl3.h>
11#else
12#include <GL/glew.h>
13#include <GLFW/glfw3.h>
14#endif
15
16#include "GLDebug.h"
17
39
43enum class GLDataType {BYTE, HALF, FLOAT};
44
49
54
63class GLEnv {
64public:
78 GLEnv(uint32_t w, uint32_t h, uint32_t s, const std::string& title, bool fpsCounter=false, bool sync=true, int major=2, int minor=1, bool core=false);
80 ~GLEnv();
81
82#ifdef __EMSCRIPTEN__
84 void setKeyCallback(em_key_callback_func f, void *userData);
86 void setMouseCallbacks(em_mouse_callback_func p,
87 em_mouse_callback_func b,
88 em_mouse_callback_func bu,
89 em_mouse_callback_func bd,
90 em_wheel_callback_func s,
91 void *userData);
93 void setResizeCallback(em_ui_callback_func f, void *userData);
94#else
96 void setKeyCallback(GLFWkeyfun f);
98 void setKeyCallbacks(GLFWkeyfun f, GLFWcharfun c);
100 void setMouseCallbacks(GLFWcursorposfun p, GLFWmousebuttonfun b, GLFWscrollfun s);
102 void setResizeCallback(GLFWframebuffersizefun f);
103#endif
104
118 bool shouldClose() const;
120 void setClose();
124 void endOfFrame();
125
130 void setCursorMode(CursorMode mode);
131
133 void setFPSCounter(bool fpsCounter);
135 void setSync(bool sync);
137 bool getSync() const {return sync;}
138
143 static void checkGLError(const std::string& id);
144
146 void setTitle(const std::string& title);
147
148private:
149#ifndef __EMSCRIPTEN__
150 GLFWwindow* window;
151#endif
152 bool sync;
153 std::string title;
154 bool fpsCounter;
155 std::chrono::high_resolution_clock::time_point last;
156 uint64_t frameCount;
157
159 static void errorCallback(int error, const char* description);
160};
Minimal OpenGL error utilities, exception type, and a debug GL() wrapper macro.
GLDataType
Element type selector for texture uploads/allocations.
Definition GLEnv.h:43
@ HALF
Definition GLEnv.h:43
@ BYTE
Definition GLEnv.h:43
@ FLOAT
Definition GLEnv.h:43
CursorMode
Mouse cursor behavior modes.
Definition GLEnv.h:53
@ NORMAL
Definition GLEnv.h:53
@ HIDDEN
Definition GLEnv.h:53
@ FIXED
Definition GLEnv.h:53
GLDepthDataType
Depth storage selection for depth textures.
Definition GLEnv.h:48
@ DEPTH24
Definition GLEnv.h:48
@ DEPTH16
Definition GLEnv.h:48
@ DEPTH32
Definition GLEnv.h:48
void setKeyCallbacks(GLFWkeyfun f, GLFWcharfun c)
Set key + char callbacks for GLFW builds.
Definition GLEnv.cpp:194
GLEnv(uint32_t w, uint32_t h, uint32_t s, const std::string &title, bool fpsCounter=false, bool sync=true, int major=2, int minor=1, bool core=false)
Construct a GL environment and create a window/canvas + context.
Definition GLEnv.cpp:44
static void checkGLError(const std::string &id)
Convenience GL error check, logs to stderr with an identifier.
Definition GLEnv.cpp:29
void setFPSCounter(bool fpsCounter)
Enable/disable FPS title updates.
Definition GLEnv.cpp:134
void endOfFrame()
End‑of‑frame housekeeping: swap/poll + optional FPS title update.
Definition GLEnv.cpp:138
void setResizeCallback(GLFWframebuffersizefun f)
Set framebuffer resize callback for GLFW builds.
Definition GLEnv.cpp:199
void setCursorMode(CursorMode mode)
Set mouse cursor behavior.
Definition GLEnv.cpp:249
Dimensions getFramebufferSize() const
Get framebuffer size in texels/pixels.
Definition GLEnv.cpp:210
Dimensions getWindowSize() const
Get logical window size.
Definition GLEnv.cpp:221
bool getSync() const
Current vsync state.
Definition GLEnv.h:137
~GLEnv()
Destroy window/context (desktop) or release resources (web).
Definition GLEnv.cpp:109
void setMouseCallbacks(GLFWcursorposfun p, GLFWmousebuttonfun b, GLFWscrollfun s)
Set mouse position/button/scroll callbacks for GLFW builds.
Definition GLEnv.cpp:203
void setKeyCallback(GLFWkeyfun f)
Set key callback for GLFW builds.
Definition GLEnv.cpp:190
void setClose()
Request closing the window (desktop only; no‑op on web).
Definition GLEnv.cpp:239
bool shouldClose() const
Query whether the window should close (desktop always; web always false).
Definition GLEnv.cpp:231
void setTitle(const std::string &title)
Set the window/page title (stored; displayed on next FPS refresh).
Definition GLEnv.cpp:245
void setSync(bool sync)
Enable/disable vsync.
Definition GLEnv.cpp:116
Simple pixel/viewport dimensions with an aspect ratio helper.
Definition GLDebug.h:45