axmol/HelloWorld.android/jni/helloworld/HelloWorld.cpp

88 lines
2.0 KiB
C++
Raw Normal View History

2010-11-04 17:51:28 +08:00
#include "HelloWorld.h"
#include <jni.h>
#include <android/log.h>
using namespace cocos2d;
#define IMG_PATH "res/drawable/helloworld.png"
#define RESOURCE_PATH "/data/app/org.cocos2dx.application-1.apk"
class MyLayer : public CCLayer
{
public:
bool init()
{
if( !CCLayer::init())
{
return false;
}
this->setIsTouchEnabled(true);
return true;
};
void ccTouchesEnded(NSSet* pTouches, UIEvent* pEvent)
{
2010-11-19 14:57:41 +08:00
CCDirector::sharedDirector()->end();
2010-11-04 17:51:28 +08:00
};
LAYER_NODE_FUNC(MyLayer);
};
2010-11-19 14:57:41 +08:00
bool HelloWorld::applicationDidFinishLaunching()
2010-11-04 17:51:28 +08:00
{
// the view is freeed in CCDirector
2010-11-19 14:57:41 +08:00
CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
CCDirector::sharedDirector()->setDisplayFPS(true);
2010-11-04 17:51:28 +08:00
// set resource path
CCFileUtils::setResourcePath(RESOURCE_PATH);
// load background image texture and get window size
CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage(IMG_PATH);
2010-11-19 14:57:41 +08:00
CGSize size = CCDirector::sharedDirector()->getWinSize();
2010-11-04 17:51:28 +08:00
// create sprite instance
CCSprite * pSprite = new CCSprite();
pSprite->initWithTexture(pTexture);
pSprite->setPosition(CGPoint(size.width / 2, size.height / 2));
// create layer instance
CCLayer * pLayer = MyLayer::node();
pLayer->addChild(pSprite);
// add layer to scene
CCScene * pScene = CCScene::node();
pScene->addChild(pLayer);
// add scene to director
2010-11-19 14:57:41 +08:00
CCDirector::sharedDirector()->runWithScene(pScene);
2010-11-04 17:51:28 +08:00
pSprite->release();
return true;
}
static HelloWorld *s_pApp;
extern "C"
{
void initProgram()
{
s_pApp = new HelloWorld();
2010-11-19 14:57:41 +08:00
s_pApp->Run();
2010-11-04 17:51:28 +08:00
}
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
{
cocos2d::CCXEGLView *view = new cocos2d::CCXEGLView();
view->setFrameWitdAndHeight(w, h);
2010-11-19 14:57:41 +08:00
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
2010-11-04 17:51:28 +08:00
initProgram();
}
}