├── CMakeLists.txt ├── LICENSE ├── README.md ├── dependencies ├── CMakeLists.txt ├── imgui-sfml │ └── CMakeLists.txt └── sfml │ └── CMakeLists.txt └── src ├── CMakeLists.txt └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(example 3 | LANGUAGES CXX 4 | VERSION 1.0 5 | ) 6 | 7 | add_subdirectory(dependencies) 8 | add_subdirectory(src) 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is the source code for the [Using CMake and managing dependencies](https://edw.is/using-cmake/). 2 | 3 | It shows how to setup a simple project which depends on SFML, Dear ImGui and ImGui-SFML. 4 | 5 | ![image](https://user-images.githubusercontent.com/1285136/119359595-76e74280-bcb2-11eb-9ad5-1e69795e5696.png) 6 | 7 | # Building 8 | 9 | ```sh 10 | git clone https://github.com/eliasdaler/cmake-fetchcontent-tutorial-code 11 | mkdir build && cd build 12 | cmake ../cmake-fetchcontent-tutorial-code 13 | cmake --build . 14 | ./src/example_exe # or ./src/Debug/example_exe if using Visual Studio 15 | ``` 16 | -------------------------------------------------------------------------------- /dependencies/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(FetchContent) 2 | 3 | # SFML 4 | FetchContent_Declare( 5 | sfml 6 | URL https://github.com/SFML/SFML/archive/refs/tags/2.5.1.zip 7 | URL_MD5 2c4438b3e5b2d81a6e626ecf72bf75be 8 | ) 9 | add_subdirectory(sfml) 10 | 11 | # Dear ImGui 12 | FetchContent_Declare( 13 | imgui 14 | GIT_REPOSITORY https://github.com/ocornut/imgui 15 | GIT_TAG 35b1148efb839381b84de9290d9caf0b66ad7d03 # 1.82 16 | ) 17 | 18 | FetchContent_MakeAvailable(imgui) 19 | 20 | # ImGui-SFML 21 | FetchContent_Declare( 22 | imgui-sfml 23 | GIT_REPOSITORY https://github.com/SFML/imgui-sfml 24 | GIT_TAG 82dc2033e51b8323857c3ae1cf1f458b3a933c35 # 2.3 25 | ) 26 | add_subdirectory(imgui-sfml) 27 | -------------------------------------------------------------------------------- /dependencies/imgui-sfml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | message(STATUS "Fetching ImGui-SFML...") 2 | 3 | set(IMGUI_DIR ${imgui_SOURCE_DIR}) 4 | set(IMGUI_SFML_FIND_SFML OFF) 5 | set(IMGUI_SFML_IMGUI_DEMO ON) 6 | 7 | FetchContent_MakeAvailable(imgui-sfml) 8 | -------------------------------------------------------------------------------- /dependencies/sfml/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | message(STATUS "Fetching SFML...") 2 | 3 | # No need to build audio and network modules 4 | set(SFML_BUILD_AUDIO FALSE) 5 | set(SFML_BUILD_NETWORK FALSE) 6 | 7 | FetchContent_MakeAvailable(sfml) 8 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(example_exe main.cpp) 2 | target_link_libraries(example_exe PRIVATE ImGui-SFML::ImGui-SFML) 3 | 4 | # Copy DLLs needed for runtime on Windows 5 | if(WIN32) 6 | if (BUILD_SHARED_LIBS) 7 | add_custom_command(TARGET example_exe POST_BUILD 8 | COMMAND ${CMAKE_COMMAND} -E copy_if_different 9 | $ 10 | $ 11 | $ 12 | $ 13 | $) 14 | endif() 15 | endif() -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | int main() { 9 | sf::RenderWindow window(sf::VideoMode(1280, 720), "ImGui + SFML = <3"); 10 | window.setFramerateLimit(60); 11 | ImGui::SFML::Init(window); 12 | 13 | sf::Clock deltaClock; 14 | while (window.isOpen()) { 15 | sf::Event event; 16 | while (window.pollEvent(event)) { 17 | ImGui::SFML::ProcessEvent(event); 18 | 19 | if (event.type == sf::Event::Closed) { 20 | window.close(); 21 | } 22 | } 23 | 24 | ImGui::SFML::Update(window, deltaClock.restart()); 25 | 26 | ImGui::ShowDemoWindow(); 27 | 28 | window.clear(); 29 | ImGui::SFML::Render(window); 30 | window.display(); 31 | } 32 | 33 | ImGui::SFML::Shutdown(); 34 | 35 | return 0; 36 | } 37 | 38 | --------------------------------------------------------------------------------