| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 71 | while (ALooper_pollOnce(g_Initialized ? 0 : -1, nullptr, &out_events, (void**)&out_data) >= 0) |
| 72 | { |
| 73 | |
| 74 | if (out_data != nullptr) |
| 75 | out_data->process(app, out_data); |
| 76 | |
| 77 | |
| 78 | if (app->destroyRequested != 0) |
| 79 | { |
| 80 | |
| 81 | |
| 82 | if (!g_Initialized) |
| 83 | Shutdown(); |
| 84 | |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 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 | |
| 103 | |
| 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(g_EglDisplay, 0, 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(g_EglDisplay, egl_attributes, nullptr, 0, &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 | |
| 120 | EGLConfig egl_config; |
| 121 | eglChooseConfig(g_EglDisplay, egl_attributes, &egl_config, 1, &num_configs); |
| 122 | EGLint egl_format; |
| 123 | eglGetConfigAttrib(g_EglDisplay, egl_config, EGL_NATIVE_VISUAL_ID, &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(g_EglDisplay, egl_config, EGL_NO_CONTEXT, 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(g_EglDisplay, g_EglSurface, g_EglSurface, g_EglContext); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | IMGUI_CHECKVERSION(); |
| 138 | ImGui::CreateContext(); |
| 139 | ImGuiIO& io = ImGui::GetIO(); |
| 140 | |
| 141 | |
| 142 | |
| 143 | g_IniFilename = std::string(app->activity->internalDataPath) + "/imgui.ini"; |
| 144 | io.IniFilename = g_IniFilename.c_str();; |
| 145 | |
| 146 | |
| 147 | ImGui::StyleColorsDark(); |
| 148 | |
| 149 | |
| 150 | |
| 151 | ImGui_ImplAndroid_Init(g_App->window); |
| 152 | ImGui_ImplOpenGL3_Init("#version 300 es"); |
| 153 | |
| 154 | |
| 155 | float main_scale = 2.0f; |
| 156 | ImGuiStyle& style = ImGui::GetStyle(); |
| 157 | style.ScaleAllSizes(main_scale); |
| 158 | style.FontScaleDpi = main_scale; |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | g_Initialized = true; |
| 194 | } |
| 195 | |
| 196 | void MainLoopStep() |
| 197 | { |
| 198 | ImGuiIO& io = ImGui::GetIO(); |
| 199 | if (g_EglDisplay == EGL_NO_DISPLAY) |
| 200 | return; |
| 201 | |
| 202 | |
| 203 | |
| 204 | static bool show_demo_window = true; |
| 205 | static bool show_another_window = false; |
| 206 | static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| 207 | |
| 208 | |
| 209 | |
| 210 | PollUnicodeChars(); |
| 211 | |
| 212 | |
| 213 | static bool WantTextInputLast = false; |
| 214 | if (io.WantTextInput && !WantTextInputLast) |
| 215 | ShowSoftKeyboardInput(); |
| 216 | WantTextInputLast = io.WantTextInput; |
| 217 | |
| 218 | |
| 219 | ImGui_ImplOpenGL3_NewFrame(); |
| 220 | ImGui_ImplAndroid_NewFrame(); |
| 221 | ImGui::NewFrame(); |
| 222 | |
| 223 | |
| 224 | if (show_demo_window) |
| 225 | ImGui::ShowDemoWindow(&show_demo_window); |
| 226 | |
| 227 | |
| 228 | { |
| 229 | static float f = 0.0f; |
| 230 | static int counter = 0; |
| 231 | |
| 232 | ImGui::Begin("Hello, world!"); |
| 233 | |
| 234 | ImGui::Text("This is some useful text."); |
| 235 | ImGui::Checkbox("Demo Window", &show_demo_window); |
| 236 | ImGui::Checkbox("Another Window", &show_another_window); |
| 237 | |
| 238 | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| 239 | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| 240 | |
| 241 | if (ImGui::Button("Button")) |
| 242 | counter++; |
| 243 | ImGui::SameLine(); |
| 244 | ImGui::Text("counter = %d", counter); |
| 245 | |
| 246 | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); |
| 247 | ImGui::End(); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | if (show_another_window) |
| 252 | { |
| 253 | ImGui::Begin("Another Window", &show_another_window); |
| 254 | ImGui::Text("Hello from another window!"); |
| 255 | if (ImGui::Button("Close Me")) |
| 256 | show_another_window = false; |
| 257 | ImGui::End(); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | ImGui::Render(); |
| 262 | glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); |
| 263 | glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); |
| 264 | glClear(GL_COLOR_BUFFER_BIT); |
| 265 | ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); |
| 266 | eglSwapBuffers(g_EglDisplay, g_EglSurface); |
| 267 | } |
| 268 | |
| 269 | void Shutdown() |
| 270 | { |
| 271 | if (!g_Initialized) |
| 272 | return; |
| 273 | |
| 274 | |
| 275 | ImGui_ImplOpenGL3_Shutdown(); |
| 276 | ImGui_ImplAndroid_Shutdown(); |
| 277 | ImGui::DestroyContext(); |
| 278 | |
| 279 | if (g_EglDisplay != EGL_NO_DISPLAY) |
| 280 | { |
| 281 | eglMakeCurrent(g_EglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 282 | |
| 283 | if (g_EglContext != EGL_NO_CONTEXT) |
| 284 | eglDestroyContext(g_EglDisplay, g_EglContext); |
| 285 | |
| 286 | if (g_EglSurface != EGL_NO_SURFACE) |
| 287 | eglDestroySurface(g_EglDisplay, g_EglSurface); |
| 288 | |
| 289 | eglTerminate(g_EglDisplay); |
| 290 | } |
| 291 | |
| 292 | g_EglDisplay = EGL_NO_DISPLAY; |
| 293 | g_EglContext = EGL_NO_CONTEXT; |
| 294 | g_EglSurface = EGL_NO_SURFACE; |
| 295 | ANativeWindow_release(g_App->window); |
| 296 | |
| 297 | g_Initialized = false; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | static int ShowSoftKeyboardInput() |
| 305 | { |
| 306 | JavaVM* java_vm = g_App->activity->vm; |
| 307 | JNIEnv* java_env = nullptr; |
| 308 | |
| 309 | jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); |
| 310 | if (jni_return == JNI_ERR) |
| 311 | return -1; |
| 312 | |
| 313 | jni_return = java_vm->AttachCurrentThread(&java_env, nullptr); |
| 314 | if (jni_return != JNI_OK) |
| 315 | return -2; |
| 316 | |
| 317 | jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); |
| 318 | if (native_activity_clazz == nullptr) |
| 319 | return -3; |
| 320 | |
| 321 | jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "showSoftInput", "()V"); |
| 322 | if (method_id == nullptr) |
| 323 | return -4; |
| 324 | |
| 325 | java_env->CallVoidMethod(g_App->activity->clazz, method_id); |
| 326 | |
| 327 | jni_return = java_vm->DetachCurrentThread(); |
| 328 | if (jni_return != JNI_OK) |
| 329 | return -5; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | static int PollUnicodeChars() |
| 338 | { |
| 339 | JavaVM* java_vm = g_App->activity->vm; |
| 340 | JNIEnv* java_env = nullptr; |
| 341 | |
| 342 | jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6); |
| 343 | if (jni_return == JNI_ERR) |
| 344 | return -1; |
| 345 | |
| 346 | jni_return = java_vm->AttachCurrentThread(&java_env, nullptr); |
| 347 | if (jni_return != JNI_OK) |
| 348 | return -2; |
| 349 | |
| 350 | jclass native_activity_clazz = java_env->GetObjectClass(g_App->activity->clazz); |
| 351 | if (native_activity_clazz == nullptr) |
| 352 | return -3; |
| 353 | |
| 354 | jmethodID method_id = java_env->GetMethodID(native_activity_clazz, "pollUnicodeChar", "()I"); |
| 355 | if (method_id == nullptr) |
| 356 | return -4; |
| 357 | |
| 358 | |
| 359 | ImGuiIO& io = ImGui::GetIO(); |
| 360 | jint unicode_character; |
| 361 | while ((unicode_character = java_env->CallIntMethod(g_App->activity->clazz, method_id)) != 0) |
| 362 | io.AddInputCharacter(unicode_character); |
| 363 | |
| 364 | jni_return = java_vm->DetachCurrentThread(); |
| 365 | if (jni_return != JNI_OK) |
| 366 | return -5; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | static int GetAssetData(const char* filename, void** outData) |
| 373 | { |
| 374 | int num_bytes = 0; |
| 375 | AAsset* asset_descriptor = AAssetManager_open(g_App->activity->assetManager, filename, AASSET_MODE_BUFFER); |
| 376 | if (asset_descriptor) |
| 377 | { |
| 378 | num_bytes = AAsset_getLength(asset_descriptor); |
| 379 | *outData = IM_ALLOC(num_bytes); |
| 380 | int64_t num_bytes_read = AAsset_read(asset_descriptor, *outData, num_bytes); |
| 381 | AAsset_close(asset_descriptor); |
| 382 | IM_ASSERT(num_bytes_read == num_bytes); |
| 383 | } |
| 384 | return num_bytes; |
| 385 | } |