Skip to content

Commit a16f05c

Browse files
committed
Add CMakeLists for Android build
Update CMakeLists.txt to download properly Update README Update CMakeLists
1 parent d7c2a0e commit a16f05c

3 files changed

Lines changed: 99 additions & 35 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
cmake_minimum_required(VERSION 3.6)
2+
3+
project(ImGuiExample)
4+
5+
set(CMAKE_CXX_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_CXX_EXTENSIONS OFF)
8+
9+
set(BUILD_FOR_ANDROID CACHE BOOL 0)
10+
11+
set(DEAR_IMGUI_SRCS
12+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_demo.cpp
14+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_draw.cpp
15+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_tables.cpp
16+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_widgets.cpp
17+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_sdl3.cpp
18+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_opengl3.cpp
19+
)
20+
21+
set(DEAR_IMGUI_HEADERS
22+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui.h
23+
${CMAKE_CURRENT_SOURCE_DIR}/../../imconfig.h
24+
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_internal.h
25+
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_rectpack.h
26+
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_textedit.h
27+
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_truetype.h
28+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_sdl3.h
29+
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_opengl3.h
30+
)
31+
32+
if(NOT DEFINED ENV{BUILD_FOR_ANDROID})
33+
set(EXE "example_sdl3_opengl3")
34+
35+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_SOURCE_DIR}/../../backends)
36+
37+
add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${DEAR_IMGUI_SRCS})
38+
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
39+
find_package(OpenGL REQUIRED)
40+
target_link_libraries(${EXE} PRIVATE GL GLU SDL3::SDL3)
41+
else()
42+
message(STATUS "Building SDL3 app for Android...")
43+
44+
if(NOT DEFINED ENV{ANDROID_HOME})
45+
message(FATAL_ERROR "Set ANDROID_HOME to path of Android Sdk.")
46+
endif()
47+
48+
if(NOT DEFINED ENV{ANDROID_NDK_HOME})
49+
message(FATAL_ERROR "Set ANDROID_NDK_HOME to path of Android Ndk.")
50+
endif()
51+
52+
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE IMGUI_IMPL_OPENGL_ES3)
53+
54+
set(ANDROID_JNI_FILES
55+
${DEAR_IMGUI_SRCS}
56+
${DEAR_IMGUI_HEADERS}
57+
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
58+
set(JAVA_PKG_NAME "com.example.imgui")
59+
set(SDL_DIR_NAME SDL-main)
60+
set(ANDROID_BUILD_SCRIPT "${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}/build-scripts/androidbuild.sh")
61+
62+
file(DOWNLOAD https://github.com/libsdl-org/SDL/archive/main.zip SHOW_PROGRESS ${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}.zip STATUS DOWNLOAD_STATUS)
63+
file(ARCHIVE_EXTRACT INPUT ${SDL_DIR_NAME}.zip)
64+
# Separate the returned status code, and error message.
65+
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
66+
list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
67+
# Check if download was successful.
68+
if(${STATUS_CODE} EQUAL 0)
69+
message(STATUS "Download completed successfully!")
70+
else()
71+
# Exit CMake if the download failed, printing the error message.
72+
message(FATAL_ERROR "Error occurred during download: ${ERROR_MESSAGE}")
73+
endif()
74+
75+
if(NOT EXISTS ${ANDROID_BUILD_SCRIPT})
76+
message(FATAL_ERROR ": ${ANDROID_BUILD_SCRIPT} is not valid.")
77+
endif()
78+
79+
set(ANDROID_APP_PREFIX ${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}/build/${JAVA_PKG_NAME})
80+
execute_process(COMMAND bash ${ANDROID_BUILD_SCRIPT} ${JAVA_PKG_NAME} ${ANDROID_JNI_FILES} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
81+
if(EXISTS ${ANDROID_APP_PREFIX}/app)
82+
execute_process(COMMAND sed -i -e "s|-lGLESv2|-lGLESv3|" ${ANDROID_APP_PREFIX}/app/jni/src/Android.mk WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
83+
execute_process(COMMAND sed -i -e "s|Game|${PROJECT_NAME}|g" ${ANDROID_APP_PREFIX}/app/src/main/res/values/strings.xml WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
84+
execute_process(COMMAND sed -i -e "s|\"SDLActivity\"|\"${PROJECT_NAME}Activity\"|g" ${ANDROID_APP_PREFIX}/app/src/main/AndroidManifest.xml WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
85+
endif()
86+
endif()
Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
1+
# Example SDL3 with OpenGL3
12

2-
# How to Build
3+
## Android
34

4-
## Windows with Visual Studio's IDE
5+
This example can run the Dear ImGui demo app on modern Android (targeting API 33+).
56

6-
Use the provided project file (.vcxproj). Add to solution (imgui_examples.sln) if necessary.
7+
Set JAVA_HOME, ANDROID_HOME, and ANDROID_NDK_HOME environment variables in order to run `CMakeLists.txt`.
78

8-
## Windows with Visual Studio's CLI
9+
Example CMake command to run from a "build" directory within this example: `BUILD_FOR_ANDROID=1 cmake ..`
910

10-
Use build_win32.bat or directly:
11-
```
12-
set SDL2_DIR=path_to_your_sdl3_folder
13-
cl /Zi /MD /utf-8 /I.. /I..\.. /I%SDL2_DIR%\include main.cpp ..\..\backends\imgui_impl_sdl3.cpp ..\..\backends\imgui_impl_opengl3.cpp ..\..\imgui*.cpp /FeDebug/example_sdl3_opengl3.exe /FoDebug/ /link /libpath:%SDL2_DIR%\lib\x86 SDL3.lib opengl32.lib /subsystem:console
14-
# ^^ include paths ^^ source files ^^ output exe ^^ output dir ^^ libraries
15-
# or for 64-bit:
16-
cl /Zi /MD /utf-8 /I.. /I..\.. /I%SDL2_DIR%\include main.cpp ..\..\backends\imgui_impl_sdl3.cpp ..\..\backends\imgui_impl_opengl3.cpp ..\..\imgui*.cpp /FeDebug/example_sdl3_opengl3.exe /FoDebug/ /link /libpath:%SDL2_DIR%\lib\x64 SDL3.lib SDL2mainopengl32.lib /subsystem:console
17-
```
18-
19-
## Linux and similar Unixes
20-
21-
Use our Makefile or directly:
22-
```
23-
c++ `sdl3-config --cflags` -I .. -I ../.. -I ../../backends
24-
main.cpp ../../backends/imgui_impl_sdl3.cpp ../../backends/imgui_impl_opengl3.cpp ../../imgui*.cpp
25-
`sdl3-config --libs` -lGL -ldl
26-
```
27-
28-
## macOS
29-
30-
Use our Makefile or directly:
31-
```
32-
brew install sdl3
33-
c++ `sdl3-config --cflags` -I .. -I ../.. -I ../../backends
34-
main.cpp ../../backends/imgui_impl_sdl3.cpp ../../backends/imgui_impl_opengl3.cpp ../../imgui*.cpp
35-
`sdl3-config --libs` -framework OpenGl -framework CoreFoundation
36-
```
37-
38-
## Emscripten
39-
40-
As of 2023-05-30 Emscripten doesn't support SDL3 yet.
11+
Example Gradle build: `ANDROID_HOME=MY_SDK_PATH ANDROID_NDK_HOME=MY_NDK_PATH build/SDL-main/build/my.java.pkg/gradlew installDebug`. This should install the demo app on a connected emulator or physical device.

examples/example_sdl3_opengl3/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
#include "imgui_impl_sdl3.h"
1212
#include "imgui_impl_opengl3.h"
1313
#include <stdio.h>
14+
<<<<<<< HEAD
1415
#include <SDL3/SDL.h>
16+
=======
17+
18+
#include <SDL3/SDL.h>
19+
#include <SDL3/SDL_main.h>
20+
21+
>>>>>>> e54b3b42 (Update CMakeLists.txt to download properly)
1522
#if defined(IMGUI_IMPL_OPENGL_ES2)
1623
#include <SDL3/SDL_opengles2.h>
1724
#else

0 commit comments

Comments
 (0)