views:

86

answers:

2

I have an iPhone application that is mostly using standard controls. There is one view that is rendered in OpenGL (fancy graphs). In the interest of saving myself a lot of work, I looked around and found Cocos2d. It looks like it has exactly what I need (OpenGL ES, relatively simple to work with), but I've run into a problem.

I don't know how to limit Cocos2d to exist in just a single view. Before, I implemented my OpenGL stuff in a view and used a simple ViewController to present it within a navigation controller. Here, Cocos2d seems to need to do a lot more, and I'm unsure how to stuff it into the same ViewController/View approach I did with raw OpenGL.

UPDATE: This is what I was doing. Flip to the next 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, how I'm trying to present it (in a button click):

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

This is the new approach, same results though.

ApplicationDidFinish portion 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's view to the window and display.
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

CocosController I'm using to present 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 is from a button click in a details ViewController I have that is supposed to make this whole thing appear:

CocosController *cc = [[CocosController alloc]init];
cc.mainView = self.view;
[[self navigationController] pushViewController:cc animated:false];
+2  A: 

What you are doing seems overkill. CC_DIRECTOR_INIT(); is used to initialize a view and a CCDirector, and is recommended where you'll be attaching cocos2d to the main view.

If you take a look at the sample provided in Cocos2d, called AttachTest (see the file attachDemo.m) you can see how to attach a CCDirector to any view. Here's the code the sample uses:

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

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

In this snippet, a new EAGLView is created, and then is set as the openGLView of the CCDirector.

My suggestion is that you create your view controller and and empty view as usual (maybe using Interface Builder, or the code above), and then, in the loadView method attach the CCDirector to that view.

pgb
Working on this right now; looks promising.
Dusda
Okay, I've implemented what the AttachTest demo was doing, but when I call "[director setOpenGLView:glview];" (after creating the EAGLView and adding that as a subview), it crashes. Specifically, line 75 of CCLabelAtlas (the variable texStepX is null). This is the same behavior I was getting before, which leads me to think I'm just not initializing something properly.
Dusda
Correction: texStepX is infinite.
Dusda
I've updated the question with my current implementation.
Dusda
A: 
float row = (a % itemsPerRow) * texStepX;

The above line of code was where the app was crashing every time I tried another way to get the view running. I finally just tried to look it up, thinking I wouldn't find anything. Turns out, cocos2d makes an assumption that an image, fps_images.png, is assumed to be in the project. If it's not, this particular bit of cocos2d explodes with an arithmetic exception.

Took two seconds to fix by just finding fps_images.png in the cocos2d directory and adding it to the Resources folder of my project.

sigh

Dusda