Simple Game Graphics Library  1.0
Functions
Callback setup

Functions

void graphics::setDrawFunction (std::function< void()> draw)
 
void graphics::setUpdateFunction (std::function< void(float)> update)
 
void graphics::setResizeFunction (std::function< void(int, int)> resize)
 

Detailed Description

Function Documentation

◆ setDrawFunction()

void graphics::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;
}

◆ setResizeFunction()

void graphics::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;
}

◆ setUpdateFunction()

void graphics::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;
}
graphics::setResizeFunction
void setResizeFunction(std::function< void(int, int)> resize)
graphics::Brush
Definition: graphics.h:33
graphics::startMessageLoop
void startMessageLoop()
graphics::setUpdateFunction
void setUpdateFunction(std::function< void(float)> update)
graphics::destroyWindow
void destroyWindow()
graphics::drawLine
void drawLine(float x1, float y1, float x2, float y2, const Brush &brush)
graphics::getKeyState
bool getKeyState(scancode_t key)
graphics::createWindow
void createWindow(int width, int height, std::string title)
graphics::setDrawFunction
void setDrawFunction(std::function< void()> draw)