libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
gl.hpp
Go to the documentation of this file.
1/**
2 * @file gl.hpp
3 * @brief OpenGL window, shader program, sprite, and text rendering classes.
4 *
5 * Provides the core OpenGL abstractions used by libmx applications:
6 * - ShaderProgram – GLSL vertex/fragment shader compilation and uniform upload.
7 * - GLText – SDL_ttf text rendered into an OpenGL texture.
8 * - GLSprite – A textured quad rendered with an arbitrary shader.
9 * - GLWindow – SDL2 + OpenGL/ES window and event loop.
10 * - GLObject – Abstract interface for scene objects attached to GLWindow.
11 *
12 * Requires the WITH_GL compile-time flag.
13 */
14#ifndef GL_H__
15#define GL_H__
16
17#ifdef __EMSCRIPTEN__
18#include"config.hpp"
19#else
20#include"config.h"
21#endif
22
23#ifdef WITH_GL
24#include "mx.hpp"
25#include<string>
26#include<memory>
27
28#ifdef __EMSCRIPTEN__
29#include <emscripten/emscripten.h>
30#include <GLES3/gl3.h>
31#include<emscripten/html5.h>
32#include "glm.hpp"
33#include "gtc/type_ptr.hpp"
34#else
35#include <glad/glad.h>
36#include <glm/glm.hpp>
37#include <glm/gtc/matrix_transform.hpp>
38#include <glm/gtc/type_ptr.hpp>
39#endif
40
41#include"console.hpp"
42
43namespace gl {
44
45 extern const char *vSource; ///< Default vertex shader source.
46 extern const char *fSource; ///< Default fragment shader source.
47
48/**
49 * @class ShaderProgram
50 * @brief GLSL shader program wrapper (shared-ownership).
51 *
52 * Compiles a vertex and fragment shader, links them into a GL program, and
53 * exposes uniform-upload helpers. Uses shared_ptr internally so multiple
54 * copies refer to the same underlying GL object; the program is deleted when
55 * the last copy is destroyed.
56 */
58 private:
59 struct SharedState {
63 std::string name_;
64 bool silent_ = false;
65
66 ~SharedState();
67 };
68 std::shared_ptr<SharedState> state;
69 public:
70 /** @brief Default constructor — allocates shared state, no program compiled yet. */
72
73 /**
74 * @brief Wrap an existing GL program ID.
75 * @param id Pre-compiled GL program identifier.
76 */
78 ~ShaderProgram() = default;
79 ShaderProgram(const ShaderProgram&) = default;
81 ShaderProgram(ShaderProgram&&) noexcept = default;
82 ShaderProgram& operator=(ShaderProgram&&) noexcept = default;
83
84 /** @brief Release the underlying GL program (decrements shared count). */
85 void release();
86
87 /** @return The GL program ID, or 0 if not loaded. */
88 GLuint id() const;
89
90 /** @return @c true if the program has been successfully linked. */
91 bool loaded() const;
92
93 /** @brief Bind this shader program for subsequent draw calls. */
94 void useProgram();
95
96 /**
97 * @brief Assign a human-readable name used in error messages.
98 * @param n Name string.
99 */
100 void setName(const std::string &n);
101
102 /**
103 * @brief Suppress compilation/link error output.
104 * @param s If @c true, errors are not printed.
105 */
106 void setSilent(bool s);
107
108 /**
109 * @brief Compile and link shaders from file paths.
110 * @param v Path to the GLSL vertex shader file.
111 * @param f Path to the GLSL fragment shader file.
112 * @return @c true on success.
113 */
114 bool loadProgram(const std::string &v, const std::string &f);
115
116 /**
117 * @brief Compile and link shaders from source-code strings.
118 * @param v Vertex shader GLSL source.
119 * @param f Fragment shader GLSL source.
120 * @return @c true on success.
121 */
122 bool loadProgramFromText(const std::string &v, const std::string &f);
123
124 /**
125 * @brief Upload an integer uniform variable.
126 * @param name Uniform name in the shader.
127 * @param value Value to upload.
128 */
129 void setUniform(const std::string &name, int value);
130
131 /**
132 * @brief Upload a float uniform variable.
133 * @param name Uniform name.
134 * @param value Float value.
135 */
136 void setUniform(const std::string &name, float value);
137
138 /**
139 * @brief Upload a vec2 uniform.
140 * @param name Uniform name.
141 * @param value glm::vec2 value.
142 */
143 void setUniform(const std::string &name, const glm::vec2 &value);
144
145 /**
146 * @brief Upload a vec3 uniform.
147 * @param name Uniform name.
148 * @param value glm::vec3 value.
149 */
150 void setUniform(const std::string &name, const glm::vec3 &value);
151
152 /**
153 * @brief Upload a vec4 uniform.
154 * @param name Uniform name.
155 * @param value glm::vec4 value.
156 */
157 void setUniform(const std::string &name, const glm::vec4 &value);
158
159 /**
160 * @brief Upload a mat4 uniform.
161 * @param name Uniform name.
162 * @param value glm::mat4 value.
163 */
164 void setUniform(const std::string &name, const glm::mat4 &value);
165 private:
166 GLuint createProgram(const char *vshaderSource, const char *fshaderSource);
167 GLuint createProgramFromFile(const std::string &vert, const std::string &frag);
168 int printShaderLog(GLuint shader);
169 void printProgramLog(int p);
170 bool checkError();
171 };
172
173/**
174 * @class GLText
175 * @brief Render SDL_ttf text into an OpenGL texture and display it on screen.
176 *
177 * Manages a simple text shader and provides helpers that rasterise a string
178 * to an SDL_Surface, upload it as an OpenGL texture, and draw a screen-space
179 * quad at the specified position.
180 */
181 class GLText {
182 public:
183 /** @brief Default constructor. */
184 GLText();
185
186 /**
187 * @brief Initialise internal state with the viewport dimensions.
188 * @param w Viewport width in pixels.
189 * @param h Viewport height in pixels.
190 */
191 void init(int w, int h);
192
193 /**
194 * @brief Rasterise a string to an OpenGL texture.
195 * @param text String to render.
196 * @param font SDL_ttf font handle.
197 * @param color Text colour.
198 * @param textWidth Output: texture pixel width.
199 * @param textHeight Output: texture pixel height.
200 * @param solid If @c true, use solid (fast) rendering; otherwise blended.
201 * @return GL texture ID.
202 */
203 GLuint createText(const std::string &text, TTF_Font *font, SDL_Color color, int &textWidth, int &textHeight, bool solid = true);
204
205 /**
206 * @brief Draw a pre-created text texture at a given position.
207 * @param texture GL texture ID from createText().
208 * @param x Destination X (pixels).
209 * @param y Destination Y (pixels).
210 * @param textWidth Texture pixel width.
211 * @param textHeight Texture pixel height.
212 * @param screenWidth Current viewport width.
213 * @param screenHeight Current viewport height.
214 */
215 void renderText(GLuint texture, float x, float y, int textWidth, int textHeight, int screenWidth, int screenHeight);
216
217 /**
218 * @brief Convenience: create and immediately draw solid text.
219 * @param f mx::Font reference.
220 * @param x X position.
221 * @param y Y position.
222 * @param text String to render.
223 */
224 void printText_Solid(const mx::Font &f, float x, float y, const std::string &text);
225
226 /**
227 * @brief Convenience: create and immediately draw blended (anti-aliased) text.
228 * @param f mx::Font reference.
229 * @param x X position.
230 * @param y Y position.
231 * @param text String to render.
232 */
233 void printText_Blended(const mx::Font &f, float x, float y, const std::string &text);
234
235 /**
236 * @brief Set the colour used for subsequent printText calls.
237 * @param col SDL_Color.
238 */
239 void setColor(SDL_Color col);
240 private:
241 ShaderProgram textShader; ///< Internal text quad shader.
242 SDL_Color color = {255,255,255,255}; ///< Current text colour.
243 int w = 0, h = 0; ///< Viewport dimensions.
244 };
245
246/**
247 * @class GLSprite
248 * @brief A textured screen-space quad drawn with an OpenGL shader.
249 *
250 * Manages a VAO/VBO pair that holds the quad geometry and a GL texture.
251 * Supports loading from file, drawing at arbitrary positions/sizes, and
252 * updating the texture from an SDL_Surface or raw pixel buffer at runtime.
253 * Copy and move are disabled; create on the heap or as a member.
254 */
255 class GLSprite {
256 public:
257 /** @brief Default constructor. */
258 GLSprite();
259 /** @brief Destructor — releases GL buffers and texture. */
260 ~GLSprite();
261
262 GLSprite(const GLSprite&) = delete;
263 GLSprite& operator=(const GLSprite&) = delete;
264 GLSprite(GLSprite&&) = delete;
266
267 /** @brief Free all GL resources associated with this sprite. */
268 void release();
269
270 /**
271 * @brief Set the native display size of the sprite.
272 * @param w Width in pixels.
273 * @param h Height in pixels.
274 */
275 void initSize(float w, float h);
276
277 /**
278 * @brief Assign a debug name (used in log messages).
279 * @param name Name string.
280 */
281 void setName(const std::string &name);
282
283 /**
284 * @brief Bind a shader program by extracting its GL ID.
285 * @tparam ShaderT Any type exposing id() -> GLuint.
286 * @param program Pointer to the shader (nullptr clears the binding).
287 */
288 template<typename ShaderT>
289 void setShader(ShaderT *program) {
290 active_shader_id = program ? program->id() : 0;
291 }
292
293 /**
294 * @brief Initialise the sprite from an existing GL texture.
295 * @tparam ShaderT Shader type.
296 * @param program Active shader.
297 * @param texture Existing GL texture ID.
298 * @param x Destination X.
299 * @param y Destination Y.
300 * @param textWidth Texture width.
301 * @param textHeight Texture height.
302 */
303 template<typename ShaderT>
304 void initWithTexture(ShaderT *program, GLuint texture, float x, float y, int textWidth, int textHeight) {
305 active_shader_id = program ? program->id() : 0;
306 initWithTextureImpl(texture, x, y, textWidth, textHeight);
307 }
308
309 /**
310 * @brief Load a texture from a file and set the sprite geometry.
311 * @tparam ShaderT Shader type.
312 * @param shader Active shader.
313 * @param tex Image file path.
314 * @param x Display X.
315 * @param y Display Y.
316 * @param textWidth Display width.
317 * @param textHeight Display height.
318 */
319 template<typename ShaderT>
320 void loadTexture(ShaderT *shader, const std::string &tex, float x, float y, int textWidth, int textHeight) {
321 active_shader_id = shader ? shader->id() : 0;
322 loadTextureImpl(tex, x, y, textWidth, textHeight);
323 }
324
325 /**
326 * @brief Load a texture from a file (natural dimensions).
327 * @tparam ShaderT Shader type.
328 * @param shader Active shader.
329 * @param tex Image file path.
330 * @param x Display X.
331 * @param y Display Y.
332 */
333 template<typename ShaderT>
334 void loadTexture(ShaderT *shader, const std::string &tex, float x, float y) {
335 active_shader_id = shader ? shader->id() : 0;
336 loadTextureImpl(tex, x, y);
337 }
338
339 /** @brief Draw at the sprite's stored position and size. */
340 void draw();
341
342 /**
343 * @brief Draw at the given position (stored size).
344 * @param x Destination X.
345 * @param y Destination Y.
346 */
347 void draw(int x, int y);
348
349 /**
350 * @brief Draw at an explicit position and size.
351 * @param x Destination X.
352 * @param y Destination Y.
353 * @param w Display width.
354 * @param h Display height.
355 */
356 void draw(int x, int y, int w, int h);
357
358 /**
359 * @brief Draw an arbitrary texture at a position and size.
360 * @param texture_id External GL texture ID.
361 * @param x Destination X.
362 * @param y Destination Y.
363 * @param w Display width.
364 * @param h Display height.
365 */
366 void draw(GLuint texture_id, float x, float y, int w, int h);
367
368 /**
369 * @brief Replace the sprite texture with the contents of an SDL_Surface.
370 * @param surf New surface (not consumed; caller still owns it).
371 */
372 void updateTexture(SDL_Surface *surf);
373
374 /**
375 * @brief Replace the sprite texture from a raw pixel buffer.
376 * @param buffer Pointer to RGBA pixel data.
377 * @param width Buffer width in pixels.
378 * @param height Buffer height in pixels.
379 */
380 void updateTexture(void *buffer, int width, int height);
381
382 int width = 0; ///< Texture pixel width.
383 int height = 0; ///< Texture pixel height.
384 private:
385 void initWithTextureImpl(GLuint texture, float x, float y, int textWidth, int textHeight);
386 void loadTextureImpl(const std::string &tex, float x, float y, int textWidth, int textHeight);
387 void loadTextureImpl(const std::string &tex, float x, float y);
390 GLuint VBO = 0, VAO = 0;
391 std::vector<float> vertices;
392 float screenWidth = 0.0f, screenHeight = 0.0f;
393 int texWidth = 0, texHeight = 0;
394 std::string textureName;
395 };
396
397 /** @brief OpenGL mode: desktop GL or OpenGL ES. */
398 enum class GLMode { DESKTOP, ES };
399
400 class GLObject;
401
402/**
403 * @class GLWindow
404 * @brief SDL2 + OpenGL/ES window with event loop and console support.
405 *
406 * Creates an SDL2 window with an OpenGL (or GLES) context. Derived classes
407 * implement event() and draw(). An optional GLObject can be attached to
408 * delegate rendering. A built-in GLConsole can be activated for in-app
409 * debugging.
410 */
411 class GLWindow {
412 private:
414 public:
415#ifdef __EMSCRIPTEN__
416 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webglContext = 0;
417 void restoreContext();
418#endif
419 /**
420 * @brief Construct and open an SDL2 + OpenGL window.
421 * @param text Window title.
422 * @param width Width in pixels.
423 * @param height Height in pixels.
424 * @param resize_ Allow window resizing if @c true.
425 * @param mode GL mode (DESKTOP or ES).
426 */
427 GLWindow(const std::string &text, int width, int height, bool resize_ = true, GLMode mode = GLMode::DESKTOP) : gl_mode(mode), glContext{nullptr}, window{nullptr} {
428 initGL(text, width, height, resize_);
429 }
430
431 /**
432 * @brief Construct a window from dimensions and mode (internal use).
433 * @param width Width.
434 * @param height Height.
435 * @param mode GL mode.
436 */
437 GLWindow(int width, int height, GLMode mode);
438
439 /** @brief Destructor — destroys the GL context and SDL window. */
440 virtual ~GLWindow();
441
442 GLWindow(const GLWindow&) = delete;
443 GLWindow& operator=(const GLWindow&) = delete;
444 GLWindow(GLWindow&&) = delete;
446
447 /**
448 * @brief Initialise the GL context and SDL window.
449 * @param title Window title.
450 * @param width Width.
451 * @param height Height.
452 * @param resize_ Allow resizing.
453 */
454 void initGL(const std::string &title, int width, int height, bool resize_ = true);
455
456 /**
457 * @brief Reinitialise with only dimensions (used by Emscripten).
458 * @param width New width.
459 * @param height New height.
460 */
461 void initGL(int width, int height);
462
463 /** @brief Update the GL viewport to match the current window size. */
464 void updateViewport();
465
466 /** @brief Swap front and back buffers to display the rendered frame. */
467 void swap();
468
469 /**
470 * @brief Process a single SDL event (pure virtual).
471 * @param e SDL event.
472 */
473 virtual void event(SDL_Event &e) = 0;
474
475 /** @brief Render the current frame (pure virtual). */
476 virtual void draw() = 0;
477
478 /**
479 * @brief Called when the window is resized (optional override).
480 * @param w New width.
481 * @param h New height.
482 */
483 virtual void resize(int w, int h) {}
484
485 /**
486 * @brief Attach a GLObject to receive draw/event callbacks.
487 * @param o Pointer to the object (window does not take ownership).
488 */
489 void setObject(GLObject *o);
490
491 /** @brief Request the event loop to stop. */
492 void quit();
493
494 /** @brief Run the event+draw loop until quit() is called. */
495 void proc();
496
497 /** @brief Start the blocking main loop. */
498 void loop();
499
500 /** @brief Insert a small sleep to cap frame rate. */
501 void delay();
502
503 /**
504 * @brief Set the asset search path.
505 * @param path Directory containing assets.
506 */
507 void setPath(const std::string &path) { util.path = path; }
508
509 /**
510 * @brief Change the window title bar text.
511 * @param title New title.
512 */
513 void setWindowTitle(const std::string &title);
514
515 /**
516 * @brief Resize the SDL window.
517 * @param w New width.
518 * @param h New height.
519 */
520 void setWindowSize(int w, int h);
521
522 /**
523 * @brief Set the window icon from an SDL_Surface.
524 * @param ico Icon surface.
525 */
526 void setWindowIcon(SDL_Surface *ico);
527
528 /**
529 * @brief Toggle fullscreen mode.
530 * @param full @c true for fullscreen.
531 */
532 void setFullScreen(bool full);
533
534 /**
535 * @brief Activate the built-in debug console.
536 * @param fnt Font file path.
537 * @param size Font size in points.
538 * @param color Text colour.
539 */
540 void activateConsole(const std::string &fnt, int size, const SDL_Color &color);
541
542 /**
543 * @brief Activate the console at a specific screen region.
544 * @param rc Console rectangle.
545 * @param fnt Font file path.
546 * @param size Font size.
547 * @param color Text colour.
548 */
549 void activateConsole(const SDL_Rect &rc, const std::string &fnt, int size, const SDL_Color &color);
550
551 /** @brief Draw the console overlay onto the current frame. */
552 void drawConsole();
553
554 /**
555 * @brief Show or hide the console overlay.
556 * @param show @c true to show, @c false to hide.
557 */
558 void showConsole(bool show);
559
560 std::unique_ptr<gl::GLObject> object; ///< Optional attached scene object.
561 mx::mxUtil util; ///< Asset path and utility helpers.
562 console::GLConsole console; ///< Built-in debug console.
563 int w = 0; ///< Current window width.
564 int h = 0; ///< Current window height.
565
566 /** @return Pointer to the underlying SDL_Window. */
567 SDL_Window *getWindow() { return window; }
568
569 /** @brief Restore this window's GL context as current on the calling thread. */
570 void makeCurrent();
571#ifdef WITH_MIXER
573#endif
575 bool console_visible = false;
576 bool console_active = false;
577 private:
578 SDL_GLContext glContext;
579 SDL_Window *window = nullptr;
580 bool active = false;
581 SDL_Event e;
582 bool hide_console = false;
583 };
584
585/**
586 * @class GLObject
587 * @brief Abstract interface for scene objects managed by a GLWindow.
588 *
589 * Derived classes implement load(), draw(), event(), and optionally resize()
590 * to participate in the GLWindow render/event loop. Attach an instance via
591 * GLWindow::setObject().
592 */
593 class GLObject {
594 public:
595 GLObject() = default;
596 virtual ~GLObject() = default;
597
598 /**
599 * @brief Load GPU resources (textures, shaders, geometry).
600 * @param win Parent window.
601 */
602 virtual void load(GLWindow *win) = 0;
603
604 /**
605 * @brief Render the scene for the current frame.
606 * @param win Parent window.
607 */
608 virtual void draw(GLWindow *win) = 0;
609
610 /**
611 * @brief Handle an SDL event.
612 * @param window Parent window.
613 * @param e SDL event.
614 */
615 virtual void event(GLWindow *window, SDL_Event &e) = 0;
616
617 /**
618 * @brief Called when the window is resized (optional).
619 * @param win Parent window.
620 * @param w New width.
621 * @param h New height.
622 */
623 virtual void resize(gl::GLWindow *win, int w, int h) {}
624 };
625
626 /**
627 * @brief Load a GL texture from a file.
628 * @param filename Image file path.
629 * @return GL texture ID.
630 */
631 GLuint loadTexture(const std::string &filename);
632
633 /**
634 * @brief Load a GL texture from a file and retrieve its dimensions.
635 * @param filename Image file path.
636 * @param w Output: width.
637 * @param h Output: height.
638 * @return GL texture ID.
639 */
640 GLuint loadTexture(const std::string &filename, int &w, int &h);
641
642 /**
643 * @brief Replace a GL texture's contents from an SDL_Surface.
644 * @param texture Target GL texture ID.
645 * @param surface Source surface.
646 * @param flip If @c true, flip vertically before uploading.
647 */
648 void updateTexture(GLuint texture, SDL_Surface *surface, bool flip);
649
650 /**
651 * @brief Create a new GL texture from an SDL_Surface.
652 * @param surface Source surface.
653 * @param flip If @c true, flip vertically.
654 * @return New GL texture ID.
655 */
656 GLuint createTexture(SDL_Surface *surface, bool flip);
657
658 /**
659 * @brief Create a new GL texture from a raw pixel buffer.
660 * @param buffer Pointer to RGBA data.
661 * @param width Image width.
662 * @param height Image height.
663 * @return New GL texture ID.
664 */
665 GLuint createTexture(void *buffer, int width, int height);
666
667 /**
668 * @brief Allocate a blank RGBA SDL_Surface.
669 * @param w Width in pixels.
670 * @param h Height in pixels.
671 * @return Newly allocated surface (caller owns it).
672 */
673 SDL_Surface *createSurface(int w, int h);
674}
675#endif
676#endif
OpenGL-rendered overlay console built on top of Console.
Definition console.hpp:396
Abstract interface for scene objects managed by a GLWindow.
Definition gl.hpp:593
GLObject()=default
virtual void resize(gl::GLWindow *win, int w, int h)
Called when the window is resized (optional).
Definition gl.hpp:623
virtual void event(GLWindow *window, SDL_Event &e)=0
Handle an SDL event.
virtual void draw(GLWindow *win)=0
Render the scene for the current frame.
virtual ~GLObject()=default
virtual void load(GLWindow *win)=0
Load GPU resources (textures, shaders, geometry).
void setShader(ShaderT *program)
Bind a shader program by extracting its GL ID.
Definition gl.hpp:289
int texHeight
Definition gl.hpp:393
void loadTexture(ShaderT *shader, const std::string &tex, float x, float y, int textWidth, int textHeight)
Load a texture from a file and set the sprite geometry.
Definition gl.hpp:320
GLuint VAO
Definition gl.hpp:390
void loadTexture(ShaderT *shader, const std::string &tex, float x, float y)
Load a texture from a file (natural dimensions).
Definition gl.hpp:334
int height
Texture pixel height.
Definition gl.hpp:383
void initWithTexture(ShaderT *program, GLuint texture, float x, float y, int textWidth, int textHeight)
Initialise the sprite from an existing GL texture.
Definition gl.hpp:304
GLSprite & operator=(GLSprite &&)=delete
std::vector< float > vertices
Definition gl.hpp:391
GLuint active_shader_id
Definition gl.hpp:388
GLSprite()
Default constructor.
Definition gl.cpp:998
GLSprite(GLSprite &&)=delete
GLuint texture
Definition gl.hpp:389
void setName(const std::string &name)
Assign a debug name (used in log messages).
Definition gl.cpp:1005
std::string textureName
Definition gl.hpp:394
void initSize(float w, float h)
Set the native display size of the sprite.
Definition gl.cpp:1000
GLSprite(const GLSprite &)=delete
float screenWidth
Definition gl.hpp:392
GLSprite & operator=(const GLSprite &)=delete
GLuint VBO
Definition gl.hpp:390
int width
Texture pixel width.
Definition gl.hpp:382
float screenHeight
Definition gl.hpp:392
void loadTextureImpl(const std::string &tex, float x, float y, int textWidth, int textHeight)
Definition gl.cpp:1077
int texWidth
Definition gl.hpp:393
void release()
Free all GL resources associated with this sprite.
Definition gl.cpp:1009
void initWithTextureImpl(GLuint texture, float x, float y, int textWidth, int textHeight)
Definition gl.cpp:1037
Render SDL_ttf text into an OpenGL texture and display it on screen.
Definition gl.hpp:181
void setColor(SDL_Color col)
Set the colour used for subsequent printText calls.
Definition gl.cpp:894
int w
Definition gl.hpp:243
void printText_Solid(const mx::Font &f, float x, float y, const std::string &text)
Convenience: create and immediately draw solid text.
Definition gl.cpp:970
SDL_Color color
Current text colour.
Definition gl.hpp:242
int h
Viewport dimensions.
Definition gl.hpp:243
void init(int w, int h)
Initialise internal state with the viewport dimensions.
Definition gl.cpp:885
ShaderProgram textShader
Internal text quad shader.
Definition gl.hpp:241
GLText()
Default constructor.
Definition gl.cpp:881
void printText_Blended(const mx::Font &f, float x, float y, const std::string &text)
Convenience: create and immediately draw blended (anti-aliased) text.
Definition gl.cpp:984
void renderText(GLuint texture, float x, float y, int textWidth, int textHeight, int screenWidth, int screenHeight)
Draw a pre-created text texture at a given position.
Definition gl.cpp:939
GLuint createText(const std::string &text, TTF_Font *font, SDL_Color color, int &textWidth, int &textHeight, bool solid=true)
Rasterise a string to an OpenGL texture.
Definition gl.cpp:898
SDL2 + OpenGL/ES window with event loop and console support.
Definition gl.hpp:411
SDL_Window * window
Definition gl.hpp:579
mx::Mixer mixer
Definition gl.hpp:572
int w
Current window width.
Definition gl.hpp:563
int h
Current window height.
Definition gl.hpp:564
bool console_visible
Definition gl.hpp:575
GLText text
Definition gl.hpp:574
std::unique_ptr< gl::GLObject > object
Optional attached scene object.
Definition gl.hpp:560
virtual void resize(int w, int h)
Called when the window is resized (optional override).
Definition gl.hpp:483
virtual void event(SDL_Event &e)=0
Process a single SDL event (pure virtual).
bool hide_console
Definition gl.hpp:582
GLWindow & operator=(GLWindow &&)=delete
SDL_Window * getWindow()
Definition gl.hpp:567
GLWindow(GLWindow &&)=delete
void swap()
Swap front and back buffers to display the rendered frame.
Definition gl.cpp:315
bool active
Definition gl.hpp:580
void setPath(const std::string &path)
Set the asset search path.
Definition gl.hpp:507
virtual void draw()=0
Render the current frame (pure virtual).
SDL_GLContext glContext
Definition gl.hpp:578
void initGL(const std::string &title, int width, int height, bool resize_=true)
Initialise the GL context and SDL window.
Definition gl.cpp:138
GLMode gl_mode
Definition gl.hpp:413
SDL_Event e
Definition gl.hpp:581
GLWindow & operator=(const GLWindow &)=delete
mx::mxUtil util
Asset path and utility helpers.
Definition gl.hpp:561
void updateViewport()
Update the GL viewport to match the current window size.
Definition gl.cpp:46
GLWindow(const GLWindow &)=delete
GLWindow(const std::string &text, int width, int height, bool resize_=true, GLMode mode=GLMode::DESKTOP)
Construct and open an SDL2 + OpenGL window.
Definition gl.hpp:427
bool console_active
Definition gl.hpp:576
GLSL shader program wrapper (shared-ownership).
Definition gl.hpp:57
std::shared_ptr< SharedState > state
Definition gl.hpp:68
bool checkError()
Definition gl.cpp:515
bool loaded() const
Definition gl.cpp:465
void setSilent(bool s)
Suppress compilation/link error output.
Definition gl.cpp:475
void release()
Release the underlying GL program (decrements shared count).
Definition gl.cpp:440
void setUniform(const std::string &name, int value)
Upload an integer uniform variable.
Definition gl.cpp:629
ShaderProgram(ShaderProgram &&) noexcept=default
~ShaderProgram()=default
ShaderProgram()
Default constructor — allocates shared state, no program compiled yet.
Definition gl.cpp:431
bool loadProgram(const std::string &v, const std::string &f)
Compile and link shaders from file paths.
Definition gl.cpp:613
GLuint createProgramFromFile(const std::string &vert, const std::string &frag)
Definition gl.cpp:593
ShaderProgram(const ShaderProgram &)=default
GLuint createProgram(const char *vshaderSource, const char *fshaderSource)
Definition gl.cpp:526
void setName(const std::string &n)
Assign a human-readable name used in error messages.
Definition gl.cpp:469
bool loadProgramFromText(const std::string &v, const std::string &f)
Compile and link shaders from source-code strings.
Definition gl.cpp:621
void useProgram()
Bind this shader program for subsequent draw calls.
Definition gl.cpp:481
ShaderProgram & operator=(const ShaderProgram &)=default
int printShaderLog(GLuint shader)
Definition gl.cpp:487
void printProgramLog(int p)
Definition gl.cpp:501
RAII wrapper around a TTF_Font handle.
Definition font.hpp:25
SDL_mixer–based audio manager.
Definition sound.hpp:31
Utility bag for SDL-based applications.
Definition util.hpp:33
Compile-time feature flags and version macros for libmx.
OpenGL-backed in-game terminal console for libmx2.
unsigned int GLuint
Definition console.hpp:38
Central SDL2 application window class and related includes.
Definition console.hpp:31
const char * vSource
Default vertex shader source.
Definition gl.cpp:853
GLuint createTexture(SDL_Surface *surface, bool flip)
Create a new GL texture from an SDL_Surface.
Definition gl.cpp:743
GLMode
OpenGL mode: desktop GL or OpenGL ES.
Definition gl.hpp:398
@ DESKTOP
Definition gl.hpp:398
GLuint loadTexture(const std::string &filename)
Load a GL texture from a file.
Definition gl.cpp:689
SDL_Surface * createSurface(int w, int h)
Allocate a blank RGBA SDL_Surface.
Definition gl.cpp:811
const char * fSource
Default fragment shader source.
Definition gl.cpp:862
void updateTexture(GLuint texture, SDL_Surface *surface, bool flip)
Replace a GL texture's contents from an SDL_Surface.
Definition gl.cpp:795