[转]cocos2dx源码分析-程序启动与主循环

https://www.cnblogs.com/liangzhimy/p/4422007.html

1 cocos2dx源码分析-程序启动与主循环

 

 

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: 0 ];

    // 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 0; } -(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(); }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-x 中的富文本可以通过 RichText 类来实现。RichText 类提供了一种简单的方式来创建并渲染富文本。通过 RichText 可以添加不同的字体、颜色、大小和对齐方式等属性,同时还支持添加图片和超链接等元素。 以下是一个简单的使用 RichText 的示例: ``` auto label = cocos2d::ui::RichText::create(); label->ignoreContentAdaptWithSize(false); label->setContentSize(Size(200, 100)); // 添加文字 auto text = cocos2d::ui::RichElementText::create(1, Color3B::WHITE, 255, "Hello World", "Arial", 12); label->pushBackElement(text); // 添加图片 auto image = cocos2d::ui::RichElementImage::create(2, Color3B::WHITE, 255, "path/to/image.png"); label->pushBackElement(image); // 添加超链接 auto link = cocos2d::ui::RichElementText::create(3, Color3B::YELLOW, 255, "click me", "Arial", 12); link->setUnderline(true); link->setUrl("http://www.example.com"); label->pushBackElement(link); label->setPosition(Vec2(100, 100)); addChild(label); ``` 在上面的示例中,我们创建了一个 RichText 对象,并添加了一些不同类型的元素:文本、图片和超链接。可以通过设置 setContentSize() 方法来指定 RichText 的大小,并使用 pushBackElement() 方法添加元素。元素的类型可以通过创建不同类型的 RichElement 对象来实现。在示例中,我们创建了 RichElementText 、RichElementImage 和 RichElementText 对象,分别表示文本、图片和超链接。 在创建 RichElementText 和 RichElementImage 对象时,需要指定一些属性,例如字体、颜色、大小和路径等。对于超链接,可以使用 setUrl() 方法来指定链接地址,同时还可以设置下划线等样式。 最后,我们将 RichText 添加到场景中,并设置其位置。通过这种方式,就可以轻松地创建和渲染富文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值