Simple Game Graphics Library 1.1
Callback setup

Functions

void setDrawFunction (std::function< void()> draw)
void setPreDrawFunction (std::function< void()> predraw)
void setPostDrawFunction (std::function< void()> postdraw)
void setUpdateFunction (std::function< void(float)> update)
void setResizeFunction (std::function< void(int, int)> resize)

Detailed Description

Function Documentation

◆ setDrawFunction()

void setDrawFunction ( std::function< void()> draw)

Specifies a user-defined function to be called each time the window is to be redisplayed.

The custom draw function is the only one that should contain all SGG draw functions to be used for painting the contents of the canvas. Drawing occurs a) when a window size change is reported, b) at predetermied and equally spaced intervals, triggered by the event processing loop of the SGG engine.

Parameters
drawis the user-defined draw function passed.

Typical usage:

void draw()
{
the_game.render(); // here, "the_game" is a global static variable (class instance) of a custom
// class that encapsulates the logic and state of a game.
// We call its render() method to perform all relevant drawing.
// In addition, here we also add some extra drawing commands on top.
graphics::drawLine(10.0f, 10.0f, 400.0f, 10.0f, br);
}
void update(float ms)
{
... // update is a user-defined function to perform all
// user input polling and application state updates.
}
int main()
{
graphics::createWindow(960, 540, "Night of the Living Arkanoid");
... // application initialization code.
return 0;
}
void setDrawFunction(std::function< void()> draw)
void setUpdateFunction(std::function< void(float)> update)
void drawLine(float x1, float y1, float x2, float y2, const Brush &brush)
void startMessageLoop()
void createWindow(int width, int height, std::string title)
void destroyWindow()
Definition graphics.h:33

◆ setPostDrawFunction()

void setPostDrawFunction ( std::function< void()> postdraw)

Specifies a function to call after entering the 2D rendering draw call.

The post-draw function is useful when one needs to render 3D content or call custom rendering functionality that is going to write over the 2D canvas typically generated with SGG functions. For example, one can use SGG to draw a GUI and draw a custom video overlay as a post-draw operation over some part of it.

Parameters
postdrawis the user-defined draw function passed to call.

◆ setPreDrawFunction()

void setPreDrawFunction ( std::function< void()> predraw)

Specifies a function to call prior to entering the 2D rendering draw call.

The pre-draw function is useful when one needs to render 3D content or call custom rendering functionality prior to drawing a 2D overlay over it. For example, one can use typical 3D graphics to render a 3D game view and overlay the 3D view with a GUI created with SGG.

Parameters
predrawis the user-defined draw function passed to call.

◆ setResizeFunction()

void setResizeFunction ( std::function< void(int, int)> resize)

Specifies a user-defined function to be called each time the window is resized.

Typically, there is no need to call this function in most situations, as the dimensions of the window and the drawing canvas are independent. Still, in some case, one may want to change what is displayed according to the available screen area, or adapt the aspect ratio of the canvas to match that of the host window.

Parameters
resizeis the user-defined function passed. The callback function must accept two int parameters, the new width and height of the window.

Potential usage:

void resize(int new_w, int new_h)
{
the_game.adjustCanvas(new_w, new_h); // here, "the_game" is a global static variable (class instance) of a custom
// class that encapsulates the logic and state of a game.
// We call its adjustCanvas() method here to rescale the canvas so that it
// matches the aspect ratio of the window.
}
int main()
{
...
graphics::createWindow(960, 540, "Night of the Living Arkanoid");
...
return 0;
}
void setResizeFunction(std::function< void(int, int)> resize)

◆ setUpdateFunction()

void setUpdateFunction ( std::function< void(float)> update)

Specifies a user-defined function to be called each time the state of the application needs to be updated.

This callback function is invoked a) when an input event is triggered in the event processing loop of the engine, such as mouse state change or keyboard key press, b) at regularly timed intervals, so that time-dependent states in the application (such as animations, collisions, simulations) may advance their values.

Typically, in the update callback function the developer places code for polling the input devices and using the time increment (passed as an argument) to update the application mechanics. Keep also in mind that the time increment passed in the callback is also globally available thoughout the code through the getDeltaTime() function.

Parameters
updateis the user-defined function passed. The callback function must accept a single parameter, the time passed in miliseconds from the last triggered update.

Typical usage:

void draw()
{
... // includes all the draw calls for repainting the canvas.
}
bool toggle_help = false;
void update(float ms)
{
the_game.update(); // here, "the_game" is a global static variable (class instance) of a custom
// class that encapsulates the logic and state of a game.
// We call its update() method to perform all relevant state changes in internal fields.
// Please note here that the time increment ms is not passed to the update() method,
// as it internally calls the graphics::getDeltaTime() itself for this purpose.
// In addition, check whether the user pressed the F1 function key.
if (graphics::getKeyState(graphics::SCANCODE_F1))
{
// If pressed, toggle a global variable that enables the display of a help message overlay.
toggle_help = !toggle_help;
}
}
int main()
{
graphics::createWindow(960, 540, "Night of the Living Arkanoid");
... // application initialization code.
return 0;
}
bool getKeyState(scancode_t key)