axmol/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp

321 lines
10 KiB
C++
Raw Normal View History

2013-01-25 10:29:39 +08:00
#include "AppDelegate.h"
#include "PlayerStatus.h"
2013-03-20 14:27:46 +08:00
#include "MainSceneHelper.h"
2013-01-25 10:29:39 +08:00
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "ScriptingCore.h"
#include "generated/jsb_cocos2dx_auto.hpp"
2013-03-20 14:27:46 +08:00
#include "generated/jsb_cocos2dx_extension_auto.hpp"
#include "jsb_cocos2dx_extension_manual.h"
2013-01-25 10:29:39 +08:00
#include "cocos2d_specifics.hpp"
#include "js_bindings_chipmunk_registration.h"
#include "js_bindings_ccbreader.h"
2013-03-20 14:27:46 +08:00
#include "js_bindings_system_registration.h"
#include "jsb_opengl_registration.h"
2013-01-25 10:29:39 +08:00
char *_js_log_buf_ccbuilder = NULL;
static bool firstTime = true;
2013-01-25 10:29:39 +08:00
USING_NS_CC;
using namespace CocosDenshion;
2013-04-18 08:24:59 +08:00
CCScene *mainScene;
2013-01-25 10:29:39 +08:00
2013-04-18 08:24:59 +08:00
AppDelegate::AppDelegate(): isRetina(false), isIPhone(false)
2013-01-25 10:29:39 +08:00
{
}
AppDelegate::~AppDelegate()
{
CCScriptEngineManager::sharedManager()->purgeSharedManager();
}
2013-04-18 08:24:59 +08:00
bool runMainScene() {
/* Push the new scene with a fancy transition. */
ccColor3B transitionColor;
transitionColor.r = 0;
transitionColor.g = 0;
transitionColor.b = 0;
mainScene = PlayerStatus::loadMainScene("StatusLayer.ccbi");
if(CCDirector::sharedDirector()->getRunningScene() != NULL && CCDirector::sharedDirector()->getRunningScene() != mainScene) {
2013-04-18 08:24:59 +08:00
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(0.5f, mainScene, transitionColor));
} else if(CCDirector::sharedDirector()->getRunningScene() != NULL) {
}else {
2013-04-18 08:24:59 +08:00
CCDirector::sharedDirector()->runWithScene(CCTransitionFade::create(0.5f, mainScene, transitionColor));
}
return true;
}
2013-01-25 10:29:39 +08:00
void handle_ccb_run() {
2013-01-25 21:52:35 +08:00
CCFileUtils::sharedFileUtils()->purgeCachedEntries();
CCFileUtils::sharedFileUtils()->loadFilenameLookupDictionaryFromFile("fileLookup.plist");
2013-01-25 21:52:35 +08:00
ScriptingCore::getInstance()->runScript("main.js");
2013-01-25 10:29:39 +08:00
}
void handle_connected() {
CCBHelper::setStatusMessage("Connected!");
}
void resetCocosApp();
void initViews(CCSize designSize) {
(CCScriptEngineManager::sharedManager())->removeScriptEngine();
resetCocosApp();
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
pDirector->setProjection(kCCDirectorProjection2D);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
}
static void setViewValues(CCEGLView *pView, CCSize frameSize, CCSize designSize) {
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// (CCScriptEngineManager::sharedManager())->removeScriptEngine();
// resetCocosApp();
//#endif
//
pView->setFrameSize(frameSize.height, frameSize.width);
pView->setDesignResolutionSize(designSize.height, designSize.width, kResolutionNoBorder);
CCLOG("Design Size %f x %f", designSize.height, designSize.width);
cocos2d::CCDirector::sharedDirector()->setProjection(kCCDirectorProjection2D);
}
void handle_set_orient(bool isPortrait) {
cocos2d::CCEGLView* pView = CCEGLView::sharedOpenGLView();
if (pView != NULL)
{
CCSize frameSize = pView->getFrameSize();
CCSize designSize = pView->getDesignResolutionSize();
CCLOG("is Portrait %d", isPortrait);
if (((frameSize.width > frameSize.height && isPortrait))
|| (frameSize.width < frameSize.height && !isPortrait)) {
setViewValues(pView, frameSize, designSize);
}
}
}
2013-04-18 08:24:59 +08:00
void handle_set_message(const char* msg) {
CCBHelper::setInstructionsMessage(msg);
}
void handle_set_status(const char* msg) {
CCBHelper::setStatusMessage(msg);
}
2013-01-25 10:29:39 +08:00
void handle_disconnected() {
CCBHelper::setStatusMessage("Disconnected");
}
void handle_ccb_stop() {
2013-04-18 08:24:59 +08:00
runMainScene();
2013-01-25 10:29:39 +08:00
}
void sendLogMsg(const char *msg);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
void cocos2d::CCLog(const char * pszFormat, ...)
{
printf("Cocos2d: ");
char szBuf[kMaxLogLen+1] = {0};
va_list ap;
va_start(ap, pszFormat);
vsnprintf(szBuf, kMaxLogLen, pszFormat, ap);
va_end(ap);
printf("%s", szBuf);
printf("\n");
sendLogMsg(szBuf);
}
#endif
2013-01-25 10:29:39 +08:00
2013-04-18 08:24:59 +08:00
extern "C" {
2013-01-25 10:29:39 +08:00
2013-04-18 08:24:59 +08:00
const char * getCCBDirectoryPath();
2013-01-25 10:29:39 +08:00
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
pDirector->setProjection(kCCDirectorProjection2D);
2013-04-18 08:24:59 +08:00
2013-01-25 10:29:39 +08:00
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
2013-04-18 08:24:59 +08:00
2013-01-25 10:29:39 +08:00
CCSize designSize = CCSizeMake(320, 480);
CCSize resourceSize = CCSizeMake(320, 480);
2013-04-18 08:24:59 +08:00
std::vector<std::string> resDirOrders;
std::string res;
TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform();
if (platform == kTargetIphone || platform == kTargetIpad)
{
std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Published files iOS");
searchPaths.insert(searchPaths.begin(), getCCBDirectoryPath());
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
if (screenSize.height > 1024)
{
res = "iPad";
designSize = CCSizeMake(768, 1024);
2013-04-18 08:24:59 +08:00
resourceSize = CCSizeMake(1536, 2048);
resDirOrders.push_back("resources-ipadhd");
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = true;
}
else if (screenSize.height > 960)
{
res = "iPad";
designSize = CCSizeMake(768, 1024);
2013-04-18 08:24:59 +08:00
resourceSize = CCSizeMake(768, 1024);
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = false;
}
else if (screenSize.height > 480)
{
res = "iPhone";
resourceSize = CCSizeMake(640, 960);
resDirOrders.push_back("resources-iphonehd");
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = true;
}
else
{
res = "iPhone";
resourceSize = CCSizeMake(320, 480);
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = false;
}
}
else if (platform == kTargetAndroid || platform == kTargetWindows)
{
if (screenSize.height > 960)
{
res = "xlarge";
resourceSize = CCSizeMake(1280, 1920);
resDirOrders.push_back("resources-xlarge");
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 720)
{
res = "large";
resourceSize = CCSizeMake(640, 960);
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 480)
{
res = "medium";
resourceSize = CCSizeMake(480, 720);
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else
{
res = "small";
resourceSize = CCSizeMake(320, 480);
resDirOrders.push_back("resources-small");
}
}
CCFileUtils *pFileUtils = CCFileUtils::sharedFileUtils();
pFileUtils->setSearchResolutionsOrder(resDirOrders);
pDirector->setContentScaleFactor(resourceSize.width/designSize.width);
2013-01-25 10:29:39 +08:00
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
2013-04-18 08:24:59 +08:00
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
2013-02-06 18:04:40 +08:00
searchPaths.insert(searchPaths.begin(), pFileUtils->getWritablePath());
pFileUtils->setSearchPaths(searchPaths);
PlayerStatus::setDeviceResolution(res);
2013-01-25 10:29:39 +08:00
// 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);
2013-03-20 14:27:46 +08:00
2013-01-25 10:29:39 +08:00
ScriptingCore* sc = ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
2013-03-20 14:27:46 +08:00
sc->addRegisterCallback(register_all_cocos2dx_extension);
2013-01-25 10:29:39 +08:00
sc->addRegisterCallback(register_cocos2dx_js_extensions);
2013-03-20 14:27:46 +08:00
sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
2013-01-25 10:29:39 +08:00
sc->addRegisterCallback(register_CCBuilderReader);
2013-03-20 14:27:46 +08:00
sc->addRegisterCallback(jsb_register_system);
2013-01-25 10:29:39 +08:00
sc->addRegisterCallback(jsb_register_chipmunk);
sc->addRegisterCallback(JSB_register_opengl);
2013-01-25 10:29:39 +08:00
sc->start();
2013-03-20 14:27:46 +08:00
2013-01-25 10:29:39 +08:00
CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
if(firstTime) {
runMainScene();
firstTime = false;
} else {
handle_ccb_run();
}
2013-01-25 10:29:39 +08:00
}
void handle_signal(int signal) {
static int internal_state = 0;
ScriptingCore* sc = ScriptingCore::getInstance();
// should start everything back
CCDirector* director = CCDirector::sharedDirector();
if (director->getRunningScene()) {
director->popToRootScene();
} else {
CCPoolManager::sharedPoolManager()->finalize();
if (internal_state == 0) {
//sc->dumpRoot(NULL, 0, NULL);
sc->start();
internal_state = 1;
} else {
sc->runScript("hello.js");
internal_state = 0;
}
}
}
// 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();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
}