Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

SFML C++ Basis

I'm posting this here because it's useful to me, so it might be useful to others.

In a visual C++ project with SFML in it, this is your "main", equivalent to the RGSS main.

C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol><span style="color: #339933;">#include <SFML/Graphics.hpp>

<span style="color: #339933;">#include <SFML/Audio.hpp>

 

<span style="color: #993333;">int main()

{

    // the game object

    Game game;

 

    // Create main window

    sf::<span style="color: #202020;">RenderWindow App(sf::<span style="color: #202020;">VideoMode(<span style="color: #cc66cc;">800, <span style="color: #cc66cc;">600), "Fantasia");

 

    // Create a clock for measuring the time elapsed

    sf::<span style="color: #202020;">Clock Clock1;

    

    // Start game loop

    <span style="color: #b1b100;">while (App.<span style="color: #202020;">IsOpened())

    {

        <span style="color: #993333;">float delay=Clock1.<span style="color: #202020;">GetElapsedTime();

        // Process events

        sf::<span style="color: #202020;">Event Event;

        <span style="color: #b1b100;">while (App.<span style="color: #202020;">GetEvent(Event))

        {

            // Close window : exit

            <span style="color: #b1b100;">if (Event.<span style="color: #202020;">Type == sf::<span style="color: #202020;">Event::<span style="color: #202020;">Closed)

                App.<span style="color: #202020;">Close();

            

            // Key press

            <span style="color: #b1b100;">if (Event.<span style="color: #202020;">Type == sf::<span style="color: #202020;">Event::<span style="color: #202020;">KeyPressed)

            {

                <span style="color: #b1b100;">if(Event.<span style="color: #202020;">Key.<span style="color: #202020;">Code == sf::<span style="color: #202020;">Key::<span style="color: #202020;">Escape)// Escape key : exit

                {App.<span style="color: #202020;">Close();}

                <span style="color: #b1b100;">else // Pass key press to game

                { game.<span style="color: #202020;">keyHandler(Event.<span style="color: #202020;">Key.<span style="color: #202020;">Code); }

            }

            

        }

 

        //play the game every tenth of a second

        <span style="color: #b1b100;">if(delay><span style="color: #cc66cc;">0.1) {

            game.<span style="color: #202020;">play();

            Clock1.<span style="color: #202020;">Reset();

        }

 

        //Draw the game

        game.<span style="color: #202020;">draw();

 

        // Finally, display the rendered frame on screen

        App.<span style="color: #202020;">Display();

    }

  

 

    <span style="color: #b1b100;">return EXIT_SUCCESS;

 

}

 

Provided by my uni and slimmed down to remove unnecessary bits.

All you need to do now is create a Game class, which has the functions:

draw();
keyHandler(keyCode);
play();

Consider play to be equivalent to RGSS's "refresh".
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top