1 COCOS2DX Source Analysis-Program start and main loop

Source: Internet
Author: User

1 StartIn the iOS system, the default call is initiated by the main function appcontrollermain.mnsautoreleasepool * pool = [[NSAutoreleasePool alloc] init];int retVal = Uiapplicationmain (argc, argv, nil, @ "AppController"); [Pool Release];return RetVal; 2 AppControlleriOS Viewcontroller are familiar with the iOS program's base controller. 1. Initialize ApplicationCocos2d::application *app = Cocos2d::application::getinstance ();
App->initglcontextattrs (); Cocos2d::glviewimpl::convertattrs (); 2. Initialize Openglview, use OpenGL under iOS must implement a Eagllayer view we call it eagview here, This view is the canvas that OpenGL eventually displays, where we initialized the work related to OpenGL. such as:1> initialization eaglcontext2> renderbuffer 3> FrameBuffer 4> associated renderbuffer with FrameBuffer 5> In this view there is a swapbuffer is actually [context Renderbuffer:gl_renderbuffer], this function is called in every rendering cycle, mainly in the director::d Rawscene.  Drawscene is implemented by calling the iOS self-updating function displaylinker:mainloop in the following run. 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 proxy mode, agent of the Cceaglview object we created before, so we can go to the use of unified Cocos Glview, and later this glview to the director. In this way, our director and Glview are all initialized. Important:setting The Glview should is done after creating the Rootviewcontroller
Cocos2d::glview *glview = Cocos2d::glviewimpl::createwitheaglview (Eaglview); Cocos2d::D irector::getinstance ()->setopenglview (Glview); 4. Main program main loop Mainloop ()The call here is Application->run.      This run ends with a call to the director's Mainloop, from where the control of the program is formally vested in the director's hand. 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::D irector* Director = cocos2d::D irector::getinstance ();
[Eaglcontext setcurrentcontext: [(cceaglview*) Director->getopenglview ()->geteaglview () context]];
Director->mainloop ();
}

1 COCOS2DX Source Analysis-Program start and main loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.