TinyGL-C++
Single-header, cross-platform, OpenGL 4.0 2D drawing framework
tinygl::Window Class Reference

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.
 

Detailed Description

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.

 

Constructor & Destructor Documentation

◆ Window()

tinygl::Window::Window ( int  width,
int  height 
)
inline

Constructor.

Parameters
widthThe screen width (in pixels)
heightThe screen height (in pixels)

Override this class to create a custom application.

Member Function Documentation

◆ background()

void tinygl::Window::background ( float  r,
float  g,
float  b 
)
inlineprotected

Set the background color.

This function can be called from draw and will additionally clear the color and depth buffers

◆ circle()

void tinygl::Window::circle ( int  x,
int  y,
float  diameter 
)
inlineprotected

Draws a circle with the current color in pixel coordinates.

Parameters
xThe horizontal position of the center
yThe vertical position of the center
diameterThe width of the circle

◆ color()

void tinygl::Window::color ( float  r,
float  g,
float  b,
float  a = 1.0f 
)
inlineprotected

Set the current RGB color.

Parameters
rThe red component (range [0,1])
gThe red component (range [0,1])
bThe red component (range [0,1])

◆ dt()

float tinygl::Window::dt ( ) const
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

◆ ellipsoid()

void tinygl::Window::ellipsoid ( int  x,
int  y,
float  width,
float  height 
)
inlineprotected

Draws an ellipse with the current color in pixel coordinates.

Parameters
xThe horizontal position of the center
yThe vertical position of the center
widthThe width of the ellipse
heightThe height of the ellipse

◆ keyDown()

virtual void tinygl::Window::keyDown ( int  key,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to key presses (button down)

Parameters
keyThe key, e.g. as either a constant or capital ascii letter, such as 'P'.
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/group__keys.html

See also
keyUp(int,int)

◆ keyIsDown()

bool tinygl::Window::keyIsDown ( int  key) const
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

◆ keyUp()

virtual void tinygl::Window::keyUp ( int  key,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to key presses (button up)

Parameters
keyThe key, e.g. as either a constant or capital ascii letter, such as 'P'.
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/group__keys.html

See also
keyDown(int,int)
keyIsDown(int)

◆ loadSprite()

void tinygl::Window::loadSprite ( const std::string &  name,
const std::string &  filename 
)
inlineprotected

Loads a texture to be shown on a sprite.

Parameters
nameThe name tp associate with the texture
filenameThe file containing the texture image

This function should be called once during setup()

◆ mouseDown()

virtual void tinygl::Window::mouseDown ( int  button,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to mouse press (button down)

Parameters
buttonThe mouse button that is pressed, e.g. GLFW_MOUSE_BUTTON_LEFT
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/input_guide.html

See also
mouseUp(int, int)
mouseIsDown(int)

◆ mouseIsDown()

bool tinygl::Window::mouseIsDown ( int  button) const
inlineprotected

Query whether the given mouse button is down.

Parameters
buttonThe 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

◆ mouseMotion()

virtual void tinygl::Window::mouseMotion ( int  x,
int  y,
int  dx,
int  dy 
)
inlineprotectedvirtual

Override this method to respond to mouse movement.

Parameters
xThe current x position
yThe current y position
dxThe change in x since the last mouse event (in pixels)
dyThe 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.

◆ mouseUp()

virtual void tinygl::Window::mouseUp ( int  button,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to mouse press (button up)

See also
mouseDown(int, int)
mouseIsDown(int)

◆ noLoop()

void tinygl::Window::noLoop ( )
inlineprotected

Stop the main application loop.

Call this function to terminate the main loop, either from setup() or draw()

◆ run()

void tinygl::Window::run ( )
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)

◆ scroll()

virtual void tinygl::Window::scroll ( float  dx,
float  dy 
)
inlineprotectedvirtual

Override this method to respond to scrolling the middle mouse button.

Parameters
dxThe change in the x direction (in scroll coordinates)
dyThe change in the x direction (in scroll coordinates)

◆ sprite()

void tinygl::Window::sprite ( const std::string &  textureName,
float  x,
float  y,
float  scale = 1 
)
inlineprotected

Draws a sprite with the current color in pixel coordinates.

Parameters
textureNameThe texture to show on the sprite.
xThe horizontal position of the center
yThe vertical position of the center
scaleSpecify 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.

◆ square()

void tinygl::Window::square ( float  x,
float  y,
float  width,
float  height 
)
inlineprotected

Draws a square with the current color in pixel coordinates.

Parameters
xThe horizontal position of the center
yThe vertical position of the center
width
height

◆ triangle()

void tinygl::Window::triangle ( int  x,
int  y,
float  width,
float  height 
)
inlineprotected

Draw a triangle with the current color using pixel coordinates.

Parameters
xThe horizontal position of the center
yThe vertical position of the center
widthof the triangle base
heightof the triangle

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