3
\$\begingroup\$

Is it possible to create a terminal application which uses cocos2d? I've tried to make one using cocos2d 2.x, but it requires a MacGLView to be initialized.

I need it so that I could program a terminal application that generates a screenshot given a TMX file and an optional preferred width or height parameter (for resizing). Then I can automate the generation of map previews for my game, instead of manually taking screenshots. It's not practical to load the actual TMX and resize it inside the game (what I'm currently doing), because each TMX file has 7 layers, my tile sheet is huge, and I have lots of levels.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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)

  1. Create a command line utility project with "Foundation" as its type.

  2. 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.

  3. 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)

  4. In target build settings, Add -ObjC and -lz to "Other Linker Flags".

  5. 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).

\$\endgroup\$
1
  • \$\begingroup\$ Update: I added [view prepareOpenGL]; after initializing the view. Without this, transparencies don't work. \$\endgroup\$ Commented Aug 29, 2012 at 1:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.