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 ...
随机推荐
- Dbf文件操作
package cn.com.szhtkj.util; import java.io.File; import java.io.IOException; import java.lang.reflec ...
- vue项目input的placeholder根据用户的选择改变
html部分 <el-input :placeholder="holder" v-model="searchKey"> <el-select ...
- VBA 中Dim含义
楼主是个初学者,在应用vba时遇到了dim方面的问题,查了很多资料后想把关于dim的这点儿知识简单整理出来 首先,从我遇到的问题作为切入点吧, (不得不承认我遇到的错误是很低级的) 具体的情境就不还原 ...
- React 中组件间通信的几种方式
在使用 React 的过程中,不可避免的需要组件间进行消息传递(通信),组件间通信大体有下面几种情况: 父组件向子组件通信 子组件向父组件通信 非嵌套组件间通信 跨级组件之间通信 1.父组件向子组件通 ...
- Axios 使用时遇到的问题
最近使用 vue 构建一个小项目,在使用 axios 发送 post 请求的时候,发现 axios 发送数据默认使用 json 格式,百度搜了下,更改 ContentType 不管用,最终问题原来是: ...
- SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)
上一篇SpringBoot实战(二)Restful风格API接口中写了一个控制器,获取了前端请求的参数,现在我们就参数的获取与校验做一个介绍: 一:获取参数 SpringBoot提供的获取参数注解包括 ...
- 【BZOJ 1588】 [HNOI2002]营业额统计
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每天的最小波动值指的是和之前所有天的差值的绝对值中的最小值. 用set.的lower_bound函数. 每次找和他差值最小的数字就好 ...
- Mysql学习总结(23)——MySQL统计函数和分组查询
1.使用count统计条数:select count(字段名...) from tablename; 2.使用avg计算字段的平均值:select avg(字段名) from tablename: 这 ...
- [MST] Attach Behavior to mobx-state-tree Models Using Actions
Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...
- 教你怎样做个有“钱”途的測试project师
百度百科说測试project师这一职业的待遇,薪酬上升空间很大.但測试project师也有自己的烦恼,比方在程序出错后,将问题反馈给程序猿,然后程序猿给的答复是:"oh,howisthatp ...