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 ...
随机推荐
- 解决无法启动“start web server”:
1.提示1080端口被占用: Cmd——Netstat -ano——找到端口号为1080的pid——打开任务管理器——干掉pid 2.inter error:your request was unsu ...
- tf.slice()解释
转载:https://www.jianshu.com/p/71e6ef6c121b def slice(input_, begin, size, name=None): 其中“input_”是你输入的 ...
- Maven项目的坐标GroupId和ArtifactId
GroupId和ArtifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. GroupId一 ...
- 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...
- deeplink技术的两篇资料
两篇资料如下: https://zhuanlan.zhihu.com/p/20694818?refer=ouyangchen http://www.sohu.com/a/122694049_49134 ...
- Java设置Client Socket链接Server超时时间
Java设置Client Socket链接Server超时时间 学习了:http://blog.csdn.net/tterminator/article/details/52494141 http:/ ...
- Unity3D的场景单位 和 3D建模软件的单位 之间的关系
转载自 : http://www.ceeger.com/Unity/Doc/2011/3D_to_Unity.html Date:2011-08-24 03:52 Unity的系统单位为米,其他3D软 ...
- 高速掌握Lua 5.3 —— I/O库 (1)
Q:什么是"Simple Model"? A:全部的文件操作都基于一个默认的输入文件和一个默认的输出文件.这就意味着同一时间对于输入和输出来说,仅仅可操作一个文件(默认的文件). ...
- zzulioj--1746--三角形面积(几何水题)
1746: 三角形面积 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 100 Solved: 31 SubmitStatusWeb Board De ...
- zzulioj--1832--贪吃的松鼠(位运算好题)
1832: 贪吃的松鼠 Time Limit: 3 Sec Memory Limit: 2 MB Submit: 43 Solved: 7 SubmitStatusWeb Board Descri ...