cocos2dx学习笔记(多线程)
来源:互联网 发布:高维数据降维方法研究 编辑:程序博客网 时间:2024/05/29 04:47
多线程的实现:
PthreadTest.h
#ifndef __MTNotificationQueue_h#define __MTNotificationQueue_h#include "cocos2d.h"#include <iostream>#include"pthread\pthread.h"USING_NS_CC;using namespace std;//线程安全的消息队列class MTNotificationQueue : public CCObject { //队列对象static MTNotificationQueue * mInstance; class CGarbo { public: ~CGarbo() { if (MTNotificationQueue::mInstance) delete MTNotificationQueue::mInstance;mInstance = NULL; } }; static CGarbo Garbo; typedef struct { string name; CCObject* object; } NotificationArgs; //消息队列 vector<NotificationArgs> notifications; MTNotificationQueue(void); public: static MTNotificationQueue* sharedNotificationQueue(); void postNotifications(float dt); ~MTNotificationQueue(void); void postNotification(const char* name, CCObject* object); }; #endif
PthreadTest.cpp
#include "PthreadTest.h"//互斥锁,全局唯一pthread_mutex_t sharedNotificationQueueLock;//管理互斥锁的生命周期class LifeManager_PThreadMutex { pthread_mutex_t* mutex; public: LifeManager_PThreadMutex(pthread_mutex_t* mut) : mutex(mut) { //初始化 pthread_mutex_init(mutex, NULL); } ~LifeManager_PThreadMutex() { //销毁 pthread_mutex_destroy(mutex); } }__LifeManager_sharedNotificationQueueLock(&sharedNotificationQueueLock); //生命周期锁类,管理锁开关class LifeCircleMutexLocker { pthread_mutex_t* mutex; public: LifeCircleMutexLocker(pthread_mutex_t* aMutex) : mutex(aMutex) { //上锁 pthread_mutex_lock(mutex); } ~LifeCircleMutexLocker(){ //解锁 pthread_mutex_unlock(mutex); } }; #define LifeCircleMutexLock(mutex) LifeCircleMutexLocker __locker__(mutex) MTNotificationQueue* MTNotificationQueue::mInstance = NULL;MTNotificationQueue::MTNotificationQueue(void){}MTNotificationQueue::~MTNotificationQueue(void){}//消息队列的单例MTNotificationQueue* MTNotificationQueue::sharedNotificationQueue(){if (!mInstance) {mInstance = new MTNotificationQueue();}return mInstance;}//消息队列的处理void MTNotificationQueue::postNotifications(float dt) { LifeCircleMutexLock(&sharedNotificationQueueLock); for(uint16_t i = 0; i < notifications.size(); i++) { NotificationArgs &arg = notifications[i]; // void postNotification(const char *name);// 发送一个消息 CCNotificationCenter::sharedNotificationCenter()-> postNotification(arg.name.c_str(), arg.object); } notifications.clear(); } //单个消息加入void MTNotificationQueue::postNotification(const char* name, CCObject* object) { LifeCircleMutexLock(&sharedNotificationQueueLock); NotificationArgs arg; arg.name = name; if(object != NULL) arg.object = object; //object->copy(); else arg.object = NULL; notifications.push_back(arg); }
BackgroundLayer.cpp
#include "BackgroundLayer.h"#include "User.h"#include"PthreadTest.h"USING_NS_CC;CCScene* BackgroundLayer::scene(){ // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object BackgroundLayer *layer = BackgroundLayer::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instanceCCImage *bgImage;void* loadImages(void* arg) { bgImage = new CCImage(); bgImage->initWithImageFileThreadSafe("HelloWorld.png"); // 发送一个已经完成加载的消息 MTNotificationQueue::sharedNotificationQueue()-> postNotification("loadImageFinish", NULL); return NULL; } void BackgroundLayer::loadImageFinish(CCObject* sender) { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //加载图片 CCTexture2D* texture = CCTextureCache::sharedTextureCache()-> addUIImage(bgImage, "HelloWorld.png"); //释放bgImage内存,因为bgImage为进程间的共享数据 bgImage->release(); CCSprite* bg = CCSprite::createWithTexture(texture); CCSize size = bg->getContentSize(); bg->setPosition(ccp(winSize.width / 2, winSize.height / 2)); float f = max(winSize.width/size.width,winSize.height/size.height); bg->setScale(f); this->addChild(bg); } bool BackgroundLayer::init() { //启动消息队列定时器,使postNotifications每帧都被调用 CCDirector::sharedDirector()->getScheduler()->scheduleSelector( schedule_selector(MTNotificationQueue::postNotifications), MTNotificationQueue::sharedNotificationQueue(), 1.0 / 60.0, false); bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //注册消息 //void addObserver(CCObject *target, //接收消息的对象 // SEL_CallFuncO selector, //响应消息的函数 // const char *name, //待接收的消息 // CCObject *obj); //指定消息的发送者,目前暂时为无用参数 CCNotificationCenter::sharedNotificationCenter()->addObserver( this, callfuncO_selector(BackgroundLayer::loadImageFinish), "loadImageFinish", NULL); pthread_t tid; // 创建线程,loadImages为入口指针 pthread_create(&tid, NULL, &loadImages, NULL); bRet = true; } while (0); return bRet; }
0 0
- cocos2dx学习笔记(多线程)
- cocos2dx学习笔记(cocos2dx特效)
- cocos2dx学习笔记(cocos2dx特效)
- 【cocos2dx】cocos2dx学习笔记
- cocos2dx学习笔记(转)
- cocos2dx学习笔记(选择器)
- cocos2dx学习笔记(定时器)
- cocos2dx学习笔记(截图)
- cocos2dx学习笔记(定时器)
- cocos2dx学习笔记(选择器)
- cocos2dx学习笔记(截图)
- cocos2dx学习笔记(一)
- Cocos2dx学习笔记36 多线程与异步加载
- cocos2dx学习笔记(一)环境搭建
- cocos2dx学习笔记(新建工程)
- cocos2dx学习笔记(内存管理机制)
- cocos2dx学习笔记(新建工程)
- cocos2dx学习笔记(内存管理机制)
- 从零开始学习OpenGL ES之七 – 变换和矩阵
- 1407281202-hd-Add ‘em.cpp
- 你会健身吗?
- 解决cocos2d-x 中文乱码的问题
- hdu 2162 Add ‘em
- cocos2dx学习笔记(多线程)
- 触摸屏校正原理
- 维基oi 3285 转圈游戏
- java开发编程规范
- UITableView的使用大全
- 从零开始学习OpenGL ES之四补遗 – setupView重写
- Android开发工具下载地址
- V4L2 Video Capture - 02
- Programming trivia: 4x4 integer matrix transpose in SSE2