1 cocos2dx源码分析-程序启动与主循环
2024-08-28 08:22:45
1 启动
在iOS系统中,由main函数启动默认调用了AppController
main.m
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
[pool release];
return retVal;
2 AppController
iOS的ViewController都熟悉吧,iOS程序的基础Controller 。
1. 初始化Application
cocos2d::Application *app = cocos2d::Application::getInstance();
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
2. 初始化OpenGLView, 在iOS下使用OpenGL必须实现一个EAGLLayer 的View 我们这里就叫它EAGView吧,
这个View是OpenGL 最终显示的画布, 在这里我们以初始化了与OpenGL相关的工作。
如:
1>初始化EAGLContext
2> RenderBuffer
3> FrameBuffer
4> 关联RenderBuffer 与FrameBuffer
5> 在这个View里有一个swapBuffer 其实就是 [context RenderBuffer:GL_RenderBuffer], 这个函数在每个渲染循环都要调用, 主要是在Director::drawScene 里调用。 drawScene 是在下面要说到的run 里调用了iOS自更新函数displayLinker:mainLoop来实现的。
// Override point for customization after application launch. // Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
depthFormat: cocos2d::GLViewImpl::_depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: ]; // Use RootViewController manage CCEAGLView
_viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
_viewController.wantsFullScreenLayout = YES;
_viewController.view = eaglView;
3. cocos:GLView 代理模式,代理了之前我们创建的CCEAGLView对象, 这样我们就可以转到使用统一的cocos的GLView了, 后面又把这个GLView给了director。 这样我们director及GLView就都算是初始化了。
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
4. 最主要的程序主循环mainLoop()
这里调用的是Application->run。 这个run最后用调用 到director的mainLoop, 从这里开始程序的控制权正式归入到director手中。
app->run(); int Application::run()
{
if (applicationDidFinishLaunching())
{
[[CCDirectorCaller sharedDirectorCaller] startMainLoop];
}
return ;
} -(void) startMainLoop
{
// Director::setAnimationInterval() is called, we should invalidate it first
[self stopMainLoop]; displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:selfselector:@selector(doCaller:)];
[displayLink setFrameInterval: self.interval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
} -(void) doCaller: (id) sender
{
cocos2d::Director* director = cocos2d::Director::getInstance();
[EAGLContext setCurrentContext: [(CCEAGLView*)director->getOpenGLView()->getEAGLView()context]];
director->mainLoop();
}
最新文章
- Python中remove,del和pop的区别
- NOI2005 聪聪和可可
- UIView 的autoresizingMask属性
- algo_预备
- RedHat Install
- R语言 多元线性回归分析
- javascript继承的写法
- PHP数组函数的分组归纳
- Yii2前后台分离
- TestNG+ExtentReports生成超漂亮的测试报告
- 05浏览器-02-操作DOM
- ehcachexml文件解释
- Win7硬盘的AHCI模式
- java操作elasticsearch实现批量添加数据(bulk)
- JQuery动画详解(四)
- C#反射实现
- Development Tools
- Maven 项目报告插件
- 第11课 enum、sizeof、typedef 分析
- EventBus的基本使用步骤
热门文章
- CentOS7.5安装Mysql5.7.22
- 【PAT】1002. A+B for Polynomials (25)
- 【LOJ】#2351. 「JOI 2017/2018 决赛」毒蛇越狱
- 关于<;c:if>;没有<;c:else>;解决方案-转载
- (17) go 协程管道
- 【LCA/tarjan】POJ1470-Closest Common Ancestors
- 零配置文件搭建SpringMvc
- web.xml2.3配置需要注意的顺序问题
- spoj4155 OTOCI LCT
- BSGS 模板