怎样在 UIKit 中使用 Cocos2d-x

如题所述

众所周知,cocos2d-x是一款基于c++/Opengl 的强劲跨平台引擎,通过一次编码,你可以同时用于android,iOS等大多数主流平台。这使得多平台的开发更加方便。

虽然比起cocos2d-x的有名的兄弟-cocos2d-iphone,-x还很年轻并缺少教程,但是由于团队的不懈努力,使得-x逐渐完善并不断被市场认可。

如果你想把UIKit折腾进Cocos2d-x,Github有一个开源工程,推荐给大家学习一下:open source project on GitHub .

相反,如果你想把coocs2d-x折腾进UIKit(UIVIewController),以下是详细步骤:
怎样在UIKit中使用cocos2d-x

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并添加到我们现在这个工程中。

怎样在UIKit中使用cocos2d-x
像上面截图那样注释掉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

怎样在UIKit中使用cocos2d-x
16. 现在,只需最后一步,Cocos2d-x 的OpenGL view就整合进controller’s 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:UIBarButtonItemStyleDonetarget: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.
}
温馨提示:答案为网友推荐,仅供参考
相似回答