Skip to content

Commit c497438

Browse files
authored
Initial commit
0 parents  commit c497438

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
defaults:
6+
run:
7+
shell: bash
8+
9+
jobs:
10+
build:
11+
name: ${{ matrix.platform.name }} ${{ matrix.config.name }}
12+
runs-on: ${{ matrix.platform.os }}
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
platform:
18+
- { name: Windows VS2019, os: windows-2019 }
19+
- { name: Windows VS2022, os: windows-2022 }
20+
- { name: Linux GCC, os: ubuntu-latest }
21+
- { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ }
22+
- { name: macOS, os: macos-latest }
23+
config:
24+
- { name: Shared, flags: -DBUILD_SHARED_LIBS=TRUE }
25+
- { name: Static, flags: -DBUILD_SHARED_LIBS=FALSE }
26+
27+
steps:
28+
- name: Install Linux Dependencies
29+
if: runner.os == 'Linux'
30+
run: sudo apt-get update && sudo apt-get install libxrandr-dev libxcursor-dev libxi-dev libudev-dev libflac-dev libvorbis-dev libgl1-mesa-dev libegl1-mesa-dev libfreetype-dev
31+
32+
- name: Checkout
33+
uses: actions/checkout@v4
34+
35+
- name: Configure
36+
run: cmake -B build ${{matrix.platform.flags}} ${{matrix.config.flags}}
37+
38+
- name: Build
39+
run: cmake --build build --config Release

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build
2+
out
3+
.cache
4+
.idea
5+
.vs
6+
.vscode

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
project(CMakeSFMLProject LANGUAGES CXX)
3+
4+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
5+
6+
include(FetchContent)
7+
FetchContent_Declare(SFML
8+
GIT_REPOSITORY https://github.com/SFML/SFML.git
9+
GIT_TAG 3.0.1
10+
GIT_SHALLOW ON
11+
EXCLUDE_FROM_ALL
12+
SYSTEM)
13+
FetchContent_MakeAvailable(SFML)
14+
15+
add_executable(main src/main.cpp)
16+
target_compile_features(main PRIVATE cxx_std_17)
17+
target_link_libraries(main PRIVATE SFML::Graphics)

LICENSE.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# CMake SFML Project Licenses
2+
3+
*This software is available under 2 licenses -- choose whichever you prefer.*
4+
5+
## Public Domain
6+
7+
This is free and unencumbered software released into the public domain.
8+
9+
Anyone is free to copy, modify, publish, use, compile, sell, or
10+
distribute this software, either in source code form or as a compiled
11+
binary, for any purpose, commercial or non-commercial, and by any
12+
means.
13+
14+
In jurisdictions that recognize copyright laws, the author or authors
15+
of this software dedicate any and all copyright interest in the
16+
software to the public domain. We make this dedication for the benefit
17+
of the public at large and to the detriment of our heirs and
18+
successors. We intend this dedication to be an overt act of
19+
relinquishment in perpetuity of all present and future rights to this
20+
software under copyright law.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28+
OTHER DEALINGS IN THE SOFTWARE.
29+
30+
## MIT License
31+
32+
Copyright (c) 2022 Lukas Dürrenberger
33+
34+
Permission is hereby granted, free of charge, to any person obtaining a copy
35+
of this software and associated documentation files (the "Software"), to deal
36+
in the Software without restriction, including without limitation the rights
37+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38+
copies of the Software, and to permit persons to whom the Software is
39+
furnished to do so, subject to the following conditions:
40+
41+
The above copyright notice and this permission notice shall be included in all
42+
copies or substantial portions of the Software.
43+
44+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50+
SOFTWARE.

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# CMake SFML Project Template
2+
3+
This repository template should allow for a fast and hassle-free kick start of your next SFML project using CMake.
4+
Thanks to [GitHub's nature of templates](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template), you can fork this repository without inheriting its Git history.
5+
6+
The template starts out very basic, but might receive additional features over time:
7+
8+
- Basic CMake script to build your project and link SFML on any operating system
9+
- Basic [GitHub Actions](https://github.com/features/actions) script for all major platforms
10+
11+
## How to Use
12+
13+
1. Install [Git](https://git-scm.com/downloads) and [CMake](https://cmake.org/download/). Use your system's package manager if available.
14+
2. Follow [GitHub's instructions](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template) for how to use their project template feature to create your own project. If you don't want to use GitHub, see the section below.
15+
3. Clone your new GitHub repo and open the repo in your text editor of choice.
16+
4. Open [CMakeLists.txt](CMakeLists.txt). Rename the project and the target name of the executable to whatever name you want. Make sure to change all occurrences.
17+
5. If you want to add or remove any .cpp files, change the source files listed in the `add_executable` call in CMakeLists.txt to match the source files your project requires. If you plan on keeping the default main.cpp file then no changes are required.
18+
6. If your code uses the Audio or Network modules then add `SFML::Audio` or `SFML::Network` to the `target_link_libraries` call alongside the existing `SFML::Graphics` library that is being linked.
19+
7. If you use Linux, install SFML's dependencies using your system package manager. On Ubuntu and other Debian-based distributions you can use the following commands:
20+
```
21+
sudo apt update
22+
sudo apt install \
23+
libxrandr-dev \
24+
libxcursor-dev \
25+
libxi-dev \
26+
libudev-dev \
27+
libfreetype-dev \
28+
libflac-dev \
29+
libvorbis-dev \
30+
libgl1-mesa-dev \
31+
libegl1-mesa-dev \
32+
libfreetype-dev
33+
```
34+
8. Configure and build your project. Most popular IDEs support CMake projects with very little effort on your part.
35+
36+
- [VS Code](https://code.visualstudio.com) via the [CMake extension](https://code.visualstudio.com/docs/cpp/cmake-linux)
37+
- [Visual Studio](https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-170)
38+
- [CLion](https://www.jetbrains.com/clion/features/cmake-support.html)
39+
- [Qt Creator](https://doc.qt.io/qtcreator/creator-project-cmake.html)
40+
41+
Using CMake from the command line is straightforward as well.
42+
Be sure to run these commands in the root directory of the project you just created.
43+
44+
```
45+
cmake -B build
46+
cmake --build build
47+
```
48+
49+
9. Enjoy!
50+
51+
## Upgrading SFML
52+
53+
SFML is found via CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) module.
54+
FetchContent automatically downloads SFML from GitHub and builds it alongside your own code.
55+
Beyond the convenience of not having to install SFML yourself, this ensures ABI compatibility and simplifies things like specifying static versus shared libraries.
56+
57+
Modifying what version of SFML you want is as easy as changing the `GIT_TAG` argument.
58+
Currently it uses SFML 3 via the `3.0.0` tag.
59+
60+
## But I want to...
61+
62+
Modify CMake options by adding them as configuration parameters (with a `-D` flag) or by modifying the contents of CMakeCache.txt and rebuilding.
63+
64+
### Not use GitHub
65+
66+
You can use this project without a GitHub account by [downloading the contents](https://github.com/SFML/cmake-sfml-project/archive/refs/heads/master.zip) of the repository as a ZIP archive and unpacking it locally.
67+
This approach also avoids using Git entirely if you would prefer to not do that.
68+
69+
### Change Compilers
70+
71+
See the variety of [`CMAKE_<LANG>_COMPILER`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER.html) options.
72+
In particular you'll want to modify `CMAKE_CXX_COMPILER` to point to the C++ compiler you wish to use.
73+
74+
### Change Compiler Optimizations
75+
76+
CMake abstracts away specific optimizer flags through the [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html) option.
77+
By default this project recommends `Release` builds which enable optimizations.
78+
Other build types include `Debug` builds which enable debug symbols but disable optimizations.
79+
If you're using a multi-configuration generator (as is often the case on Windows), you can modify the [`CMAKE_CONFIGURATION_TYPES`](https://cmake.org/cmake/help/latest/variable/CMAKE_CONFIGURATION_TYPES.html#variable:CMAKE_CONFIGURATION_TYPES) option.
80+
81+
### Change Generators
82+
83+
While CMake will attempt to pick a suitable default generator, some systems offer a number of generators to choose from.
84+
Ubuntu, for example, offers Makefiles and Ninja as two potential options.
85+
For a list of generators, click [here](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html).
86+
To modify the generator you're using you must reconfigure your project providing a `-G` flag with a value corresponding to the generator you want.
87+
You can't simply modify an entry in the CMakeCache.txt file unlike the above options.
88+
Then you may rebuild your project with this new generator.
89+
90+
## More Reading
91+
92+
Here are some useful resources if you want to learn more about CMake:
93+
94+
- [Official CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/)
95+
- [How to Use CMake Without the Agonizing Pain - Part 1](https://alexreinking.com/blog/how-to-use-cmake-without-the-agonizing-pain-part-1.html)
96+
- [How to Use CMake Without the Agonizing Pain - Part 2](https://alexreinking.com/blog/how-to-use-cmake-without-the-agonizing-pain-part-2.html)
97+
- [Better CMake YouTube series by Jefferon Amstutz](https://www.youtube.com/playlist?list=PL8i3OhJb4FNV10aIZ8oF0AA46HgA2ed8g)
98+
99+
## License
100+
101+
The source code is dual licensed under Public Domain and MIT -- choose whichever you prefer.

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <SFML/Graphics.hpp>
2+
3+
int main()
4+
{
5+
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "CMake SFML Project");
6+
window.setFramerateLimit(144);
7+
8+
while (window.isOpen())
9+
{
10+
while (const std::optional event = window.pollEvent())
11+
{
12+
if (event->is<sf::Event::Closed>())
13+
{
14+
window.close();
15+
}
16+
}
17+
18+
window.clear();
19+
window.display();
20+
}
21+
}

0 commit comments

Comments
 (0)