Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
Update: I added [view prepareOpenGL]; after initializing the view. Without this, transparencies don't work. –  Hasyimi Bahrudin Aug 29 '12 at 1:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.