<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sami Hamrouni on Medium]]></title>
        <description><![CDATA[Stories by Sami Hamrouni on Medium]]></description>
        <link>https://medium.com/@sami.hmr?source=rss-96461aefb5e8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eL1uvZiiUg6zvk0mKaJ-fQ.jpeg</url>
            <title>Stories by Sami Hamrouni on Medium</title>
            <link>https://medium.com/@sami.hmr?source=rss-96461aefb5e8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 09:13:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sami.hmr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to CSFML Graphical Programming]]></title>
            <link>https://medium.com/@sami.hmr/introduction-to-csfml-graphical-programming-c8aa995c7b18?source=rss-96461aefb5e8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8aa995c7b18</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[basics]]></category>
            <category><![CDATA[sfml]]></category>
            <dc:creator><![CDATA[Sami Hamrouni]]></dc:creator>
            <pubDate>Sun, 20 Oct 2024 02:00:29 GMT</pubDate>
            <atom:updated>2024-10-20T11:55:53.928Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*FoA3hfW-OA7vv8v6.jpg" /></figure><p>If you are a first year student at EPITECH, you will have the chance to work with the best graphical library of all time : CSFML.</p><p>CSFML is a C port of the SFML library in C++, and is used by a total of 12 people including me. Of course there is like 2 tutorials on the internet, that&#39;s why i&#39;m here helping you using this wonderful graphical library</p><h3>💻Openning a window</h3><p>First, create some parameters for your <a href="https://26.customprotocol.com/csfml/Graphics_2Types_8h.htm#a148019f5ac4a52bcfef172ebe66d6477"><strong>window</strong></a>, like the size with the <a href="https://26.customprotocol.com/csfml/structsfVideoMode.htm"><strong>mode</strong></a>, the title or the settings. <br>Create your window using the <a href="https://csfml.1l.is/api/Graphics/RenderWindow.html#c.sfRenderWindow_create"><strong>SfRenderWindow_create(</strong></a><strong>)</strong> function.<br>To keep it oppened, loop using the <a href="https://csfml.1l.is/api/Graphics/RenderWindow.html#c.sfRenderWindow_isOpen"><strong>SfRenderWindow_isOpen()</strong></a> function</p><pre>#include &lt;SFML/Graphics.h&gt;<br><br>int main(void)<br>{<br>    sfVideoMode mode = {800, 600, 32};<br>    sfUint32 style = {sfClose | sfResize};<br>    sfRenderWindow *window = sfRenderWindow_create(mode, &quot;My Window&quot;, style, NULL);<br>    sfEvent event = {0};<br><br>    while (sfRenderWindow_isOpen(window)) {<br>        continue;<br>    }<br>}</pre><p>Compile your main.c or whatever c file you have with this command</p><pre>gcc &lt;your.c&gt; -lcsfml-graphics -lcsfml-window -lcsfml-system -lcsfml-audio</pre><p>These flags are necessary if you want your program to work.<br>Now execute your binary (./a.out), and voila, you have your first window.<br>One problem, you cant do anything, and depending on your computer, you might have a “My Window has stopped responding”.</p><blockquote>😕 Why is my window not responding ???</blockquote><p>This is because in CSFML, you have to do <strong>everything, </strong>like testing the events to close the window for instance.</p><h3>📊Managing events</h3><p>Every time you press a key on your keyboard, you try to close your window, or you press a button on your mouse, it triggers an <a href="https://www.sfml-dev.org/tutorials/2.6/window-events.php">event</a> stored in a queue that you can access with the function <a href="https://csfml.1l.is/api/Graphics/RenderWindow.html#c.sfRenderWindow_pollEvent"><strong>SfRenderWindow_pollEvent()</strong></a>. This function returns <strong>True</strong> if there is an event or more in the queue and stores it in a pointer, or <strong>False</strong> otherwise. It also takes two parameters: the window it refers to, and a pointer that will store the first event.<br>Let’s make a loop that looks for the sfClose event and actually make it close the window.</p><blockquote>💡 Here is the <a href="https://26.customprotocol.com/csfml/Event_8h.htm#a18e8028e83dbf54a65b18b465634e2f9"><strong>complete list of event types</strong></a>.</blockquote><pre>#include &lt;SFML/Graphics.h&gt;<br><br>int main(void)<br>{<br>    sfVideoMode mode = {800, 600, 32};<br>    sfUint32 style = {sfClose | sfResize};<br>    sfRenderWindow *window = sfRenderWindow_create(mode, &quot;My Window&quot;, style, NULL);<br>    sfEvent event = {0};<br><br>    while (sfRenderWindow_isOpen(window)) {<br>        while (sfRenderWindow_pollEvent(window, &amp;event)) {<br>            if (event.type == sfEvtClosed)<br>                sfRenderWindow_close(window);<br>        }<br>    }<br>}</pre><p>You have now a fully functionnal openning and closing window !</p><h3>🖼️Displaying elements</h3><p><strong>Now it is time we learn about rendering, and precisely render images on your window.</strong></p><p>First you have to remember that every time you draw something on your window, you have to refresh it so the changes will apply.<br>You can refresh your window using the<strong> </strong><a href="https://csfml.1l.is/api/Graphics/RenderWindow.html#c.sfRenderWindow_display"><strong>SfRenderWindow_display(</strong>)</a> function. Now let’s create our first Sprite.</p><blockquote>💡 A sprite is a two-dimensional image or animation that is integrated into a larger scene</blockquote><p>You can create your sprite using the <a href="https://csfml.1l.is/api/Graphics/Sprite.html#c.sfSprite_create"><strong>SfSprite_create(</strong></a><strong>) </strong>function, it returns a pointer to a <strong>SfSprite</strong> value;</p><p>You can now create a texture from an image that you will apply to your sprite using the <a href="https://csfml.1l.is/api/Graphics/Texture.html#c.sfTexture_createFromFile"><strong>sfTexture_createFromFile()</strong></a> function. This function takes 2 parameters, the path to your image, and the area of the image you want to use (set it to NULL to use the whole image).</p><blockquote>🤓 You can create textures from various sources, but here we’ll focus on loading images from your computer</blockquote><p>Assign your texture to your sprite using the <a href="https://csfml.1l.is/api/Graphics/Sprite.html#c.sfSprite_setTexture"><strong>SfSprite_setTexture()</strong></a> function, this function takes 2 parameters, the sprite you want to be modified, and the texture you want to apply to this sprite.</p><p>You can now draw your sprite using the <a href="https://csfml.1l.is/api/Graphics/RenderWindow.html#c.sfRenderWindow_drawSprite"><strong>SfRenderWindow_drawSprite()</strong></a> function, <strong>dont forget to refresh your window !</strong></p><pre>#include &lt;SFML/Graphics.h&gt;<br><br>sfSprite *create_sprite(char *filepath) //Returns a sprite with the texture of the image path put in parameter<br>{<br>    sfSprite *sprite = sfSprite_create();<br>    sfTexture *texture = sfTexture_createFromFile(filepath, NULL);<br><br>    sfSprite_setTexture(sprite, texture, sfTrue);<br>    return sprite;<br>}<br><br>int main(void)<br>{<br>    sfVideoMode mode = {800, 600, 32};<br>    sfUint32 style = {sfClose | sfResize};<br>    sfRenderWindow *window = sfRenderWindow_create(mode, &quot;My Window&quot;, style, NULL);<br>    sfEvent event = {0};<br>    sfSprite *first_sprite = create_sprite(&quot;oizo.png&quot;); //We create our first sprite<br><br>    while (sfRenderWindow_isOpen(window)) {<br>        while (sfRenderWindow_pollEvent(window, &amp;event)) {<br>            if (event.type == sfEvtClosed)<br>                sfRenderWindow_close(window);<br>        }<br>        sfRenderWindow_drawSprite(window, first_sprite, NULL);<br>        sfRenderWindow_display(window);<br>    }<br>}</pre><p>And here is the result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*650fvacblY4F08Ei8Se_hQ.png" /></figure><blockquote>💡 You can adjust the scale, position and rotation of your sprite using functions like <a href="https://csfml.1l.is/api/Graphics/Sprite.html#c.sfSprite_setScale"><strong>sfSprite_setScale()</strong></a><strong>, </strong>read the <a href="https://csfml.1l.is/api/Graphics/Sprite.html#sprite"><strong>CSFML documentation about sprites</strong></a> for more !</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8aa995c7b18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>