After hours of doing weird stuff (like creating an app that hides the window and terminates itself upon load just to make it appear like a command line app), it turns out the solution is too simple. Create that CCGLView yourself. Duh.
Steps to create a skeleton cocos2d command-line application Xcode project:
(Note: I'm using cocos2d 2.x, so if you're using cocos2d 1.0.1 or something, make changes where necessary)
Create a command line utility project with "Foundation" as its type.
Copy the libs folder from a generated cocos2d Xcode project into the same folder as the .xcodeproj of this project. Then drag it into Xcode, and make sure it is added to the target.
In target build settings, add "libs/kazmath/include" (without quotes) to "Header Search Paths", and make sure the check box is ticked (so that search is recursive)
In target build settings, Add -ObjC and -lz to "Other Linker Flags".
In target build phases, under "Link Binary with Binaries", add these frameworks:
- AppKit.framework
- AudioToolBox.framework
- OpenAL.framework
- OpenGL.framework
- QuartzCore.framework
Here's the source code:
// main.m
#import <Foundation/Foundation.h>
#import "cocos2d.h"
int main(int argc, char *argv[])
{
@autoreleasepool
{
// Just create it with any rect, since it's not going to be displayed anyway
CCGLView *view = [[[CCGLView alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 800, 600))] autorelease];
// Update: this needs to be called, or transparencies don't work
[view prepareOpenGL];
[[CCDirector sharedDirector] setView:view];
// Do stuff here
}
return EXIT_SUCCESSFUL;
}
And, voila! But without any modifications, 3 lines of error will be output (program still runs though):
ImageIO: <ERROR> CGImageSourceCreateWithData data parameter is nil
ImageIO: <ERROR> CGImageSourceCreateWithData data parameter is nil
ImageIO: <ERROR> CGImageSourceCreateWithData data parameter is nil
This happens because the director attempts to create the FPS labels when setView: is called, but the FPS texture file is missing. Simply comment out the method call to createStatsLabel inside CCDirector's setView: to solve the problem (CCDirector.h, around line 319).