-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnapp.cpp
278 lines (218 loc) · 7.23 KB
/
napp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <unistd.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include <android/window.h>
#include <algorithm>
#include <filesystem>
#include <chrono>
#include "log.h"
#include "imgui.h"
#include "backends/imgui_impl_android.h"
#include "backends/imgui_impl_opengl3.h"
#include "android_native_app_glue.h"
#define ANDROID_DEFAULT_FONT "/system/fonts/DroidSans.ttf"
#define LOG_TAG "napp"
thread_local ImGuiContext* MyImGuiTLS;
namespace napp {
template <typename F>
auto ensure(F&& func)
{
class _Ensure {
F _func;
public:
_Ensure(F&& func)
: _func(std::move(func))
{
}
~_Ensure()
{
_func();
}
};
return _Ensure(std::move(func));
};
struct NativeApp {
struct android_app* _app;
bool _initialized { false };
EGLDisplay _egl_display { EGL_NO_DISPLAY };
EGLConfig _egl_context { EGL_NO_CONTEXT };
EGLSurface _egl_surface { EGL_NO_SURFACE };
static void onAppCmd(struct android_app* app, int32_t cmd)
{
auto* self = reinterpret_cast<NativeApp*>(app->userData);
switch (cmd) {
case APP_CMD_CONFIG_CHANGED:
LOGI("density %d", AConfiguration_getDensity(app->config));
AConfiguration_setDensity(app->config, ACONFIGURATION_DENSITY_ANY);
break;
case APP_CMD_SAVE_STATE:
break;
case APP_CMD_INIT_WINDOW:
self->init();
break;
case APP_CMD_TERM_WINDOW:
self->shutdown();
break;
}
}
static int32_t onInputEvent(struct android_app*, AInputEvent* input_event)
{
return ImGui_ImplAndroid_HandleInputEvent(input_event);
}
void init()
{
if (_initialized) {
return;
}
ANativeWindow_acquire(_app->window);
_egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (_egl_display == EGL_NO_DISPLAY) {
LOGE("eglGetDisplay failed");
return;
}
if (eglInitialize(_egl_display, 0, 0) != EGL_TRUE) {
LOGE("eglInitialize failed");
return;
}
const EGLint egl_attributes[] = {
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 16,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLint num_configs{0};
if (eglChooseConfig(_egl_display, egl_attributes, nullptr, 0, &num_configs) != EGL_TRUE) {
LOGE("eglChooseConfig failed");
return;
}
if (num_configs == 0) {
LOGE("eglChooseConfig returns no config");
return;
}
EGLConfig egl_config{0};
eglChooseConfig(_egl_display, egl_attributes, &egl_config, 1, &num_configs);
EGLint egl_format{0};
eglGetConfigAttrib(_egl_display, egl_config, EGL_NATIVE_VISUAL_ID, &egl_format);
ANativeWindow_setBuffersGeometry(_app->window, 0, 0, egl_format);
const EGLint egl_context_attrs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
};
_egl_context = eglCreateContext(_egl_display, egl_config, EGL_NO_CONTEXT, egl_context_attrs);
if (_egl_context == EGL_NO_CONTEXT) {
LOGE("eglCreateContext failed");
return;
}
_egl_surface = eglCreateWindowSurface(_egl_display, egl_config, _app->window, nullptr);
if (_egl_surface == EGL_NO_SURFACE) {
LOGE("eglCreateWindowSurface failed");
return;
}
if (eglMakeCurrent(_egl_display, _egl_surface, _egl_surface, _egl_context) != EGL_TRUE) {
LOGE("eglMakeCurrent failed");
return;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
auto& io = ImGui::GetIO();
ImGui_ImplAndroid_Init(_app->window);
ImGui_ImplOpenGL3_Init("#version 300 es");
float scale = 1.0f;
{
auto& io = ImGui::GetIO();
float window_width = ANativeWindow_getWidth(_app->window);
float window_height = ANativeWindow_getHeight(_app->window);
scale = window_width / AConfiguration_getScreenWidthDp(_app->config);
}
LOGI("scale %f", scale);
const auto font_size = 22.0f * scale;
if (std::filesystem::exists(ANDROID_DEFAULT_FONT)) {
io.FontDefault = io.Fonts->AddFontFromFileTTF(ANDROID_DEFAULT_FONT, font_size);
} else {
ImFontConfig font_cfg{};
font_cfg.SizePixels = font_size;
io.Fonts->AddFontDefault(&font_cfg);
}
ImGui::GetStyle().ScaleAllSizes(scale);
LOGI("initialized");
_initialized = true;
}
void shutdown()
{
if (not _initialized)
return;
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplAndroid_Shutdown();
ImGui::DestroyContext();
if (_egl_display != EGL_NO_DISPLAY) {
eglMakeCurrent(_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (_egl_context != EGL_NO_CONTEXT) {
eglDestroyContext(_egl_display, _egl_context);
_egl_context = EGL_NO_CONTEXT;
}
if (_egl_surface != EGL_NO_SURFACE) {
eglDestroySurface(_egl_display, _egl_surface);
_egl_surface = EGL_NO_SURFACE;
}
eglTerminate(_egl_display);
_egl_display = EGL_NO_DISPLAY;
}
ANativeWindow_release(_app->window);
_initialized = false;
}
void tick()
{
if (not _initialized) {
return;
}
const ImVec4 clear_color{ 0.45f, 0.55f, 0.60f, 1.00f };
auto& io = ImGui::GetIO();
if (_egl_display == EGL_NO_DISPLAY) {
return;
}
ImGui_ImplAndroid_NewFrame();
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
#if 0
ImGui::Begin("Hello", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
ImGui::SetWindowPos({0.0f, 0.0f});
ImGui::SetWindowSize(io.DisplaySize);
ImGui::Text("This is some useful text.");
ImGui::End();
#else
ImGui::ShowDemoWindow();
#endif
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(0.0f, 0.0f,0.0f, .7f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
eglSwapBuffers(_egl_display, _egl_surface);
}
};
} // namespace napp
void android_main(struct android_app* app)
{
using namespace napp;
NativeApp napp {};
napp._app = app;
app->userData = &napp;
app->onAppCmd = NativeApp::onAppCmd;
app->onInputEvent = NativeApp::onInputEvent;
while (true) {
int ident;
int events;
struct android_poll_source* source;
while ((ident = ALooper_pollAll(napp._initialized ? 0 : -1, nullptr, &events, (void**)&source)) >= 0) {
if (source != nullptr) {
source->process(app, source);
}
if (app->destroyRequested != 0) {
napp.shutdown();
}
}
napp.tick();
}
}