TinyGL-C++
Single-header, cross-platform, OpenGL 4.0 2D drawing framework
|
Manages the window and user input. More...
#include <tinygl-cpp.h>
Public Member Functions | |
Window (int width, int height) | |
Constructor. More... | |
void | run () |
Opens the window and starts the main application loop. More... | |
Protected Member Functions | |
void | noLoop () |
Stop the main application loop. More... | |
void | background (float r, float g, float b) |
Set the background color. More... | |
GLFWwindow * | window () const |
Draw commands | |
void | loadSprite (const std::string &name, const std::string &filename) |
Loads a texture to be shown on a sprite. More... | |
void | sprite (const std::string &textureName, float x, float y, float scale=1) |
Draws a sprite with the current color in pixel coordinates. More... | |
void | square (float x, float y, float width, float height) |
Draws a square with the current color in pixel coordinates. More... | |
void | triangle (int x, int y, float width, float height) |
Draw a triangle with the current color using pixel coordinates. More... | |
void | circle (int x, int y, float diameter) |
Draws a circle with the current color in pixel coordinates. More... | |
void | ellipsoid (int x, int y, float width, float height) |
Draws an ellipse with the current color in pixel coordinates. More... | |
void | color (float r, float g, float b, float a=1.0f) |
Set the current RGB color. More... | |
Respond to events | |
virtual void | setup () |
Override this method to perform setup before the main application loop. | |
virtual void | draw () |
Override this method to draw. | |
virtual void | mouseMotion (int x, int y, int dx, int dy) |
Override this method to respond to mouse movement. More... | |
virtual void | mouseDown (int button, int mods) |
Override this method to respond to mouse press (button down) More... | |
virtual void | mouseUp (int button, int mods) |
Override this method to respond to mouse press (button up) More... | |
virtual void | scroll (float dx, float dy) |
Override this method to respond to scrolling the middle mouse button. More... | |
virtual void | keyUp (int key, int mods) |
Override this method to respond to key presses (button up) More... | |
virtual void | keyDown (int key, int mods) |
Override this method to respond to key presses (button down) More... | |
Query input and time | |
bool | keyIsDown (int key) const |
Query whether the given key is down @key The key to test. More... | |
bool | mouseIsDown (int button) const |
Query whether the given mouse button is down. More... | |
float | mouseX () const |
Return the current mouse position X coordinate (in screen coordinates) | |
float | mouseY () const |
Return the current mouse position Y coordinate (in screen coordinates) | |
float | dt () const |
Return the amount of time since the previous frame (in seconds) More... | |
float | elapsedTime () const |
Return the amount of time since the setup() was called (in seconds) | |
float | height () const |
Return the window height in pixels. | |
float | width () const |
Return the window width in pixels. | |
Manages the window and user input.
Override this class to create a custom application.
The scene size corresponds to the window width and height. All coordinates are in screen coordinates. Y corresponds to the vertical axis; X corresponds to the horizontal axis. Colors are RGBA, with each component in the range [0,1]
Override setup() to do initialization at the beginning of the application. setup() is called once when the application starts.
Call circle(), square(), background(), etc. in draw() to draw different primitives. draw() is called once per frame.
Override mouseUp(), mouseDown(), keyUp(), etc. to respond to inputs Poll input using mouseX(), mouseY(), isKeyDown(), etc.
|
inline |
Constructor.
width | The screen width (in pixels) |
height | The screen height (in pixels) |
Override this class to create a custom application.
|
inlineprotected |
Set the background color.
This function can be called from draw and will additionally clear the color and depth buffers
|
inlineprotected |
Draws a circle with the current color in pixel coordinates.
x | The horizontal position of the center |
y | The vertical position of the center |
diameter | The width of the circle |
|
inlineprotected |
Set the current RGB color.
r | The red component (range [0,1]) |
g | The red component (range [0,1]) |
b | The red component (range [0,1]) |
|
inlineprotected |
Return the amount of time since the previous frame (in seconds)
If the frame rate is 30 frames per second, dt would be approximately 1/30 = 0.033333 seconds each frame
|
inlineprotected |
Draws an ellipse with the current color in pixel coordinates.
x | The horizontal position of the center |
y | The vertical position of the center |
width | The width of the ellipse |
height | The height of the ellipse |
|
inlineprotectedvirtual |
Override this method to respond to key presses (button down)
key | The key, e.g. as either a constant or capital ascii letter, such as 'P'. |
mods | Modifiers that are pressed (e.g. shift, control, etc) |
https://www.glfw.org/docs/latest/group__keys.html
|
inlineprotected |
Query whether the given key is down @key The key to test.
More than one key can be pressed at once. The key can be an ascii capital letter or a GLFW constant https://www.glfw.org/docs/latest/input_guide.html
|
inlineprotectedvirtual |
Override this method to respond to key presses (button up)
key | The key, e.g. as either a constant or capital ascii letter, such as 'P'. |
mods | Modifiers that are pressed (e.g. shift, control, etc) |
https://www.glfw.org/docs/latest/group__keys.html
|
inlineprotected |
Loads a texture to be shown on a sprite.
name | The name tp associate with the texture |
filename | The file containing the texture image |
This function should be called once during setup()
|
inlineprotectedvirtual |
Override this method to respond to mouse press (button down)
button | The mouse button that is pressed, e.g. GLFW_MOUSE_BUTTON_LEFT |
mods | Modifiers that are pressed (e.g. shift, control, etc) |
https://www.glfw.org/docs/latest/input_guide.html
|
inlineprotected |
Query whether the given mouse button is down.
button | The button to test |
More than one button can be pressed at once. The button should be a GLFW constant, such as GLFW_MOUSE_BUTTON_LEFT https://www.glfw.org/docs/latest/input.html
|
inlineprotectedvirtual |
Override this method to respond to mouse movement.
x | The current x position |
y | The current y position |
dx | The change in x since the last mouse event (in pixels) |
dy | The change in y since the last mouse event (in pixels) |
Screen coordinates are in pixels. x values are always between 0 and width. y values are always between 0 and height.
|
inlineprotectedvirtual |
Override this method to respond to mouse press (button up)
|
inlineprotected |
|
inline |
Opens the window and starts the main application loop.
This function should typically called from main after creating the Window object. This function invokes the user's setup() function and then repeatedly calls the user's draw() function. This function returns when the user closes the window (via the escape key or close menu button)
|
inlineprotectedvirtual |
Override this method to respond to scrolling the middle mouse button.
dx | The change in the x direction (in scroll coordinates) |
dy | The change in the x direction (in scroll coordinates) |
|
inlineprotected |
Draws a sprite with the current color in pixel coordinates.
textureName | The texture to show on the sprite. |
x | The horizontal position of the center |
y | The vertical position of the center |
scale | Specify a scale to display the sprite as larger or smaller than the loaded image size. |
Calling this function draws a sprite on the screen at the given (x,y) coordinate. The texture name must have been initialized previously with a call to loadSprite
in setup(). The size of the sprite is determined by the size of the image. For example, a 32x32 sprite image will fill 32x32 pixels on the screen unless it is resized using the scale. For example, setting the scale to 0.5 will display the sprite as 16x16 pixels, or half as big.
|
inlineprotected |
Draws a square with the current color in pixel coordinates.
x | The horizontal position of the center |
y | The vertical position of the center |
width | |
height |
|
inlineprotected |
Draw a triangle with the current color using pixel coordinates.
x | The horizontal position of the center |
y | The vertical position of the center |
width | of the triangle base |
height | of the triangle |