[原创]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图形的任何应用.最早引擎的名字源自 ...
随机推荐
- os.environ()
---------2016-5-9 18:56:39-- source:OS.ENVIRON()详解
- Authentication和Authorization的区别
搞不太清楚Authentication和Authorization的区别,在网上搜了一下,lucky16的一篇文章讲的通俗,看了就懂,记录下来: 你要登机,你需要出示你的身份证和机票,身份证是为了证明 ...
- VMware克隆后,网卡若干问题
网卡问题 由于克隆虚拟机,vmware只是修改了虚拟机的名字等信息,并没有修改虚拟硬盘中的任何信息, 导致克隆后网卡的MAC地址和操作系统中记录的mac地址不符,导致eth0启动不起来. 操作系统记录 ...
- 用c语言编写直接插入法
#include<stdio.h> //直接插入法 void D_insert(int s[],int n); int main() { int i; ]; printf("pl ...
- toolkit学习笔记
- iOS_autoLayout_Masonry
概述 Masonry是一个轻量级的布局框架与更好的包装AutoLayout语法. Masonry有它自己的布局方式,描述NSLayoutConstraints使布局代码更简洁易读. ...
- Angular - ng-repeat高级用法
ng-repeat高级用法: 遍历数组: <li ng-repeat="item in array">{{item}}</li> 遍历对象: k ...
- bzoj1211: prufer序列 | [HNOI2004]树的计数
题目大意: 告诉你树上每个节点的度数,让你构建出这样一棵树,问能够构建出树的种树 这里注意数量为0的情况,就是 当 n=1时,节点度数>0 n>1时,所有节点度数相加-n!=n-2 可以通 ...
- c/c++面试题(5)(c++重要的概念详解)
1.C++面向对象的三大特征? 1)封装:将客观事物封装成抽象的类,并且设计者可以对类的成员进行访问控制权限控制. 这样一方面可以做到数据的隐藏,保护数据安全;另一方面,封装可以修改类的内部 实现而不 ...
- php中json_encode UTF-8中文乱码问题
最近在接口代码当中用到过json_encode,在网上找到说json_encode编码设置为UTF-8中文就不会乱码,经验证这办法确实是有效果的,但是不知道为什么,代码在用过一段时间之后就不太管用了. ...