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 Windowwindow_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Directordirector_ = (CCDirectorIOS*)[CCDirector sharedDirector];[director_ setDisplayStats:NO];[director_ setAnimationInterval:1.0/60];// GL ViewCCGLView *__glView = [CCGLView viewWithFrame:[window_ bounds]pixelFormat:kEAGLColorFormatRGB565depthFormat:0 /* GL_DEPTH_COMPONENT24_OES */preserveBackbuffer:NOsharegroup:nilmultiSampling:NOnumberOfSamples:0];[director_ setView:__glView];[director_ setDelegate:self];director_.wantsFullScreenLayout = YES;// Retina Display ?[director_ enableRetinaDisplay:useRetinaDisplay_];// Navigation ControllernavController_ = [[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 sceneCCScene *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 于上海
轉載于:https://www.cnblogs.com/mywolrd/archive/2013/02/01/2889316.html
總結
以上是生活随笔為你收集整理的cocos2d-iphone源码分析(2):Director的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unit Testing in iOS
- 下一篇: 【SAS NOTES】if then和i