使用SDL2编译控制台Android应用程序?

时间:2014-04-23 07:27:16

标签: android opengl-es sdl sdl-2

是否有可能使用SDL2编译控制台Android应用程序?我已经有了android和toolchain的libSDL2.so,它正在android上运行并且为android编译(使用CppDroid)。

有人说,这将是OpenGL ES的冲突(因为SDL2在Android的场景背后使用Open GL)。

据说下一个源代码适用于旧的android,但即使无法在现代android上启动:

#include <stdio.h>
#include <stdlib.h>

#include <EGL/egl.h>
#include <GLES/gl.h>

//extern "C" EGLNativeWindowType android_createDisplaySurface(void);
EGLNativeWindowType android_createDisplaySurface(void);

EGLNativeWindowType displayWindow;

int width = 0;
int height = 0;

const EGLint config16bpp[] =
{
    EGL_RED_SIZE, 5,
    EGL_GREEN_SIZE, 6,
    EGL_BLUE_SIZE, 5,
    EGL_NONE
};

GLfloat colors[3][4] =
{
    {1.0f, 0.0f, 0.0f, 1.0f},
    {0.0f, 1.0f, 0.0f, 1.0f},
    {0.0f, 0.0f, 1.0f, 1.0f}
};

GLfloat vertices[3][3] =
{
    {0.0f, 0.7f, 0.0f},
    {-0.7f, -0.7f, 0.0f},
    {0.7f, -0.7f, 0.0f}
};


void draw_tri()
{
    glViewport(0,0,width,height);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glColorPointer(4, GL_FLOAT, 0, colors);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    // Draw the triangle (3 vertices)
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}


int main(int argc, char** argv)
{
    EGLint majorVersion, minorVersion;
    EGLContext eglContext = NULL;
    EGLSurface eglSurface;
    EGLConfig eglConfig;
    EGLDisplay eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    int numConfigs;

    // Window surface that covers the entire screen, from libui.
    displayWindow = android_createDisplaySurface();

    eglInitialize(eglDisplay, &majorVersion, &minorVersion);

    printf("GL version: %d.%d\n",majorVersion,minorVersion);

    if (!eglChooseConfig(eglDisplay, config16bpp, &eglConfig, 1, &numConfigs)) {
    printf("eglChooseConfig failed\n");
    if (eglContext == 0) {
        printf("Error code: %d\n", eglGetError());
    }
    }

    eglContext = eglCreateContext(eglDisplay,
    eglConfig,
    EGL_NO_CONTEXT,
    NULL);

    printf("GL context: %p\n", eglContext);

    if (eglContext == 0) {
    printf("Error code: %x\n", eglGetError());
    }

    eglSurface = eglCreateWindowSurface(eglDisplay,
    eglConfig,
    displayWindow,
    NULL);

    printf("GL surface: %p\n", eglSurface);

    if (eglSurface == 0) {
    printf("Error code: %x\n", eglGetError());
    }

    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    eglQuerySurface(eglDisplay, eglSurface, EGL_WIDTH, &width);
    eglQuerySurface(eglDisplay, eglSurface, EGL_HEIGHT, &height);

    printf("w=%d, h=%d\n", width, height);

    while (1) {
    draw_tri();
    eglSwapBuffers(eglDisplay, eglSurface);
    }

    return 0;
}

0 个答案:

没有答案