axmol/cocos2dx/platform/wophone/CCApplication_wophone.cpp

298 lines
7.7 KiB
C++
Raw Normal View History

// #define COCOS2D_DEBUG 1
#include "CCApplication_wophone.h"
#include "ssBackLightControl.h"
//#include "ssKeyLockControl.h"
#include "CCScheduler.h"
NS_CC_BEGIN;
// sharedApplication pointer
2011-03-07 18:20:48 +08:00
CCApplication * CCApplication::sm_pSharedApplication = 0;
2011-03-07 18:20:48 +08:00
static const Int32 CC_ON_APPLICATION_IDLE = (EVENT_FirstUser + EVENT_LastUser) / 2;
2011-01-20 10:49:30 +08:00
#ifdef _TRANZDA_VM_
static LARGE_INTEGER s_nAnimationInterval;
static LARGE_INTEGER s_nFreq;
static LARGE_INTEGER s_nLast;
#else
#include <sys/time.h>
static long long s_nAnimationInterval;
static long long s_nLast;
static long long getTimeOfDayMicroSecond()
{
timeval val;
gettimeofday(&val, NULL);
return (long long)val.tv_sec * 1000 * 1000 + val.tv_usec;
}
#endif
2011-03-07 18:20:48 +08:00
CCApplication::CCApplication()
: m_bRunning(FALSE)
, m_bNeedStop(FALSE)
, m_bInBackground(FALSE)
{
memset(&m_tMsg, 0, sizeof(m_tMsg));
SS_GetCurrentGTID(&m_tMsg.gtid);
2011-03-07 18:20:48 +08:00
m_tMsg.type = CC_ON_APPLICATION_IDLE;
2011-03-07 18:20:48 +08:00
Sys_RegisterMessageCallBack(CC_ON_APPLICATION_IDLE, CCApplication::_OnAppIdle, (UInt32)this);
memset(m_AppDataPath, 0, sizeof(char) * EOS_FILE_MAX_PATH);
do
{
TUChar AppID[EOS_FILE_MAX_PATH] = {0};
UInt32 nCmdType = 0;
Int32 nRet = SS_AppRequest_GetAppName(AppID, &nCmdType);
2011-03-07 18:20:48 +08:00
CC_BREAK_IF(nRet < 0);
TUChar AppPath[EOS_FILE_MAX_PATH] = {0};
char DataPath[EOS_FILE_MAX_PATH] = {0};
SS_GetApplicationPath(AppID, SS_APP_PATH_TYPE_CONST, AppPath);
TUString::StrUnicodeToStrUtf8((Char*) DataPath, AppPath);
#ifndef _TRANZDA_VM_
char *pszDriver = "";
#else
char *pszDriver = "D:/Work7";
#endif
// record the data path
strcpy(m_AppDataPath, pszDriver);
strcat(m_AppDataPath, DataPath);
} while (0);
2011-03-07 18:20:48 +08:00
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
2011-03-07 18:20:48 +08:00
CCApplication::~CCApplication()
{
2011-03-07 18:20:48 +08:00
Sys_RegisterMessageCallBack(CC_ON_APPLICATION_IDLE, NULL, NULL);
2011-03-07 18:20:48 +08:00
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
2011-03-07 18:20:48 +08:00
Boolean CCApplication::EventHandler(EventType* pEvent)
{
Boolean bHandled = FALSE;
switch(pEvent->eType)
{
case EVENT_AppLoad:
if (! initInstance() || ! applicationDidFinishLaunching())
{
CCScheduler::purgeSharedScheduler();
SendStopEvent();
}
2011-01-20 10:49:30 +08:00
#ifdef _TRANZDA_VM_
QueryPerformanceFrequency(&s_nFreq);
QueryPerformanceCounter(&s_nLast);
#else
s_nLast = getTimeOfDayMicroSecond();
#endif
bHandled = TRUE;
break;
case EVENT_AppStopNotify:
break;
case EVENT_AppActiveNotify:
if (pEvent->sParam1 == 0)
{
if (!m_bInBackground)
{
applicationDidEnterBackground();
m_bInBackground = true;
}
if (CCDirector::sharedDirector()->isPaused())
{
StopMainLoop();
}
// restore back light open mode
if (CfgGetBackLightStatus())
{
CfgTurnOnBackLightEx(SYS_BACK_LIGHT_MODE_TIME_LONG);
CCLOG("AppActiveNotify::TurnOnBackLight:MODE_TIME_LONG");
}
// EnableKeyLock();
// CCLOG("AppActiveNotify::InBackground");
}
else if (pEvent->sParam1 > 0)
{
if (m_bInBackground)
{
applicationWillEnterForeground();
m_bInBackground = false;
}
StartMainLoop();
// modify back light open mode
if (CfgGetBackLightStatus())
{
CfgTurnOnBackLightDelay(0x7fffffff);
CCLOG("AppActiveNotify::TurnOnBackLight:0x7fffffff");
}
// if KeyLock disactived, disable it.
// if (! CfgKeyLock_GetActive())
// {
// DisableKeyLock();
// CCLOG("AppActiveNotify::DisableKeyLock");
// }
}
break;
}
if (FALSE == bHandled)
{
return TApplication::EventHandler(pEvent);
}
return bHandled;
}
2011-03-07 18:20:48 +08:00
void CCApplication::setAnimationInterval(double interval)
{
#ifdef _TRANZDA_VM_
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
s_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
#else
s_nAnimationInterval = (long long)(interval * 1000 * 1000);
#endif
}
2011-03-07 18:20:48 +08:00
CCApplication::Orientation CCApplication::setOrientation(CCApplication::Orientation orientation)
{
return orientation;
}
2011-03-07 18:20:48 +08:00
void CCApplication::statusBarFrame(CCRect * rect)
{
}
2011-03-07 18:20:48 +08:00
const char* CCApplication::getAppDataPath()
{
return m_AppDataPath;
}
2011-03-07 18:20:48 +08:00
void CCApplication::switchNotify(int nTurnOn)
{
bool bInBack = isInBackground();
// set the auto close screen and auto key lock status
do
{
// if the app have be in background,don't handle this message
2011-03-07 18:20:48 +08:00
CC_BREAK_IF(bInBack);
if (! nTurnOn) // turn off screen
{
// CCDirector::sharedDirector()->pause();
applicationDidEnterBackground();
StopMainLoop();
// EnableKeyLock();
// CCLOG("BLswitchNotify::EnableKeyLock");
}
else
{
// modify back light open mode
CfgTurnOnBackLightDelay(0x7fffffff);
CCLOG("AppActiveNotify::TurnOnBackLight:0x7fffffff");
// // if KeyLock disactived, disable it.
// if (! CfgKeyLock_GetActive())
// {
// DisableKeyLock();
// CCLOG("BLswitchNotify::DisableKeyLock");
// }
//
// CCDirector::sharedDirector()->resume();
applicationWillEnterForeground();
StartMainLoop();
}
} while (0);
}
2011-03-07 18:20:48 +08:00
bool CCApplication::isInBackground()
{
return m_bInBackground;
}
2011-03-07 18:20:48 +08:00
void CCApplication::StartMainLoop()
{
if (m_bRunning)
{
m_bNeedStop = FALSE;
return;
}
Sys_PostMessage2(MESSAGE_PRIOR_LOWEST, &m_tMsg);
m_bRunning = TRUE;
}
2011-03-07 18:20:48 +08:00
void CCApplication::StopMainLoop()
{
m_bNeedStop = TRUE;
}
2011-03-07 18:20:48 +08:00
Int32 CCApplication::_OnAppIdle(MESSAGE_t * pMsg, UInt32 uData)
{
2011-03-07 18:20:48 +08:00
CCApplication& rThis = (CCApplication&) CCApplication::sharedApplication();
CCEGLView * pView = CCDirector::sharedDirector()->getOpenGLView();
if (pView && rThis.m_bRunning)
{
if (rThis.m_bNeedStop)
{
rThis.m_bNeedStop = FALSE;
rThis.m_bRunning = FALSE;
}
else
{
2011-01-20 10:49:30 +08:00
#ifdef _TRANZDA_VM_
LARGE_INTEGER nNow;
QueryPerformanceCounter(&nNow);
if (nNow.QuadPart - s_nLast.QuadPart >= s_nAnimationInterval.QuadPart)
{
pView->UpdateWindow(0);
s_nLast.QuadPart = nNow.QuadPart;
}
#else
long long nNow = getTimeOfDayMicroSecond();
if (nNow - s_nLast >= s_nAnimationInterval)
{
pView->UpdateWindow(0);
s_nLast = nNow;
}
#endif
else
{
Sys_Sleep(0);
}
Sys_PostMessage2(MESSAGE_PRIOR_LOWEST, &rThis.m_tMsg);
}
}
return 1;
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
2011-03-07 18:20:48 +08:00
CCApplication& CCApplication::sharedApplication()
{
2011-03-07 18:20:48 +08:00
CC_ASSERT(sm_pSharedApplication);
return *sm_pSharedApplication;
}
NS_CC_END;