Try to set SDL with OpenGL context on Android. Get error message "Failed loading eglChooseConfig"

1.9k views Asked by At

I am trying to run my first SDL hello-world project on android. I use the android project template provided in SDL source package, it use a class called SDLActivity to communicate with android and native c/c++ code. I import them into Eclipse android project, then slightly modify AndroidManifest.xml, Application.mk in folder jni, and Android.mk in folder jni/src .

The project compiled successfully. But when it runs on simulator (I use GenyMotion), I got error message in logcat.

E/SDLActivity(1472): Failed loading eglChooseConfig: Invalid library handle

I also try to debug on a real device. I got this message:

E/SDLActivity(28020): Failed loading eglChooseConfig: dlsym library handle is null

I tried to add "System.loadLibrary("EGL");" in SDLActivity.java . Not work.

Here is my Android.mk in jni/src

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := main

SDL_PATH := ../SDL

LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include

# Add your application source files here...
LOCAL_SRC_FILES :=  $(SDL_PATH)/src/main/android/SDL_android_main.c \
main.cpp

LOCAL_SHARED_LIBRARIES :=SDL2

LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog -lEGL

include $(BUILD_SHARED_LIBRARY)
1

There are 1 answers

0
Shan-Hung Hsu On

I eventually found a good tutorial on youtube. I followed this video and my app works fine. I've tested my app on virtual and real devices.

Here is the sample code used by the youtube recorder. I am not the author of the code below. Just put a copy here.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL.h"
int main(int argc, char *argv[])
{
    SDL_Window* window = 0;
    SDL_GLContext gl = 0;
    if(0 != SDL_Init(SDL_INIT_VIDEO))
    {
        fprintf(stderr,"Unable to initialize SDL: %s\n",SDL_GetError());
        return 1;
    }
    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,    SDL_GL_CONTEXT_PROFILE_ES);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
    SDL_DisplayMode mode;
    SDL_GetDisplayMode(0,0,&mode);
    int width = mode.w;
    int height = mode.h;
    SDL_Log("Width = %d, Heigh = %d. \n",width,height);
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24);
    window = SDL_CreateWindow(NULL,0,0,width,height,SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);
    if(window == 0)
    {
        SDL_Log("Failed to create window.");
        SDL_Quit();
        return 1;
    }
    //Create an opengl context
    gl = SDL_GL_CreateContext(window);
    /*Main Render Loop*/
    Uint8 done = 0;
    SDL_Event event;
    int count = 0;
    while(!done)
    {
        /*Check for events*/
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
            {
                done = 1;
            }
        }
        SDL_Log("%d\n",count++);
        SDL_GL_SwapWindow(window);
        SDL_Delay(10);
    }
    exit(0);
}

This code will let you know how to initialize SDL on android. There is one thing needs to take care of: version of openGL ES needs to set correctly. For example, I use openGLES 1.1, so I change

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2);

to

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,1);