cocos2d-xでゲーム開発を学ぼうとしているものです。
ubuntu 13.04でandroid向けゲームを開発しようとしています。
サンプルソースにmain関数が記述されていなくて困っています。
なぜかコンパイルできていて実行できています。
grep -r -n -i main
で検索しても見つかりません。
本を読んでいてもサンプルコードにコードを付け足すという方式をとっているので、
main関数をお目にかかれません。
なにか特殊な仕様があるのでしょうか。
よろしくおねがいします。
cocos2d-xのサンプルコードでmain関数が見当たらない。
Re: cocos2d-xのサンプルコードでmain関数が見当たらない。
Androidアプリのソースコードにmain関数はありません。
ライフサイクルに合わせてActivityに用意されたメソッドが呼び出される仕組みです。
「Activity ライフサイクル」のキーワードで検索するとたくさんの解説記事が見付かるはずです。
cocos2d-xには詳しくないですが、プラットフォームを限定しないフレームワークであればActivityを内包してしまっているかもしれません。
その場合は、ドキュメントにフレームワークのライフサイクルの記述があるはずです。
ライフサイクルに合わせてActivityに用意されたメソッドが呼び出される仕組みです。
「Activity ライフサイクル」のキーワードで検索するとたくさんの解説記事が見付かるはずです。
cocos2d-xには詳しくないですが、プラットフォームを限定しないフレームワークであればActivityを内包してしまっているかもしれません。
その場合は、ドキュメントにフレームワークのライフサイクルの記述があるはずです。
Re: cocos2d-xのサンプルコードでmain関数が見当たらない。
申し遅れてしまいましたが、
開発言語はC++です。
検索した結果、ある程度理解しました。
onCreateをgrep検索した結果
遠いディレクトリに一致したファイルがありましたが、
java言語だったんでサンプルコードとの関連がわかりませんでした。
onResumeなどを検索すると、バイナリとしての一致はほとんどだったので
たぶん内包されていると思います。
ドキュメントは英語であることと、
量があまりにも多く、一部点在しているので
本当に困っています。
必死に検索して見ましたが
日本語資料が少なくプログラムの流れを説明しているサイトが見つからなかったので
途方に暮れています。
そういえばソースコードを載せていなかったので、
載せます。
HelloWorldScene.cpp
HelloWorldScene.h
AppDelegate.cpp
AppDelegate.h
よろしくおねがいします。
開発言語はC++です。
検索した結果、ある程度理解しました。
onCreateをgrep検索した結果
遠いディレクトリに一致したファイルがありましたが、
java言語だったんでサンプルコードとの関連がわかりませんでした。
onResumeなどを検索すると、バイナリとしての一致はほとんどだったので
たぶん内包されていると思います。
ドキュメントは英語であることと、
量があまりにも多く、一部点在しているので
本当に困っています。
必死に検索して見ましたが
日本語資料が少なくプログラムの流れを説明しているサイトが見つからなかったので
途方に暮れています。
そういえばソースコードを載せていなかったので、
載せます。
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *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 ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->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
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate()
{
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
/**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
Re: cocos2d-xのサンプルコードでmain関数が見当たらない。
proj.linuxとかproj.androidとかプラットフォームごとに分かれているようですね。
当然というかproj.androidにmain関数はなくて、cocos2d-xに用意されたActivityを継承してメインアクティビティを作成しているようです。
コピペにしてもエントリポイントを自分で定義する必要があるってことですかね。
当然というかproj.androidにmain関数はなくて、cocos2d-xに用意されたActivityを継承してメインアクティビティを作成しているようです。
コピペにしてもエントリポイントを自分で定義する必要があるってことですかね。
Re: cocos2d-xのサンプルコードでmain関数が見当たらない。
返事が遅れてしまいました。
h2so5さん、 拡張子で限定して検索できるとは初めて知りました。
ありがとうございます。
ISLeさん、 どうやらそのようです。
CCLayerなどのクラスを継承して、
付け足していくという方式のようです。
main.cppは全くいじくらず、
AppDelegate.h,AppDelegate.cppなどのファイルは
必要に応じて一部編集し、
HelloWorldScene.cppなどのファイルを自由につけ足すようです。
とにかく付け足すのがモットーで
エントリー関数は気にする必要が無いと思います。
皆様のヒントで解決しました。
ありがとうございました。
h2so5さん、 拡張子で限定して検索できるとは初めて知りました。
ありがとうございます。
ISLeさん、 どうやらそのようです。
CCLayerなどのクラスを継承して、
付け足していくという方式のようです。
main.cppは全くいじくらず、
AppDelegate.h,AppDelegate.cppなどのファイルは
必要に応じて一部編集し、
HelloWorldScene.cppなどのファイルを自由につけ足すようです。
とにかく付け足すのがモットーで
エントリー関数は気にする必要が無いと思います。
皆様のヒントで解決しました。
ありがとうございました。