解析Helloworld项目

在cocos2dx中的helloworld目录中

android 项目:

jni目录 Android.mk文件解析

LOCAL_PATH := $(call my-dir) //项目的根路径
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld //工程项目名字

LOCAL_SRC_FILES := main.cpp //包含c c++文件的名字

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../cocos2dx \ //包含路径
$(LOCAL_PATH)/../../../../cocos2dx/platform \
$(LOCAL_PATH)/../../../../cocos2dx/include \
$(LOCAL_PATH)/../../../Classes //类文件在class目录中

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)) \ // 连接的库文件
-lcocos2d -llog -lgame_logic

include $(BUILD_SHARED_LIBRARY)


main.cpp文件:
入口文件

#include "AppDelegate.h"     //主要头文件   此应用的委托文件
#include "cocos2d.h"
#include <jni.h>
#include <android/log.h>

#define LOG_TAG "main"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

using namespace cocos2d;

extern "C"
{

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
{
if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())
{
cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
view->setFrameWidthAndHeight(w, h);
// if you want to run in WVGA with HVGA resource, set it
// view->create(480, 320);
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);

AppDelegate *pAppDelegate = new AppDelegate();
cocos2d::CCApplication::sharedApplication().run();
}
else
{
cocos2d::CCTextureCache::reloadAllTextures();
cocos2d::CCDirector::sharedDirector()->setGLDefaultValues();
}
}

}



到classes目录中会看到c++的类文件

AppDelegate.h //函数变量的声明

#ifndef  _APP_DELEGATE_H_
#define _APP_DELEGATE_H_

#include "CCApplication.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 for initialize OpenGL instance, set source path, etc...
*/
virtual bool initInstance();

virtual bool applicationDidFinishLaunching(); //加载窗口完成

virtual void applicationDidEnterBackground();

virtual void applicationWillEnterForeground();
};

#endif // _APP_DELEGATE_H_


AppDelegate.cpp


#include "AppDelegate.h"

#include "cocos2d.h"
#include "HelloWorldScene.h" //HelloWorld场景文件

#include "CCEGLView.h" //CCEGLView.h

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate() {
}

//实例化 并且 判断当前是哪个平台
bool AppDelegate::initInstance() {
bool bRet = false;
do {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) //如果平台是CC WIN32

// Initialize OpenGLView instance, that release by CCDirector when application terminate.
// The HelloWorld is designed as HVGA.
CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd
|| ! pMainWnd->Create(TEXT("cocos2d: Hello JuneChiu"), 480, 320));

#endif // CC_PLATFORM_WIN32

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) //如果平台是CC ios

// OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here.

#endif // CC_PLATFORM_IOS

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //如果平台是CC android

// OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp
// the default setting is to create a fullscreen view
// if you want to use auto-scale, please enable view->create(320,480) in main.cpp
// if the resources under '/sdcard" or other writeable path, set it.
// warning: the audio source should in assets/
// cocos2d::CCFileUtils::setResourcePath("/sdcard");

#endif // CC_PLATFORM_ANDROID

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WOPHONE) //如果平台是CC WOPHONE

// Initialize OpenGLView instance, that release by CCDirector when application terminate.
// The HelloWorld is designed as HVGA.
CCEGLView* pMainWnd = new CCEGLView(this);
CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480, WM_WINDOW_ROTATE_MODE_CW));

#ifndef _TRANZDA_VM_
// on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file
cocos2d::CCFileUtils::setResource("HelloWorld.zip");
#endif

#endif // CC_PLATFORM_WOPHONE
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
// MaxAksenov said it's NOT a very elegant solution. I agree, haha
CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

// Initialize OpenGLView instance, that release by CCDirector when application terminate.
// The HelloWorld is designed as HVGA.
CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd
|| ! pMainWnd->Create("cocos2d: Hello World", 480, 320 ,480, 320));

CCFileUtils::setResourcePath("../Resource/");

#endif // CC_PLATFORM_LINUX

#if (CC_TARGET_PLATFORM == CC_PLATFORM_BADA)

CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(this, 480, 320));
pMainWnd->setDeviceOrientation(Osp::Ui::ORIENTATION_LANDSCAPE);
CCFileUtils::setResourcePath("/Res/");

#endif // CC_PLATFORM_BADA

#if (CC_TARGET_PLATFORM == CC_PLATFORM_QNX)
CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(480, 320));
CCFileUtils::setResourcePath("./app/native/Resource");
#endif // CC_PLATFORM_QNX
bRet = true;
} while (0);
return bRet;
}

//加载窗口
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector(); // 实例化导演

pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); //设置OpenGLView

// enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
// pDirector->enableRetinaDisplay(true);

// turn on display FPS
pDirector->setDisplayFPS(true); //设置是否显示帧率显示

// pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);

// 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()->pause(); //导演 暂停

// 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()->resume(); //导演 运行

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); //设置背景音乐
}



HelloWorldScene.h

#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 recommand to return the exactly class pointer
static cocos2d::CCScene* scene();

// a selector callback
virtual void menuCloseCallback(CCObject* pSender); //关闭菜单

// implement the "static node()" method manually
LAYER_NODE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__



HelloWorldScene.cpp


#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene() //实例化 HelloWorld场景
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node();

// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::node();

// 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;
}

/
// 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::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
//设置位置
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

//创建菜单
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::menuWithItems(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::labelWithString("Hello World", "Arial", 24);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize(); //用导演获取窗口的大小

// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 50) ); //设置标签的位置

// add the label as a child to this layer
this->addChild(pLabel, 1); //添加标签到布景

// add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png"); //创建一个精灵 用图片

// position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) ); //设置精灵的位置

// add the sprite as a child to this layer
this->addChild(pSprite, 0); //添加这个精灵

return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender) //关闭菜单的事件
{
CCDirector::sharedDirector()->end(); //调用导演的end方法

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值