diff --git a/examples/example_sdl3_opengl3/Makefile.android b/examples/example_sdl3_opengl3/Makefile.android new file mode 100644 index 000000000..aad1e3c5b --- /dev/null +++ b/examples/example_sdl3_opengl3/Makefile.android @@ -0,0 +1,69 @@ +# Makefile to use with SDL+Android +# See https://wiki.libsdl.org/SDL3/README/android +# for details on how SDL interacts with Android. +# +# This Makefile assumes you have wget or curl and Python v3 installed. +# +# Running `make -f Makefile.android` will produce these files or directories: +# - SDL-main.zip +# - SDL-main +# - com.imgui.example +# + +# Variables +PACKAGE_NAME := com.imgui.sdl3opengl3 +VARIANT := copy +VERSION := 0.1.0 +URL := https://github.com/libsdl-org/SDL/archive/refs/heads/main.zip +ARCHIVE_NAME := SDL-main.zip +SDL_ROOT := SDL-main +CREATED_ANDROID_PROJECT := $(CURDIR)/$(PACKAGE_NAME) +ANDROID_SRCS_DIR := $(CREATED_ANDROID_PROJECT)/app/jni/src +SDL_PYTHON_SCRIPT := $(SDL_ROOT)/build-scripts/create-android-project.py +IMGUI_ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))../..) + +SOURCES := \ + $(IMGUI_ROOT)/backends/imgui_impl_opengl3.cpp \ + $(IMGUI_ROOT)/backends/imgui_impl_sdl3.cpp \ + $(IMGUI_ROOT)/imgui.cpp \ + $(IMGUI_ROOT)/imgui_draw.cpp \ + $(IMGUI_ROOT)/imgui_widgets.cpp \ + $(IMGUI_ROOT)/imgui_demo.cpp \ + $(IMGUI_ROOT)/imgui_tables.cpp \ + main.cpp + +HEADERS := $(IMGUI_ROOT)/imstb_rectpack.h \ + $(IMGUI_ROOT)/imstb_textedit.h \ + $(IMGUI_ROOT)/imstb_truetype.h \ + $(IMGUI_ROOT)/imgui.h \ + $(IMGUI_ROOT)/imgui_internal.h \ + $(IMGUI_ROOT)/imconfig.h \ + $(IMGUI_ROOT)/backends/imgui_impl_sdl3.h \ + $(IMGUI_ROOT)/backends/imgui_impl_opengl3.h \ + $(IMGUI_ROOT)/backends/imgui_impl_opengl3_loader.h + +# Targets +.PHONY: all clean setup download extract run + +all: setup run + +clean: + rm -rf $(SDL_ROOT) $(ARCHIVE_NAME) $(CREATED_ANDROID_PROJECT) + +setup: download extract + @echo "Setting up the Android project..." + @which python3 > /dev/null || which python > /dev/null || { echo "Error: Python v3 is not installed."; exit 1;} + +download: + @echo "Downloading Android project..." + wget -O $(ARCHIVE_NAME) $(URL) || curl -L -o $(ARCHIVE_NAME) $(URL) + +extract: + @echo "Extracting Android project..." + mkdir -p $(SDL_ROOT) + unzip -o $(ARCHIVE_NAME) -d $(CURDIR) + +run: + @echo "Creating Android project..." + python3 $(SDL_PYTHON_SCRIPT) --variant $(VARIANT) --output $(CURDIR) $(PACKAGE_NAME) $(SOURCES) + cp -r $(HEADERS) $(ANDROID_SRCS_DIR) diff --git a/examples/example_sdl3_opengl3/README.md b/examples/example_sdl3_opengl3/README.md index a032f8752..62d9f567e 100644 --- a/examples/example_sdl3_opengl3/README.md +++ b/examples/example_sdl3_opengl3/README.md @@ -38,3 +38,65 @@ c++ `sdl3-config --cflags` -I .. -I ../.. -I ../../backends ## Emscripten As of 2023-05-30 Emscripten doesn't support SDL3 yet. + +## Android + +- You will need at least Java 9, preferably Java 17, to handle recent Gradle 8.9.x versions. + +- You need a way to install Android SDK and NDK and add their PATHS, and have access to a virtual device or real Android device to run the app. + - Android SDK version 21+ + - Android NDK version 19+ + +- Android Studio is an option for handling Java, Android SDK and NDK dependencies altogether: [https://developer.android.com/studio](https://developer.android.com/studio). + +- The provided Makefile will download SDL from the main Git branch and create an Android project using SDL's provided `create-android-project.py` script. + +- Run the Makefile using `make all`. It downloads and unpacks SDL, runs the included Python script, and generate a folder with the Android project in this directory. The project contains the Dear ImGui sources and SDL3 backends. + +- After the project is generated, the Gradle version can be automatically updated in Android Studio, or updated manually via setting the `classpath` and `distributionUrl` values. + +In `com.imgui.example/build.gradle`: +``` +dependencies { + classpath 'com.android.tools.build:gradle:8.7.2' + ... +} +``` + +In `com.imgui.example/gradle/wrapper/gradle-wrapper.properties`: +``` +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +``` + +In `com.imgui.example/app/build.grade`, update the min SDK version: +``` +minSdkVersion 21 +``` + +For this OpenGL3 example, add the `GLESv3` library links in `com.imgui.example/app/jni/src/Android.mk` +``` +LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -lGLESv3 -lOpenSLES -llog -landroid # SDL +``` + +## How to Run + +To run on a local machine using Windows PowerShell: + + 1. Run `make all` and change into the generated Android project folder, for example: `cd com.imgui.example/` + 2. Run `.\gradlew.bat build` to start the Gradle Daemons and build the project. This requires the `JAVA_HOME`, `ANDROID_HOME`, and `ANDROID_NDK_HOME` environment variables if not done in Android Studio. + - Android Studio handles these environment variables itself + 3. Run `.\gradlew.bat installDebug` to install the APK package file onto an Android virtual device or a connected real device. + + +## Some other notes + +On Windows, this variable may need to get set in the `com.imgui.sdlrenderer3/app/jni/SDL/Android.mk` file after variables are cleared. + +``` +include $(CLEAR_VARS) + +# Add this if there's Makefile error 87 +LOCAL_SHORT_COMMANDS := true + +LOCAL_MODULE := SDL3 +``` diff --git a/examples/example_sdl3_opengl3/main.cpp b/examples/example_sdl3_opengl3/main.cpp index 1b0e100e4..bcb4d1f7f 100644 --- a/examples/example_sdl3_opengl3/main.cpp +++ b/examples/example_sdl3_opengl3/main.cpp @@ -22,6 +22,10 @@ #include "../libs/emscripten/emscripten_mainloop_stub.h" #endif +#if defined(__ANDROID__) +#include +#endif + // Main code int main(int, char**) { @@ -55,6 +59,13 @@ int main(int, char**) SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); +#elif defined(__ANDROID__) + // GLES 3.0 + GLSL 100 + const char* glsl_version = "#version 100"; + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #else // GL 3.0 + GLSL 130 const char* glsl_version = "#version 130"; diff --git a/examples/example_sdl3_sdlrenderer3/Makefile.android b/examples/example_sdl3_sdlrenderer3/Makefile.android new file mode 100644 index 000000000..ae84750af --- /dev/null +++ b/examples/example_sdl3_sdlrenderer3/Makefile.android @@ -0,0 +1,68 @@ +# Makefile to use with SDL+Android +# See https://wiki.libsdl.org/SDL3/README/android +# for details on how SDL interacts with Android. +# +# This Makefile assumes you have wget or curl and Python v3 installed. +# +# Running `make -f Makefile.android` will produce these files or directories: +# - SDL-main.zip +# - SDL-main +# - com.imgui.example +# + +# Variables +PACKAGE_NAME := com.imgui.sdl3renderer3 +VARIANT := copy +VERSION := 0.1.0 +URL := https://github.com/libsdl-org/SDL/archive/refs/heads/main.zip +ARCHIVE_NAME := SDL-main.zip +SDL_ROOT := SDL-main +CREATED_ANDROID_PROJECT := $(CURDIR)/$(PACKAGE_NAME) +ANDROID_SRCS_DIR := $(CREATED_ANDROID_PROJECT)/app/jni/src +SDL_PYTHON_SCRIPT := $(SDL_ROOT)/build-scripts/create-android-project.py +IMGUI_ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))../..) + +SOURCES := \ + $(IMGUI_ROOT)/backends/imgui_impl_sdlrenderer3.cpp \ + $(IMGUI_ROOT)/backends/imgui_impl_sdl3.cpp \ + $(IMGUI_ROOT)/imgui.cpp \ + $(IMGUI_ROOT)/imgui_draw.cpp \ + $(IMGUI_ROOT)/imgui_widgets.cpp \ + $(IMGUI_ROOT)/imgui_demo.cpp \ + $(IMGUI_ROOT)/imgui_tables.cpp \ + main.cpp + +HEADERS := $(IMGUI_ROOT)/imstb_rectpack.h \ + $(IMGUI_ROOT)/imstb_textedit.h \ + $(IMGUI_ROOT)/imstb_truetype.h \ + $(IMGUI_ROOT)/imgui.h \ + $(IMGUI_ROOT)/imgui_internal.h \ + $(IMGUI_ROOT)/imconfig.h \ + $(IMGUI_ROOT)/backends/imgui_impl_sdl3.h \ + $(IMGUI_ROOT)/backends/imgui_impl_sdlrenderer3.h + +# Targets +.PHONY: all clean setup download extract run + +all: setup run + +clean: + rm -rf $(SDL_ROOT) $(ARCHIVE_NAME) $(CREATED_ANDROID_PROJECT) + +setup: download extract + @echo "Setting up the Android project..." + @which python3 > /dev/null || which python > /dev/null || { echo "Error: Python v3 is not installed."; exit 1;} + +download: + @echo "Downloading Android project..." + wget -O $(ARCHIVE_NAME) $(URL) || curl -L -o $(ARCHIVE_NAME) $(URL) + +extract: + @echo "Extracting Android project..." + mkdir -p $(SDL_ROOT) + unzip -o $(ARCHIVE_NAME) -d $(CURDIR) + +run: + @echo "Creating Android project..." + python3 $(SDL_PYTHON_SCRIPT) --variant $(VARIANT) --output $(CURDIR) $(PACKAGE_NAME) $(SOURCES) + cp -r $(HEADERS) $(ANDROID_SRCS_DIR) diff --git a/examples/example_sdl3_sdlrenderer3/README.md b/examples/example_sdl3_sdlrenderer3/README.md new file mode 100644 index 000000000..b59b32eb0 --- /dev/null +++ b/examples/example_sdl3_sdlrenderer3/README.md @@ -0,0 +1,57 @@ +## How to Build the Android project + +- You will need at least Java 9, preferably Java 17, to handle recent Gradle 8.9.x versions. + +- You need a way to install Android SDK and NDK and add their PATHS, and have access to a virtual device or real Android device to run the app. + - Android SDK version 21+ + - Android NDK version 19+ + +- Android Studio is an option for handling Java, Android SDK and NDK dependencies altogether: [https://developer.android.com/studio](https://developer.android.com/studio). + +- The provided Makefile will download SDL from the main Git branch and create an Android project using SDL's provided `create-android-project.py` script. + +- Run the Makefile using `make all`. It downloads and unpacks SDL, runs the included Python script, and generate a folder with the Android project in this directory. The project contains the Dear ImGui sources and SDL3 backends. + +- After the project is generated, the Gradle version can be automatically updated in Android Studio, or updated manually via setting the `classpath` and `distributionUrl` values. + +In `com.imgui.example/build.gradle`: +``` +dependencies { + classpath 'com.android.tools.build:gradle:8.7.2' + ... +} +``` + +In `com.imgui.example/gradle/wrapper/gradle-wrapper.properties`: +``` +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +``` + +In `com.imgui.example/app/build.grade`, update the min SDK version: +``` +minSdkVersion 21 +``` + + +## How to Run + +To run on a local machine using Windows PowerShell: + + 1. Run `make all` and change into the generated Android project folder, for example: `cd com.imgui.example/` + 2. Run `.\gradlew.bat build` to start the Gradle Daemons and build the project. This requires the `JAVA_HOME`, `ANDROID_HOME`, and `ANDROID_NDK_HOME` environment variables if not done in Android Studio. + - Android Studio handles these environment variables itself + 3. Run `.\gradlew.bat installDebug` to install the APK package file onto an Android virtual device or a connected real device. + + +## Some other notes + +On Windows, this variable may need to get set in the `com.imgui.sdlrenderer3/app/jni/SDL/Android.mk` file after variables are cleared. + +``` +include $(CLEAR_VARS) + +# Add this if there's Makefile error 87 +LOCAL_SHORT_COMMANDS := true + +LOCAL_MODULE := SDL3 +``` diff --git a/examples/example_sdl3_sdlrenderer3/main.cpp b/examples/example_sdl3_sdlrenderer3/main.cpp index e275e5f07..d90545316 100644 --- a/examples/example_sdl3_sdlrenderer3/main.cpp +++ b/examples/example_sdl3_sdlrenderer3/main.cpp @@ -20,6 +20,10 @@ #include "../libs/emscripten/emscripten_mainloop_stub.h" #endif +#if defined(__ANDROID__) +#include +#endif + // Main code int main(int, char**) {