Задача: чтобы сделать правильный изменение размера, который будет работать на разных дисплеях, с разными размерами экрана и DPI < /p>
о коде: Основной код - C ++, UI - IMGUI, Render - Open Gles 3 в файле Java. Java встроена в часть C ++ через C Extern. Приложение - Android. Скомпилируйте Aide (Adroid App) < /p>
main.cpp:
Код: Выделить всё
extern "C" {
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_init(JNIEnv* env, jclass cls);
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_resize(JNIEnv* env, jobject obj, jint width, jint height);
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_step(JNIEnv* env, jobject obj);
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_imgui_Shutdown(JNIEnv* env, jobject obj);
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_MotionEventClick(JNIEnv* env, jobject obj,jboolean down,jfloat PosX,jfloat PosY);
JNIEXPORT jstring JNICALL Java_com_AnexityMOD_GLES3JNIView_getWindowRect(JNIEnv *env, jobject thiz);
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_real(JNIEnv* env, jobject obj, jint width, jint height);
};
JNIEXPORT void JNICALL
Java_com_AnexityMOD_GLES3JNIView_init(JNIEnv* env, jclass cls) {
//... ImGui init
}
JNIEXPORT void JNICALL
Java_com_AnexityMOD_GLES3JNIView_resize(JNIEnv* env, jobject obj, jint width, jint height) {
screenWidth = (int) width;
screenHeight = (int) height;
glViewport(0, 0, width, height);
ImGuiIO &io = ImGui::GetIO();
io.ConfigWindowsMoveFromTitleBarOnly = true;
io.IniFilename = NULL;
ImGui::GetIO().DisplaySize = ImVec2((float)width, (float)height);
}
void BeginDraw() {
//... GUI code with ImGui
}
JNIEXPORT void JNICALL
Java_com_AnexityMOD_GLES3JNIView_step(JNIEnv* env, jobject obj) {
if(!AllChecksDone) return;
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplAndroid_NewFrame(screenWidth, screenHeight);
ImGui::NewFrame();
//===== Main Menu
if (show_MainMenu_window) { BeginDraw(); }
ImGui::Render();
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_imgui_Shutdown(JNIEnv* env, jobject obj){
if (!g_Initialized)
return;
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplAndroid_Shutdown();
ImGui::DestroyContext();
g_Initialized=false;
}
JNIEXPORT void JNICALL Java_com_AnexityMOD_GLES3JNIView_MotionEventClick(JNIEnv* env, jobject obj,jboolean down,jfloat PosX,jfloat PosY){
ImGuiIO & io = ImGui::GetIO();
io.MouseDown[0] = down;
io.MousePos = ImVec2(PosX,PosY);
}
JNIEXPORT jstring JNICALL Java_com_AnexityMOD_GLES3JNIView_getWindowRect(JNIEnv *env, jobject thiz) {
char result[256]="0|0|0|0";
if(g_window){
sprintf(result,"%d|%d|%d|%d",(int)g_window->Pos.x,(int)g_window->Pos.y,(int)g_window->Size.x,(int)g_window->Size.y);
}
return env->NewStringUTF(result);
}
< /code>
gles3jniview.java:
package com.AnexityMOD;
import android.opengl.GLSurfaceView;
import android.content.Context;
import android.graphics.PixelFormat;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;
public class GLES3JNIView extends GLSurfaceView implements GLSurfaceView.Renderer {
public static byte fontData[];
public GLES3JNIView(Context context) {
super(context);
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(-3);
setEGLContextClientVersion(3);
setRenderer(this);
}
public void onDrawFrame(GL10 gl) {
step();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
resize(width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
init();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
imgui_Shutdown();
}
public static native void init();
public static native void resize(int width, int height);
public static native void step();
public static native void imgui_Shutdown();
public static native void MotionEventClick(boolean down,float PosX,float PosY);
public static native String getWindowRect();
}
< /code>
mainactivity.java:
package com.AnexityMOD;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.LinearLayout;
import android.view.WindowManager;
import com.AnexityMOD.GLES3JNIView;
import java.io.InputStream;
import android.view.WindowManager.LayoutParams;
import android.view.Display;
import android.provider.Settings;
import android.widget.Toast;
import android.content.Intent;
import android.Manifest;
import android.net.Uri;
import java.io.IOException;
import java.io.File;
import android.util.Log;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import android.content.pm.PackageManager;
public class MainActivity extends Activity {
static{
System.loadLibrary("Anexity");
}
public static WindowManager manager;
public static WindowManager.LayoutParams vParams;
public static View vTouch;
public static WindowManager windowManager,xfqManager;
public static int 真实宽;//分辨率x
public static int 真实高;//分辨率y
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!Settings.canDrawOverlays(this)) {
}
Start(this);
}
public static void Start(Context context) {
manager = ((Activity) context).getWindowManager();
vParams = getAttributes(false);
WindowManager.LayoutParams wParams = getAttributes(true);
GLES3JNIView display = new GLES3JNIView(context);
vTouch = new View(context);
manager.addView(vTouch, vParams);
manager.addView(display, wParams);
vTouch.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
GLES3JNIView.MotionEventClick(action != MotionEvent.ACTION_UP, event.getRawX(), event.getRawY());
break;
default:
break;
}
return false;
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
String rect[] = GLES3JNIView.getWindowRect().split("\\|");
vParams.x = Integer.parseInt(rect[0]);
vParams.y = Integer.parseInt(rect[1]);
vParams.width = Integer.parseInt(rect[2]);
vParams.height = Integer.parseInt(rect[3]);
manager.updateViewLayout(vTouch, vParams);
} catch (Exception e) {
}
handler.postDelayed(this, 20);
}
}, 20);
}
public static WindowManager.LayoutParams getAttributes(boolean isWindow) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
0,
100,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_SPLIT_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
if (isWindow) {
params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
}
params.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
params.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗显示的停靠位置为左侧置顶
params.x = params.y = 0;
params.width = params.height = isWindow ? WindowManager.LayoutParams.MATCH_PARENT : 0;
return params;
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -game-size