#include "utils/init.h" #include "utils/glutil.h" #include "utils/dialog.h" #include "utils/logger.h" #include "utils/utils.h" #include "audio.h" #include "cursor.h" #include "pause_menu.h" #include #include #include #include #include #ifndef NDK_PORT #include "reimpl/controls.h" #else #include #endif int _newlib_heap_size_user = 256 * 1024 * 1024; #ifdef USE_SCELIBC_IO int sceLibcHeapSize = 4 * 1024 * 1024; #endif so_module so_mod; typedef jint (*jni_on_load_fn)(JavaVM *, void *); typedef void (*jni_void_fn)(JNIEnv *, jobject); typedef void (*jni_size_fn)(JNIEnv *, jobject, jint, jint); typedef void (*jni_touch_fn)(JNIEnv *, jobject, jint, jfloat, jfloat); typedef void (*jni_touch_move_fn)(JNIEnv *, jobject, jintArray, jfloatArray, jfloatArray); typedef jboolean (*jni_key_fn)(JNIEnv *, jobject, jint); static jni_void_fn native_render; static jni_void_fn native_pause; static jni_void_fn native_resume; static jni_size_fn native_surface_changed; static jni_touch_fn native_touch_begin; static jni_touch_fn native_touch_end; static jni_touch_move_fn native_touch_move; static jni_key_fn native_key_down; static int fake_android_activity; static int fake_android_classloader; static int fake_android_asset_manager; static void loader_progress(float progress, float r, float g, float b) { (void)r; (void)g; (void)b; cursor_loading_draw(progress); return; /* Emergency fallback retained for systems where the PNG overlay cannot be initialized. */ int width = (int)(760.0f * progress); if (width < 0) width = 0; if (width > 760) width = 760; glDisable(GL_SCISSOR_TEST); glViewport(0, 0, 960, 544); glClearColor(0.025f, 0.025f, 0.030f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); /* Border and fill are drawn with scissored clears, so this loading UI does not depend on the game's shader pipeline. */ glEnable(GL_SCISSOR_TEST); glScissor(96, 247, 768, 50); glClearColor(0.18f, 0.18f, 0.20f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glScissor(100, 251, 760, 42); glClearColor(0.045f, 0.045f, 0.055f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); if (width) { glScissor(100, 251, width, 42); glClearColor(r, g, b, 1.0f); glClear(GL_COLOR_BUFFER_BIT); } glDisable(GL_SCISSOR_TEST); gl_swap(); } static void *required_symbol(const char *name) { void *symbol = (void *)so_symbol(&so_mod, name); if (!symbol) fatal_error("Build %s: missing required export %s", THEHOUSE2_BUILD_ID, name); return symbol; } static void vitagl_splash(void) { gl_init(); if (!cursor_init()) l_warn("OVERLAY PNG initialization failed; using fallback loader"); loader_progress(0.08f, 0.55f, 0.04f, 0.04f); } int main() { log_init_file(DATA_PATH "port.log"); l_info("BOOT build=%s", THEHOUSE2_BUILD_ID); l_info("APK expected_sha256=%s", THEHOUSE2_APK_SHA256); l_info("PHASE soloader_init_all begin"); soloader_init_all(); l_success("PHASE soloader_init_all complete"); /* The imported Android runtime can reuse low file descriptors while it is being relocated. Reopen the Vita-owned diagnostic afterwards so a stale descriptor cannot leave a zero-byte port.log. */ log_reopen_file(DATA_PATH "port.log"); l_info("BOOT build=%s logger=reopened-after-soloader", THEHOUSE2_BUILD_ID); jni_on_load_fn JNI_OnLoad = (jni_on_load_fn)required_symbol("JNI_OnLoad"); l_info("PHASE JNI_OnLoad begin"); if (JNI_OnLoad(&jvm, NULL) < JNI_VERSION_1_4) fatal_error("Build %s: JNI_OnLoad failed", THEHOUSE2_BUILD_ID); l_success("PHASE JNI_OnLoad complete"); l_info("PHASE VitaGL splash begin"); vitagl_splash(); l_success("PHASE VitaGL splash complete"); loader_progress(0.18f, 0.62f, 0.05f, 0.05f); #ifndef NDK_PORT if (!file_exists(APK_PATH)) fatal_error("Place the supported APK at %s (SHA-256 %s).", APK_PATH, THEHOUSE2_APK_SHA256); void (*native_set_apk_path)(JNIEnv *, jclass, jstring) = (void *)required_symbol("Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath"); void (*native_set_context)(JNIEnv *, jclass, jobject, jobject) = (void *)required_symbol("Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetContext"); jni_size_fn native_init = (jni_size_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit"); jintArray (*get_gl_context_attrs)(JNIEnv *, jobject) = (void *)required_symbol("Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs"); native_render = (jni_void_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender"); native_pause = (jni_void_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause"); native_resume = (jni_void_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume"); native_surface_changed = (jni_size_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnSurfaceChanged"); native_touch_begin = (jni_touch_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin"); native_touch_end = (jni_touch_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd"); native_touch_move = (jni_touch_move_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove"); native_key_down = (jni_key_fn)required_symbol( "Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown"); jstring apk_path = jni->NewStringUTF(&jni, APK_PATH); if (!apk_path) fatal_error("Build %s: could not allocate APK path jstring", THEHOUSE2_BUILD_ID); l_info("PHASE nativeSetApkPath begin path=%s", APK_PATH); native_set_apk_path(&jni, NULL, apk_path); l_success("PHASE nativeSetApkPath complete"); loader_progress(0.30f, 0.68f, 0.06f, 0.06f); l_info("PHASE nativeSetContext/AAssetManager begin"); native_set_context(&jni, NULL, (jobject)&fake_android_activity, (jobject)&fake_android_asset_manager); l_success("PHASE nativeSetContext/AAssetManager complete"); loader_progress(0.42f, 0.74f, 0.07f, 0.07f); l_info("PHASE getGLContextAttrs/cocos_android_app_init begin"); jintArray gl_attrs = get_gl_context_attrs( &jni, (jobject)&fake_android_activity); if (!gl_attrs) fatal_error("Build %s: getGLContextAttrs returned NULL", THEHOUSE2_BUILD_ID); l_success("PHASE getGLContextAttrs complete result=%p", gl_attrs); loader_progress(0.55f, 0.80f, 0.08f, 0.08f); jclass classloader_class = jni->FindClass(&jni, "java/lang/ClassLoader"); jmethodID loadclass_method = jni->GetMethodID( &jni, classloader_class, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); if (!classloader_class || !loadclass_method) fatal_error("Build %s: could not create FalsoJNI classloader bridge", THEHOUSE2_BUILD_ID); jobject *cocos_classloader = (jobject *)required_symbol( "_ZN7cocos2d9JniHelper11classloaderE"); jmethodID *cocos_loadclass_method = (jmethodID *)required_symbol( "_ZN7cocos2d9JniHelper24loadclassMethod_methodIDE"); *cocos_classloader = jni->NewGlobalRef( &jni, (jobject)&fake_android_classloader); *cocos_loadclass_method = loadclass_method; l_success("PHASE JniHelper classloader injected object=%p method=%d", *cocos_classloader, (int)*cocos_loadclass_method); loader_progress(0.68f, 0.86f, 0.09f, 0.09f); /* v0.23's known-good ordering: display the completed loader and destroy its temporary GL objects before Cocos creates programs and caches GL state. Never draw or delete overlay objects after nativeInit. */ loader_progress(0.90f, 0.96f, 0.13f, 0.13f); cursor_loading_complete(); l_success("PHASE loader renderer released before nativeInit"); l_info("PHASE nativeInit begin size=960x544"); native_init(&jni, NULL, 960, 544); l_success("PHASE nativeInit complete"); l_info("PHASE nativeOnSurfaceChanged begin size=960x544"); native_surface_changed(&jni, NULL, 960, 544); l_success("PHASE nativeOnSurfaceChanged complete"); l_info("PHASE nativeOnResume begin"); native_resume(&jni, NULL); l_success("PHASE nativeOnResume complete"); l_success("PHASE render_loop entered"); pause_menu_init(); unsigned int frame = 0; while (1) { controls_poll(); native_render(&jni, NULL); float cursor_x, cursor_y; int cursor_pressed; controls_get_cursor(&cursor_x, &cursor_y, &cursor_pressed); uint32_t pressed_buttons = controls_get_pressed(); pause_menu_handle_buttons(pressed_buttons); pause_menu_handle_touch(cursor_x, cursor_y, cursor_pressed); if (pause_menu_active()) { pause_menu_draw(); } pause_menu_apply_brightness(); int cursor_state = CURSOR_STATE_POINTER; if (cursor_pressed) { cursor_state = CURSOR_STATE_CLICK; } else { unsigned char pixel[4] = {0}; int sample_x = (int)cursor_x; int sample_y = 543 - (int)cursor_y; if (sample_x >= 0 && sample_x < 960 && sample_y >= 0 && sample_y < 544) { glReadPixels(sample_x, sample_y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); if (pixel[3] > 0 && (pixel[0] > 35 || pixel[1] > 35 || pixel[2] > 35)) cursor_state = CURSOR_STATE_HOVER; } } cursor_draw(cursor_x, cursor_y, cursor_state); if (frame < 10 || (frame % 300) == 0) { GLenum error = glGetError(); GLint program = 0; GLint viewport[4] = {0}; GLint scissor[4] = {0}; GLint framebuffer = 0; GLint blend_src = 0, blend_dst = 0, texture = 0; GLboolean color_mask[4] = {0}; unsigned char samples[20] = {0}; glGetIntegerv(GL_CURRENT_PROGRAM, &program); glGetIntegerv(GL_VIEWPORT, viewport); glGetIntegerv(GL_SCISSOR_BOX, scissor); glGetIntegerv(GL_FRAMEBUFFER_BINDING, &framebuffer); glGetIntegerv(GL_BLEND_SRC_RGB, &blend_src); glGetIntegerv(GL_BLEND_DST_RGB, &blend_dst); glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture); glGetBooleanv(GL_COLOR_WRITEMASK, color_mask); glReadPixels(120, 136, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, samples + 0); glReadPixels(480, 136, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, samples + 4); glReadPixels(840, 136, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, samples + 8); glReadPixels(240, 408, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, samples + 12); glReadPixels(720, 408, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, samples + 16); l_info("GL frame=%u error=0x%04x program=%d draws=%u fb=%d tex=%d color=%d%d%d%d scissor=%d box=%d,%d,%d,%d depth=%d blend=%d func=0x%x,0x%x cull=%d viewport=%d,%d,%d,%d center=%u,%u,%u,%u", frame + 1, error, program, gl_draw_call_count(), framebuffer, texture, color_mask[0], color_mask[1], color_mask[2], color_mask[3], glIsEnabled(GL_SCISSOR_TEST), scissor[0], scissor[1], scissor[2], scissor[3], glIsEnabled(GL_DEPTH_TEST), glIsEnabled(GL_BLEND), blend_src, blend_dst, glIsEnabled(GL_CULL_FACE), viewport[0], viewport[1], viewport[2], viewport[3], samples[4], samples[5], samples[6], samples[7]); l_info("GL samples frame=%u tl=%u,%u,%u mid=%u,%u,%u tr=%u,%u,%u bl=%u,%u,%u br=%u,%u,%u", frame + 1, samples[0], samples[1], samples[2], samples[4], samples[5], samples[6], samples[8], samples[9], samples[10], samples[12], samples[13], samples[14], samples[16], samples[17], samples[18]); audio_log_status(frame + 1); } gl_swap(); ++frame; if (frame == 1 || (frame % 300) == 0) l_info("FRAME presented=%u", frame); } #else // Build a fake ANativeActivity that the game's onCreate will receive ANativeActivity *activity = malloc(sizeof(ANativeActivity)); activity->callbacks = malloc(sizeof(ANativeActivityCallbacks)); activity->env = &jni; // from FalsoJNI activity->vm = &jvm; // from FalsoJNI activity->clazz = (jclass)0x42424242; activity->internalDataPath = DATA_PATH "assets/"; activity->externalDataPath = DATA_PATH "assets/"; activity->sdkVersion = 14; activity->instance = NULL; // Drive the activity lifecycle int (*ANativeActivity_onCreate)(ANativeActivity *, void *, size_t) = (void *)so_symbol(&so_mod, "ANativeActivity_onCreate"); ANativeActivity_onCreate(activity, NULL, 0); activity->callbacks->onStart(activity); activity->callbacks->onResume(activity); // Wire up input and the native window AInputQueue *aInputQueue = AInputQueue_create(); activity->callbacks->onInputQueueCreated(activity, aInputQueue); ANativeWindow *aNativeWindow = ANativeWindow_create(); activity->callbacks->onNativeWindowCreated(activity, aNativeWindow); activity->callbacks->onWindowFocusChanged(activity, 1); #endif sceKernelExitDeleteThread(0); } #ifndef NDK_PORT void controls_handler_key(int32_t keycode, ControlsAction action) { if (action == CONTROLS_ACTION_DOWN && native_key_down) native_key_down(&jni, NULL, keycode); } void controls_handler_touch(int32_t id, float x, float y, ControlsAction action) { /* Cocos2d-x 3.x expects stable, small pointer IDs. */ (void)id; if (action == CONTROLS_ACTION_DOWN && native_touch_begin) native_touch_begin(&jni, NULL, 0, x, y); else if (action == CONTROLS_ACTION_MOVE && native_touch_move) { jint touch_id = 0; jfloat touch_x = x, touch_y = y; jintArray ids = jni->NewIntArray(&jni, 1); jfloatArray xs = jni->NewFloatArray(&jni, 1); jfloatArray ys = jni->NewFloatArray(&jni, 1); jni->SetIntArrayRegion(&jni, ids, 0, 1, &touch_id); jni->SetFloatArrayRegion(&jni, xs, 0, 1, &touch_x); jni->SetFloatArrayRegion(&jni, ys, 0, 1, &touch_y); native_touch_move(&jni, NULL, ids, xs, ys); jni->DeleteLocalRef(&jni, ids); jni->DeleteLocalRef(&jni, xs); jni->DeleteLocalRef(&jni, ys); } else if (action == CONTROLS_ACTION_UP && native_touch_end) native_touch_end(&jni, NULL, 0, x, y); } void controls_handler_analog(ControlsStickId which, float x, float y, ControlsAction action) { // Call into the .so here } #endif