Using Cocos2d in a single iPhone window

I have an iPhone app that mainly uses standard controls. There is one view that appears in OpenGL (fancy graphics). In the interest of saving a lot of work, I looked around and found Cocos2d. It looks like it has exactly what I need (OpenGL ES, relatively easy to use), but I ran into a problem.

I do not know how to limit Cocos2d to existing in only one view. I used to implement my OpenGL stuff in a presentation and used a simple ViewController to present it in a navigation controller. Here Cocos2d seems to need to do a lot more, and I'm not sure how to do it in the same ViewController / View approach that I did with raw OpenGL.

UPDATE: This is what I did. Flip to the next in bold to see what I'm doing now.

#import <UIKit/UIKit.h>


@interface CocosController : UIViewController {
    UIWindow *window;
}

@property (nonatomic,retain) UIWindow *window;

@end

And my CocosController.m:

#import "CocosController.h"
#import "SomeScene.h"
#import "cocos2d.h"

@implementation CocosController

@synthesize window;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    // Initialization code
    CC_DIRECTOR_INIT();
    CCDirector *director = [CCDirector sharedDirector];

    //landscape
    [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
    [director setDisplayFPS:true];

    //turn on multi-touch
    EAGLView *cocosView = [director openGLView];
    [cocosView setMultipleTouchEnabled:true];

    self.view = cocosView;

    //default texture formats...
    [CCTexture2D  setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

    [[CCDirector sharedDirector] runWithScene:[SomeScene scene]];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [[CCDirector sharedDirector] purgeCachedData];
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [[CCDirector sharedDirector] release];
    [window release];
    [super dealloc];
}


@end

And finally, as I try to present it (at the click of a button):

CocosController *cocosController = [[CocosController alloc] init];
[[self navigationController] pushViewController:cocosController animated:true];

This is a new approach, but nonetheless.

ApplicationDidFinish part of my AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
        [CCDirector setDirectorType:kCCDirectorTypeThreadMainLoop];

    CCDirector *director = [CCDirector sharedDirector];
    [director setDisplayFPS:YES];

    state = kStateEnd;

    // Add the navigation controller view to the window and display.
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

CocosController, which I use to represent the view:

#import "CocosController.h"
#import "cocos2d.h"
#import "SomeScene.h"

@implementation CocosController

@synthesize mainView;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
    [mainView addSubview:glview];

    CCDirector *director = [CCDirector sharedDirector];
    [director setOpenGLView:glview];

    CCScene *scene = [CCScene node];
    id node = [SomeScene node];
    [scene addChild: node];

    [director runWithScene:scene];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

And this bit from the click button in the ViewController detail that I have that should do all of this:

CocosController *cc = [[CocosController alloc]init];
cc.mainView = self.view;
[[self navigationController] pushViewController:cc animated:false];
+3
source share
2 answers
float row = (a % itemsPerRow) * texStepX;

, , . , , , , . , cocos2d , , fps_images.png, . , cocos2d .

, , fps_images.png cocos2d "" .

+1

, , . CC_DIRECTOR_INIT(); CCDirector, , cocos2d .

, Cocos2d, AttachTest (. attachDemo.m), , CCDirector . , :

EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
[mainView addSubview:glview];

CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];

EAGLView, openGLView CCDirector.

, (, Interface Builder ), loadView CCDirector .

+3

Source: https://habr.com/ru/post/1752334/