0%

Android外部imgui绘制(一)

前言

上篇文章通过 Magisk Hook eglSwapBuffers 实现绘制,但是某些使用 ue4 引擎制作的游戏却并不能绘制成功.-_-
那就只能用悬浮窗的办法来绘制一个悬浮的 imgui.

方案

注意到,imgui 的绘制基本上都借助了 opengl 函数。而且 imgui 也有官方的安卓版本示例.
https://github.com/ocornut/imgui
基本上可以确定要用 ndk 开发.

其实 opengl 在 ndk 的官方示例里也有.
https://github.com/android/ndk-samples

github

gles3jni 是 opengl3 的
hello-gl2 是 opengl2 的
选哪个都可以
我选的是 hello-gl2.

然后把 imgui 的代码 clone 下来丢到 cpp 文件夹里面
imgui
如图所示.

然后打开 GL2JNIView.java , 设置透明为 true

1
2
3
4
public GL2JNIView(Context context) {
super(context);
init(true, 0, 0);
}

只需要修改 Renderer 类的 onDrawFrame,onSurfaceChanged,onSurfaceCreated 就行.

onSurfaceCreated

在 onSurfaceCreated 里完成 imgui 的初始化即可,没啥特别的。把 imgui 初始化的代码 copy 过来就行

1
2
3
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
init_ImGui(this.holder.getSurface());
}
1
2
3
4
5
6
7
8
extern "C"
JNIEXPORT void JNICALL
Java_com_shocker_nativeactivity_GL2JNIView_init_1ImGui(JNIEnv *env, jclass clazz, jobject surface) {
// TODO: implement init_ImGui()
ANativeWindow* w = ANativeWindow_fromSurface(env, (jobject)surface);
ImGuiInit(w);
ANativeWindow_release(w);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void ImGuiInit(ANativeWindow *pWindow) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
// Disable loading/saving of .ini file from disk.
// FIXME: Consider using LoadIniSettingsFromMemory() / SaveIniSettingsToMemory() to save in appropriate location for Android.
io.IniFilename = NULL;
// Setup Dear ImGui style
ImGui::StyleColorsLight();
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends

ImGui::SetColorEditOptions(ImGuiColorEditFlags_PickerHueWheel);

io.Fonts->AddFontFromMemoryTTF((void *)font_data, font_size, 24.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());

ImGui_ImplAndroid_Init(pWindow);
ImGui_ImplOpenGL3_Init("#version 300 es");
}

onSurfaceChanged

这个函数一般会在屏幕旋转的时候调用,这里重新设置下 imgui 的大小就行

1
2
3
public void onSurfaceChanged(GL10 gl, int width, int height) {
update_ImGui(width,height);
}
1
2
3
4
5
6
7
8
extern "C"
JNIEXPORT void JNICALL
Java_com_shocker_nativeactivity_GL2JNIView_update_1ImGui(JNIEnv *env, jclass clazz, jint width,
jint height) {
// TODO: implement update_ImGui()
glViewport(0, 0, width, height);
ImGui::GetIO().DisplaySize = ImVec2((float)width, (float)height);
}

onDrawFrame

这个函数就是 imgui 的绘制函数了。这里也基本上可以照搬 imgui 的 tick 函数.

1
2
3
public void onDrawFrame(GL10 gl) {
step();
}
1
2
3
4
5
6
extern "C"
JNIEXPORT void JNICALL
Java_com_shocker_nativeactivity_GL2JNIView_step(JNIEnv *env, jclass clazz) {
//....处理触摸事件
tick();
}
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
void tick()
{
ImGuiIO& io = ImGui::GetIO();
if (g_EglDisplay == EGL_NO_DISPLAY)
return;

// Our state
static bool show_demo_window = true;
static bool show_another_window = false;
static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

// Poll Unicode characters via JNI
// FIXME: do not call this every frame because of JNI overhead
PollUnicodeChars();

// Open on-screen (soft) input if requested by Dear ImGui
static bool WantTextInputLast = false;
if (io.WantTextInput && !WantTextInputLast)
ShowSoftKeyboardInput();
WantTextInputLast = io.WantTextInput;

// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplAndroid_NewFrame();
ImGui::NewFrame();

// 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!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);

// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
{
static float f = 0.0f;
static int counter = 0;

ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.

ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);

ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color

if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}

// 3. Show another simple window.
if (show_another_window)
{
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
show_another_window = false;
ImGui::End();
}

// Rendering
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// eglSwapBuffers(g_EglDisplay, g_EglSurface);
}

不过这里貌似不需要调用 eglSwapBuffers 函数.

悬浮窗

没啥好说的,直接参考我之前的 github 或者 52 论坛都行
https://github.com/PShocker/Android_float_view_draw
https://www.52pojie.cn/thread-1584152-1-1.html

注意

在我的安卓 12 的版本上,如果要添加悬浮窗必须要添加悬浮窗权限。先判断是否有权限然后再申请

1
2
3
4
if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}

aqtw