CSFML : Graphical Programming

Table of contents

A soon-to-be complete and human-readable CSFML API reference.

Reference

Window

Window managing

sfRenderWindow

This is the most basic element of any graphic program : a window.

sfRenderWindow *sfRenderWindow_create(sfVideoMode mode, const char *title, sfUint32 style, const sfContextSettings *settings)

This function will setup a new window, based on the parameters given.

Parameters
mode:

sfVideoMode - The video mode to use (width, height and bits per pixel of the window).

title:

const char * - The title of the window.

style:

sfUint32 - You must specify at least one of the following :

  • sfNone : None of the other style options will be used.
  • sfResize : Will add a “resize” button that will allow you to change the size of the window.
  • sfClose : Will add a “close” button that will allow you to close your window.
  • sfFullscreen : Will make your window full screen.

If you want to add two or more parameters, simply separate them using pipes (see example below).

settings:

sfContextSettings * - Those are advanced render settings you can add. NULL to use default values.

Return
sfRenderWindow * - New render window.
sfRenderWindow *sfRenderWindow_createUnicode(sfVideoMode mode, const sfUint32 *title, sfUint32 style, const sfContextSettings *settings)

This function takes the same parameters as sfRenderWindow_create(), but this will take an array of integers as title, allowing you to have a unicode title.

sfVideoMode mode = {800, 600, 32};
window = sfRenderWindow_create(mode, "My awesome window", sfResize | sfClose, NULL)
void sfRenderWindow_destroy(sfRenderWindow *window)

This function allows you to destroy an existing window.

Parameter
window:sfRenderWindow * - The render window to destroy.
void sfRenderWindow_close(sfRenderWindow *window)

This function will close a render window (while not destroying it’s internal data.)

Parameter
window:sfrenderWindow * - The RenderWindow to close.
void sfRenderWindow_clear(sfRenderWindow *window, sfColor color)

This function will clear all pixels in the window, replacing them with the given color.

Parameters
window:sfRenderWindow * - The RenderWindow to clear.
color:sfColor - The color to which all pixel will change.
void sfRenderWindow_display(sfRenderWindow *window)

This function will display all sprites on screen.

Parameter
window:sfRenderWindow * - The RenderWindow to display.

Getting window data

There are a lot of data that can be obtained from a sfRenderWindow object. As I don’t want to spend my whole life writing this paragraph while knowing that no one will ever read this, I’ll only list a bunch of useful functions. I’ll probably add the others sooner or later.

sfVector2u sfRenderWindow_getSize(const sfRenderWindow *window)

This function allows you to know the size of a given render window.

Return
sfVector2u - Contains the size of the window in pixels.
sfBool sfRenderWindow_hasFocus(sfRenderWindow *window)
Return
sfBool - Will be sfTrue if the window has focus (i.e. it can receive inputs), sfFalse otherwise. This function can be useful if you want to pose your program if the window doesn’t have focus.
sfBool sfRenderWindow_isOpen(sfRenderWindow *window)

Tell whether or not a render window is open.

Return
sfBool - Will be sfTrue if the window is open, sfFalse otherwise.

Other useful options

Here are all other functions that I (Oursin) find useful for our first projects.

sfBool sfRenderWindow_pollEvent(sfRenderWindow *window, sfEvent *event)

This function will check the event queue and pop one. As such, if you want your program to take all inputs into account, it is highly advised to empty the event queue at the start of your game loop.

Parameters
window:sfRenderWindow * - The target window.
event:sfEvent * - This must take a pointer to the sfEvent object which will be filled.
Return
A sfBool will be returned, indicating whether or not an event was returned.
void sfRenderWindow_setFramerateLimit(sfRenderWindow *window, unsigned int limit)

This function allows you to set your program’s framerate.

Parameters
window:sfRenderWindow - Target render window.
limit:unsigned int - Defines the maximum number of frames your program should display each second.
void sfRenderWindow_setKeyRepeatEnabled(sfRenderWindow *window, sfBool enabled)

This function enables or disables automatic key-repeat for keydown events. If enabled, a key pressed down will create new sfEvent objects all time until it is released.

Parameters
window:sfRenderWindow - Target render window.
enabled:sfBool - sfTrue to enable, sfFalse to disable.
void sfRenderWindow_setMouseCursorVisible(sfRenderWindow *window, sfBool show)

This function allows you to hide the mouse cursor on a render window.

Parameters
window:sfRenderWindow - Target render window.
show:sfBool - sfTrue to show, sfFalse to hide.

Drawing

In CSFML, there are 4 types of objects that can be displayed, 3 of them beign ready to be used : sprites, text and shapes. The other one, vertex arrays, is designed to help you create your own drawable entities, but you would probably not use it for now.

void sfRenderWindow_drawSprite(sfRenderWindow *window, const sfSprite *sprite, sfRenderStates *states)
Parameters
window:sfRenderWindow * - The window to draw to.
sprite:sfSprite * - The sprite to draw.
states:sfRenderStates * - This can be used to use advanced render options, such as shaders, transfomations etc…
void sfRenderWindow_drawText(sfRenderWindow *window, sfText *text, sfRenderStates *states)
Parameters
window:sfRenderWindow * - The window to draw to.
sprite:sfText * - The text object to display on screen. Note that this is not a char *, but an sfText object, which must be created first.
states:sfRenderStates * - This can be used to use advanced render options, such as shaders, transfomations etc…
void sfRenderWindow_drawShape(sfRenderWindow *window, sfShape *shape, sfRenderStates *states)
Parameters
window:sfRenderWindow * - The window to draw to.
sprite:sfShape * - The shape object to display on screen.
states:sfRenderStates * - This can be used to use advanced render options, such as shaders, transfomations etc…

Examples

RenderWindow

Here is a sample example of most functions related to sfRenderWindow. For the following example, we will assume that sprite, text and shape were already created, and are variables of type sfSprite, sfText and sfShape, respectively.

We will also assume that event is of type sfEvent.

sfVideoMode mode = {1080, 720, 32};
sfRenderWindow *window;

window = sfRenderWindow_create(mode, "window", sfClose, NULL);
sfRenderWindow_setFramerateLimit(window, 60);
while (sfRenderWindow_isOpen(window) && sfRenderWindow_hasFocus
                                        (window)) {
        while (sfRenderWindow_pollEvent(window, &event)) {
                if (event.type == sfEvtClosed)
                        sfRenderWindow_close(window);
        }
        sfRenderWindow_clear(window, sfBlack);
        sfRenderWindow_drawSprite(window, sprite, NULL);
        sfRenderWindow_drawShape(window, shape, NULL);
        sfRenderWindow_drawText(window, text, NULL);
        sfRenderWindow_display(window);
}
sfRenderWindow_destroy(window);