[原创]cocos2d-x研习录-第二阶 基本框架
了解完Cocos2D-x的基本概念和概念类之后,是不是有一种蠢蠢欲动的冲动,想要探究Cocos2D-x是如何完成这一切的。接着我将通过对Cocos2D-x自代的HelloCpp项目进行分析,初步了解Cocos2D-x游戏的基本框架,揭开Cocos2D-x神秘的面纱。
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);//这是一个编译宏,用于告诉编译器,忽略未使用变量而产生的告警信息。
UNREFERENCED_PARAMETER(lpCmdLine);AppDelegate app; // 创建一个Cocos2D-x程序实例
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setFrameSize(960, 640 ); //处理windows窗体注册和创建
return CCApplication::sharedApplication()->run(); //运行程序实例
}
int CCApplication::run()
{
PVRFrameEnableControlWindow(false);// Main message loop:
MSG msg;
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nLast);// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();
pMainWnd->centerWindow();
ShowWindow(pMainWnd->getHWnd(), SW_SHOW);//主消息循环
while (1)
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Get current time tick.
QueryPerformanceCounter(&nNow);// If it's the time to draw next frame, draw it, else sleep a while.
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
CCDirector::sharedDirector()->mainLoop(); }
else
{
Sleep(0);
}
continue;
}if (WM_QUIT == msg.message)
{
// Quit message loop.
break;
}// Deal with windows message.
if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}return (int) msg.wParam;
}
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();virtual bool applicationDidFinishLaunching(); //程序启动时调用
virtual void applicationDidEnterBackground(); //程序被切换到后台处于非激活时调用
virtual void applicationWillEnterForeground();//程序被切换到前台处于激活状态时调用
};
bool AppDelegate::applicationDidFinishLaunching() {
CCDirector *pDirector = CCDirector::sharedDirector(); //这里得到一个导演pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
TargetPlatform target = getTargetPlatform();
if (target == kTargetIpad)
{
// ipad
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
// don't enable retina because we don't have ipad hd resource
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
}
else if (target == kTargetIphone)
{
// iphone
if (pDirector->enableRetinaDisplay(true))
{
// iphone hd
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
}
else
{
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
}
}
else
{
// android, windows, blackberry, linux or mac
// use 960*640 resources as design resolution size
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
}// turn on display FPS
pDirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //这里得到一个场景// run
pDirector->runWithScene(pScene); //导演启动场景渲染return true;
}
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = NULL; \
return NULL; \
} \
}
CCScene *pScene = HelloWorld::scene(); //这里获取一个场景
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); //这里真正创建一个场景CCScene
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //创建一个布景CCLayer// add layer as a child to scene
scene->addChild(layer);//将布景添加到场景里// return the scene
return scene;//返回创建的场景
}
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
if (CCApplication::sharedApplication()->getTargetPlatform() == kTargetIphone)
{
pCloseItem->setPosition(ccp(visibleSize.width - 20 + origin.x, 20 + origin.y));
}
else
{
pCloseItem->setPosition(ccp(visibleSize.width - 40 + origin.x, 40 + origin.y));
}// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);/////////////////////////////
// 3. add your codes below...// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);// position the label on the center of the screen
pLabel->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height - 50 + origin.y));// add the label as a child to this layer
this->addChild(pLabel, 1);// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); //这里创建精灵// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));// add the sprite as a child to this layer
this->addChild(pSprite, 0);//将精灵添加到布景中
return true;
}
注:本人在本博客的原创文章采用创作共用版权协议(http://creativecommons.org/licenses/by-nc-sa/2.5/cn/), 要求署名、非商业用途和保持一致。要求署名包含注明我的网名及文章来源(我的博客地址:http://www.cnblogs.com/binbingg)。
[原创]cocos2d-x研习录-第二阶 基本框架的更多相关文章
- [原创]cocos2d-x研习录-第二阶 概念类之导演类(CCDirector)
CCDirector类是游戏的组织和控制中心(总指挥),它控制着主屏幕的显示.场景的切换和显示,以及游戏的开始.结束和暂停.它的继承关系图如下: CCDirector继承自基类CCObject, ...
- [原创]cocos2d-x研习录-第二阶 概念类之节点类(CCNode)
节点类CCNode在基本概念中并不存在,它是为了建立基本概念之间的关联关系而抽象出来的中间辅助类.这个类在Cocos2D-x中极为重要,它为概念类之间搭建了一座宏伟的桥梁.它的继承关系图如下: ...
- [原创]cocos2d-x研习录-第二阶 基本概念
在Cocos2D-x引擎中,有几个非常重要的概念:导演(CCDirector).摄像机(CCCamera).场景(CCSecen).布景(CCLayer).精灵(CCSPrite)和动作(CCActi ...
- [原创]cocos2d-x研习录-第二阶 概念类之摄相机类(CCCamera)
在Cocos2D-x中,每个CCNode都拥有一个摄像机类CCCamera.只有通过CCCamera,CCNode才会被渲染出来.当CCNode发生缩放.旋转和位置变化时,都需要覆盖CCCamera, ...
- [原创]cocos2d-x研习录-第二阶 概念类之精灵类(CCSprite)
上一节说布景层CCLayer是小容器,那么精灵类CCSprite就是容器添加的内容,它是构成游戏的主要元素.精灵这个名称应该是游戏专用,它表示游戏中玩家操作的主角.敌人.NPC(Non Player ...
- [原创]cocos2d-x研习录-第二阶 概念类之布场层类(CCLayer)
上面说场景CCScene相当于一个大容器,那么布景层类CCLayer就是大容器里的若干个小容器.每个游戏场景CCScene会有很多层CCLayer,每一层CCLayer负责各自的任务.看一下CCLay ...
- [原创]cocos2d-x研习录-第二阶 概念类之场景类(CCScene)
场景类CCScene是Cocos2D-x在屏幕显示的内容,相当于游戏关卡或界面.CCDirector任何时候只能显示一个场景CCScene,游戏中可能存在若干场景,CCDirector通过场景切换达到 ...
- [原创]cocos2d-x研习录—前言
我认为很多开发者面对层出不穷的新技术.新思想和新理念,最为之苦恼的是找不到行之有效的学习方法,对于知识的本质缺乏认识,虽阅读了大量教材,却无法将其融入自己的知识体系,并搭建自己的知识树.不可否认,教材 ...
- [原创]cocos2d-x研习录-第一阶 背景介绍 之 cocos2d家族史
Cocos2D是一个2D开源游戏引擎,它最早是由Ricardo Quesada(阿根廷人,社区简称Riq)和他的朋友们用Python开发的,用于开发2D游戏和基于2D图形的任何应用.最早引擎的名字源自 ...
随机推荐
- 邮箱输入(仿gmail)
年前同事做邮件,我调研了几个如163.qq等的邮箱,最终觉得还是gmail的用着舒服,看着也舒服.就仿照写了个.还有问题.记录下,有时间再整理下代码. demo
- SVM学习(续)
SVM的文章可以看:http://www.cnblogs.com/charlesblc/p/6193867.html 有写的最好的文章来自:http://www.blogjava.net/zhenan ...
- 横向滑动的HorizontalListView滑动指定位置的解决方法
项目中用到了自定义横向滑动的控件:HorizontalListView,点击其中一项,跳转到另外一个大图界面,大图界面也是HorizontalListView,想使用setSelection方法设定 ...
- Tomcat下使用war包发布项目
Tomcat下使用war包发布项目 转自<Tomcat下使用war包发布项目 >,地址:http://blog.csdn.net/wy818/article/details/7240294 ...
- eclipse 本地项目提交到远程库以及从远程库中添加项目 ---git
本地项目提交到远程库 1.右击项目->team->share project 2.选择本地库 从远处库中的项目拉到本地 1.右击项目->import项目
- SQL Server中开发常用的数据类型
我们在日常开发过程中,在进行数据库设计的时候,经常会遇到如果选择数据类型的问题,选择什么样的数据类型最合适,然后当我们在项目中建立对应实体的时候,如何选择对应的CLR类型.针对这些问题,于是这篇文章诞 ...
- 实现用CSS切割图片的方法
切割图片这里不是真正的切割,只是用CSS取图片中的一部分而已.这样做的好处就是减少了打开网页时请求图片的次数.主要有两种方式,一是做为某一元素的背景图片,二是用img元素的属性. 方法一: 用CSS中 ...
- sql数据库 管理处理问题--维护计划
问题:SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 MYPC/Administrator' 的信息,错误代码 0x534. [SQLSTATE 42000] ( ...
- Ionic 2.0.0-rc.1 发布,HTML5 移动应用框架
Ionic 2.0.0-rc.1 发布了,Ionic Framework 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应用前端框架.本次更新内容如下: Bug 修 ...
- MagicalRecord 多表关联数据操作
最近在使用MagicalRecord做数据持久层CoreData的操作库,今天做了一个多表关联数据的操作,整理了一个demo,特此记录一下. 关于如何使用Cocopads 和 MagicalRecor ...