2011-02-17 14:31:52 +08:00
|
|
|
#include "Application.h"
|
2010-12-04 18:16:09 +08:00
|
|
|
#include "ssBackLightControl.h"
|
|
|
|
#include "ssKeyLockControl.h"
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
using namespace cocos2d;
|
2010-08-03 11:23:12 +08:00
|
|
|
|
2011-02-21 16:14:13 +08:00
|
|
|
bool Application::initInstance()
|
|
|
|
{
|
|
|
|
CCXEGLView* pMainWnd = new CCXEGLView(this);
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CCX_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480));
|
|
|
|
|
|
|
|
#ifndef _TRANZDA_VM_
|
|
|
|
// on uphone emulator, we copy resources files to Work7/TG3/APP/ folder instead of zip file
|
|
|
|
cocos2d::CCFileUtils::setResource("HelloWorld.zip");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
static const Int32 CCX_ON_APPLICATION_IDLE = (EVENT_FirstUser + EVENT_LastUser) / 2;
|
2010-08-03 11:23:12 +08:00
|
|
|
|
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-02-17 14:31:52 +08:00
|
|
|
Application::Application()
|
2010-12-02 18:44:13 +08:00
|
|
|
: m_bRunning(FALSE)
|
|
|
|
, m_bNeedStop(FALSE)
|
2011-01-20 13:41:58 +08:00
|
|
|
, m_bInBackground(FALSE)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
|
|
|
memset(&m_tMsg, 0, sizeof(m_tMsg));
|
|
|
|
SS_GetCurrentGTID(&m_tMsg.gtid);
|
|
|
|
m_tMsg.type = CCX_ON_APPLICATION_IDLE;
|
2010-08-03 11:23:12 +08:00
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
Sys_RegisterMessageCallBack(CCX_ON_APPLICATION_IDLE, Application::_OnAppIdle, (UInt32)this);
|
2011-01-18 10:14:44 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
CCX_BREAK_IF(nRet < 0);
|
|
|
|
|
|
|
|
TUChar AppPath[EOS_FILE_MAX_PATH] = {0};
|
|
|
|
SS_GetApplicationPath(AppID, SS_APP_PATH_TYPE_EXECUTABLE, AppPath);
|
|
|
|
TUString::StrUnicodeToStrUtf8((Char*) m_AppDataPath, AppPath);
|
|
|
|
} while (0);
|
2010-12-02 18:44:13 +08:00
|
|
|
}
|
2010-08-03 11:23:12 +08:00
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
Application::~Application()
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
|
|
|
Sys_RegisterMessageCallBack(CCX_ON_APPLICATION_IDLE, NULL, NULL);
|
|
|
|
}
|
2010-08-03 11:23:12 +08:00
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
Boolean Application::EventHandler(EventType* pEvent)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
Boolean bHandled = FALSE;
|
|
|
|
|
|
|
|
switch(pEvent->eType)
|
2010-08-03 11:23:12 +08:00
|
|
|
{
|
2010-12-02 18:44:13 +08:00
|
|
|
case EVENT_AppLoad:
|
2011-02-17 14:31:52 +08:00
|
|
|
setSharedApplication(*this);
|
|
|
|
if (! m_Delegate.applicationDidFinishLaunching())
|
2010-08-03 11:23:12 +08:00
|
|
|
{
|
2010-12-02 18:44:13 +08:00
|
|
|
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
|
2010-12-02 18:44:13 +08:00
|
|
|
bHandled = TRUE;
|
|
|
|
break;
|
2010-08-03 11:23:12 +08:00
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
case EVENT_AppStopNotify:
|
|
|
|
break;
|
2011-02-17 14:31:52 +08:00
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
case EVENT_AppActiveNotify:
|
|
|
|
if (pEvent->sParam1 == 0)
|
|
|
|
{
|
2011-01-20 13:41:58 +08:00
|
|
|
if (!m_bInBackground)
|
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
m_Delegate.applicationDidEnterBackground();
|
2011-01-20 13:41:58 +08:00
|
|
|
m_bInBackground = true;
|
|
|
|
}
|
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
if (CCDirector::sharedDirector()->isPaused())
|
2010-09-25 11:54:33 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
StopMainLoop();
|
2010-09-25 11:54:33 +08:00
|
|
|
}
|
2010-12-09 11:41:32 +08:00
|
|
|
CfgTurnOnBackLight();
|
2010-12-04 18:16:09 +08:00
|
|
|
EnableKeyLock();
|
2010-08-03 11:23:12 +08:00
|
|
|
}
|
2010-12-02 18:44:13 +08:00
|
|
|
else if (pEvent->sParam1 > 0)
|
2010-08-03 11:23:12 +08:00
|
|
|
{
|
2011-01-20 13:41:58 +08:00
|
|
|
if (m_bInBackground)
|
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
m_Delegate.applicationWillEnterForeground();
|
2011-01-20 13:41:58 +08:00
|
|
|
m_bInBackground = false;
|
|
|
|
}
|
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
StartMainLoop();
|
2011-02-17 14:31:52 +08:00
|
|
|
|
2010-12-09 11:41:32 +08:00
|
|
|
CfgTurnOnBackLightDelay(0x7fffffff);
|
2010-12-04 18:16:09 +08:00
|
|
|
// if KeyLock disactived, disable it.
|
|
|
|
if (! CfgKeyLock_GetActive())
|
|
|
|
{
|
|
|
|
DisableKeyLock();
|
|
|
|
}
|
2010-08-03 11:23:12 +08:00
|
|
|
}
|
2010-12-02 18:44:13 +08:00
|
|
|
break;
|
2010-08-03 11:23:12 +08:00
|
|
|
}
|
2011-02-17 14:31:52 +08:00
|
|
|
if (FALSE == bHandled)
|
2010-08-05 10:29:29 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
return TApplication::EventHandler(pEvent);
|
2010-08-05 10:29:29 +08:00
|
|
|
}
|
2011-02-17 14:31:52 +08:00
|
|
|
|
2010-12-02 18:44:13 +08:00
|
|
|
return bHandled;
|
|
|
|
}
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
void Application::setAnimationInterval(double interval)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
#ifdef _TRANZDA_VM_
|
|
|
|
LARGE_INTEGER nFreq;
|
|
|
|
QueryPerformanceFrequency(&nFreq);
|
|
|
|
s_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
|
|
|
|
#else
|
|
|
|
s_nAnimationInterval = (long long)(interval * 1000 * 1000);
|
|
|
|
#endif
|
2010-12-02 18:44:13 +08:00
|
|
|
}
|
2010-08-10 12:03:05 +08:00
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
Application::Orientation Application::setOrientation(Application::Orientation orientation)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
return orientation;
|
2010-12-02 18:44:13 +08:00
|
|
|
}
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
void Application::statusBarFrame(CGRect * rect)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Application::getAppDataPath()
|
|
|
|
{
|
|
|
|
return m_AppDataPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::switchNotify(int nTurnOn)
|
|
|
|
{
|
|
|
|
bool bInBack = isInBackground();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// if the app have be in background,don't handle this message
|
|
|
|
CCX_BREAK_IF(bInBack);
|
|
|
|
|
|
|
|
if (! nTurnOn) // turn off screen
|
|
|
|
{
|
|
|
|
// CCDirector::sharedDirector()->pause();
|
|
|
|
m_Delegate.applicationDidEnterBackground();
|
|
|
|
StopMainLoop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// CCDirector::sharedDirector()->resume();
|
|
|
|
m_Delegate.applicationWillEnterForeground();
|
|
|
|
StartMainLoop();
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Application::isInBackground()
|
|
|
|
{
|
|
|
|
return m_bInBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::StartMainLoop()
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
|
|
|
if (m_bRunning)
|
2010-08-10 12:03:05 +08:00
|
|
|
{
|
2010-12-02 18:44:13 +08:00
|
|
|
m_bNeedStop = FALSE;
|
|
|
|
return;
|
2010-08-10 12:03:05 +08:00
|
|
|
}
|
2010-12-02 18:44:13 +08:00
|
|
|
Sys_PostMessage2(MESSAGE_PRIOR_LOWEST, &m_tMsg);
|
|
|
|
m_bRunning = TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
void Application::StopMainLoop()
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
|
|
|
m_bNeedStop = TRUE;
|
|
|
|
}
|
2010-08-10 12:03:05 +08:00
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
Int32 Application::_OnAppIdle(MESSAGE_t * pMsg, UInt32 uData)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
Application& rThis = (Application&) Application::sharedApplication();
|
|
|
|
CCXEGLView * pView = CCDirector::sharedDirector()->getOpenGLView();
|
|
|
|
if (pView && rThis.m_bRunning)
|
2010-08-05 10:29:29 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
if (rThis.m_bNeedStop)
|
2010-12-02 18:44:13 +08:00
|
|
|
{
|
2011-02-17 14:31:52 +08:00
|
|
|
rThis.m_bNeedStop = FALSE;
|
|
|
|
rThis.m_bRunning = FALSE;
|
2010-12-02 18:44:13 +08:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2011-02-17 14:31:52 +08:00
|
|
|
Sys_PostMessage2(MESSAGE_PRIOR_LOWEST, &rThis.m_tMsg);
|
2010-12-02 18:44:13 +08:00
|
|
|
}
|
2010-08-05 10:29:29 +08:00
|
|
|
}
|
2010-12-02 18:44:13 +08:00
|
|
|
return 1;
|
|
|
|
}
|