Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to get started with CSFML?  (Read 6684 times)

0 Members and 1 Guest are viewing this topic.

Fabrizio Caruso

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
How to get started with CSFML?
« on: January 08, 2022, 03:04:22 pm »
Hi everyone!

How can I get started with CSFML? I have downloaded and uncompressed CSFML Windows 64-bit. I am using Cygwin as my main environment.

I supposed I need to have CSFML location in the PATH variable.

Could someone please give a trivial helloworld example in ANSI C (no C++) and the exact command line to compile it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11007
    • View Profile
    • development blog
    • Email
Re: How to get started with CSFML?
« Reply #1 on: January 08, 2022, 07:45:23 pm »
I can't really give you any command lines, but there are tens of thousands of examples on the internet how to compile C code and how to link a library.
Also this is not ANSI C but just regular C. ;)

The documentation of CSFML contains this example:

#include <SFML/Audio.h>
#include <SFML/Graphics.h>

int main()
{
    sfVideoMode mode = {800, 600, 32};
    sfRenderWindow* window;
    sfTexture* texture;
    sfSprite* sprite;
    sfFont* font;
    sfText* text;
    sfMusic* music;
    sfEvent event;

    /* Create the main window */
    window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);
    if (!window)
        return EXIT_FAILURE;

    /* Load a sprite to display */
    texture = sfTexture_createFromFile("cute_image.jpg", NULL);
    if (!texture)
        return EXIT_FAILURE;
    sprite = sfSprite_create();
    sfSprite_setTexture(sprite, texture, sfTrue);

    /* Create a graphical text to display */
    font = sfFont_createFromFile("arial.ttf");
    if (!font)
        return EXIT_FAILURE;
    text = sfText_create();
    sfText_setString(text, "Hello SFML");
    sfText_setFont(text, font);
    sfText_setCharacterSize(text, 50);

    /* Load a music to play */
    music = sfMusic_createFromFile("nice_music.ogg");
    if (!music)
        return EXIT_FAILURE;

    /* Play the music */
    sfMusic_play(music);

    /* Start the game loop */
    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;
}
 

Personally, I still recommend you to use C++ and SFML itself, especially if you have little experience with C or the toolchain. It won't be much easier, but you'll find more resources for it, especially in combination with SFML.
« Last Edit: January 09, 2022, 01:14:22 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fabrizio Caruso

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: How to get started with CSFML?
« Reply #2 on: January 09, 2022, 12:27:48 am »
Thanks!

I think your example is incomplete as it has no includes. Where can I find the full example?

I know how to link a static library (gcc ... -l<name of the lib without lib prefix>).
So I suppose I will have to statically link all the .a files (or most of them).

Where can I find the documentation? It is not included in the file I downloaded.

I cannot use C++ for my main project because it is written in C and there are no C++ compilers for 99% of the targets I am interested in:
https://github.com/Fabrizio-Caruso/CROSS-LIB

I could use C++ for other projects.
« Last Edit: January 09, 2022, 12:40:44 am by Fabrizio Caruso »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11007
    • View Profile
    • development blog
    • Email
Re: How to get started with CSFML?
« Reply #3 on: January 09, 2022, 01:16:41 pm »
Forgot to include the two header includes and added them now, but you should really have been able to figure this out on your own. ;)

If your traget system doesn't have a C++ compiler, then you also won't be able to use CSFML for those targets.

You can generate the documentation for CSFML with CMake, doxygen and the source code: https://github.com/SFML/CSFML
« Last Edit: January 09, 2022, 01:21:16 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fabrizio Caruso

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: How to get started with CSFML?
« Reply #4 on: January 09, 2022, 05:20:06 pm »
Thanks! Indeed I had figured out the includes.

Thanks for the github link. This is where I got the zip file with CSML which comes with no documentation.
I now have to figure out if and how it can be used in Cygwin.
Otherwise I will switch to Linux.

Fabrizio Caruso

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: How to get started with CSFML?
« Reply #5 on: January 13, 2022, 11:20:29 am »
The example above won't compile with my setup...
What do I need to use CSFML? Which compilers should I use?
GCC under Cygwin won't work probably because it targets Cygwin (I get an error about the system not being supported).
I have also tried MINGW which targets Windows and I get these error:

x86_64-w64-mingw32-gcc test.c -o test   -I../CSFML-2.5.1-windows-64-bit/include/  -L../CSFML-2.5.1-windows-64-bit/lib/gcc/ -lcsfml-window -lcsfml-graphics -lcsfml-system
In file included from ../CSFML-2.5.1-windows-64-bit/include/SFML/Window/Window.h:32,
                 from ../CSFML-2.5.1-windows-64-bit/include/SFML/Window/Context.h:33,
                 from ../CSFML-2.5.1-windows-64-bit/include/SFML/Window.h:34,
                 from ../CSFML-2.5.1-windows-64-bit/include/SFML/Graphics.h:32,
                 from test.c:2:
../CSFML-2.5.1-windows-64-bit/include/SFML/Window/Event.h:132:1: warning: type is deprecated [-Wdeprecated-declarations]
  132 | } sfMouseWheelEvent;
      | ^
../CSFML-2.5.1-windows-64-bit/include/SFML/Window/Event.h:126:9: note: declared here
  126 | typedef struct CSFML_DEPRECATED
      |         ^~~~~~
test.c: In function ‘main’:
test.c:18:16: error: ‘EXIT_FAILURE’ undeclared (first use in this function)
   18 |         return EXIT_FAILURE;
      |                ^~~~~~~~~~~~
test.c:18:16: note: each undeclared identifier is reported only once for each function it appears in
test.c:76:12: error: ‘EXIT_SUCCESS’ undeclared (first use in this function)
   76 |     return EXIT_SUCCESS;
      |            ^~~~~~~~~~~~
make: *** [Makefile:8: test] Error 1

(EXIT_SUCCESS and FAILURE is just one more missing include with their defs).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11007
    • View Profile
    • development blog
    • Email
Re: How to get started with CSFML?
« Reply #6 on: January 13, 2022, 11:41:15 am »
EXIT_FAILURE and EXIT_SUCCESS are defined in the stdlib.h, as a simple google search reveals. The other thing is a warning, you can alternatively use sfMouseScrollWheelEvent, see the C++ SFML documentation for more details.

Your linking order is wrong, it should follow the rule of thumb: If X depends on Y, then X has to come before Y
csfml-graphics depends on csfml-window, so csfml-graphics needs to be listed first
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fabrizio Caruso

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: How to get started with CSFML?
« Reply #7 on: January 13, 2022, 11:33:54 pm »
Thanks! Of course I have figured out EXIT_SUCCESS/FAILURE. I will see if I can find one in the github page.

I was looking for a trivial example that would just compile with the simplest command line, just to get started. I didn't know that window depended on graphics. Thanks for the hint.

By the way, I understand I cannot use CSFML on my other targets. I only want to use it with Windows and/or Linux. Cygwin is my prefered enviroment because it makes my life easier to handle my massively multi-target project (as it is both POSIX and capable of running Windows binaries).
« Last Edit: January 13, 2022, 11:40:50 pm by Fabrizio Caruso »