X
X
xverizex2020-12-31 13:03:42
Java
xverizex, 2020-12-31 13:03:42

How to make opengl work through jni in android?

I want to try to make a game for android in c++. And this seems to be possible. I would like to do it on sdl2, but the version that compiles for android and this is 2.0.10 is buggy, like buggy 2.0.9. There are such problems with them that the jamb on the jamb. How they generally test their software is not clear. In general, I decided to do in android studio in jni. I'm thinking of using GLSurfaceView and passing it to jni to create a native window, but at this stage an error occurs, first SIGABRT, then SIGSEGV. Here is the java code.

package com.example.gametest;

import androidx.appcompat.app.AppCompatActivity;

import android.opengl.GLSurfaceView;

import android.os.Bundle;



public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        GLSurfaceView view = findViewById(R.id.glsurface);
        gametest(view);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native void gametest(GLSurfaceView view);
}

Then c++ code.
#include <jni.h>
#include <string>
#include <EGL/egl.h>
#include <android/native_window_jni.h>
#include <android/native_activity.h>
#include <android/rect.h>
#include <android/surface_control.h>
#include <android/window.h>
#include <GLES3/gl3.h>
#include <unistd.h>

EGLDisplay display;
EGLSurface window;

EGLBoolean initializeWindow ( EGLNativeWindowType win ) {
    const EGLint configAttribs[] =
            {
                EGL_RENDERABLE_TYPE, EGL_WINDOW_BIT,
                EGL_RED_SIZE, 8,
                EGL_GREEN_SIZE, 8,
                EGL_BLUE_SIZE, 8,
                EGL_ALPHA_SIZE, 8,
                EGL_DEPTH_SIZE, 24,
                EGL_NONE
            };
    const EGLint contextAttribs[] =
            {
            EGL_CONTEXT_CLIENT_VERSION, 3,
            EGL_NONE
            };
    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if ( display == EGL_NO_DISPLAY ) {
        return EGL_FALSE;
    }
    EGLint major, minor;

    if ( !eglInitialize(display, &major, &minor ) ) {
        return EGL_FALSE;
    }
    EGLConfig config;
    EGLint numConfigs;
    if ( !eglChooseConfig(display, configAttribs, &config, 1, &numConfigs ) ) {
        return EGL_FALSE;
    }

    window = eglCreateWindowSurface(display, config, win, NULL );
        if ( window == EGL_NO_SURFACE ) {
            return EGL_FALSE;
        }

    EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs );
        if ( context == EGL_NO_CONTEXT ) {
            return EGL_FALSE;
        }
        if ( !eglMakeCurrent(display, window, window, context ) ) {
            return EGL_FALSE;
        }
        return EGL_TRUE;

}
extern "C" JNIEXPORT void JNICALL
Java_com_example_gametest_MainActivity_gametest (
        JNIEnv *env,
        jobject jobj,
        jobject surface) {

    ANativeWindow *win = ANativeWindow_fromSurface(env, surface);
    EGLBoolean res = initializeWindow( win );
    int32_t width = ANativeWindow_getWidth(win);
    int32_t height = ANativeWindow_getHeight(win);

    while ( 1 ) {

        eglSwapBuffers(display, window);
        usleep(1000);
    }
    return;
}


And here in this function it seems there is an error. But I need an opengl surface. How can I figure out what to do.
ANativeWindow *win = ANativeWindow_fromSurface(env, surface);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2020-12-31
@jamakasi666

https://github.com/android/ndk-samples/tree/master...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question