Skip to content

Commit

Permalink
Add CMakeLists for Android build
Browse files Browse the repository at this point in the history
Update CMakeLists.txt to download properly

Update README

Update CMakeLists
  • Loading branch information
zmertens committed Jan 19, 2024
1 parent d7c2a0e commit a16f05c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 35 deletions.
86 changes: 86 additions & 0 deletions examples/example_sdl3_opengl3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
cmake_minimum_required(VERSION 3.6)

project(ImGuiExample)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(BUILD_FOR_ANDROID CACHE BOOL 0)

set(DEAR_IMGUI_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_demo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_draw.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_tables.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_widgets.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_sdl3.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_opengl3.cpp
)

set(DEAR_IMGUI_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui.h
${CMAKE_CURRENT_SOURCE_DIR}/../../imconfig.h
${CMAKE_CURRENT_SOURCE_DIR}/../../imgui_internal.h
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_rectpack.h
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_textedit.h
${CMAKE_CURRENT_SOURCE_DIR}/../../imstb_truetype.h
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_sdl3.h
${CMAKE_CURRENT_SOURCE_DIR}/../../backends/imgui_impl_opengl3.h
)

if(NOT DEFINED ENV{BUILD_FOR_ANDROID})
set(EXE "example_sdl3_opengl3")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_SOURCE_DIR}/../../backends)

add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${DEAR_IMGUI_SRCS})
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
find_package(OpenGL REQUIRED)
target_link_libraries(${EXE} PRIVATE GL GLU SDL3::SDL3)
else()
message(STATUS "Building SDL3 app for Android...")

if(NOT DEFINED ENV{ANDROID_HOME})
message(FATAL_ERROR "Set ANDROID_HOME to path of Android Sdk.")
endif()

if(NOT DEFINED ENV{ANDROID_NDK_HOME})
message(FATAL_ERROR "Set ANDROID_NDK_HOME to path of Android Ndk.")
endif()

target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE IMGUI_IMPL_OPENGL_ES3)

set(ANDROID_JNI_FILES
${DEAR_IMGUI_SRCS}
${DEAR_IMGUI_HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
set(JAVA_PKG_NAME "com.example.imgui")
set(SDL_DIR_NAME SDL-main)
set(ANDROID_BUILD_SCRIPT "${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}/build-scripts/androidbuild.sh")

file(DOWNLOAD https://github.com/libsdl-org/SDL/archive/main.zip SHOW_PROGRESS ${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}.zip STATUS DOWNLOAD_STATUS)
file(ARCHIVE_EXTRACT INPUT ${SDL_DIR_NAME}.zip)
# Separate the returned status code, and error message.
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
# Check if download was successful.
if(${STATUS_CODE} EQUAL 0)
message(STATUS "Download completed successfully!")
else()
# Exit CMake if the download failed, printing the error message.
message(FATAL_ERROR "Error occurred during download: ${ERROR_MESSAGE}")
endif()

if(NOT EXISTS ${ANDROID_BUILD_SCRIPT})
message(FATAL_ERROR ": ${ANDROID_BUILD_SCRIPT} is not valid.")
endif()

set(ANDROID_APP_PREFIX ${CMAKE_BINARY_DIR}/${SDL_DIR_NAME}/build/${JAVA_PKG_NAME})
execute_process(COMMAND bash ${ANDROID_BUILD_SCRIPT} ${JAVA_PKG_NAME} ${ANDROID_JNI_FILES} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
if(EXISTS ${ANDROID_APP_PREFIX}/app)
execute_process(COMMAND sed -i -e "s|-lGLESv2|-lGLESv3|" ${ANDROID_APP_PREFIX}/app/jni/src/Android.mk WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
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})
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})
endif()
endif()
41 changes: 6 additions & 35 deletions examples/example_sdl3_opengl3/README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
# Example SDL3 with OpenGL3

# How to Build
## Android

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

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

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

Use build_win32.bat or directly:
```
set SDL2_DIR=path_to_your_sdl3_folder
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
# ^^ include paths ^^ source files ^^ output exe ^^ output dir ^^ libraries
# or for 64-bit:
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
```

## Linux and similar Unixes

Use our Makefile or directly:
```
c++ `sdl3-config --cflags` -I .. -I ../.. -I ../../backends
main.cpp ../../backends/imgui_impl_sdl3.cpp ../../backends/imgui_impl_opengl3.cpp ../../imgui*.cpp
`sdl3-config --libs` -lGL -ldl
```

## macOS

Use our Makefile or directly:
```
brew install sdl3
c++ `sdl3-config --cflags` -I .. -I ../.. -I ../../backends
main.cpp ../../backends/imgui_impl_sdl3.cpp ../../backends/imgui_impl_opengl3.cpp ../../imgui*.cpp
`sdl3-config --libs` -framework OpenGl -framework CoreFoundation
```

## Emscripten

As of 2023-05-30 Emscripten doesn't support SDL3 yet.
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.
7 changes: 7 additions & 0 deletions examples/example_sdl3_opengl3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
#include "imgui_impl_sdl3.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
<<<<<<< HEAD
#include <SDL3/SDL.h>
=======

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

>>>>>>> e54b3b42 (Update CMakeLists.txt to download properly)
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL3/SDL_opengles2.h>
#else
Expand Down

0 comments on commit a16f05c

Please sign in to comment.