Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何使用纯c代码强制NDK使用横向模式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一般来说,它工作正常.但如果我锁定屏幕,并等待APP_CMD_LOST_FOCUS发生,然后我解锁srceen.它变成了肖像!但我发现egl buff仍然是风景设置,并且所有坐标都更大.

我的@L_924_0@manifest.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 init 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 EGL@R_489_8237@ttribs[] =
    { EGL_SURFACE_TYPE,EGL_WINDOW_BIT,EGL_RENDERABLE_TYPE,EGL_OPENGL_ES2_BIT,EGL_BLUE_SIZE,8,EGL_GREEN_SIZE,EGL_RED_SIZE,EGL_DEPTH_SIZE,EGL_NONE };
    EGLint w,h,dummy,format;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;

    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    eglInitialize(display,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,format);

    surface = eglCreateWindowSurface(display,pENGIne->m_app->window,null);
    const EGL@R_489_8237@i32ContextAttribs[] =
    { EGL_COntexT_CLIENT_VERSION,2,EGL_NONE };

    context = eglCreateContext(display,NULL,ai32ContextAttribs);

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

    eglQuerySurface(display,EGL_WIDTH,&w);
    eglQuerySurface(display,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;

lock screeen – > APP_CMD_LOST_FOCUS – >解锁屏

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

窗户变成了肖像!
但是egl buff仍然是风景设置,并且所有坐标都更大.

解决方法

当你得到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;如果这是一个问题,您也可以执行延迟初始化,并在APP_CMD_CONfig_CHANGED和APP_CMD_INIT_WINDOW中使配置无效.

这是来自实时代码;在我们的测试中,它在2.3及更高版本(之前未经测试)中大约99%的时间工作,但是从锁定屏幕启动程序然后解锁的非常少的时间会导致不调用CONfig_CHANGED的竞争条件我们醒来时陷入困境.对于我们的游戏,这不是用户代码路径(从锁定屏幕启动),所以我没有进一步调查.

大佬总结

以上是大佬教程为你收集整理的android – 如何使用纯c代码强制NDK使用横向模式全部内容,希望文章能够帮你解决android – 如何使用纯c代码强制NDK使用横向模式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。