본문 바로가기

게임 생활, 관심/Tool, 게임엔진

cocos2d-x 학습 진행 중...

CoCos2d-x를 학습하려합니다.


이것 참...

첨에 유니티3D로 갈려고했는데


유니티3D에 비해서 아래의 장점이 있지만

완전 무료 : 추후엔 어떻게 될지모르지만...

빠른 실행속도 (아마도 2D게임일 경우가 그럴듯...)

그래도

유니티3D에 비해서 어려운 프로그래밍.

에디터기능 좀 부족.

3D게임구현 좀 부족?


참고로 주 개발언어: C++ , 자바스크립트 등


하지만, 개인적으론 C# 프로그래머인지라 아무래도 유니티3D를 해야하나? 하는 생각도 계속드네요...

이것때문에 다시 한번 C++ 을 봐야할듯... (사실 C++도 하다만 경우인지라...)


일단 설치, 프로젝트 생성은 건너뛰고 (다른 사이트에서 더 설명을 잘해놓은지라...)

프로젝트 생성후 만들어지는 HelloWorldScene 소스부터 간략하게 보는것부터 하겠습니다.


참고로 저는 Cocos2d-x 3.x (아마도 3.7버전??) 버전입니다. http://www.cocos2d-x.org/download

그리고 또한 제가 현재 참조중인 서적은 Cocos2d-x 3 모바일 게임프로그래밍; 에이콘출판사 입니다.



<HelloWorldScene.h>


#ifndef __HELLOWORLD_SCENE_H__     //헤더파일이 중복참조되는것을 막기위해서

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::Layer        //화면 구성하는 클래스

{

public:

    // there's no 'id' in cpp, so we recommend returning the class instance pointer

    static cocos2d::Scene* createScene();    //Scene을 생성하고 반환. 다른화면에서 해당화면으로 전환시 등에 사용.


    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone

    virtual bool init();    //Layer를 상속받은 클래스가 생성될때 생성자 다음으로 가장 먼저 호출되는 메소드

    

    // a selector callback

    void menuCloseCallback(cocos2d::Ref* pSender); // 프로그램 종료위한 함수정의

    

    // implement the "static create()" method manually

    CREATE_FUNC(HelloWorld);    //코코스2d-x에서 제공하는 create메소드를 사용할수있게해주는 전역메소드 ???

};


#endif // __HELLOWORLD_SCENE_H__


-> 필요한 함수들을 Header파일에 정의. 이후 cpp파일에 구현해놓는 일반적인 구조


<HelloWorldScene.cpp>


#include "HelloWorldScene.h"


Scene* HelloWorld::createScene()

{

    // 'scene' is an autorelease object

    auto scene = Scene::create();    

   //보통 int, float 등 자료형을 쓰겠지만 auto라는 신규자료형을 많이 쓰는듯. C++문법도 바뀌거나 업그레이드 되는듯

    

    // 'layer' is an autorelease object

    auto layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !Layer::init() )

    {

        return false;

    }

    

    Size visibleSize = Director::getInstance()->getVisibleSize();

    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    /////////////////////////////

    // 2. add a menu item with "X" image, which is clicked to quit the program

    //    you may modify it.


    // add a "close" icon to exit the progress. it's an autorelease object

    auto closeItem = MenuItemImage::create(

                                           "CloseNormal.png",

                                           "CloseSelected.png",

                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,

                                origin.y + closeItem->getContentSize().height/2));


    // create menu, it's an autorelease object

    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Vec2::ZERO);

    this->addChild(menu, 1);


    /////////////////////////////

    // 3. add your codes below...


    // add a label shows "Hello World"

    // create and initialize a label

    

    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

    

    // position the label on the center of the screen

    label->setPosition(Vec2(origin.x + visibleSize.width/2,

                            origin.y + visibleSize.height - label->getContentSize().height));


    // add the label as a child to this layer

    this->addChild(label, 1);


    // add "HelloWorld" splash screen"

    auto sprite = Sprite::create("HelloWorld.png");


    // position the sprite on the center of the screen

    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));


    // add the sprite as a child to this layer

    this->addChild(sprite, 0);

    

    return true;

}


void HelloWorld::menuCloseCallback(Ref* pSender)

{

    Director::getInstance()->end();


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}


사실 많이 알지도 못하고, 게임을 만들고싶어서 무작정덤빈지라..

위에서보면 Layer와 Scene이란 부분이 좀 헷갈리는데,

차츰 공부하면서 이해할려 합니다.