axmol/cocos2dx/platform/win32/CCApplication_win32.cpp

122 lines
3.0 KiB
C++

#include "CCApplication.h"
#include "CCDirector.h"
#include "CCEGLView.h"
NS_CC_BEGIN;
// sharedApplication pointer
CCApplication * CCApplication::sm_pSharedApplication = 0;
CCApplication::CCApplication()
: m_hInstance(NULL)
, m_hAccelTable(NULL)
{
m_hInstance = GetModuleHandle(NULL);
m_nAnimationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
CCApplication::~CCApplication()
{
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
int CCApplication::run()
{
// Main message loop:
MSG msg;
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nLast);
// Initialize instance and cocos2d.
if (! initInstance() || ! applicationDidFinishLaunching())
{
return 0;
}
CCEGLView& mainWnd = CCEGLView::sharedOpenGLView();
mainWnd.centerWindow();
ShowWindow(mainWnd.getHWnd(), SW_SHOW);
while (1)
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// Get current time tick.
QueryPerformanceCounter(&nNow);
// If it's the time to draw next frame, draw it, else sleep a while.
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart;
CCDirector::sharedDirector()->mainLoop();
}
else
{
Sleep(0);
}
continue;
}
if (WM_QUIT == msg.message)
{
// Quit message loop.
break;
}
// Deal with windows message.
if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
void CCApplication::setAnimationInterval(double interval)
{
LARGE_INTEGER nFreq;
QueryPerformanceFrequency(&nFreq);
m_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
}
CCApplication::Orientation CCApplication::setOrientation(Orientation orientation)
{
// swap width and height
CCEGLView * pView = CCDirector::sharedDirector()->getOpenGLView();
if (pView)
{
return (Orientation)pView->setDeviceOrientation(orientation);
}
return (Orientation)CCDirector::sharedDirector()->getDeviceOrientation();
}
void CCApplication::statusBarFrame(CCRect * rect)
{
if (rect)
{
// Windows doesn't have status bar.
*rect = CCRectMake(0, 0, 0, 0);
}
}
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
CCApplication& CCApplication::sharedApplication()
{
CC_ASSERT(sm_pSharedApplication);
return *sm_pSharedApplication;
}
NS_CC_END;