怎樣在 UIKit 中使用 Cocos2d-x

翻譯:Iven,校對:jesse,子龍山人

衆所周知,cocos2d-x是一款基於c++/Opengl 的強勁跨平臺引擎,通過一次編碼,你可以同時用於androidiOS等大多數主流平臺。這使得多平臺的開發更加方便。

雖然比起cocos2d-x的有名的兄弟-cocos2d-iphone-x還很年輕並缺少教程,但是由於團隊的不懈努力,使得-x逐漸完善並不斷被市場認可。

如果你想把UIKit折騰進Cocos2d-xGithub有一個開源工程,推薦給大家學習一下:open source project on GitHub .

相反,如果你想把coocs2d-x折騰進UIKit(UIVIewController),以下是詳細步驟:


0. 廢話少說,這裏是本文的示例代碼:Sample project Cocos2dxOnUikit.zip.
1.我們有一個UIKit工程,同時希望在某一個 view controller中顯示Cocos2d-x (OpenGL view)。
2. 下載Cocos2d-x代碼(哥說了句廢話,你知道該去哪下載的)

3. 解壓(廢話again
4. 創建工程,如果你不會,推薦看看泰然以往的教程。
5. 移除cocos2dx/platform/目錄下的CCImage.cpp 和 CCThread.cpp
6. 打開xcode的項目配置,點擊build Settings,在Library Search Paths” 參數添加$(SRCROOT)/cocos2dx/platform/third_party/ios/libraries/(爲了在cocos2dx中使用curl庫)

7. 接着,在“Headers Search Paths參數添加

$(SRCROOT)/cocos2dx and $(SDKROOT)/usr/include/libxml2 (爲了使用libxml 庫)
8. 在 “Other C++ Flags 參數添加-DCC_UNDER_IOS

9.從HelloWorld示例工程中拷貝AppDelegate.cpp 和 AppDelegate.h並添加到我們現在這個工程中。


像上面截圖那樣註釋掉applicationDidFinishLaunching()。這些代碼我們稍後會轉移到ViewController 中去。
譯者注:確保AppDelegate 在你的UIKit工程中沒有多次調用,不然你的項目會有兩個AppDelegate.h頭文件。
10. 修改你YourAppDelegate.m的擴展名爲:YourAppDelegate.mm(.mm將其中代碼變成Objective C++,這樣使其被編譯成用c++方式調用)
11. 在你的YourAppDelegate.mm添加:

#import "AppDelegate.h"

static AppDelegate s_sharedApplication;

12. 繼續把YourViewController.m擴展名改爲YourViewController.mm.
13. 修改一個文件:

在cocos2d-x的CCdirectorCaller.mm文件中

將代碼

+(void) destroy {
[[s_sharedDirectorCaller release];
}

修改爲:

+(void) destroy {
[s_sharedDirectorCaller destroy];
[s_sharedDirectorCaller release];

s_sharedDirectorCaller=nil;
}
-(void) destroy {
[displayLink invalidate];

displayLink=nil;

}

說明:在cocos2d-x中使用UIKit view,我們會常常使用到director 的create和end。當我們調用director->end()方法時,CCDirectorCaller 並沒有真正釋放(顯示連接displayLink 依然存在),以上方法可以修復這個問題。這就相當於我們爲了釋放CCDirectorCaller,我們不得不先讓顯示連接(displayLink )無效。
14.在cocos2dx/support/zip_support/ioapi.cpp中,修改fopen64接口名稱 爲fopen,fseeko64 修改爲fseeko,ftello64 修改爲ftello。

15. 添加以下庫:

OpenGLES.framework

libxml2.dylib

libz.1.2.5.dylib

QuartzCore.framework


16. 現在,只需最後一步,Cocos2d-x 的OpenGL view就整合進controllers view了(類似modal view controller):

-(void)loadOpenglScene:(CGRect)rect {
    cocos2d::CCApplication::sharedApplication().run();

    EAGLView *glView = [EAGLView viewWithFrame: rect
                                    pixelFormat: kEAGLColorFormatRGBA8
                                    depthFormat: GL_DEPTH_COMPONENT16_OES
                                    preserveBackbuffer: NO
                                    sharegroup:nil
                                    multiSampling:NO
                                    numberOfSamples:0];

    [self.view insertSubview:glView atIndex:0];

    cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector();

    pDirector->setOpenGLView(&cocos2d::CCEGLView::sharedOpenGLView());

    // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
    // pDirector->enableRetinaDisplay(true);

    // turn on display FPS
    //pDirector->setDisplayFPS(true);

    // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    cocos2d::CCScene *pScene_ = cocos2d::MyGraphicsEngine::scene(myparams);

    // run
    pDirector->runWithScene(pScene_);

    // Retained because if not, we get an BAD ACCESS exception when we try to release the Cocos2d-x environment later
    [[EAGLView sharedEGLView] retain];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // In this example we use a navigation bar and a "close" button, the Cocos2d-x view will be added below the navigation bar
    UINavigationBar *bar=[[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)] autorelease];
    [self.view insertSubview:bar atIndex:0];
    UIBarButtonItem *closeButton=[[[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDone target:self action:@selector(close)] autorelease];

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@""];
    item.rightBarButtonItem = closeButton;
    item.hidesBackButton = YES;
    [bar pushNavigationItem:item animated:NO];
    [item release];

    [self loadOpenglScene:CGRectMake(0, bar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-bar.frame.size.height)];

}

-(void)close {
    //TODO
}

-(void)viewDidDisappear:(BOOL)animated {
    //Release Cocos2d-x stack
    cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector();
    //CCDirector::end() is asynchronous. That's why you can't release [EAGLView sharedEGLView] here after (you'll get a BAD ACCESS exception), and that's why we put it un viewDidUnload.
    pDirector->end();

    [self checkWin];
}

- (void)viewDidUnload {
    [[EAGLView sharedEGLView] release];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

本文轉載自:http://article.ityran.com/archives/2028#more-2028
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章