cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop
文章出自于 http://blog.csdn.net/zhouyunxuan
cocos2d到底是怎样把场景展示给我们的,我一直非常好奇。
凭个人猜想,引擎内部的结构类似于这样
while(true)
{
if(update_span < min_update_span)
{
update_game();
if(done)
{
break;
}
}
else
{
cal_update_span();
}
}
在app開始执行时会调用里面的方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
来看看这个函数最后return YES之前的一行代码
cocos2d::Application::getInstance()->run();
没错,就是这个,然后我们进入到run函数里面来看个到底
int Application::run()
{
if (applicationDidFinishLaunching())
{
//这个函数在这里调用了startMainLoop
[[CCDirectorCaller sharedDirectorCaller] startMainLoop];
}
return 0;
}
然后我们继续跟进看看startMainLoop
-(void) startMainLoop
{
// Director::setAnimationInterval() is called, we should invalidate it first
[displayLink invalidate];
displayLink = nil; //给CADisplayLink传了一个doCaller函数,让CADisplayLink不断的调用
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];
//设置调用频率
[displayLink setFrameInterval: self.interval];
//開始循环吧!
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
CADisplayLink,须要增加QuartzCore.framework
这个函数类似于update函数,默认每秒被调用60次,如今我们再进入doCaller函数吧
-(void) doCaller: (id) sender
{
cocos2d::Director* director = cocos2d::Director::getInstance();
[EAGLContext setCurrentContext: [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context]];
//最终进入到这场表演的主角了,我期待了好久!! 。
director->mainLoop();
}
事实上Director::getInstance();返回的不是Director。被骗了好久-= -
Director::getInstance() 返回的并非Director。而是Director的子类DisplayLinkDirector();
Director* Director::getInstance()
{
if (!s_SharedDirector)
{
s_SharedDirector = new DisplayLinkDirector();
s_SharedDirector->init();
}
return s_SharedDirector;
}
程序的主要逻辑都通过调用mainloop来完毕,这种方法负责调用计时器。画图,发送全局通知,并处理内存回收池,这种方法按帧调用,每帧调用一次。而帧之间取决于两个因素,一个是预设的帧频(默觉得每秒六十次),还有一个是每帧的计算量大小,当逻辑处理与画图计算量过大时,设备无法完毕每秒六十次的绘制,此时帧率就会减少。
void DisplayLinkDirector::mainLoop()
{
//是否在下一循环中清除
//bool _purgeDirectorInNextLoop; // this flag will be set to true in end()
if (_purgeDirectorInNextLoop)
{
log("clear director");
_purgeDirectorInNextLoop = false;
//会做一些清理
purgeDirector();
}
//假设不清除的话(且为合法的)ps:一般都是会进入到这里。然后进行绘制等等。 else if (! _invalid)
{
//画场景
drawScene(); // release the objects
PoolManager::getInstance()->getCurrentPool()->clear();
}
}
然后我们接着看看这伟大的 drawScene里面做了什么吧!
void Director::drawScene()
{
//计算时间增量
// calculate "global" dt
calculateDeltaTime(); // 假设两帧间隔时间太短(_deltaTime等于0)就直接忽略这次的绘制
// FLT_EPSILON 是 __FLT_EPSILON__ 的宏。__FLT_EPSILON__ 是c99的特征,它是满足 x+1.0不等于1.0的最小的正数,直接输出为0。
if(_deltaTime < FLT_EPSILON)
{
return;
} //
if (_openGLView)
{
_openGLView->pollInputEvents();
} //tick before glClear: issue #533
//仅仅要游戏没有暂停,调度器神马的就会在这里被运行。
if (! _paused)
{
_scheduler->update(_deltaTime);
_eventDispatcher->dispatchEvent(_eventAfterUpdate);
} glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* to avoid flickr, nextScene MUST be here: after tick and before draw.
XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
if (_nextScene)
{
setNextScene();
} pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //画场景
if (_runningScene)
{
_runningScene->visit(_renderer, Mat4::IDENTITY, false);
_eventDispatcher->dispatchEvent(_eventAfterVisit);
} // 画通知节点
if (_notificationNode)
{
_notificationNode->visit(_renderer, Mat4::IDENTITY, false);
} //假设设置了显示debug信息,就会在这里进行每帧的更新。
if (_displayStats)
{
showStats();
} _renderer->render();
_eventDispatcher->dispatchEvent(_eventAfterDraw); popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); _totalFrames++; // 交换缓冲区
if (_openGLView)
{
_openGLView->swapBuffers();
} //计算fps上的信息
if (_displayStats)
{
calculateMPF();
}
}
最初提出来的结构和发现的结构还是有点相似的。
仅仅只是引擎更友好的抽象封装出来了。且做了非常多防止异常的处理。程序猿还都是非常小心的嘛。。。
调用的CADisplayLink是ios平台的,假设换成其它平台就不一样啦。
毕竟win是木有CADisplayLink的。
不相信?
好吧,我们来看看win是怎样调用的吧,首先找到Application::run()函数。
//假设窗体不关闭
while(!glview->windowShouldClose())
{
//计算时间
QueryPerformanceCounter(&nNow);
//两帧间距时间要大一点才给你画哦
if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{
//上一帧的时间就等于如今这一帧,用于下次计算两帧的间隔时间。 nLast.QuadPart = nNow.QuadPart; //进入到我们伟大的mainloop了。是不是有点小激动 - -
director->mainLoop();
glview->pollEvents();
}
else
{
//神马!睡0秒。 好吧。光是看表面还是非常骗人的。
Sleep(0);
}
}
Sleep(0)是指CPU交出当前线程的运行权,让CPU去运行其它线程。也就是放弃当前线程的时间片。转而运行其它线程。
一般来说,假设当前线程比較耗时比較占CPU资源。能够在结尾处加上Sleep(0), 这样效率会得到大大的提高。
看了win上面的调用,发现和我一開始的猜想更像有木有!!!
有时候。做笔记记录下学习过程也挺不错的。
肯定没人会转载的- -
可是为了防止蜘蛛爬走了我的文章,我还是凝视下- -
cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop的更多相关文章
- [XMPP]iOS聊天软件学习笔记[一]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- cocos2d-x 3.1.1 学习笔记[3]Action 动作
这些动画貌似都非常多的样子,就所有都创建一次. 代码例如以下: /* 动画*/ auto sp = Sprite::create("card_bg_big_26.jpg"); Si ...
- cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵
Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...
- cocos2d-x 3.1.1 学习笔记[21]cocos2d-x 创建过程
文章出自于 http://blog.csdn.net/zhouyunxuan RootViewController.h #import <UIKit/UIKit.h> @interfac ...
- cocos2d-x 3.1.1 学习笔记[4]GridActions 网格动画
文章写的 http://blog.csdn.net/zhouyunxuan 老样子.见代码. //GridActions can only used on NodeGrid auto nodeGri ...
- cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析
//http须要引入的头文件和命名空间 #include <network/HttpClient.h> using namespace network; //json须要引入的头文件 #i ...
- [XMPP]iOS聊天软件学习笔记[四]
昨天完成了聊天界面,基本功能算告一段落 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework git clone https://githu ...
- [XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...
- [XMPP]iOS聊天软件学习笔记[二]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
随机推荐
- 系统出现0x0000006B蓝屏修复,系统文件损坏 bootcat.cache、driver.stl
系统蓝屏,无论如何都不能进入系统,所以你需要一个U盘启动器,就是能绕过电脑的系统进入电脑,可以用U盘做一个U盘启动器,或者其他方法均可以,只要能进入到你的电脑访问C盘即可 2 下载链接内的文件解压后放 ...
- POJ 1742 Coins(多重背包?)
题解 一个自然的思路是对于每一个物品做一次01背包 然后T飞了. 试着用二进制拆分,还是T了. 单调队列,对不起,懒,不想写. 我们这样想.设dp[i]代表i这个面值前几种硬币是否能凑到 然后对于每一 ...
- Windows和Linux的编译理解
Windows一般编译出来的x86的软件,就是只能在x86的系统上才能运行,同理,在x64系统上也是一样的道理. Linux利用gcc编译器编译,可以在Linux上面运行,但是想要在嵌入式系统上运行的 ...
- CentOS 7.2 (mini) 里iptables防火墙怎么关闭?
centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但是iptables的服务是没安装的.所以你只要停止firewalld服务即可:sudo ...
- Ignatius and the Princess III(杭电1028)(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- RabbitMQ inequivalent arg 'durable' for exchange 'csExchange' in vhost '/': received
错误:inequivalent arg 'durable' for exchange 'csExchange' in vhost '/': received 使用不同的MQ客户端时,常常会出现以上错误 ...
- 手动配置三大框架整合:Spring+Struts2+mybatis
如今主流的项目框架中,数据库持久层有可能不是hibernate,而是mybatis或者ibatis,事实上它们都是一样的,以下我来把环境搭建一下: [导入相关jar包]新建web项目projectms ...
- [PHP]怎样在SAE的CodeIgniter项目中隐藏掉index.php
第一步:改动项目根文件夹的config.yaml文件.加入例如以下内容: handle: - rewrite: if(!is_dir() && !is_file() && ...
- bzoj1801: [Ahoi2009]chess 中国象棋(DP)
1801: [Ahoi2009]chess 中国象棋 题目:传送门 题解: 表示自己的DP菜的抠脚 %题解... 定义f[i][j][k]表示前i行 仅有一个棋子的有j列 有两个棋子的有k个 的方案数 ...
- Tuples are immutable
A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in ...