新建一个工程,相信感兴趣的同学都想知道cocos引擎都是如何运行的

想知道是如何运行的,看懂四个文件即可

话不多说,上代码:

1、首先解释 AppDelegate.h

 #ifndef  _APP_DELEGATE_H_
#define _APP_DELEGATE_H_ #include "cocos2d.h" /**
@brief The cocos2d Application. Private inheritance here hides part of interface from Director.
*/ //从这里可以看到AppDelegate继承了cocos2d::Application ,而cocos2d::Application是cocos2d-x引擎提供的基类
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
/* */
virtual void initGLContextAttrs(); /**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*游戏启动时调用的函数,在这里可以初始化导演对象和场景对象
*/
virtual bool applicationDidFinishLaunching(); /**
@brief Called when the application moves to the background
@param the pointer of the application
*游戏进入后台时调用的函数
*/
virtual void applicationDidEnterBackground(); /**
@brief Called when the application reenters the foreground
@param the pointer of the application
*游戏进入前台时调用的函数
*/
virtual void applicationWillEnterForeground();
}; #endif // _APP_DELEGATE_H_

2、AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h" USING_NS_CC;//这个是cocos2d-x提供的一个宏,它是用来替换 using namespace cocos2d语句的。 static cocos2d::Size designResolutionSize = cocos2d::Size(, );
static cocos2d::Size smallResolutionSize = cocos2d::Size(, );
static cocos2d::Size mediumResolutionSize = cocos2d::Size(, );
static cocos2d::Size largeResolutionSize = cocos2d::Size(, ); AppDelegate::AppDelegate()
{
} AppDelegate::~AppDelegate()
{
} // if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {, , , , , }; GLView::setGLContextAttrs(glContextAttrs);
} // if you want to use the package manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return ; //flag for packages manager
} // 游戏启动时调用的函数
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();//初始化导演类
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("NotesDamo", cocos2d::Rect(, , designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create("NotesDamo");
#endif
director->setOpenGLView(glview);//设置导演类的OpenGL视图
} // turn on display FPS
director->setDisplayStats(true);//设置是否在屏幕上显示帧率信息(一般都是为了测试,实际发布时是不会显示的) // set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / );//一秒执行60帧 // Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
auto frameSize = glview->getFrameSize();
// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{
director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{
director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium size.
else
{
director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
} register_all_packages(); // create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();//创建导演类对象scene // run
director->runWithScene(scene);//运行该场景(会使游戏进入该场景) return true;
} // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. //游戏进入后台时调用的函数
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();//停止场景中的动画 // if you use SimpleAudioEngine, it must be paused
// 停止背景音乐,默认时注释掉的
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
} // this function will be called when the app is active again
// 游戏进入前台时调用的函数
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();//开始场景中的动画 // if you use SimpleAudioEngine, it must resume here
// 继续背景音乐的,默认是注释掉的
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

3、HelloWorldScene.h

 #ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" /*
*在这里我们可以看出,HelloWorld类继承了cocos2d::Layer类;它被称为层(layer),这些层被放到了场景(scene)中,场景类是:cocos2d::Scene;
注意:HelloWorld.h虽然命名为场景,但是它内部定义的HelloWorld类是一个层
*/
//HelloWorld继承了cocos2d::Layer,HelloWorld是一个层,而不是场景。
class HelloWorld : public cocos2d::Layer
{ public: static cocos2d::Menu* m_pSelectedItem(); virtual ~HelloWorld(){} static cocos2d::Scene* createScene();//声明创建当前的层HelloWorld所在场景的静态函数createScene(); virtual bool init();//声明初始化层HelloWorld实例函数。 // a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);//声明菜单回调函数menuCloseCallback,用于触摸菜单事件的回调。 CREATE_FUNC(HelloWorld);//CREATE_FUNC是cocos2d-x中定义的一个宏(作用是:创建一个静态函数"static create()",该函数可以用来创建层); // implement the "static create()" method manually }; #endif // __HELLOWORLD_SCENE_H__

4、HelloWorldScene.cpp

 #include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" USING_NS_CC;
/*
说明:createScene()函数式是在游戏应用启动的时候,在AppDelegate中的bool AppDelegate::applicationDidFinishLaunching()函数中通过 auto scene = HelloWorld::createScene()语句调用的。
createScene()中做了三件事情,首先创建了HelloWorld层所在的场景对象,其次创建了HelloWorld层,最后将HelloWorld层添加到场景scene中;
*/
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
// 当调用到这句创建层的时候,会调用HelloWorld的实例函数init(),达到初始化HelloWorld层的目的。
auto layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
// 初始化父类
if ( !Layer::init() )
{
return false;
} auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->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
// 增加一个菜单项,单机的时候退出程序
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/ ,
origin.y + closeItem->getContentSize().height/)); // create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);//自定义菜单对象的位置
this->addChild(menu, );//把菜单对象添加到当前HelloWorld层上 /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label //添加label标签标题
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", ); // position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/,
origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer
this->addChild(label, ); // add "HelloWorld" splash screen"
//添加精灵,也就是cocos2d-x的logo,定义到屏幕中央
auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y)); // add the sprite as a child to this layer
this->addChild(sprite, ); return true;
} // 菜单回调函数(返回主界面)
void HelloWorld::menuCloseCallback(Ref* pSender)
{
//Close the cocos2d-x game scene and quit the application
Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)//IOS表示iOS平台
exit();
#endif /*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/ //EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent); // PhysicsShape物理引擎类精灵(也属于精灵) // 节点
//(1)创建节点
Node * chilNode = Node::create();
//(2)查找子节点
Node *node = node ->getChildByTag();
//(3)增加新的子节点
node->addChild(chilNode,,);
//(4)删除子节点,并停止该节点上的一切动作
node->removeChildByTag(,true);
//(5)通过NOde指针删除节点
node ->removeChild(node);
//(6)删除所有子节点,并停止这些节点上的一切动作
node ->removeAllChildrenWithCleanup(true);
//(7)从父节点中删除 node 节点,并停止该节点上的一切动作。
node->removeFromParentAndCleanup(true);
/*Node重要属性*/
// setPosition; 坐标
// setAnchorPoint(Vce2(0.5,.05)); 锚点 //坐标
// Vec2 touchLocation = touch ->getLocationInView();
// Vec2 touchLocation2 = Director::getInstance()->convertToGL(touchLocation); } /**
cocos2d-x的事件响应机制:即菜单层最先接收到系统事件,则排在后面的是精灵层,最后是背景层,在事件的传递过程中 ,如果有一个层处理了该事件,则排在后面的层将不再接受到该事件。
*/

cocos2d-x C++ 原始工程引擎运行机制解析的更多相关文章

  1. 前端读者 | 由setTimeout引发的JS引擎运行机制的研究

    本文来自 @xiaoyuze88 链接:http://xiaoyuze88.github.io/ 太久没碰代码了,那天想到关于循环调用setTimeout实现每隔一秒输出递增的数的那个问题,搞了搞,发 ...

  2. [转]OpenStack Neutron运行机制解析概要

    转载自:http://panpei.net.cn/2013/12/04/openstack-neutron-mechanism-introduce/ 自从开学以来,玩OpenStack也已经3个月了, ...

  3. 深入GPU硬件架构及运行机制

    目录 一.导言 1.1 为何要了解GPU? 1.2 内容要点 1.3 带着问题阅读 二.GPU概述 2.1 GPU是什么? 2.2 GPU历史 2.2.1 NV GPU发展史 2.2.2 NV GPU ...

  4. MVC运行机制[转]

    原:http://www.cnblogs.com/jyan/archive/2012/06/29/2569566.html#3122335 ASP.NET是一种建立动态Web应用程序的技术.它是.NE ...

  5. Scrapy各部分运行机制?Xpath为None?多层Response如何编写?搞定Scrapy的坑

    前言 Scrapy那么多模块都是怎么结合的啊?明明在chrome上的xpath helper插件写好了xpath,为什么到程序就读取的是None?Scrapy可以直接写多层response么?难道必须 ...

  6. JVM虚拟机运行机制

    JVM虚拟机运行机制 什么是JVM?虚拟机是物理机器的软件实现.Java是用在VM上运行的WORA(Write Once Run Anywhere)概念而开发的.编译器将Java文件编译为Java . ...

  7. javascript运行机制

    太久没更新博客了,Javascript运行机制 Record it 1.代码块 JavaScript中的代码块是指由<script>标签分割的代码段.例如: <script type ...

  8. (转)浅析JS运行机制

    原文 从一个简单的问题谈起: 1 <script type="text/javascript"> 2 alert(i); // ? 3 var i = 1; 4 < ...

  9. PHP的运行机制与原理(底层) [转]

    说到php的运行机制还要先给大家介绍php的模块,PHP总共有三个模块:内核.Zend引擎.以及扩展层:PHP内核用来处理请求.文件流.错误处理等相关操作:Zend引擎(ZE)用以将源文件转换成机器语 ...

随机推荐

  1. css学习_css复合选择器

    css复合选择器 a.交集选择器  (即...又...:选择器之间不能有空格) p.one{color:red;] b.并集选择器(中间由逗号隔开) p,div{color:red;} c.后代选择器 ...

  2. ROS基础

    在ROS中启动Gazebo物理仿真环境 roslaunch gazebo_ros empty_world.launch 打开后一片漆黑是以为gazebo需要从国外的网站上下载模型,国内网络不行,一直下 ...

  3. robot framework ——关键字run keyword if 如何在一个条件下接多个执行语句,以及如何写复杂条件句

    曾一度疯狂搜索run keyword if 的用法,帖子是挺多的,可惜,没有一个我想要的.现在我终于把我想要的用法,收集好了,在此总结下. 1.曾经天真的以为  run keyword if +条件 ...

  4. { MySQL基础数据类型}一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型

    MySQL基础数据类型 阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己 ...

  5. autofac生命周期入门(如何避免内存泄漏)

    如果你是一个IOC新手,那么生命周期可能会比较难以理解.以至于谈到这个问题时,一些老手也时常表示疑虑和害怕.一个令人不安的问题就是-对象没有在合适的时机被销毁.这样一来内存的使用率就会一直攀升,直到程 ...

  6. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案

    Springboot中静态资源和拦截器处理(踩了坑)   背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...

  7. screen基本用法

    当某些命令执行时间过长或者某些程序关闭shell后自动断开时,就能使用screen 1.安装yum -y install screen 2.输入screen命令进入screen控制台 3.输入scre ...

  8. Page3:组合系统状态空间输入输出描述、矩阵指数函数性质[Linear System Theory]

    内容包含组合系统的状态空间描述以及输入输出描述,零输入响应的概念以及矩阵指数函数的性质

  9. JsLint 的安装和使用

    JSLint 是一款Javascript验证工具,在定位错误并确保基本指南得以遵循时,非常有用.如果你正在编写专业级的javascript,应该使用 JSLint 或者类似的验证工具(JSHint). ...

  10. 动态代理Dynamic Proxy

    代理模式是常用的Java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类 预处理消息,过滤消息,把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常会存在关联关系,一 ...