A Slushie Loading textures when coming in from a JNI Call
Posts 8
Added by A Slushie about 1 year ago

I'm running into an issue of something going wrong with my OpenGL context when coming "into" my C++ code from Java.

I have this function which gets called fine from the Java.

void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeShowQuitOverlay() {
    CCScene* scene = CCDirector::sharedDirector()->getRunningScene();
    scene->addChild(QuitOverlay::node());
}

And it makes it into the init().

bool QuitOverlay::init(void) {    
    if (!CCLayer::init()) {
        return false;
    }
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sheetQuit.plist");
    return true;
}

But it crashes on the line:

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sheetQuit.plist");

With this error in LogCat:

04-05 02:34:07.846: E/libEGL(4935): call to OpenGL ES API with no current context (logged once per thread)

It seems odd that I can utilize all of my app's other functions from JNI methods except when loading textures. If anyone knows a way around this or a solution to this issue it would be much appreciated. Thanks.

Minggo Zhang RE: Loading textures when coming in from a JNI Call
Posts 1641
Added by Minggo Zhang about 1 year ago

Can not invoke opengl es functions in new thread.

Andre Rudlaff RE: Loading textures when coming in from a JNI Call
Posts 48
Added by Andre Rudlaff about 1 year ago

You can mot invoke OpenGL calls on a thread that does not have an OpenGL context.
You could create a shared OpenGL context on this thread if you really have to do the calls there. However, there is no magic Android API function call that will do that, but you have to take the hard way and use the EGL methods to create the context.
Also not all gpu's support multiple contexts (Althoug I guess that only were Adreno 130 and similar older chips).

A far simpler solution would be to schedule an method on CCScheduler which then makes your GL calls during the next frame.

Timothy Zhang RE: Loading textures when coming in from a JNI Call
Posts 37
Added by Timothy Zhang about 1 year ago

is CCScheduler thread-safe?

zohar schwartz RE: Loading textures when coming in from a JNI Call
Posts 6
Added by zohar schwartz 8 months ago

you can instead invoke your function from a gl thread , like that : {
@Override
public void run() {
nativeShowQuitOverlay();
}
});*

*mGLView.queueEvent(new Runnable()

when calling in java nativeShowQuitOverlay();

@ Carpe Diem @

zohar schwartz RE: Loading textures when coming in from a JNI Call
Posts 6
Added by zohar schwartz 8 months ago

sry meant

@ mGLView.queueEvent(new Runnable() {
@Override
public void run() {
nativePassShakeOccurred();
}
});@

@ Carpe Diem @

The Devil RE: Loading textures when coming in from a JNI Call
Posts 54
Added by The Devil 4 months ago

Any other way to do this? I am creating a sprite during a native c++ code call from Java and need to add it to a particular layer.


(1-6/6)