cocos2d-iphone源碼分析(2):Director

簡介

CCDirector負責創建和處理主窗口,和管理場景的的執行。同時負責:

  • 初始化OpenGL ES的context
  • 設置OpenGL像素格式(默認是RGB565)
  • 設置OpenGL緩衝深度(默認是0-bit)
  • 設置投影模式(默認是3D)

CCDirector一般作爲單件使用,標準用法是:[[CCDirector sharedDirector] methodName]。IOS下[CCDirector sharedDirector]返回的對象是CCDirectorDisplayLink。

CCDirector繼承自UIViewController。CDirector是真個引擎的核心,它控制整個運行過程。一般初始化代碼如下:

// Main Window
     window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

     // Director
     director_ = (CCDirectorIOS*)[CCDirector sharedDirector];
     [director_ setDisplayStats:NO];
     [director_ setAnimationInterval:1.0/60];

     // GL View
     CCGLView *__glView = [CCGLView viewWithFrame:[window_ bounds]
                                             pixelFormat:kEAGLColorFormatRGB565
                                             depthFormat:0 /* GL_DEPTH_COMPONENT24_OES */
                                     preserveBackbuffer:NO
                                               sharegroup:nil
                                           multiSampling:NO
                                        numberOfSamples:0
                                ];

     [director_ setView:__glView];
     [director_ setDelegate:self];
     director_.wantsFullScreenLayout = YES;

     // Retina Display ?
     [director_ enableRetinaDisplay:useRetinaDisplay_];

     // Navigation Controller
     navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
     navController_.navigationBarHidden = YES;

     // AddSubView doesn't work on iOS6
     [window_ addSubview:navController_.view];
//     [window_ setRootViewController:navController_];

     [window_ makeKeyAndVisible];

     // create the main scene
     CCScene *scene = [CCScene node];
     ....
     // and run it!
     [director_ pushScene: scene];

初始化流程:

 

1. director_ = (CCDirectorIOS*)[CCDirector sharedDirector];

[CCDirector sharedDirector]返回的對象是CCDirectorDisplayLink,創建CCScheduler(調度器)、CActionManager(動作管理器)、CCTouchDispatcher(觸摸事件處理器),並把創建的動作管理器加入調度器,然後調度器就在時間片離調用CActionManager相關方法。(相關文件:CCDirector.m, CCDirectorIOS.m)

2. [director_ setAnimationInterval:1.0/60];  設置FPS。

3. 初始化GLView,爲渲染準備一個視圖。

3.   [director_ setView:__glView];

[director_ setDelegate:self];

Director是繼承於UIViewController,設置視圖和代理者。

4.   CCScene *scene = [CCScene node];

創建主場景。

5   [director_ pushScene: scene];

把主場景推入場景堆棧,並執行。

任務

  • Memory Helper:使用purgeCacheData方法,可以自動清除所有cocos2d緩存的數據
  • Scene OpenGL Helper:可以設置OpenGL的Alpha混合和深度檢測(setAlphaBlending、setDethTest)
  • Director Integration with a UIKit view:
  • Director Scene Landscape:場景佈局
  • Director Scene Management:管理場景(runWithscene, pushScene, popScene, replaceScene, end, pause, resume, stopAnimation,startAnimation,drawScene)

屬性

  • runningThread:cocos2d線程
  • runningScene:當前正在運行的場景,cocos2d一次只能運行一個場景
  • animationInterval:FPS
  • displayStats:控制是否顯示一些統計信息
  • isPaused:控制Director是否暫停
  • isAnimating:控制Director是否運行
  • projection:設置OpenGL的投影
  • totalFrames:從Director開始運行,執行的幀數
  • secondsPerFrame:每一幀用時
  • deltegate:實現CDirectorDelegate協議的代理對象
  • scheduler:調度器
  • actionManager:動作管理器
  • touchDispatcher:用戶觸摸操作的處理器

查看更多相關代碼:test/DirectorTest.m

2013/01/24  14:44 於上海

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章