File: example.c

package info (click to toggle)
libcsfml 3.0.0~rc3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,236 kB
  • sloc: cpp: 7,741; ansic: 2,616; sh: 791; makefile: 16
file content (87 lines) | stat: -rw-r--r-- 2,336 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <CSFML/Audio.h>
#include <CSFML/Graphics.h>

#include <stdlib.h>

int main(void)
{
    // Create the main window
    const sfVideoMode mode   = {{800, 600}, 32};
    sfRenderWindow*   window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, sfWindowed, NULL);
    if (!window)
        return EXIT_FAILURE;

    // Load a sprite to display
    const sfTexture* texture = sfTexture_createFromFile("sfml_logo.png", NULL);
    if (!texture)
    {
        sfRenderWindow_destroy(window);
        return EXIT_FAILURE;
    }
    sfSprite*        sprite         = sfSprite_create(texture);
    const sfVector2f spritePosition = {200, 200};
    sfSprite_setPosition(sprite, spritePosition);

    // Create a graphical text to display
    const sfFont* font = sfFont_createFromFile("tuffy.ttf");
    if (!font)
    {
        sfSprite_destroy(sprite);
        sfTexture_destroy(texture);
        sfRenderWindow_destroy(window);
        return EXIT_FAILURE;
    }
    sfText* text = sfText_create(font);
    sfText_setString(text, "Hello, SFML!");
    sfText_setCharacterSize(text, 50);

    // Load a music to play
    sfMusic* music = sfMusic_createFromFile("doodle_pop.ogg");
    if (!music)
    {
        sfText_destroy(text);
        sfFont_destroy(font);
        sfSprite_destroy(sprite);
        sfTexture_destroy(texture);
        sfRenderWindow_destroy(window);
        return EXIT_FAILURE;
    }

    // Play the music
    sfMusic_play(music);

    // Start the game loop
    sfEvent event;
    while (sfRenderWindow_isOpen(window))
    {
        // Process events
        while (sfRenderWindow_pollEvent(window, &event))
        {
            // Close window : exit
            if (event.type == sfEvtClosed)
                sfRenderWindow_close(window);
        }

        // Clear the screen
        sfRenderWindow_clear(window, sfBlack);

        // Draw the sprite
        sfRenderWindow_drawSprite(window, sprite, NULL);

        // Draw the text
        sfRenderWindow_drawText(window, text, NULL);

        // Update the window
        sfRenderWindow_display(window);
    }

    // Cleanup resources
    sfMusic_destroy(music);
    sfText_destroy(text);
    sfFont_destroy(font);
    sfSprite_destroy(sprite);
    sfTexture_destroy(texture);
    sfRenderWindow_destroy(window);

    return EXIT_SUCCESS;
}