Magnum::Platform::GlutApplication class

GLUT application.

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

This application library is available only on desktop OpenGL (Linux, Windows, macOS). It depends on the GLUT library and is built if WITH_GLUTAPPLICATION is enabled when building Magnum.

Bootstrap application

Fully contained base application using GlutApplication along with CMake setup is available in base-glut 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 request the GlutApplication component of the Magnum package and link to the Magnum::GlutApplication target:

find_package(Magnum REQUIRED GlutApplication)

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

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_GLUTAPPLICATION_MAIN(). See Platform support for more information.

class MyApplication: public Platform::GlutApplication {
    // implement required methods...
};
MAGNUM_GLUTAPPLICATION_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 GLConfiguration
OpenGL context configuration.
class InputEvent
Base for input events.
class KeyEvent
Key event.
class MouseEvent
Mouse event.
class MouseMoveEvent
Mouse move event.

Constructors, destructors, conversion operators

GlutApplication(const Arguments& arguments, const Configuration& configuration, const GLConfiguration& glConfiguration) explicit
Construct with given configuration for OpenGL context.
GlutApplication(const Arguments& arguments, const Configuration& configuration) explicit
Construct with given configuration.
GlutApplication(const Arguments& arguments) explicit
Construct with default configuration.
GlutApplication(const Arguments& arguments, NoCreateT) explicit
Construct without creating a window.
GlutApplication(const Arguments& arguments, std::nullptr_t) deprecated explicit
Construct without creating a window.
GlutApplication(const GlutApplication&) deleted
Copying is not allowed.
GlutApplication(GlutApplication&&) deleted
Moving is not allowed.

Public functions

auto operator=(const GlutApplication&) -> GlutApplication& deleted
Copying is not allowed.
auto operator=(GlutApplication&&) -> GlutApplication& deleted
Moving is not allowed.
auto exec() -> int
Execute main loop.

Protected functions

void create(const Configuration& configuration, const GLConfiguration& glConfiguration)
Create a window with given configuration for OpenGL context.
void create(const Configuration& configuration)
Create a window with given configuration and OpenGL context.
void create()
Create a window with default configuration and OpenGL context.
void createContext(const Configuration& configuration) deprecated
Create a window with given configuration for OpenGL context.
void createContext() deprecated
Create a window with default configuration and OpenGL context.
auto tryCreate(const Configuration& configuration, const GLConfiguration& glConfiguration) -> bool
Try to create context with given configuration for OpenGL context.
auto tryCreate(const Configuration& configuration) -> bool
Try to create context with given configuration and OpenGL context.
auto tryCreateContext(const Configuration& configuration) -> bool deprecated
Try to create context with given configuration for OpenGL context.

Screen handling

void swapBuffers() protected
Swap buffers.
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

enum class MouseCursor: int { Default = GLUT_CURSOR_INHERIT, None = GLUT_CURSOR_NONE }
Mouse cursor.
void setMouseTracking(bool enabled)
Enable or disable mouse tracking.
void setMouseCursor(MouseCursor cursor)
Set mouse cursor.
void warpMouseCursor(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.

Enum documentation

enum class Magnum::Platform::GlutApplication::MouseCursor: int

Mouse cursor.

Enumerators
Default

Default cursor provided by parent window

None

No cursor

Function documentation

Magnum::Platform::GlutApplication::GlutApplication(const Arguments& arguments, const Configuration& configuration, const GLConfiguration& glConfiguration) explicit

Construct with given configuration for OpenGL context.

Parameters
arguments Application arguments
configuration Application configuration
glConfiguration OpenGL context configuration

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

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

Construct with given configuration.

Equivalent to calling GlutApplication(const Arguments&, const Configuration&, const GLConfiguration&) with default-constructed GLConfiguration.

Magnum::Platform::GlutApplication::GlutApplication(const Arguments& arguments) explicit

Construct with default configuration.

Equivalent to calling GlutApplication(const Arguments&, const Configuration&) with default-constructed Configuration.

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

Construct without creating a window.

Parameters
arguments Application arguments

Unlike above, the window is not created and must be created later with create() or tryCreate().

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

Construct without creating a window.

int Magnum::Platform::GlutApplication::exec()

Execute main loop.

Returns Value for returning from main()

See MAGNUM_GLUTAPPLICATION_MAIN() for usage information.

void Magnum::Platform::GlutApplication::create(const Configuration& configuration, const GLConfiguration& glConfiguration) protected

Create a window with given configuration for OpenGL context.

Parameters
configuration Application configuration
glConfiguration OpenGL context configuration

Must be called only if the context wasn't created by the constructor itself, i.e. when passing NoCreate to it. Error message is printed and the program exits if the context cannot be created, see tryCreate() for an alternative.

void Magnum::Platform::GlutApplication::create(const Configuration& configuration) protected

Create a window with given configuration and OpenGL context.

Equivalent to calling create(const Configuration&, const GLConfiguration&) with default-constructed GLConfiguration.

void Magnum::Platform::GlutApplication::create() protected

Create a window with default configuration and OpenGL context.

Equivalent to calling create(const Configuration&) with default-constructed Configuration.

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

Create a window with given configuration for OpenGL context.

void Magnum::Platform::GlutApplication::createContext() protected

Create a window with default configuration and OpenGL context.

bool Magnum::Platform::GlutApplication::tryCreate(const Configuration& configuration, const GLConfiguration& glConfiguration) protected

Try to create context with given configuration for OpenGL context.

Unlike create(const Configuration&, const GLConfiguration&) returns false if the context cannot be created, true otherwise.

bool Magnum::Platform::GlutApplication::tryCreate(const Configuration& configuration) protected

Try to create context with given configuration and OpenGL context.

Unlike create(const Configuration&) returns false if the context cannot be created, true otherwise.

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

Try to create context with given configuration for OpenGL context.

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

Swap buffers.

Paints currently rendered framebuffer on screen.

void Magnum::Platform::GlutApplication::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::GlutApplication::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 GL::DefaultFramebuffer::setViewport() (if using OpenGL) 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 GL::DefaultFramebuffer::viewport().

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

Draw event.

Called when the screen is redrawn. You should clean the framebuffer using GL::DefaultFramebuffer::clear() (if using OpenGL) 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::GlutApplication::keyPressEvent(KeyEvent& event) virtual protected

Key press event.

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

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

Key release event.

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

void Magnum::Platform::GlutApplication::setMouseTracking(bool enabled)

Enable or disable mouse tracking.

When mouse tracking is enabled, mouseMoveEvent() is called even when no button is pressed. Mouse tracking is disabled by default.

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

Mouse press event.

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

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

Mouse release event.

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

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

Mouse move event.

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