-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdraw.cpp
More file actions
181 lines (160 loc) · 5.67 KB
/
draw.cpp
File metadata and controls
181 lines (160 loc) · 5.67 KB
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
#include "font.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_android.h"
#include "imgui/imgui_impl_opengl3.h"
#include "xhook/xhook.h"
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <android/asset_manager.h>
#include <dlfcn.h>
#include <jni.h>
#include <pthread.h>
typedef EGLBoolean (*eglSwapBuffersFunc)(EGLDisplay display,
EGLSurface surface);
static eglSwapBuffersFunc original_eglSwapBuffers = NULL;
static bool init = false;
static ImVec4 clear_color = ImVec4(0, 0, 0, 0);
static double g_Time = 0;
int dpi_scale = 3;
static bool setup = false;
static bool show_demo_window = false;
int glWidth;
int glHeight;
// custom newframe function, the one provided by imgui doesnt work and im too
// lazy to fix it
void Android_NewFrame() {
ImGuiIO &io = ImGui::GetIO();
int32_t window_width = glWidth;
int32_t window_height = glHeight;
int display_width = window_width;
int display_height = window_height;
io.DisplaySize = ImVec2((float)window_width, (float)window_height);
if (window_width > 0 && window_height > 0)
io.DisplayFramebufferScale = ImVec2((float)display_width / window_width,
(float)display_height / window_height);
struct timespec current_timespec;
clock_gettime(CLOCK_MONOTONIC, ¤t_timespec);
double current_time = (double)(current_timespec.tv_sec) +
(current_timespec.tv_nsec / 1000000000.0);
io.DeltaTime =
g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
g_Time = current_time;
}
void DrawMenu() {
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
ImGui::SetNextWindowSize(ImVec2(250 * dpi_scale, 100 * dpi_scale), 0);
ImGui::Begin("Hello, world!");
ImGui::Text("This is some useful text.");
ImGui::Checkbox("Demo Window", &show_demo_window);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
void DrawImGuiMenu() {
if (init) {
ImGuiIO &io = ImGui::GetIO();
ImGui_ImplOpenGL3_NewFrame();
Android_NewFrame();
ImGui::NewFrame();
DrawMenu();
ImGui::Render();
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w,
clear_color.z * clear_color.w, clear_color.w);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
}
void SetupImGui() {
if (!init) {
auto context = ImGui::CreateContext();
if (!context) {
return;
}
ImGuiIO &io = ImGui::GetIO();
ImFontConfig font_cfg;
io.DisplaySize = ImVec2((float)glWidth, (float)glHeight);
font_cfg.SizePixels = 22.0f;
io.IniFilename = NULL;
/*
io.KeyMap[ImGuiKey_UpArrow] = 19;
io.KeyMap[ImGuiKey_DownArrow] = 20;
io.KeyMap[ImGuiKey_LeftArrow] = 21;
io.KeyMap[ImGuiKey_RightArrow] = 22;
io.KeyMap[ImGuiKey_Enter] = 66;
io.KeyMap[ImGuiKey_Backspace] = 67;
io.KeyMap[ImGuiKey_PageUp] = 92;
io.KeyMap[ImGuiKey_PageDown] = 93;
io.KeyMap[ImGuiKey_Escape] = 111;
io.KeyMap[ImGuiKey_Delete] = 112;
io.KeyMap[ImGuiKey_Home] = 122;
io.KeyMap[ImGuiKey_End] = 123;
io.KeyMap[ImGuiKey_Insert] = 124;
io.KeyMap[ImGuiKey_UpArrow] = 19;
io.KeyMap[ImGuiKey_DownArrow] = 20;
io.KeyMap[ImGuiKey_LeftArrow] = 21;
io.KeyMap[ImGuiKey_RightArrow] = 22;
io.KeyMap[ImGuiKey_Enter] = 66;
io.KeyMap[ImGuiKey_Backspace] = 67;
io.KeyMap[ImGuiKey_PageUp] = 92;
io.KeyMap[ImGuiKey_PageDown] = 93;
io.KeyMap[ImGuiKey_Escape] = 111;
io.KeyMap[ImGuiKey_Delete] = 112;
io.KeyMap[ImGuiKey_Home] = 122;
io.KeyMap[ImGuiKey_End] = 123;
io.KeyMap[ImGuiKey_Insert] = 124;
*/
ImGui::StyleColorsDark();
ImGui_ImplAndroid_Init(nullptr);
ImGui_ImplOpenGL3_Init("#version 300 es");
font_cfg.SizePixels = 22.0f;
io.Fonts->AddFontFromMemoryTTF(inter_medium, sizeof(inter_medium),
16 * dpi_scale, &font_cfg,
io.Fonts->GetGlyphRangesCyrillic());
init = true;
}
}
EGLBoolean my_eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
eglQuerySurface(display, surface, EGL_WIDTH, &glWidth);
eglQuerySurface(display, surface, EGL_HEIGHT, &glHeight);
if (!setup) {
SetupImGui();
}
setup = true;
DrawImGuiMenu();
EGLBoolean result = original_eglSwapBuffers(display, surface);
return result;
}
#define HOOK(ret, func, ...) \
ret (*orig##func)(__VA_ARGS__); \
ret my##func(__VA_ARGS__)
// hook for input handling if we dont hook it, then the application doesnt get
// our input
HOOK(void, Input, void *thiz, void *ex_ab, void *ex_ac) {
origInput(thiz, ex_ab, ex_ac);
ImGui_ImplAndroid_HandleInputEvent((AInputEvent *)thiz);
return;
}
void hook_init() {
xhook_register(".*\\.so$", "eglSwapBuffers", (void *)my_eglSwapBuffers,
(void **)&original_eglSwapBuffers); // hooking eglswapbuffers
xhook_register(".*\\.so$",
"_ZN7android13InputConsumer21initializeMotionEventEPNS_"
"11MotionEventEPKNS_12InputMessageE",
(void *)myInput, (void **)&origInput); // hooking input handler
xhook_refresh(1);
}
void *main_thread(void *) {
hook_init();
pthread_exit(NULL);
}
__attribute__((constructor)) void _init() {}
extern "C" jint JNIEXPORT JNI_OnLoad(JavaVM *vm, void *key) {
if (key != (void *)1337)
return JNI_VERSION_1_6;
pthread_t t;
pthread_create(&t, 0, main_thread, 0);
return JNI_VERSION_1_6;
}