Magnum::Platform::GlfwApplication class

GLFW application.

Application using the GLFW toolkit. Supports keyboard and mouse handling with support for changing cursor and mouse tracking and warping.

This application library is available on all platforms where GLFW is ported. It depends on the GLFW library and is built if WITH_GLFWAPPLICATION is enabled when building Magnum.

Bootstrap application

Fully contained base application using GlfwApplication along with CMake setup is available in base-glfw branch of Magnum Bootstrap repository, download it as tar.gz or zip file. After extracting the downloaded archive you can build and run the application with these four commands:

mkdir build && cd build
cmake ..
cmake --build .
./src/MyApplication # or ./src/Debug/MyApplication

See Usage with CMake for more information.

General usage

In order to use this library from CMake, you need to copy FindGLFW.cmake from the modules directory in Magnum source to the modules/ dir in your project (so it is able to find the GLFW library). Request the GlfwApplication component of the Magnum package and link to the Magnum::GlfwApplication target:

find_package(Magnum REQUIRED GlfwApplication)

# ...
target_link_libraries(your-app Magnum::GlfwApplication)

If no other application is requested, you can also use the generic Magnum::Application alias to simplify porting. Again, see Downloading and building and Usage with CMake for more information.

In C++ code you need to implement at least drawEvent() to be able to draw on the screen. The subclass can be then used directly in main() — see convenience macro MAGNUM_GLFWAPPLICATION_MAIN(). See Platform support for more information.

class MyApplication: public Platform::GlfwApplication {
    // implement required methods...
};
MAGNUM_GLFWAPPLICATION_MAIN(MyApplication)

If no other application header is included, this class is also aliased to Platform::Application and the macro is aliased to MAGNUM_APPLICATION_MAIN() to simplify porting.

Public types

struct Arguments
Application arguments.
class Configuration
Configuration.
class InputEvent
Base for input events.
class KeyEvent
Key event.
class MouseEvent
Mouse event.
class MouseMoveEvent
Mouse move event.
class MouseScrollEvent
Mouse scroll event.
class TextInputEvent
Text input event.

Constructors, destructors, conversion operators

GlfwApplication(const Arguments& arguments, const Configuration& configuration = Configuration()) explicit
Default constructor.
GlfwApplication(const Arguments& arguments, NoCreateT) explicit
Constructor.
GlfwApplication(const Arguments& arguments, std::nullptr_t) deprecated explicit
Constructor.
GlfwApplication(const GlfwApplication&) deleted
Copying is not allowed.
GlfwApplication(GlfwApplication&&) deleted
Moving is not allowed.

Public functions

auto operator=(const GlfwApplication&) -> GlfwApplication& deleted
Copying is not allowed.
auto operator=(GlfwApplication&&) -> GlfwApplication& deleted
Moving is not allowed.
auto exec() -> int
Execute main loop.
void exit()
Exit application main loop.
auto window() -> GLFWwindow*
Underlying window handle.

Protected functions

void createContext(const Configuration& configuration = Configuration())
Create context with given configuration.
auto tryCreateContext(const Configuration& configuration) -> bool
Try to create context with given configuration.

Screen handling

auto windowSize() -> Vector2i protected
Window size.
void swapBuffers() protected
Swap buffers.
void setSwapInterval(Int interval) protected
Set swap interval.
void redraw() protected
Redraw immediately.
void viewportEvent(const Vector2i& size) protected virtual
Viewport event.
void drawEvent() protected pure virtual
Draw event.

Keyboard handling

void keyPressEvent(KeyEvent& event) protected virtual
Key press event.
void keyReleaseEvent(KeyEvent& event) protected virtual
Key release event.

Mouse handling

void warpCursor(const Vector2i& position)
Warp mouse cursor to given coordinates.
void mousePressEvent(MouseEvent& event) protected virtual
Mouse press event.
void mouseReleaseEvent(MouseEvent& event) protected virtual
Mouse release event.
void mouseMoveEvent(MouseMoveEvent& event) protected virtual
Mouse move event.
void mouseScrollEvent(MouseScrollEvent& event) protected virtual
Mouse scroll event.

Text input handling

auto isTextInputActive() const -> bool
Whether text input is active.
void startTextInput()
Start text input.
void stopTextInput()
Stop text input.
void textInputEvent(TextInputEvent& event) protected virtual
Text input event.

Function documentation

Magnum::Platform::GlfwApplication::GlfwApplication(const Arguments& arguments, const Configuration& configuration = Configuration()) explicit

Default constructor.

Parameters
arguments Application arguments
configuration Configuration

Creates application with default or user-specified configuration. See Configuration for more information. The program exits if the context cannot be created, see tryCreateContext() for an alternative.

Magnum::Platform::GlfwApplication::GlfwApplication(const Arguments& arguments, NoCreateT) explicit

Constructor.

Parameters
arguments Application arguments

Unlike above, the context is not created and must be created later with createContext() or tryCreateContext().

Magnum::Platform::GlfwApplication::GlfwApplication(const Arguments& arguments, std::nullptr_t) explicit

Constructor.

int Magnum::Platform::GlfwApplication::exec()

Execute main loop.

Returns Value for returning from main()

See MAGNUM_GLFWAPPLICATION_MAIN() for usage information.

GLFWwindow* Magnum::Platform::GlfwApplication::window()

Underlying window handle.

Use in case you need to call GLFW functionality directly.

void Magnum::Platform::GlfwApplication::createContext(const Configuration& configuration = Configuration()) protected

Create context with given configuration.

Must be called if and only if the context wasn't created by the constructor itself. Error message is printed and the program exits if the context cannot be created, see tryCreateContext() for an alternative.

On desktop GL, if version is not specified in configuration, the application first tries to create core context (OpenGL 3.2+ on macOS, OpenGL 3.1+ elsewhere) and if that fails, falls back to compatibility OpenGL 2.1 context.

bool Magnum::Platform::GlfwApplication::tryCreateContext(const Configuration& configuration) protected

Try to create context with given configuration.

Unlike createContext() returns false if the context cannot be created, true otherwise.

Vector2i Magnum::Platform::GlfwApplication::windowSize() protected

Window size.

Window size to which all input event coordinates can be related. Note that especially on HiDPI systems the reported window size might not be the same as framebuffer size.

void Magnum::Platform::GlfwApplication::swapBuffers() protected

Swap buffers.

Paints currently rendered framebuffer on screen.

void Magnum::Platform::GlfwApplication::setSwapInterval(Int interval) protected

Set swap interval.

Set 0 for no VSync, 1 for enabled VSync. Some platforms support -1 for late swap tearing. Default is driver-dependent.

void Magnum::Platform::GlfwApplication::redraw() protected

Redraw immediately.

Marks the window for redrawing, resulting in call to drawEvent() in the next iteration. You can call it from drawEvent() itself to redraw immediately without waiting for user input.

void Magnum::Platform::GlfwApplication::viewportEvent(const Vector2i& size) virtual protected

Viewport event.

Called when window size changes. The default implementation does nothing, if you want to respond to size changes, you should pass the new size to DefaultFramebuffer::setViewport() and possibly elsewhere (to SceneGraph::Camera::setViewport(), other framebuffers...).

Note that this function might not get called at all if the window size doesn't change. You should configure the initial state of your cameras, framebuffers etc. in application constructor rather than relying on this function to be called. Viewport of default framebuffer can be retrieved via DefaultFramebuffer::viewport().

void Magnum::Platform::GlfwApplication::drawEvent() pure virtual protected

Draw event.

Called when the screen is redrawn. You should clean the framebuffer using DefaultFramebuffer::clear() and then add your own drawing functions. After drawing is finished, call swapBuffers(). If you want to draw immediately again, call also redraw().

void Magnum::Platform::GlfwApplication::keyPressEvent(KeyEvent& event) virtual protected

Key press event.

Called when an key is pressed. Default implementation does nothing.

void Magnum::Platform::GlfwApplication::keyReleaseEvent(KeyEvent& event) virtual protected

Key release event.

Called when an key is released. Default implementation does nothing.

void Magnum::Platform::GlfwApplication::mousePressEvent(MouseEvent& event) virtual protected

Mouse press event.

Called when mouse button is pressed. Default implementation does nothing.

void Magnum::Platform::GlfwApplication::mouseReleaseEvent(MouseEvent& event) virtual protected

Mouse release event.

Called when mouse button is released. Default implementation does nothing.

void Magnum::Platform::GlfwApplication::mouseMoveEvent(MouseMoveEvent& event) virtual protected

Mouse move event.

Called when any mouse button is pressed and mouse is moved. Default implementation does nothing.

void Magnum::Platform::GlfwApplication::mouseScrollEvent(MouseScrollEvent& event) virtual protected

Mouse scroll event.

Called when a scrolling device is used (mouse wheel or scrolling area on a touchpad). Default implementation does nothing.

bool Magnum::Platform::GlfwApplication::isTextInputActive() const

Whether text input is active.

If text input is active, text input events go to textInputEvent().

void Magnum::Platform::GlfwApplication::startTextInput()

Start text input.

Starts text input that will go to textInputEvent().

void Magnum::Platform::GlfwApplication::stopTextInput()

Stop text input.

Stops text input that went to textInputEvent().

void Magnum::Platform::GlfwApplication::textInputEvent(TextInputEvent& event) virtual protected

Text input event.

Called when text input is active and the text is being input.