C++ Ball Physics Tutorial 1 - SFML Setup

In this tutorial we will be creating a Ball Physics Simulator with ball collisions in C++. We will be using the library SFML to accomplish this. In this part, I'm going to show you how to install it for Visual Studio, so feel free to skip this part if you're already familiar with SFML and know how to get started. This is what we will be creating:

                       
                       

 

 

Installing SFML

Fire up your favorite web browser and navigate to https://www.sfml-dev.org/download.php. Click on the latest stable version, which is SFML 2.5.1 at the time i'm typing this. Now, the compiler versions have to match, but if you're using visual studio 2019, it works perfectly fine with the SFML version for Visual C++ 15 (2017). When choosing between the 32 or 64-bit library, it's based on which platform you want to compile for, and not which OS you have. To get the largest audience, download the 32-bit version. In the downloaded folder, we will only care about three subfolders. Namely,  bin, include and lib.

Open Visual Studio and create a new Empty Project, I'm naming my project Ball Physics Simulator. Create a new source file and name it Main.cpp. Create a main method and run the project. This is an important step, without the main file we won't be given all of the options we need in properties later, and when we run the project it creates a Debug folder that we need. Right click on the solution and then click on Open Folder In File Explorer, as shown in the image below.

                                                           

Here, we want to create a new folder called sfml. Copy and paste the folders bin, include and lib from the folder we downloaded before into this folder. Navigate to include and copy every file that ends with d-2.dll, plus openal32.dll. Paste the files in the Debug folder in the solution level.

                            

Done? Great! Now, in Visual Studio, right click on the project name, which is located just below the solution, and click on Properties. First thing we want to do in this window, is to make sure that all changes we make affect all configurations, not only debug or only release. To do this, we click on the dropdown in the top left, and simply choose the option All Configurations.

                                                           

Click on C/C++ (1) in the left sidebar. Next to Additional Include Directories, there's an arrow down. Click on it and then click on Edit (2). This will make make another window pop up. Click on the yellow folder to add a new line (3), and then click on the ellipsis (4) that shows up.

                                 

This will open up your explorer, navigate to the sfml folder we created and select include. Apply.

Click on Linker in the sidebar, and do the same things again, but with the lib folder this time. Click on the arrow down next to Additional Library Directories and then Edit again. Yellow folder, ellipsis, and select the lib folder.

Under Linker, click on Input, and click on the arrow -> Edit next to Additional Dependencies. Copy and paste this in the textarea:

sfml-system.lib
sfml-graphics.lib
sfml-audio.lib
sfml-network.lib
sfml-window.lib

Apply and change the configuration to Debug and open the same textarea again, add -d after every line:

sfml-system-d.lib
sfml-graphics-d.lib
sfml-audio-d.lib
sfml-network-d.lib
sfml-window-d.lib

 

To see if everything is set up correctly, paste this code in your Main.cpp.

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen()) {
        sf::Event event;

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

When you run this program, it should show you this window:

                                                                            

And that's it! If you run into any problems, see if https://www.sfml-dev.org/tutorials/2.5/start-vc.php can help you. In the next part we will create some building blocks for the logic.

Enjoyed this article? Give the teacher an apple.

cookie

0

Author

authors profile photo

Articles with similar tags

Comments