4

一般的に、それは大丈夫です。しかし、画面をロックし、APP_CMD_LOST_FOCUSが発生するのを待ってから、srceenのロックを解除するとします。似顔絵に変わります!しかし、eglバフはまだ横向きの設定であり、すべての座標が大きくなっています。

私のAndroidManifest.xml設定:

<activity android:name="android.app.NativeActivity"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:clearTaskOnLaunch="true">
    <meta-data android:name="android.app.lib_name"
            android:value="sunred" />

    <intent-filter>
          <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

私のeglinitc++コード

int engine_init_display(OSCONTEXTENGINE* pEngine, const DISPLAY_CONFIG* pConfig)
{
    // initialize OpenGL ES and EGL
    /*
     * Here specify the attributes of the desired configuration.
     * Below, we select an EGLConfig with at least 8 bits per color
     * component compatible with on-screen windows
     */
    const EGLint attribs[] =
    { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
            EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8,
            EGL_DEPTH_SIZE, 8, EGL_NONE };
    EGLint w, h, dummy, format;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;

    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    eglInitialize(display, 0, 0);

    //eglBindAPI(EGL_OPENGL_ES_API);
    /* Here, the application chooses the configuration it desires. In this
     * sample, we have a very simplified selection process, where we pick
     * the first EGLConfig that matches our criteria */
    EGLBoolean bres = eglChooseConfig(display, attribs, &config, 1,
            &numConfigs);

    if (!bres)
    {
        __android_log_print(LOGINFO_ERROR, "engine_init_display",
                "numConfigs = %d", numConfigs);
    }

    /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
     * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
     * As soon as we picked a EGLConfig, we can safely reconfigure the
     * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

    ANativeWindow_setBuffersGeometry(pEngine->m_app->window, 0, 0, format);

    surface = eglCreateWindowSurface(display, config, pEngine->m_app->window,
            NULL);
    const EGLint ai32ContextAttribs[] =
    { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };

    context = eglCreateContext(display, config, NULL,
            ai32ContextAttribs);

    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
    {
        LOGW("Unable to eglMakeCurrent");
        return P_ERR;
    }

    eglQuerySurface(display, surface, EGL_WIDTH, &w);
    eglQuerySurface(display, surface, EGL_HEIGHT, &h);

    pEngine->m_EglDisplay = display;
    pEngine->m_EglContext = context;
    pEngine->m_EglSurface = surface;
    pEngine->m_iWidth = w;
    pEngine->m_iHeight = h;

    return 0;
}

APP_CMD_INIT_WINDOWが発生したら、engine_init_displayを呼び出します。

C ++を使用して強制的にランドスケープモードに設定する方法はありますか?

レンダリングフレーム:

正常に動作します:

pEngine->m_iWidth = 960;
pEngine->m_iHeight = 540;

画面のロック->APP_CMD_LOST_FOCUS->画面のロック解除

pEngine->m_iWidth = 540;
pEngine->m_iHeight = 960;

ウィンドウがポートレートに変わります!しかし、eglバフはまだ横向きの設定であり、すべての座標が大きくなっています。

4

2 に答える 2

1

APP_CMD_CONFIG_CHANGEDを​​取得したら、別のinitを実行してみてください。

    case APP_CMD_CONFIG_CHANGED:
        if (engine->app->window != NULL && ((engine->width != ANativeWindow_getWidth(app->window)) || (engine->height != ANativeWindow_getHeight(app->window)))) {
            engine_handle_cmd(app, APP_CMD_TERM_WINDOW);
            engine_handle_cmd(app, APP_CMD_INIT_WINDOW);
        }
        break;

(NativeActivityサンプルコードの変数名など。)ウェイク時にinitを複数回実行することになります。これが問題になる場合は、レイジーinitを実行して、APP_CMD_CONFIG_CHANGEDとAPP_CMD_INIT_WINDOWの両方の構成を無効にすることもできます。

これはライブコードからのものです。私たちのテストでは、2.3以降(以前はテストされていません)では約99%の時間で動作しますが、プログラムをロック画面から開始してからロックを解除する時間はごくわずかであるため、CONFIG_CHANGEDが呼び出されない競合状態が発生しますそして私たちは風景の中で立ち往生して目を覚ます。私たちのゲームでは、これはユーザーコードパス(ロック画面から起動)ではないため、これ以上調査していません。

于 2012-12-05T17:18:00.450 に答える
0

要求された画面/アクティビティの向きを設定するための純粋なネイティブ ソリューションに関心がある場合は、以下のコードを使用してください。

関連する JAVA メソッドの詳細については、次を参照してください。

https://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)

void set_requested_screen_orientation(struct android_app * app, int an_orientation){

    JNIEnv * jni;
    app->activity->vm->AttachCurrentThread(&jni, NULL);
    jclass clazz = jni->GetObjectClass(app->activity->clazz);
    jmethodID methodID = jni->GetMethodID(clazz, "setRequestedOrientation", "(I)V");
    jni->CallVoidMethod(app->activity->clazz, methodID, an_orientation);
    app->activity->vm->DetachCurrentThread();

}
于 2019-04-12T16:11:21.910 に答える