libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
gl::GLWindow Class Referenceabstract

SDL2 + OpenGL/ES window with event loop and console support. More...

#include <gl.hpp>

Collaboration diagram for gl::GLWindow:
[legend]

Public Member Functions

 GLWindow (const std::string &text, int width, int height, bool resize_=true, GLMode mode=GLMode::DESKTOP)
 Construct and open an SDL2 + OpenGL window.
 GLWindow (int width, int height, GLMode mode)
 Construct a window from dimensions and mode (internal use).
virtual ~GLWindow ()
 Destructor — destroys the GL context and SDL window.
 GLWindow (const GLWindow &)=delete
GLWindow & operator= (const GLWindow &)=delete
 GLWindow (GLWindow &&)=delete
GLWindow & operator= (GLWindow &&)=delete
void initGL (const std::string &title, int width, int height, bool resize_=true)
 Initialise the GL context and SDL window.
void initGL (int width, int height)
 Reinitialise with only dimensions (used by Emscripten).
void updateViewport ()
 Update the GL viewport to match the current window size.
void swap ()
 Swap front and back buffers to display the rendered frame.
virtual void event (SDL_Event &e)=0
 Process a single SDL event (pure virtual).
virtual void draw ()=0
 Render the current frame (pure virtual).
virtual void resize (int w, int h)
 Called when the window is resized (optional override).
void setObject (GLObject *o)
 Attach a GLObject to receive draw/event callbacks.
void quit ()
 Request the event loop to stop.
void proc ()
 Run the event+draw loop until quit() is called.
void loop ()
 Start the blocking main loop.
void delay ()
 Insert a small sleep to cap frame rate.
void setPath (const std::string &path)
 Set the asset search path.
void setWindowTitle (const std::string &title)
 Change the window title bar text.
void setWindowSize (int w, int h)
 Resize the SDL window.
void setWindowIcon (SDL_Surface *ico)
 Set the window icon from an SDL_Surface.
void setFullScreen (bool full)
 Toggle fullscreen mode.
void activateConsole (const std::string &fnt, int size, const SDL_Color &color)
 Activate the built-in debug console.
void activateConsole (const SDL_Rect &rc, const std::string &fnt, int size, const SDL_Color &color)
 Activate the console at a specific screen region.
void drawConsole ()
 Draw the console overlay onto the current frame.
void showConsole (bool show)
 Show or hide the console overlay.
SDL_Window * getWindow ()
void makeCurrent ()
 Restore this window's GL context as current on the calling thread.

Public Attributes

std::unique_ptr< gl::GLObject > object
 Optional attached scene object.
mx::mxUtil util
 Asset path and utility helpers.
console::GLConsole console
 Built-in debug console.
int w = 0
 Current window width.
int h = 0
 Current window height.
mx::Mixer mixer
GLText text
bool console_visible = false
bool console_active = false

Private Attributes

GLMode gl_mode
SDL_GLContext glContext
SDL_Window * window = nullptr
bool active = false
SDL_Event e
bool hide_console = false

Detailed Description

SDL2 + OpenGL/ES window with event loop and console support.

Creates an SDL2 window with an OpenGL (or GLES) context. Derived classes implement event() and draw(). An optional GLObject can be attached to delegate rendering. A built-in GLConsole can be activated for in-app debugging.

Definition at line 411 of file gl.hpp.

Constructor & Destructor Documentation

◆ GLWindow() [1/4]

gl::GLWindow::GLWindow ( const std::string & text,
int width,
int height,
bool resize_ = true,
GLMode mode = GLMode::DESKTOP )
inline

Construct and open an SDL2 + OpenGL window.

Parameters
textWindow title.
widthWidth in pixels.
heightHeight in pixels.
resize_Allow window resizing if true.
modeGL mode (DESKTOP or ES).

Definition at line 427 of file gl.hpp.

References gl::DESKTOP, gl_mode, glContext, initGL(), text, and window.

Referenced by GLWindow(), GLWindow(), operator=(), and operator=().

◆ GLWindow() [2/4]

gl::GLWindow::GLWindow ( int width,
int height,
GLMode mode )

Construct a window from dimensions and mode (internal use).

Parameters
widthWidth.
heightHeight.
modeGL mode.

Definition at line 75 of file gl.cpp.

References gl_mode, glContext, initGL(), and window.

◆ ~GLWindow()

gl::GLWindow::~GLWindow ( )
virtual

Destructor — destroys the GL context and SDL window.

Definition at line 21 of file gl.cpp.

References glContext, mx::system_out, and window.

◆ GLWindow() [3/4]

gl::GLWindow::GLWindow ( const GLWindow & )
delete

References GLWindow().

◆ GLWindow() [4/4]

gl::GLWindow::GLWindow ( GLWindow && )
delete

References GLWindow().

Member Function Documentation

◆ activateConsole() [1/2]

void gl::GLWindow::activateConsole ( const SDL_Rect & rc,
const std::string & fnt,
int size,
const SDL_Color & color )

Activate the console at a specific screen region.

Parameters
rcConsole rectangle.
fntFont file path.
sizeFont size.
colorText colour.

Definition at line 299 of file gl.cpp.

References console_active.

◆ activateConsole() [2/2]

void gl::GLWindow::activateConsole ( const std::string & fnt,
int size,
const SDL_Color & color )

Activate the built-in debug console.

Parameters
fntFont file path.
sizeFont size in points.
colorText colour.

Definition at line 294 of file gl.cpp.

References console_active.

◆ delay()

void gl::GLWindow::delay ( )

Insert a small sleep to cap frame rate.

Definition at line 339 of file gl.cpp.

◆ draw()

virtual void gl::GLWindow::draw ( )
pure virtual

Render the current frame (pure virtual).

Referenced by proc().

◆ drawConsole()

void gl::GLWindow::drawConsole ( )

Draw the console overlay onto the current frame.

Definition at line 402 of file gl.cpp.

References console_visible.

Referenced by swap().

◆ event()

virtual void gl::GLWindow::event ( SDL_Event & e)
pure virtual

Process a single SDL event (pure virtual).

Parameters
eSDL event.

References e.

Referenced by proc().

◆ getWindow()

SDL_Window * gl::GLWindow::getWindow ( )
inline
Returns
Pointer to the underlying SDL_Window.

Definition at line 567 of file gl.hpp.

References window.

◆ initGL() [1/2]

void gl::GLWindow::initGL ( const std::string & title,
int width,
int height,
bool resize_ = true )

Initialise the GL context and SDL window.

Parameters
titleWindow title.
widthWidth.
heightHeight.
resize_Allow resizing.

Definition at line 138 of file gl.cpp.

References gl::DESKTOP, gl_mode, glContext, h, mx::redirect(), mx::system_err, mx::system_out, text, w, and window.

Referenced by GLWindow(), GLWindow(), and operator=().

◆ initGL() [2/2]

void gl::GLWindow::initGL ( int width,
int height )

Reinitialise with only dimensions (used by Emscripten).

Parameters
widthNew width.
heightNew height.

Definition at line 79 of file gl.cpp.

References gl::DESKTOP, gl_mode, glContext, h, mx::redirect(), mx::system_err, mx::system_out, text, w, and window.

◆ loop()

void gl::GLWindow::loop ( )

Start the blocking main loop.

Definition at line 332 of file gl.cpp.

References active, and proc().

◆ makeCurrent()

void gl::GLWindow::makeCurrent ( )

Restore this window's GL context as current on the calling thread.

Definition at line 322 of file gl.cpp.

References glContext, and window.

◆ operator=() [1/2]

GLWindow & gl::GLWindow::operator= ( const GLWindow & )
delete

References GLWindow().

◆ operator=() [2/2]

GLWindow & gl::GLWindow::operator= ( GLWindow && )
delete

◆ proc()

void gl::GLWindow::proc ( )

Run the event+draw loop until quit() is called.

Definition at line 341 of file gl.cpp.

References active, console_active, console_visible, draw(), e, event(), hide_console, updateViewport(), and window.

Referenced by loop().

◆ quit()

void gl::GLWindow::quit ( )

Request the event loop to stop.

Definition at line 328 of file gl.cpp.

References active.

Referenced by console::GLConsole::event().

◆ resize()

virtual void gl::GLWindow::resize ( int w,
int h )
inlinevirtual

Called when the window is resized (optional override).

Parameters
wNew width.
hNew height.

Definition at line 483 of file gl.hpp.

References h, and w.

◆ setFullScreen()

void gl::GLWindow::setFullScreen ( bool full)

Toggle fullscreen mode.

Parameters
fulltrue for fullscreen.

Definition at line 282 of file gl.cpp.

References updateViewport(), and window.

◆ setObject()

void gl::GLWindow::setObject ( gl::GLObject * o)

Attach a GLObject to receive draw/event callbacks.

Parameters
oPointer to the object (window does not take ownership).

Definition at line 304 of file gl.cpp.

◆ setPath()

void gl::GLWindow::setPath ( const std::string & path)
inline

Set the asset search path.

Parameters
pathDirectory containing assets.

Definition at line 507 of file gl.hpp.

References util.

◆ setWindowIcon()

void gl::GLWindow::setWindowIcon ( SDL_Surface * ico)

Set the window icon from an SDL_Surface.

Parameters
icoIcon surface.

Definition at line 60 of file gl.cpp.

References window.

◆ setWindowSize()

void gl::GLWindow::setWindowSize ( int w,
int h )

Resize the SDL window.

Parameters
wNew width.
hNew height.

Definition at line 264 of file gl.cpp.

References h, updateViewport(), w, and window.

◆ setWindowTitle()

void gl::GLWindow::setWindowTitle ( const std::string & title)

Change the window title bar text.

Parameters
titleNew title.

Definition at line 279 of file gl.cpp.

References window.

◆ showConsole()

void gl::GLWindow::showConsole ( bool show)

Show or hide the console overlay.

Parameters
showtrue to show, false to hide.

Definition at line 310 of file gl.cpp.

References console_visible.

◆ swap()

void gl::GLWindow::swap ( )

Swap front and back buffers to display the rendered frame.

Definition at line 315 of file gl.cpp.

References drawConsole(), and window.

Referenced by operator=().

◆ updateViewport()

void gl::GLWindow::updateViewport ( )

Update the GL viewport to match the current window size.

Definition at line 46 of file gl.cpp.

References console_active, h, text, w, and window.

Referenced by operator=(), proc(), setFullScreen(), and setWindowSize().

Member Data Documentation

◆ active

bool gl::GLWindow::active = false
private

Definition at line 580 of file gl.hpp.

Referenced by loop(), proc(), and quit().

◆ console

console::GLConsole gl::GLWindow::console

Built-in debug console.

Definition at line 562 of file gl.hpp.

◆ console_active

bool gl::GLWindow::console_active = false

Definition at line 576 of file gl.hpp.

Referenced by activateConsole(), activateConsole(), proc(), and updateViewport().

◆ console_visible

bool gl::GLWindow::console_visible = false

Definition at line 575 of file gl.hpp.

Referenced by drawConsole(), console::GLConsole::event(), proc(), and showConsole().

◆ e

SDL_Event gl::GLWindow::e
private

Definition at line 581 of file gl.hpp.

Referenced by event(), and proc().

◆ gl_mode

GLMode gl::GLWindow::gl_mode
private

Definition at line 413 of file gl.hpp.

Referenced by GLWindow(), GLWindow(), initGL(), and initGL().

◆ glContext

SDL_GLContext gl::GLWindow::glContext
private

Definition at line 578 of file gl.hpp.

Referenced by GLWindow(), GLWindow(), initGL(), initGL(), makeCurrent(), and ~GLWindow().

◆ h

int gl::GLWindow::h = 0

Current window height.

Definition at line 564 of file gl.hpp.

Referenced by initGL(), initGL(), console::GLConsole::load(), console::GLConsole::load(), resize(), setWindowSize(), and updateViewport().

◆ hide_console

bool gl::GLWindow::hide_console = false
private

Definition at line 582 of file gl.hpp.

Referenced by proc().

◆ mixer

mx::Mixer gl::GLWindow::mixer

Definition at line 572 of file gl.hpp.

◆ object

std::unique_ptr<gl::GLObject> gl::GLWindow::object

Optional attached scene object.

Definition at line 560 of file gl.hpp.

◆ text

GLText gl::GLWindow::text

Definition at line 574 of file gl.hpp.

Referenced by GLWindow(), initGL(), initGL(), and updateViewport().

◆ util

mx::mxUtil gl::GLWindow::util

Asset path and utility helpers.

Definition at line 561 of file gl.hpp.

Referenced by console::GLConsole::load(), console::GLConsole::load(), and setPath().

◆ w

int gl::GLWindow::w = 0

Current window width.

Definition at line 563 of file gl.hpp.

Referenced by initGL(), initGL(), console::GLConsole::load(), console::GLConsole::load(), resize(), setWindowSize(), and updateViewport().

◆ window

SDL_Window* gl::GLWindow::window = nullptr
private

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