怎样在UIKit中使用cocos2d-x

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

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

如果你想把UIKit折腾进Cocos2d-xGithub有一个开源工程,推荐给大家学习一下: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添加:

 
 
  1. #import "AppDelegate.h"
  2.  
  3. static AppDelegate s_sharedApplication;

12. 继续把YourViewController.m扩展名改为YourViewController.mm.
13. 修改一个文件:

在cocos2d-x的CCdirectorCaller.mm文件中

将代码

 
 
  1. +(void) destroy {
  2. [[s_sharedDirectorCaller release];
  3. }

修改为:

 
 
  1. +(void) destroy {
  2. [s_sharedDirectorCaller destroy];
  3. [s_sharedDirectorCaller release];
  4.  
  5. s_sharedDirectorCaller=nil;
  6. }
  7. -(void) destroy {
  8. [displayLink invalidate];
  9.  
  10. displayLink=nil;
  11.  
  12. }

说明:在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就整合进controllers view了(类似modal view controller):

 
 
  1. -(void)loadOpenglScene:(CGRect)rect {
  2. cocos2d::CCApplication::sharedApplication().run();
  3.  EAGLView *glView = [EAGLView viewWithFrame: rect
  4. pixelFormat: kEAGLColorFormatRGBA8
  5. depthFormat: GL_DEPTH_COMPONENT16_OES
  6. preserveBackbuffer: NO
  7. sharegroup:nil
  8. multiSampling:NO
  9. numberOfSamples:0];
  10.  [self.view insertSubview:glView atIndex:0];
  11. cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector();
  12. pDirector->setOpenGLView(&cocos2d::CCEGLView::sharedOpenGLView());
  13.  // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
  14.  // pDirector->enableRetinaDisplay(true);
  15.  // turn on display FPS
  16.  //pDirector->setDisplayFPS(true);
  17.  // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
  18.  // set FPS. the default value is 1.0/60 if you don't call this
  19. pDirector->setAnimationInterval(1.0 / 60);
  20.  
  21.  // create a scene. it's an autorelease object
  22. cocos2d::CCScene *pScene_ = cocos2d::MyGraphicsEngine::scene(myparams);
  23.  // run
  24. pDirector->runWithScene(pScene_);
  25.  
  26.  // Retained because if not, we get an BAD ACCESS exception when we try to release the Cocos2d-x environment later
  27.  [[EAGLView sharedEGLView] retain];
  28. }
  29.  
  30. - (void)viewDidLoad
  31. {
  32.  [super viewDidLoad];
  33.  // Do any additional setup after loading the view.
  34.  
  35.  
  36.  // In this example we use a navigation bar and a "close" button, the Cocos2d-x view will be added below the navigation bar
  37.  UINavigationBar *bar=[[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)]autorelease];
  38.  [self.view insertSubview:bar atIndex:0];
  39.  UIBarButtonItem *closeButton=[[[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleDonetarget:self action:@selector(close)] autorelease];
  40.  UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@""];
  41. item.rightBarButtonItem = closeButton;
  42. item.hidesBackButton = YES;
  43.  [bar pushNavigationItem:item animated:NO];
  44.  [item release];
  45.  [self loadOpenglScene:CGRectMake(0, bar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-bar.frame.size.height)];
  46. }
  47.  
  48. -(void)close {
  49.  //TODO
  50. }
  51.  
  52. -(void)viewDidDisappear:(BOOL)animated {
  53.  //Release Cocos2d-x stack
  54. cocos2d::CCDirector *pDirector = cocos2d::CCDirector::sharedDirector();
  55.  //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.
  56. pDirector->end();
  57.  [self checkWin];
  58. }
  59.  
  60. - (void)viewDidUnload {
  61.  [[EAGLView sharedEGLView] release];
  62.  [super viewDidUnload];
  63.  // Release any retained subviews of the main view.
  64. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值