2012-10-03 24 views
4

一般來說,它工作正常。但如果我鎖定屏幕,並等待APP_CMD_LOST_FOCUS發生,然後我解鎖srceen。它變成肖像!但我發現egl buff仍然是風景設置,並且全部協調得更大。如何強制使用純C++代碼的NDK景觀模式

我的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> 

我EGL初始化C++代碼

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; 

鎖screeen - > APP_CMD_LOST_FOCUS - >屏幕解鎖

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

窗口切換到肖像! 但egl buff仍然是橫向設置,並且所有座標都更大。

回答

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的示例代碼中的變量名,等等。)如果這是一個問題,您也可以執行lazy init並使APP_CMD_CONFIG_CHANGED和APP_CMD_INIT_WINDOW中的配置失效。

這是來自實時代碼;在我們的測試中,它在2.3和更高版本(以前未經測試)中大約有99%的時間工作,但是從鎖定屏幕開始程序然後解鎖導致CONFIG_CHANGED未調用的競爭狀態的時間非常少我們醒來時陷入了風景。對於我們的遊戲,這不是用戶代碼路徑(從鎖定屏幕啓動),所以我沒有進一步調查。