Android: Add 5 more myouAndroidGet* for JNI VM, app context, and EGL handles.

This commit is contained in:
Alberto Torres 2025-01-21 13:05:27 +01:00
parent 35497da6a6
commit 79372d6cfc
5 changed files with 64 additions and 9 deletions

View file

@ -376,6 +376,10 @@ when defined(android):
proc glfmAndroidGetActivity*(): pointer {.deprecated.} #ptr ANativeActivity
proc glfmAndroidGetActivity*(display: ptr GLFMDisplay): pointer
proc glfmGetEGLDisplay*(display: ptr GLFMDisplay): pointer
proc glfmGetEGLConfig*(display: ptr GLFMDisplay): pointer
proc glfmGetEGLContext*(display: ptr GLFMDisplay): pointer

View file

@ -2840,4 +2840,21 @@ NO_SANITIZE void *glfmGetAndroidActivity(const GLFMDisplay *display) {
return platformData->activity;
}
NO_SANITIZE void *glfmGetEGLDisplay(const GLFMDisplay *display) {
GLFMPlatformData *platformData = (GLFMPlatformData *)display->platformData;
return (void*)platformData->eglDisplay;
}
NO_SANITIZE void *glfmGetEGLConfig(const GLFMDisplay *display) {
GLFMPlatformData *platformData = (GLFMPlatformData *)display->platformData;
return (void*)platformData->eglConfig;
}
NO_SANITIZE void *glfmGetEGLContext(const GLFMDisplay *display) {
GLFMPlatformData *platformData = (GLFMPlatformData *)display->platformData;
return (void*)platformData->eglContext;
}
// #endif // __ANDROID__

View file

@ -94,10 +94,15 @@ export vmath except Quat, quat
import platform/platform
when defined(android):
export platform.myouAndroidGetActivity
export platform.myouAndroidGetJniVM
export platform.myouAndroidGetJniEnv
export platform.myouAndroidGetActivityContext
export platform.myouAndroidGetInternalDataPath
export platform.myouAndroidGetExternalDataPath
export platform.myouAndroidAPKFilePointerLength
export platform.myouCloseMobileApp
export platform.myouAndroidGetEGLDisplay
export platform.myouAndroidGetEGLConfig
export platform.myouAndroidGetEGLContext
export platform.myouSetKeyboardVisible

View file

@ -250,6 +250,7 @@ proc init_graphics*(engine: MyouEngine, width, height: int32, title: string,
glver,
GLFMColorFormatRGB888,
GLFMDepthFormat16,
# GLFMDepthFormat24,
GLFMStencilFormatNone,
GLFMMultisampleNone)
@ -309,20 +310,38 @@ proc start_platform_main_loop*(engine1: MyouEngine, main_loop1: proc(self: MyouE
echo line
echo getCurrentExceptionMsg()
glfm_breakpoint()
# if not engine.vr_active:
window.glfmSwapBuffers()
proc myouAndroidGetActivity*(): pointer =
type ANativeActivity = object
callbacks: pointer
vm: pointer
env: pointer
clazz: pointer
internalDataPath: cstring
externalDataPath: cstring
sdkVersion: int32
instance: pointer
assetManager: pointer
obbPath: cstring
# Deal with ASan false positives
{.push noSanitize: ["address","hwaddress"].} #"
proc myouAndroidGetActivity*(): ptr ANativeActivity =
when defined(android):
window.glfmAndroidGetActivity()
cast[ptr ANativeActivity](window.glfmAndroidGetActivity())
proc platform_switch_screen*(screen: Screen): bool {.inline.} =
discard
proc myouAndroidGetJniVM*(): pointer =
when defined(android):
return myouAndroidGetActivity().vm
proc myouAndroidGetJniEnv*(): pointer =
when defined(android):
let activity = cast[ptr array[2, pointer]](window.glfmAndroidGetActivity())
# Ussing offsets from ANativeActivity
let vm = cast[ptr ptr array[7, pointer]](activity[1])
let vm = cast[ptr ptr array[7, pointer]](myouAndroidGetJniVM())
# Using offsets from jni.h
let getEnv = cast[proc(vm: pointer, penv: var pointer, version: int32): int32 {.cdecl, gcsafe.}](vm[][6])
let attachCurrentThread = cast[proc(vm: pointer, penv: ptr pointer, args: pointer): int32 {.cdecl, gcsafe.}](vm[][4])
@ -336,15 +355,18 @@ proc myouAndroidGetJniEnv*(): pointer =
result = nil
# TODO: do we ever need to detach a thread from the VM?
proc myouAndroidGetActivityContext*(): pointer =
when defined(android):
return myouAndroidGetActivity().clazz
proc myouAndroidGetInternalDataPath*(): string =
when defined(android):
let activity = cast[ptr array[6, cstring]](window.glfmAndroidGetActivity())
return $activity[4]
return $myouAndroidGetActivity().internalDataPath
proc myouAndroidGetExternalDataPath*(): string =
when defined(android):
let activity = cast[ptr array[6, cstring]](window.glfmAndroidGetActivity())
return $activity[5]
let ac = myouAndroidGetActivity()
return $myouAndroidGetActivity().externalDataPath
when defined(android):
proc AAssetManager_open(asset_manager: pointer, filename: cstring, mode: int32): pointer {.importc,cdecl.}
@ -353,6 +375,10 @@ when defined(android):
proc AAsset_getLength64(asset: pointer): int64 {.importc,cdecl.}
proc ANativeActivity_finish(activity: pointer) {.importc,cdecl.}
proc myouAndroidGetEGLDisplay*(): pointer = glfmGetEGLDisplay(window)
proc myouAndroidGetEGLConfig*(): pointer = glfmGetEGLConfig(window)
proc myouAndroidGetEGLContext*(): pointer = glfmGetEGLContext(window)
type myouAAssetObj = object
asset: pointer
p*: pointer
@ -383,3 +409,5 @@ proc myouSetKeyboardVisible*(show: bool) =
proc myouCloseMobileApp*() =
ANativeActivity_finish(window.glfmAndroidGetActivity())
quit()
{.pop.}

View file

@ -75,6 +75,7 @@ proc make_window*(width, height: int32, title: string): Window =
fist_window_is_used = true
return windows[0]
let window = createWindow(width, height, title, nil, windows.get_or_default(0))
doAssert window != nil, "Could not create window."
discard glfw.setWindowCloseCallback(window, proc(window: Window) {.cdecl.} =
# TODO: flag to prevent it from being closed