Download Tutorial C Files H Files

Tutorial : main functions

You are following a tutorial that teach you the basics of LIBDRAGON. You'll learn how to create a window and use it.

launch a window

With LIBDRAGON, creating a window is really easy just use the dg_play function :

#include "libdragon.h"

int main(void)
{
    dg_play(1920, 1080, "LibDragon", 200);
    return 0;
}

DO NOT forget to include the libdragon !
Here are the parameters :

  1. 1920 : width of the window, in pixels
  2. 1080 : height of the window, in pixels
  3. "libDragon" : the name of the window, displayed on the bar.
  4. 200 : the ID of the window, usefull to distinguish one from another.
If you try the code,you'll get undefined references ! You need to add 3 functions : You can add them empty to try your code, those functions will be explained later on :

#include "libdragon.h"

void *dg_init(dg_window_t *w)
{
    return 0;
}

int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
    return 0;
}

void dg_end(void *data, int id)
{
}

int main(void)
{
    dg_play(1920, 1080, "LibDragon", 200);
    return 0;
}

You can now try the code !

init the window

dg_init is a function executed before launching the window, only once. Use it to create variables and load ressources !
Here is the prototype :

void *dg_init(dg_window_t *w);

The init function take a dg_window_t *.
Here is the structure :

typedef struct dg_window
{
    sfRenderWindow *window;
    dg_framebuffer_t *fb;
    int id;

} dg_window_t;

As you can see, the dg_window_t contain a sfRenderWindow wich can be used to get, or set data about the window (check the CSFML lib for more information).

There is also a dg_framebuffer_t, check the framebuffer tutorial for more information.

Finally, there is the ID, use it to act differently with your windows.

You can use all of those data to initialize your window.



The dg_init function return a void *, wich mean any pointer : the data returned will be passed to the dg_loop function.
The best way to pass data is to create a structure with all your data and return it.

Let's try out !

typedef struct data {
    int circle_size;
    int pos_x;
} data_t;

void *dg_init(dg_window_t *w)
{
    data_t *data = malloc(sizeof(data_t));

    data->circle_size = w->id;
    data->pos_x = 0;
    return data;
}

With this code, the int circle_size and the int pos_x will be passed to the window loop throught the data structure.

loop the window

dg_loop is a function executed in loop as long as the window is open.
Use it to put your main program.
Here is the prototype :

int dg_loop(dg_window_t *w, void *data, sfTime dt);

The loop function take a dg_window_t *. We already covered it before.

Next is the data passed with the init function.

Finally is deltaTime, or the time passed between two loop/frames.

Let's draw a moving circle :

int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
    data_t *d = (data_t *)(data);
    dg_fb_putrectangle(w->fb, (sfVector2u){0}, (sfVector2u){1920, 1080}, sfBlack);
    dg_fb_putcircle(w->fb, (sfVector2u){d->pos_x, 540}, d->circle_size, sfWhite);
    d->pos_x += dt.microseconds / 4000;
    return 0;
}

end the window

dg_end is a function executed at the end, after the window has been closed.
Use it to destroy your variables.
Here is the prototype :

void dg_end(void *data, int id);

The end function take the data passed by init and the id of the window.

Let's free what we used :

void dg_end(void *data, int id)
{
    free(data);
}

Done, your data are all freed !

The whole program

#include 
#include "libdragon.h"

typedef struct data {
    int circle_size;
    int pos_x;
} data_t;

void *dg_init(dg_window_t *w)
{
    data_t *data = malloc(sizeof(data_t));

    data->circle_size = w->id;
    data->pos_x = 0;
    return data;
}

int dg_loop(dg_window_t *w, void *data, sfTime dt)
{
    data_t *d = (data_t *)(data);

    dg_fb_putrectangle(w->fb, (sfVector2u){0}, (sfVector2u){1920, 1080}, sfBlack);
    dg_fb_putcircle(w->fb, (sfVector2u){d->pos_x, 540}, d->circle_size, sfWhite);
    d->pos_x += dt.microseconds / 4000;
    return 0;
}

void dg_end(void *data, int id)
{
    free(data);
}

int main(int argc, char **argv)
{
    dg_play(1920, 1080, "LibDragon", 200);
    return 0;
}