标签:one linux import 音视频 1.3 场景 .cpp 针对 back
- // AppDelegate.cpp
- bool AppDelegate::applicationDidFinishLaunching() {
- … …
- FlashScene* scene = FlashScene::create();
- pDirector->runWithScene(scene);
- return true;
- }
- //FlashScene.h
- struct ResourceLoadIndicator {
- pthread_mutex_t mutex;
- bool load_done;
- void *context;
- };
- class FlashScene : public Scene
- {
- public:
- FlashScene(void);
- ~FlashScene(void);
- virtual bool init();
- CREATE_FUNC(FlashScene);
- bool getResourceLoadIndicator();
- void setResourceLoadIndicator(bool flag);
- private:
- void updateScene(float dt);
- private:
- ResourceLoadIndicator rli;
- };
- // FlashScene.cpp
- bool FlashScene::init()
- {
- bool bRet = false;
- do {
- CC_BREAK_IF(!CCScene::init());
- Size winSize = Director::getInstance()->getWinSize();
- //FlashScene自己的资源仅仅能同步载入了
- Sprite *bg = Sprite::create("FlashSceenBg.png");
- CC_BREAK_IF(!bg);
- bg->setPosition(ccp(winSize.width/2, winSize.height/2));
- this->addChild(bg, 0);
- this->schedule(schedule_selector(FlashScene::updateScene)
- , 0.01f);
- //start the resource loading thread
- rli.load_done = false;
- rli.context = (void*)this;
- pthread_mutex_init(&rli.mutex, NULL);
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- pthread_t thread;
- pthread_create(&thread, &attr,
- resource_load_thread_entry, &rli);
- bRet=true;
- } while(0);
- return bRet;
- }
- static void* resource_load_thread_entry(void* param)
- {
- AppDelegate *app = (AppDelegate*)Application::getInstance();
- ResourceLoadIndicator *rli = (ResourceLoadIndicator*)param;
- FlashScene *scene = (FlashScene*)rli->context;
- //load music effect resource
- … …
- //init from config files
- … …
- //load images data in worker thread
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
- "All-Sprites.plist");
- … …
- //set loading done
- scene->setResourceLoadIndicator(true);
- return NULL;
- }
- bool FlashScene::getResourceLoadIndicator()
- {
- bool flag;
- pthread_mutex_lock(&rli.mutex);
- flag = rli.load_done;
- pthread_mutex_unlock(&rli.mutex);
- return flag;
- }
- void FlashScene::setResourceLoadIndicator(bool flag)
- {
- pthread_mutex_lock(&rli.mutex);
- rli.load_done = flag;
- pthread_mutex_unlock(&rli.mutex);
- return;
- }
- void FlashScene::updateScene(float dt)
- {
- if (getResourceLoadIndicator()) {
- Director::getInstance()->replaceScene(
- WelcomeScene::create());
- }
- }
- threadid=24: thread exiting, not yet detached (count=0)
- threadid=24: thread exiting, not yet detached (count=1)
- threadid=24: native thread exited without detaching
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
翻看了一下引擎内核的代码TextureCache::addImageAsync。在线程创建以及线程主函数中也没有发现什么特别的设置。为何内核能够创建线程,我自己创建就会崩溃呢。Debug多个来回,问题似乎聚焦在resource_load_thread_entry中运行的任务。
在我的代码里,我利用SimpleAudioEngine载入了音效资源、利用UserDefault读取了一些持久化的数据,把这两个任务去掉。游戏就会进入到下一个环节而不会崩溃。
Android开发人员官网上有这么一段话:
All threads are Linux threads, scheduled by the kernel. They‘re usually started from managed code (using Thread.start), but they can also be created elsewhere and then attached to the JavaVM. For example, a thread started with pthread_create can be attached with the JNI AttachCurrentThread or AttachCurrentThreadAsDaemon functions. Until a thread is attached, it has no JNIEnv, and cannot make JNI calls. |
好,我们来尝试一下,Cocos2d-x引擎提供了一些JniHelper方法,能够方便进行Jni相关操作。
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- #include "platform/android/jni/JniHelper.h"
- #include <jni.h>
- #endif
- static void* resource_load_thread_entry(void* param)
- {
- … …
- JavaVM *vm;
- JNIEnv *env;
- vm = JniHelper::getJavaVM();
- JavaVMAttachArgs thread_args;
- thread_args.name = "Resource Load";
- thread_args.version = JNI_VERSION_1_4;
- thread_args.group = NULL;
- vm->AttachCurrentThread(&env, &thread_args);
- … …
- //Your Jni Calls
- … …
- vm->DetachCurrentThread();
- … …
- return NULL;
- }
The JavaVM provides the "invocation interface" functions, which allow you to create and destroy a
JavaVM. In theory you can have multiple JavaVMs per process, but Android only allows one.
The JNIEnv provides most of the JNI functions. Your native functions all receive a JNIEnv as the first argument.
The JNIEnv is used for thread-local storage. For this reason, you cannot share a JNIEnv between threads.
|
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile("All-Sprites.plist");
我们的确发现了一些异常日志:
- libEGL: call to OpenGL ES API with no current context (logged once per thread)
要解决问题就 得查看一下TextureCache::addImageAsync是怎样做的了。
- static void* resource_load_thread_entry(void* param)
- {
- … …
- allSpritesImage = new Image();
- allSpritesImage->initWithImageFile("All-Sprites.png");
- … …
- }
- void FlashScene::updateScene(float dt)
- {
- if (getResourceLoadIndicator()) {
- // construct texture with preloaded images
- Texture2D *allSpritesTexture = TextureCache::getInstance()->
- addImage(allSpritesImage, "All-Sprites.png");
- allSpritesImage->release();
- SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
- "All-Sprites.plist", allSpritesTexture);
- Director::getInstance()->replaceScene(WelcomeScene::create());
- }
- }
标签:one linux import 音视频 1.3 场景 .cpp 针对 back
原文地址:http://www.cnblogs.com/gccbuaa/p/7073637.html