|
| 1 | +## SFML Setup Guide |
| 2 | + |
| 3 | +The best way to learn C++ is not by reading books or following |
| 4 | +tutorials. The best way to learn is to come up with a project idea and |
| 5 | +build it. And my favorite type of projects are video games! |
| 6 | + |
| 7 | +In this video, we will go over how to set up a C++ project with SFML. |
| 8 | +the Simple Fast Multimedia Library. This is an open source C++ library |
| 9 | +that makes it easy to build games, simulations, and interactive tools. |
| 10 | + |
| 11 | +It is used by popular YouTubers like Pezzza for their AntSimulator, and |
| 12 | +Hopson97 for their MineCraft clone. And since I spent an entire weekend |
| 13 | +learning how to set it up, I figured I could help anyone else trying to |
| 14 | +do the same thing. |
| 15 | + |
| 16 | +Keep in mind that we will go over how to setup SFML on Intel Macs, but |
| 17 | +the steps are almost identical to get it running on Macs with Apple |
| 18 | +silicon. |
| 19 | + |
| 20 | +And of course, if you like this type of content, all I ask in return is |
| 21 | +a like for the YouTube algorithm. With that out of the way, let's get |
| 22 | +started. |
| 23 | + |
| 24 | +### SFML Homebrew Setup Tutorial |
| 25 | + |
| 26 | +Note that there are many different ways to setup and run SFML, this is |
| 27 | +just the approach that helped me get things running very quickly. It boils |
| 28 | +down to 4 simple steps. |
| 29 | + |
| 30 | +1. Install homebrew and SFML (via homebrew). |
| 31 | +2. Create `main.cpp` file with sample SFML code. |
| 32 | +3. Run clang to compile + link the SFML library with the code in `main.cpp`. |
| 33 | +4. Run the executable. |
| 34 | + |
| 35 | +#### 1. Install homebrew and SFML (via homebrew). |
| 36 | + |
| 37 | +```bash |
| 38 | +# install homebrew, the package manager for MacOS |
| 39 | +# if you don't have this already, you're cooked fam |
| 40 | +# (e.g. apt for Ubuntu) |
| 41 | + |
| 42 | +# install SFML onto MacOS with homebrew |
| 43 | +brew install sfml |
| 44 | + |
| 45 | +# On Intel chips, SFML is stored here... |
| 46 | +# /usr/local/Cellar/sfml/3.0.1/ # Actual files |
| 47 | +# /usr/local/opt/sfml/ # Symlink to current version |
| 48 | + |
| 49 | +# On Apple silicon, SFML is stored here... |
| 50 | +# /opt/homebrew/Cellar/sfml/3.0.1/ # Actual files |
| 51 | +# /opt/homebrew/opt/sfml/ # Symlink to current version |
| 52 | +``` |
| 53 | + |
| 54 | +#### 2. Create `main.cpp` file with sample SFML code. |
| 55 | + |
| 56 | +```cpp |
| 57 | +#include <SFML/Graphics.hpp> |
| 58 | + |
| 59 | +// the following are included transitively in <SFML/Graphics.hpp> |
| 60 | +// #include <SFML/System.hpp> |
| 61 | +// #include <SFML/Window.hpp> |
| 62 | + |
| 63 | +int main() { |
| 64 | + sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML with Homebrew Setup Guide"); |
| 65 | + sf::CircleShape shape(100.f); |
| 66 | + shape.setFillColor(sf::Color::Green); |
| 67 | + |
| 68 | + while (window.isOpen()) { |
| 69 | + while (const std::optional event = window.pollEvent()) |
| 70 | + if (event->is<sf::Event::Closed>()) |
| 71 | + window.close(); |
| 72 | + |
| 73 | + window.clear(); |
| 74 | + window.draw(shape); |
| 75 | + window.display(); |
| 76 | + } |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +#### 3. Run clang to compile + link SFML library with the code in `main.cpp`. |
| 83 | + |
| 84 | +```bash |
| 85 | +# on Intel Macs |
| 86 | +clang++ -std=c++17 main.cpp -o sfml-app \ |
| 87 | + -I/usr/local/opt/sfml/include \ |
| 88 | + -L/usr/local/opt/sfml/lib \ |
| 89 | + -lsfml-graphics -lsfml-window -lsfml-system |
| 90 | + |
| 91 | +# on Apple Silicon Macs (haven't tested, but should work...) |
| 92 | +# clang++ -std=c++17 main.cpp -o sfml-app \ |
| 93 | +# -I/opt/homebrew/opt/sfml/include \ |
| 94 | +# -L/opt/homebrew/opt/sfml/lib \ |
| 95 | +# -lsfml-graphics -lsfml-window -lsfml-system |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +#### 4. Run the executable. |
| 100 | + |
| 101 | +```bash |
| 102 | +./sfml-app |
| 103 | +``` |
| 104 | + |
| 105 | +### Conclusion |
| 106 | + |
| 107 | +And that's how you get SFML up and running on Intel Macs. |
| 108 | + |
| 109 | +And now, I have some questions for you. Have you ever struggled with |
| 110 | +getting C++ projects up and running? Should I go over how to do this |
| 111 | +with CMake next? What about setting up ImGui for handling user input? |
| 112 | +Leave anything related to SFML, C++, or computer graphics down in the |
| 113 | +comments below. |
| 114 | + |
| 115 | +Thanks for watching, and I'll catch you guys next time. |
0 commit comments