| 1 | // dear imgui: standalone example application for Android + OpenGL ES 3 |
| 2 | |
| 3 | // Learn about Dear ImGui: |
| 4 | // - FAQ https://dearimgui.com/faq |
| 5 | // - Getting Started https://dearimgui.com/getting-started |
| 6 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 7 | // - Introduction, links and more at the top of imgui.cpp |
| 8 | |
| 9 | #include "imgui.h" |
| 10 | #include "imgui_impl_android.h" |
| 11 | #include "imgui_impl_opengl3.h" |
| 12 | #include <android/log.h> |
| 13 | #include <android_native_app_glue.h> |
| 14 | #include <android/asset_manager.h> |
| 15 | #include <EGL/egl.h> |
| 16 | #include <GLES3/gl3.h> |
| 17 | #include <string> |
| 18 | |
| 19 | // Data |
| 20 | static EGLDisplay g_EglDisplay = EGL_NO_DISPLAY; |
| 21 | static EGLSurface g_EglSurface = EGL_NO_SURFACE; |
| 22 | static EGLContext g_EglContext = EGL_NO_CONTEXT; |
| 23 | static struct android_app* g_App = nullptr; |
| 24 | static bool g_Initialized = false; |
| 25 | static char g_LogTag[] = "ImGuiExample" ; |
| 26 | static std::string g_IniFilename = "" ; |
| 27 | |
| 28 | // Forward declarations of helper functions |
| 29 | static void Init(struct android_app* app); |
| 30 | static void Shutdown(); |
| 31 | static void MainLoopStep(); |
| 32 | static int ShowSoftKeyboardInput(); |
| 33 | static int PollUnicodeChars(); |
| 34 | static int GetAssetData(const char* filename, void** out_data); |
| 35 | |
| 36 | // Main code |
| 37 | static void handleAppCmd(struct android_app* app, int32_t appCmd) |
| 38 | { |
| 39 | switch (appCmd) |
| 40 | { |
| 41 | case APP_CMD_SAVE_STATE: |
| 42 | break; |
| 43 | case APP_CMD_INIT_WINDOW: |
| 44 | Init(app); |
| 45 | break; |
| 46 | case APP_CMD_TERM_WINDOW: |
| 47 | Shutdown(); |
| 48 | break; |
| 49 | case APP_CMD_GAINED_FOCUS: |
| 50 | case APP_CMD_LOST_FOCUS: |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static int32_t handleInputEvent(struct android_app* app, AInputEvent* inputEvent) |
| 56 | { |
| 57 | return ImGui_ImplAndroid_HandleInputEvent(inputEvent); |
| 58 | } |
| 59 | |
| 60 | void android_main(struct android_app* app) |
| 61 | { |
| 62 | app->onAppCmd = handleAppCmd; |
| 63 | app->onInputEvent = handleInputEvent; |
| 64 | |
| 65 | while (true) |
| 66 | { |
| 67 | int out_events; |
| 68 | struct android_poll_source* out_data; |
| 69 | |
| 70 | // Poll all events. If the app is not visible, this loop blocks until g_Initialized == true. |
| 71 | while (ALooper_pollOnce(g_Initialized ? 0 : -1, nullptr, &out_events, (void**)&out_data) >= 0) |
| 72 | { |
| 73 | // Process one event |
| 74 | if (out_data != nullptr) |
| 75 | out_data->process(app, out_data); |
| 76 | |
| 77 | // Exit the app by returning from within the infinite loop |
| 78 | if (app->destroyRequested != 0) |
| 79 | { |
| 80 | // shutdown() should have been called already while processing the |
| 81 | // app command APP_CMD_TERM_WINDOW. But we play save here |
| 82 | if (!g_Initialized) |
| 83 | Shutdown(); |
| 84 | |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Initiate a new frame |
| 90 | MainLoopStep(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void Init(struct android_app* app) |
| 95 | { |
| 96 | if (g_Initialized) |
| 97 | return; |
| 98 | |
| 99 | g_App = app; |
| 100 | ANativeWindow_acquire(g_App->window); |
| 101 | |
| 102 | // Initialize EGL |
| 103 | // This is mostly boilerplate code for EGL... |
| 104 | { |
| 105 | g_EglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 106 | if (g_EglDisplay == EGL_NO_DISPLAY) |
| 107 | __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s" , "eglGetDisplay(EGL_DEFAULT_DISPLAY) returned EGL_NO_DISPLAY" ); |
| 108 | |
| 109 | if (eglInitialize(dpy: g_EglDisplay, major: 0, minor: 0) != EGL_TRUE) |
| 110 | __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s" , "eglInitialize() returned with an error" ); |
| 111 | |
| 112 | const EGLint egl_attributes[] = { EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; |
| 113 | EGLint num_configs = 0; |
| 114 | if (eglChooseConfig(dpy: g_EglDisplay, attrib_list: egl_attributes, configs: nullptr, config_size: 0, num_config: &num_configs) != EGL_TRUE) |
| 115 | __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s" , "eglChooseConfig() returned with an error" ); |
| 116 | if (num_configs == 0) |
| 117 | __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s" , "eglChooseConfig() returned 0 matching config" ); |
| 118 | |
| 119 | // Get the first matching config |
| 120 | EGLConfig egl_config; |
| 121 | eglChooseConfig(dpy: g_EglDisplay, attrib_list: egl_attributes, configs: &egl_config, config_size: 1, num_config: &num_configs); |
| 122 | EGLint egl_format; |
| 123 | eglGetConfigAttrib(dpy: g_EglDisplay, config: egl_config, EGL_NATIVE_VISUAL_ID, value: &egl_format); |
| 124 | ANativeWindow_setBuffersGeometry(g_App->window, 0, 0, egl_format); |
| 125 | |
| 126 | const EGLint egl_context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE }; |
| 127 | g_EglContext = eglCreateContext(dpy: g_EglDisplay, config: egl_config, EGL_NO_CONTEXT, attrib_list: egl_context_attributes); |
| 128 | |
| 129 | if (g_EglContext == EGL_NO_CONTEXT) |
| 130 | __android_log_print(ANDROID_LOG_ERROR, g_LogTag, "%s" , "eglCreateContext() returned EGL_NO_CONTEXT" ); |
| 131 | |
| 132 | g_EglSurface = eglCreateWindowSurface(g_EglDisplay, egl_config, g_App->window, nullptr); |
| 133 | eglMakeCurrent(dpy: g_EglDisplay, draw: g_EglSurface, read: g_EglSurface, ctx: g_EglContext); |
| 134 | } |
| 135 | |
| 136 | // Setup Dear ImGui context |
| 137 | IMGUI_CHECKVERSION(); |
| 138 | ImGui::CreateContext(); |
| 139 | ImGuiIO& io = ImGui::GetIO(); |
| 140 | |
| 141 | // Redirect loading/saving of .ini file to our location. |
| 142 | // Make sure 'g_IniFilename' persists while we use Dear ImGui. |
| 143 | g_IniFilename = std::string(app->activity->internalDataPath) + "/imgui.ini" ; |
| 144 | io.IniFilename = g_IniFilename.c_str();; |
| 145 | |
| 146 | // Setup Dear ImGui style |
| 147 | ImGui::StyleColorsDark(); |
| 148 | //ImGui::StyleColorsLight(); |
| 149 | |
| 150 | // Setup Platform/Renderer backends |
| 151 | ImGui_ImplAndroid_Init(g_App->window); |
| 152 | ImGui_ImplOpenGL3_Init(glsl_version: "#version 300 es" ); |
| 153 | |
| 154 | // Load Fonts |
| 155 | // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. |
| 156 | // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). |
| 157 | // - Read 'docs/FONTS.md' for more instructions and details. |
| 158 | // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! |
| 159 | // - Android: The TTF files have to be placed into the assets/ directory (android/app/src/main/assets), we use our GetAssetData() helper to retrieve them. |
| 160 | |
| 161 | // We load the default font with increased size to improve readability on many devices with "high" DPI. |
| 162 | // FIXME: Put some effort into DPI awareness. |
| 163 | // Important: when calling AddFontFromMemoryTTF(), ownership of font_data is transferred by Dear ImGui by default (deleted is handled by Dear ImGui), unless we set FontDataOwnedByAtlas=false in ImFontConfig |
| 164 | ImFontConfig font_cfg; |
| 165 | font_cfg.SizePixels = 22.0f; |
| 166 | io.Fonts->AddFontDefault(font_cfg: &font_cfg); |
| 167 | //void* font_data; |
| 168 | //int font_data_size; |
| 169 | //ImFont* font; |
| 170 | //font_data_size = GetAssetData("segoeui.ttf", &font_data); |
| 171 | //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 16.0f); |
| 172 | //IM_ASSERT(font != nullptr); |
| 173 | //font_data_size = GetAssetData("DroidSans.ttf", &font_data); |
| 174 | //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 16.0f); |
| 175 | //IM_ASSERT(font != nullptr); |
| 176 | //font_data_size = GetAssetData("Roboto-Medium.ttf", &font_data); |
| 177 | //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 16.0f); |
| 178 | //IM_ASSERT(font != nullptr); |
| 179 | //font_data_size = GetAssetData("Cousine-Regular.ttf", &font_data); |
| 180 | //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 15.0f); |
| 181 | //IM_ASSERT(font != nullptr); |
| 182 | //font_data_size = GetAssetData("ArialUni.ttf", &font_data); |
| 183 | //font = io.Fonts->AddFontFromMemoryTTF(font_data, font_data_size, 18.0f); |
| 184 | //IM_ASSERT(font != nullptr); |
| 185 | |
| 186 | // Arbitrary scale-up |
| 187 | // FIXME: Put some effort into DPI awareness |
| 188 | ImGui::GetStyle().ScaleAllSizes(scale_factor: 3.0f); |
| 189 | |
| 190 | g_Initialized = true; |
| 191 | } |
| 192 | |
| 193 | void MainLoopStep() |
| 194 | { |
| 195 | ImGuiIO& io = ImGui::GetIO(); |
| 196 | if (g_EglDisplay == EGL_NO_DISPLAY) |
| 197 | return; |
| 198 | |
| 199 | // Our state |
| 200 | // (we use static, which essentially makes the variable globals, as a convenience to keep the example code easy to follow) |
| 201 | static bool show_demo_window = true; |
| 202 | static bool show_another_window = false; |
| 203 | static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 204 | |
| 205 | // Poll Unicode characters via JNI |
| 206 | // FIXME: do not call this every frame because of JNI overhead |
| 207 | PollUnicodeChars(); |
| 208 | |
| 209 | // Open on-screen (soft) input if requested by Dear ImGui |
| 210 | static bool WantTextInputLast = false; |
| 211 | if (io.WantTextInput && !WantTextInputLast) |
| 212 | ShowSoftKeyboardInput(); |
| 213 | WantTextInputLast = io.WantTextInput; |
| 214 | |
| 215 | // Start the Dear ImGui frame |
| 216 | ImGui_ImplOpenGL3_NewFrame(); |
| 217 | ImGui_ImplAndroid_NewFrame(); |
| 218 | ImGui::NewFrame(); |
| 219 | |
| 220 | // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). |
| 221 | if (show_demo_window) |
| 222 | ImGui::ShowDemoWindow(p_open: &show_demo_window); |
| 223 | |
| 224 | // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window. |
| 225 | { |
| 226 | static float f = 0.0f; |
| 227 | static int counter = 0; |
| 228 | |
| 229 | ImGui::Begin(name: "Hello, world!" ); // Create a window called "Hello, world!" and append into it. |
| 230 | |
| 231 | ImGui::Text(fmt: "This is some useful text." ); // Display some text (you can use a format strings too) |
| 232 | ImGui::Checkbox(label: "Demo Window" , v: &show_demo_window); // Edit bools storing our window open/close state |
| 233 | ImGui::Checkbox(label: "Another Window" , v: &show_another_window); |
| 234 | |
| 235 | ImGui::SliderFloat(label: "float" , v: &f, v_min: 0.0f, v_max: 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f |
| 236 | ImGui::ColorEdit3(label: "clear color" , col: (float*)&clear_color); // Edit 3 floats representing a color |
| 237 | |
| 238 | if (ImGui::Button(label: "Button" )) // Buttons return true when clicked (most widgets return true when edited/activated) |
| 239 | counter++; |
| 240 | ImGui::SameLine(); |
| 241 | ImGui::Text(fmt: "counter = %d" , counter); |
| 242 | |
| 243 | ImGui::Text(fmt: "Application average %.3f ms/frame (%.1f FPS)" , 1000.0f / io.Framerate, io.Framerate); |
| 244 | ImGui::End(); |
| 245 | } |
| 246 | |
| 247 | // 3. Show another simple window. |
| 248 | if (show_another_window) |
| 249 | { |
| 250 | ImGui::Begin(name: "Another Window" , p_open: &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) |
| 251 | ImGui::Text(fmt: "Hello from another window!" ); |
| 252 | if (ImGui::Button(label: "Close Me" )) |
| 253 | show_another_window = false; |
| 254 | ImGui::End(); |
| 255 | } |
| 256 | |
| 257 | // Rendering |
| 258 | ImGui::Render(); |
| 259 | glViewport(x: 0, y: 0, width: (int)io.DisplaySize.x, height: (int)io.DisplaySize.y); |
| 260 | glClearColor(red: clear_color.x * clear_color.w, green: clear_color.y * clear_color.w, blue: clear_color.z * clear_color.w, alpha: clear_color.w); |
| 261 | glClear(GL_COLOR_BUFFER_BIT); |
| 262 | ImGui_ImplOpenGL3_RenderDrawData(draw_data: ImGui::GetDrawData()); |
| 263 | eglSwapBuffers(dpy: g_EglDisplay, surface: g_EglSurface); |
| 264 | } |
| 265 | |
| 266 | void Shutdown() |
| 267 | { |
| 268 | if (!g_Initialized) |
| 269 | return; |
| 270 | |
| 271 | // Cleanup |
| 272 | ImGui_ImplOpenGL3_Shutdown(); |
| 273 | ImGui_ImplAndroid_Shutdown(); |
| 274 | ImGui::DestroyContext(); |
| 275 | |
| 276 | if (g_EglDisplay != EGL_NO_DISPLAY) |
| 277 | { |
| 278 | eglMakeCurrent(dpy: g_EglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 279 | |
| 280 | if (g_EglContext != EGL_NO_CONTEXT) |
| 281 | eglDestroyContext(dpy: g_EglDisplay, ctx: g_EglContext); |
| 282 | |
| 283 | if (g_EglSurface != EGL_NO_SURFACE) |
| 284 | eglDestroySurface(dpy: g_EglDisplay, surface: g_EglSurface); |
| 285 | |
| 286 | eglTerminate(dpy: g_EglDisplay); |
| 287 | } |
| 288 | |
| 289 | g_EglDisplay = EGL_NO_DISPLAY; |
| 290 | g_EglContext = EGL_NO_CONTEXT; |
| 291 | g_EglSurface = EGL_NO_SURFACE; |
| 292 | ANativeWindow_release(g_App->window); |
| 293 | |
| 294 | g_Initialized = false; |
| 295 | } |
| 296 | |
| 297 | // Helper functions |
| 298 | |
| 299 | // Unfortunately, there is no way to show the on-screen input from native code. |
| 300 | // Therefore, we call ShowSoftKeyboardInput() of the main activity implemented in MainActivity.kt via JNI. |
| 301 | static int ShowSoftKeyboardInput() |
| 302 | { |
| 303 | JavaVM* java_vm = g_App->activity->vm; |
| 304 | JNIEnv* java_env = nullptr; |
| 305 | |
| 306 | jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); |
| 307 | if (jni_return == JNI_ERR) |
| 308 | return -1; |
| 309 | |
| 310 | jni_return = java_vm->AttachCurrentThread(&java_env, nullptr); |
| 311 | if (jni_return != JNI_OK) |
| 312 | return -2; |
| 313 | |
| 314 | jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); |
| 315 | if (native_activity_clazz == nullptr) |
| 316 | return -3; |
| 317 | |
| 318 | jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "showSoftInput" , "()V" ); |
| 319 | if (method_id == nullptr) |
| 320 | return -4; |
| 321 | |
| 322 | java_env->CallVoidMethod(g_App->activity->clazz, method_id); |
| 323 | |
| 324 | jni_return = java_vm->DetachCurrentThread(); |
| 325 | if (jni_return != JNI_OK) |
| 326 | return -5; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | // Unfortunately, the native KeyEvent implementation has no getUnicodeChar() function. |
| 332 | // Therefore, we implement the processing of KeyEvents in MainActivity.kt and poll |
| 333 | // the resulting Unicode characters here via JNI and send them to Dear ImGui. |
| 334 | static int PollUnicodeChars() |
| 335 | { |
| 336 | JavaVM* java_vm = g_App->activity->vm; |
| 337 | JNIEnv* java_env = nullptr; |
| 338 | |
| 339 | jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); |
| 340 | if (jni_return == JNI_ERR) |
| 341 | return -1; |
| 342 | |
| 343 | jni_return = java_vm->AttachCurrentThread(&java_env, nullptr); |
| 344 | if (jni_return != JNI_OK) |
| 345 | return -2; |
| 346 | |
| 347 | jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); |
| 348 | if (native_activity_clazz == nullptr) |
| 349 | return -3; |
| 350 | |
| 351 | jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "pollUnicodeChar" , "()I" ); |
| 352 | if (method_id == nullptr) |
| 353 | return -4; |
| 354 | |
| 355 | // Send the actual characters to Dear ImGui |
| 356 | ImGuiIO& io = ImGui::GetIO(); |
| 357 | jint unicode_character; |
| 358 | while ((unicode_character = java_env->CallIntMethod(g_App->activity->clazz, method_id)) != 0) |
| 359 | io.AddInputCharacter(c: unicode_character); |
| 360 | |
| 361 | jni_return = java_vm->DetachCurrentThread(); |
| 362 | if (jni_return != JNI_OK) |
| 363 | return -5; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | // Helper to retrieve data placed into the assets/ directory (android/app/src/main/assets) |
| 369 | static int GetAssetData(const char* filename, void** outData) |
| 370 | { |
| 371 | int num_bytes = 0; |
| 372 | AAsset* asset_descriptor = AAssetManager_open(g_App->activity->assetManager, filename, AASSET_MODE_BUFFER); |
| 373 | if (asset_descriptor) |
| 374 | { |
| 375 | num_bytes = AAsset_getLength(asset_descriptor); |
| 376 | *outData = IM_ALLOC(num_bytes); |
| 377 | int64_t num_bytes_read = AAsset_read(asset_descriptor, *outData, num_bytes); |
| 378 | AAsset_close(asset_descriptor); |
| 379 | IM_ASSERT(num_bytes_read == num_bytes); |
| 380 | } |
| 381 | return num_bytes; |
| 382 | } |
| 383 | |