Skip to content

Commit 72df1fd

Browse files
committed
Merge branch 'develop'
ImGui-SFML v.2.0 * Better CMake support * Compiler warning fixes * Can now compile as shared library
2 parents e5bc24e + 399b698 commit 72df1fd

File tree

9 files changed

+680
-405
lines changed

9 files changed

+680
-405
lines changed

CMakeLists.txt

Lines changed: 134 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,152 @@
1-
# Only tested in 3.1, may work in older versions.
2-
# .
3-
# Install SFML or set SFML_ROOT and set IMGUI_ROOT prior to running cmake
4-
# .
5-
# Provides the follow variables:
6-
# IMGUI_SFML_INCLUDE_DIRS - imgui and imgui_sfml include paths
7-
# IMGUI_SOURCES - imgui.cpp source path to link with your binary
8-
# IMGUI_SFML_SOURCES - imgui_sfml.cpp source path to link with your binary
9-
# IMGUI_SFML_DEPENDENCIES - found dependencies to link with your library (sfml)
10-
# .
11-
# Sample usage:
12-
# add_subdirectory(repos/imgui-sfml)
13-
# include_directories("${IMGUI_SFML_INCLUDE_DIRS}")
14-
# add_executable(MY_PROJECT ${IMGUI_SOURCES} ${IMGUI_SFML_SOURCES} ${SRCS})
15-
# ...
16-
# target_link_libraries(MY_PROJECT ${IMGUI_SFML_DEPENDENCIES})
17-
# .
181
cmake_minimum_required(VERSION 3.1)
192

20-
project(imgui_sfml)
3+
project(imgui_sfml
4+
LANGUAGES CXX
5+
VERSION 2.0
6+
)
217

22-
option(IMGUI_SFML_BUILD_EXAMPLES "Build ImGui_SFML examples" ON)
8+
option(IMGUI_SFML_BUILD_EXAMPLES "Build ImGui_SFML examples" OFF)
9+
option(IMGUI_SFML_FIND_SFML "Use find_package to find SFML" ON)
2310

24-
# Find required libraries
25-
find_package(SFML 2.2 COMPONENTS graphics window system)
26-
message(status "** SFML Include: ${SFML_INCLUDE_DIR}")
27-
message(status "** SFML Libraries: ${SFML_LIBRARIES}")
28-
if(NOT SFML_FOUND)
29-
set(SFML_ROOT "" CACHE PATH "SFML top-level directory")
30-
message("---> SFML 2 directory not found. Set SFML_ROOT to SFML's top-level path (containing \"include\" and \"lib\" directories).\n")
11+
# If you want to use your own user config when compiling ImGui, please set the following variables
12+
# For example, if you have your config in /path/to/dir/with/config/myconfig.h, set the variables as follows:
13+
#
14+
# IMGUI_SFML_USE_DEFAULT_CONFIG = OFF
15+
# IMGUI_SFML_USER_CONFIG_DIR = /path/to/dir/with/config
16+
# IMGUI_SFML_USER_CONFIG_NAME = "myconfig.h"
17+
#
18+
# If you set IMGUI_SFML_CONFIG_INSTALL_DIR, ImGui-SFML won't install your custom config, because
19+
# you might want to do it yourself
20+
option(IMGUI_SFML_USE_DEFAULT_CONFIG "Use default imconfig-SFML.h" ON)
21+
set(IMGUI_SFML_CONFIG_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "Path to a directory containing user ImGui config")
22+
set(IMGUI_SFML_CONFIG_NAME "imconfig-SFML.h" CACHE STRING "Name of a custom user ImGui config header")
23+
set(IMGUI_SFML_CONFIG_INSTALL_DIR "" CACHE PATH "Path where user's config header will be installed")
24+
25+
# For FindImGui.cmake
26+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
27+
28+
if (IMGUI_SFML_FIND_SFML)
29+
find_package(SFML 2.5 COMPONENTS graphics system window)
30+
31+
if(NOT SFML_FOUND)
32+
message(FATAL_ERROR "SFML 2 directory not found. Set SFML_DIR to directory where SFML was built (or one which ccontains SFMLConfig.cmake)")
33+
endif()
3134
endif()
3235

3336
# ImGui does not provide native support for CMakeLists, workaround for now to have
34-
# users specify IMGUI_ROOT. See:
35-
# https://github.com/ocornut/imgui/pull/255
36-
if(NOT IMGUI_ROOT)
37-
set(IMGUI_ROOT "" CACHE PATH "imgui top-level directory")
38-
message("---> ImGui directory not found. Set IMGUI_ROOT to imgui's top-level path (containing \"imgui.cpp\" and \"imgui.h\" files).\n")
37+
# users specify IMGUI_DIR. Waiting for this PR to get merged...
38+
# https://github.com/ocornut/imgui/pull/1713
39+
if(NOT IMGUI_DIR)
40+
set(IMGUI_DIR "" CACHE PATH "imgui top-level directory")
41+
message(FATAL_ERROR "ImGui directory not found. Set IMGUI_ROOT to imgui's top-level path (containing 'imgui.h' and other files).\n")
3942
endif()
4043

41-
# Do a pseudo find files for ImGui once IMGUI_ROOT is set
42-
if(IMGUI_ROOT)
43-
set(IMGUI_SERACH_PATH
44-
${IMGUI_ROOT}
45-
$ENV{IMGUI_ROOT}
46-
)
47-
find_path(IMGUI_INCLUDE_DIR imgui.cpp
48-
PATHS ${IMGUI_SERACH_PATH})
49-
if(NOT IMGUI_INCLUDE_DIR)
50-
message(FATAL_ERROR "---> IMGUI imgui.cpp not found. Set IMGUI_ROOT to imgui's top-level path (containing \"imgui.cpp\" and \"imgui.h\" files).\n")
51-
else()
52-
file(GLOB IMGUI_FILES
53-
"${IMGUI_INCLUDE_DIR}/imgui_draw.cpp"
54-
"${IMGUI_INCLUDE_DIR}/imgui_demo.cpp"
55-
"${IMGUI_INCLUDE_DIR}/imgui.cpp" )
56-
message("Found imgui.cpp in ${IMGUI_INCLUDE_DIR}")
57-
# Rename that pesky imconfig.h file for the user.
58-
install(FILES ${IMGUI_INCLUDE_DIR}/imconfig.h DESTINATION include RENAME imconfig-sample.h)
59-
endif()
44+
# This uses FindImGui.cmake provided in ImGui-SFML repo for now
45+
find_package(ImGui 1.68 REQUIRED)
46+
47+
# these headers will be installed alongside ImGui-SFML
48+
set(IMGUI_PUBLIC_HEADERS
49+
${IMGUI_INCLUDE_DIR}/imconfig.h
50+
${IMGUI_INCLUDE_DIR}/imgui.h
51+
${IMGUI_INCLUDE_DIR}/imgui_internal.h # not actually public, but users might need it
52+
${IMGUI_INCLUDE_DIR}/imstb_rectpack.h
53+
${IMGUI_INCLUDE_DIR}/imstb_textedit.h
54+
${IMGUI_INCLUDE_DIR}/imstb_truetype.h
55+
${IMGUI_INCLUDE_DIR}/misc/cpp/imgui_stdlib.h
56+
)
57+
58+
# CMake 3.11 and later prefer to choose GLVND, but we choose legacy OpenGL just because it's safer
59+
# (unless the OpenGL_GL_PREFERENCE was explicitly set)
60+
# See CMP0072 for more details (cmake --help-policy CMP0072)
61+
if ((NOT ${CMAKE_VERSION} VERSION_LESS 3.11) AND (NOT OpenGL_GL_PREFERENCE))
62+
set(OpenGL_GL_PREFERENCE "LEGACY")
6063
endif()
6164

62-
# Glob up both source and headers as sources for VS users.
63-
file(GLOB IMGUI_SFML_FILES "${PROJECT_SOURCE_DIR}/*.cpp" "${PROJECT_SOURCE_DIR}/*.h")
65+
find_package(OpenGL REQUIRED)
6466

65-
# Set these for users to use
66-
set(IMGUI_SFML_INCLUDE_DIRS
67-
${PROJECT_SOURCE_DIR}
68-
${IMGUI_INCLUDE_DIR}
69-
${SFML_INCLUDE_DIR} CACHE INTERNAL "")
67+
add_library(ImGui-SFML
68+
imgui-SFML.cpp
69+
${IMGUI_SOURCES}
70+
)
7071

71-
set(IMGUI_SOURCES
72-
${IMGUI_FILES} CACHE INTERNAL "")
72+
# Add pretty alias
73+
add_library(ImGui-SFML::ImGui-SFML ALIAS ImGui-SFML)
7374

74-
set(IMGUI_SFML_SOURCES
75-
${IMGUI_SFML_FILES} CACHE INTERNAL "")
75+
target_link_libraries(ImGui-SFML
76+
PUBLIC
77+
sfml-graphics
78+
sfml-system
79+
sfml-window
80+
${OPENGL_LIBRARIES}
81+
)
7682

77-
set(IMGUI_SFML_DEPENDENCIES
78-
${SFML_DEPENDENCIES}
79-
${SFML_LIBRARIES} CACHE INTERNAL "")
83+
include(GNUInstallDirs)
84+
85+
target_include_directories(ImGui-SFML
86+
PUBLIC
87+
$<BUILD_INTERFACE:${IMGUI_INCLUDE_DIR}>
88+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
89+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
90+
)
91+
92+
if(NOT IMGUI_SFML_USE_DEFAULT_CONFIG)
93+
if (IMGUI_SFML_CONFIG_DIR)
94+
target_include_directories(ImGui-SFML
95+
PUBLIC
96+
$<BUILD_INTERFACE:${IMGUI_SFML_CONFIG_DIR}>
97+
$<INSTALL_INTERFACE:${IMGUI_SFML_INSTALL_CONFIG_DIR}>
98+
)
99+
else()
100+
message(FATAL_ERROR "IMGUI_SFML_CONFIG_DIR should be set if IMGUI_SFML_USE_DEFAULT_CONFIG is OFF")
101+
endif()
102+
endif()
103+
104+
target_compile_definitions(ImGui-SFML
105+
PUBLIC
106+
IMGUI_USER_CONFIG="${IMGUI_SFML_CONFIG_NAME}"
107+
)
108+
109+
if(BUILD_SHARED_LIBS)
110+
target_compile_definitions(ImGui-SFML PRIVATE IMGUI_SFML_SHARED_LIB)
111+
set_target_properties(ImGui-SFML PROPERTIES
112+
DEFINE_SYMBOL "IMGUI_SFML_EXPORTS"
113+
)
114+
endif()
115+
116+
set(IMGUI_SFML_PUBLIC_HEADERS
117+
"${CMAKE_CURRENT_LIST_DIR}/imgui-SFML.h"
118+
"${CMAKE_CURRENT_LIST_DIR}/imgui-SFML_export.h"
119+
)
120+
121+
if (IMGUI_SFML_USE_DEFAULT_CONFIG OR
122+
(NOT DEFINED "${IMGUI_SFML_CONFIG_INSTALL_DIR}"))
123+
list(APPEND IMGUI_SFML_PUBLIC_HEADERS
124+
"${IMGUI_SFML_CONFIG_DIR}/${IMGUI_SFML_CONFIG_NAME}"
125+
)
126+
# If user set IMGUI_SFML_INSTALL_CONFIG_DIR, it means that they'll install file themselves
127+
endif()
128+
129+
list(APPEND IMGUI_SFML_PUBLIC_HEADERS "${IMGUI_PUBLIC_HEADERS}")
130+
131+
set_target_properties(ImGui-SFML PROPERTIES
132+
PUBLIC_HEADER "${IMGUI_SFML_PUBLIC_HEADERS}"
133+
)
80134

81135
if(IMGUI_SFML_BUILD_EXAMPLES)
82-
# Build examples
83136
add_subdirectory(examples)
84-
endif(IMGUI_SFML_BUILD_EXAMPLES)
137+
endif()
138+
139+
# installation rules
140+
install(TARGETS ImGui-SFML
141+
EXPORT ImGui-SFML
142+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
143+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
144+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
145+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
146+
)
147+
148+
install(EXPORT ImGui-SFML
149+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ImGui-SFML
150+
NAMESPACE ImGui-SFML::
151+
FILE ImGui-SFMLConfig.cmake
152+
)

README.md

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
1-
ImGui + SFML
1+
ImGui + SFML v2.0
22
=======
33

44
Library which allows you to use [ImGui](https://github.com/ocornut/imgui) with [SFML](https://github.com/SFML/SFML)
55

6-
> Use [ImGui-SFML's v1.0](https://github.com/eliasdaler/imgui-sfml/releases/tag/v.1.0) if you're using ImGui's stable release (v.1.53)! This repo's master will be kept up to date with breaking changes in ImGui's master.
7-
86
![screenshot](https://i2.wp.com/i.imgur.com/iQibpSk.gif)
97

108
Based on [this repository](https://github.com/Mischa-Alff/imgui-backends) with big improvements and changes.
119

10+
Dependencies
11+
-----
12+
13+
* [SFML](https://github.com/SFML/SFML) >= 2.5.0
14+
* [ImGui](https://github.com/ocornut/imgui) >= 1.68
15+
1216
How-to
1317
----
1418

1519
- [**Detailed tutorial on my blog**](https://eliasdaler.github.io/using-imgui-with-sfml-pt1)
1620
- [**Using ImGui with modern C++ and STL**](https://eliasdaler.github.io/using-imgui-with-sfml-pt2/)
1721
- [**Thread on SFML forums**](https://en.sfml-dev.org/forums/index.php?topic=20137.0). Feel free to ask your questions there.
1822

19-
Setting up:
23+
Building and integrating into your CMake project
24+
---
25+
26+
```sh
27+
cmake <ImGui-SFML repo folder> -DIMGUI_DIR=<ImGui repo folder> -DSFML_DIR=<path with built SFML>
28+
```
29+
30+
If you have SFML installed on your system, you don't need to set SFML_DIR during
31+
configuration.
32+
33+
You can also specify `BUILD_SHARED_LIBS=ON` to build ImGui-SFML as a shared library. To build ImGui-SFML examples, set `IMGUI_SFML_BUILD_EXAMPLES=ON`.
34+
35+
After the building, you can install the library on your system by running:
36+
```sh
37+
cmake --build . --target install
38+
```
39+
40+
If you set `CMAKE_INSTALL_PREFIX` during configuration, you can install ImGui-SFML locally.
41+
42+
Integrating into your project is simple.
43+
```cmake
44+
find_package(ImGui-SFML REQUIRED)
45+
target_link_libraries(my_target PRIVATE ImGui-SFML::ImGui-SFML)
46+
```
47+
48+
If CMake can't find ImGui-SFML on your system, just define `ImGui-SFML_DIR` before calling `find_package`.
2049

50+
Integrating into your project manually
51+
---
2152
- Download [ImGui](https://github.com/ocornut/imgui)
2253
- Add ImGui folder to your include directories
2354
- Add `imgui.cpp` and `imgui_draw.cpp` to your build/project
@@ -26,13 +57,14 @@ Setting up:
2657
- Add `imgui-SFML.cpp` to your build/project
2758
- Link OpenGL if you get linking errors
2859

29-
In your code:
60+
Using ImGui-SFML in your code
61+
---
3062

3163
- Call `ImGui::SFML::Init` and pass your `sf::Window` + `sf::RenderTarget` or `sf::RenderWindow` there. You can create your font atlas and pass the pointer in Init too, otherwise the default internal font atlas will be created for you.
3264
- For each iteration of a game loop:
3365
- Poll and process events:
3466

35-
```c++
67+
```cpp
3668
sf::Event event;
3769
while (window.pollEvent(event)) {
3870
ImGui::SFML::ProcessEvent(event);
@@ -54,7 +86,7 @@ Example code
5486

5587
See example file [here](examples/main.cpp)
5688

57-
```c++
89+
```cpp
5890
#include "imgui.h"
5991
#include "imgui-SFML.h"
6092

@@ -106,7 +138,7 @@ Default font is loaded if you don't pass `false` in `ImGui::SFML::Init`. Call `I
106138

107139
* Load your fonts like this:
108140

109-
```c++
141+
```cpp
110142
IO.Fonts->Clear(); // clear fonts if you loaded some before (even if only default one was loaded)
111143
// IO.Fonts->AddFontDefault(); // this will load default font as well
112144
IO.Fonts->AddFontFromFileTTF("font1.ttf", 8.f);
@@ -117,7 +149,7 @@ ImGui::SFML::UpdateFontTexture(); // important call: updates font texture
117149
118150
* And use them like this:
119151
120-
```c++
152+
```cpp
121153
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[0]);
122154
ImGui::Button("Look at this pretty button");
123155
ImGui::PopFont();
@@ -129,47 +161,49 @@ ImGui::PopFont();
129161

130162
The first loaded font is treated as the default one and doesn't need to be pushed with `ImGui::PushFont`.
131163

132-
CMake how-to
133-
---
134-
- Checkout the repository as a submoudle
135-
- Set IMGUI_ROOT
136-
- Modify your builds to copy imgui-SFML and dependencies (sfml) to your project
137-
```CMakeLists
138-
add_subdirectory(repos/imgui-sfml)
139-
include_directories("${IMGUI_SFML_INCLUDE_DIRS}")
140-
add_executable(MY_PROJECT ${IMGUI_SOURCES} ${IMGUI_SFML_SOURCES} ${SRCS})
141-
...
142-
target_link_libraries(MY_PROJECT ${IMGUI_SFML_DEPENDENCIES})
143-
```
144-
145164
SFML related ImGui overloads / new widgets
146165
---
147166

148167
There are some useful overloads implemented for SFML objects (see header for overloads):
149-
```c++
168+
```cpp
150169
ImGui::Image(const sf::Sprite& sprite);
151170
ImGui::Image(const sf::Texture& texture);
152171
ImGui::ImageButton(const sf::Sprite& sprite);
153172
ImGui::ImageButton(const sf::Texture& texture);
154173
```
155174
175+
Mouse cursors
176+
---
177+
You can change your cursors in ImGui like this:
178+
179+
```cpp
180+
ImGui::SetMouseCursor(ImGuiMouseCursor_TextInput);
181+
```
182+
183+
By default, your system cursor will change and will be rendered by your system. If you want SFML to draw your cursor with default ImGui cursors (the system cursor will be hidden), do this:
184+
185+
```cpp
186+
ImGuiIO& io = ImGui::GetIO();
187+
io.MouseDrawCursor = true;
188+
```
189+
156190
Keyboard/Gamepad navigation
157191
---
158192
Starting with [ImGui 1.60](https://github.com/ocornut/imgui/releases/tag/v1.60), there's a feature to control ImGui with keyboard and gamepad. To use keyboard navigation, you just need to do this:
159193

160-
```c++
194+
```cpp
161195
ImGuiIO& io = ImGui::GetIO();
162196
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
163197
```
164198

165199
Gamepad navigation requires more work, unless you have XInput gamepad, in which case the mapping is automatically set for you. But you can still set it up for your own gamepad easily, just take a look how it's done for the default mapping [here](https://github.com/eliasdaler/imgui-sfml/blob/navigation/imgui-SFML.cpp#L697). And then you need to do this:
166200

167-
```c++
201+
```cpp
168202
ImGuiIO& io = ImGui::GetIO();
169203
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
170204
```
171205
By default, the first active joystick is used for navigation, but you can set joystick id explicitly like this:
172-
```c++
206+
```cpp
173207
ImGui::SFML::SetActiveJoystickId(5);
174208
```
175209

0 commit comments

Comments
 (0)