#include "CCApplication.h"
#include "CCDirector.h"
/**
@brief This function change the PVRFrame show/hide setting in register.
@param bEnable If true show the PVRFrame window, otherwise hide.
*/
static void PVRFrameEnableControlWindow(bool bEnable);
//在注册表中写入对于PVRFrame的显示和隐藏的设置
NS_CC_BEGIN;
// sharedApplication pointer
CCApplication * CCApplication::sm_pSharedApplication = 0;
// CCApplication的静态成员指针变量, 单例对象指针
CCApplication::CCApplication()
: m_hInstance(NULL)
, m_hAccelTable(NULL)
{
m_hInstance = GetModuleHandle(NULL);//获取当前程序句柄
m_nAnimationInterval.QuadPart = 0; //初始化m_nAnimationInterval
CC_ASSERT(! sm_pSharedApplication); //断言程序中只有一个sm_pSharedApplication。确保当前类只有一个实例对象
sm_pSharedApplication = this; //设置单例对象指针指向当前单例程序类对象实例
}
CCApplication::~CCApplication()
{//断言程序:只有一个sm_pSharedApplication,就是指向当前类的实例对象的指针
CC_ASSERT(this == sm_pSharedApplication);
sm_pSharedApplication = NULL;
}
int CCApplication::run()
{ //设置注册表PVRFrame隐藏
PVRFrameEnableControlWindow(false);
//主消息循环
// Main message loop:
MSG msg;
LARGE_INTEGER nFreq;
LARGE_INTEGER nLast;
LARGE_INTEGER nNow;
QueryPerformanceFrequency(&nFreq);//WINDOWS高精度定时器的用法,先获取频率
QueryPerformanceCounter(&nLast);//获取当前的计数值,即频率x当前时间
// Initialize instance and cocos2d.初始化实例和Cocos2d
// initInstance函数调用成功之后调用applicationDidFinishLaunching函数完成一些初始化处理
// initInstance函数为虚函数,由派生类AppDelegate进行了重载。
if (! initInstance() || ! applicationDidFinishLaunching())
{
return 0;
}
//CCEGLView代表OpenGL显示窗口,封装了使用OpengGL作为显示底层API的一个基本的windows窗体的创建与控制。
//CCEGLView::sharedOpenGLView();取得当前使用的OPENGL窗口管理的单件实例对象
CCEGLView& mainWnd = CCEGLView::sharedOpenGLView();
//让窗口居中,正常显示
mainWnd.centerWindow();
ShowWindow(mainWnd.getHWnd(), SW_SHOW);
while (1)
{
if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))//如果没有获取到WINDOWS消息
{
// Get current time tick.
QueryPerformanceCounter(&nNow);//取得当前的计数值,即频率x当前时间
// If it's the time to draw next frame, draw it, else sleep a while.
if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
{//m_nAnimationInterval.QuadPart的值 为setAnimationInterval函数进行设置的固定值
nLast.QuadPart = nNow.QuadPart;//如果时间流逝达到了设定的FPS时间差,则更新计数值。
CCDirector::sharedDirector()->mainLoop();//这里是设备渲染场景的函数
}
else
{//sleep0秒的意义是让CPU做下时间片切换,防止死循环而使系统其它程序得不到响应。
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);
// TranslateMessage()处理Windows消息,该函数将虚拟键消息转换为字符消息。
// 字符消息被寄送到调用线程的消息队列里,当下一次线程调用函数GetMessage或PeekMessage时被读出。
// DispatchMessage()消息传递给操作系统,然后操作系统去调用我们的回调函数,也就是在窗体的过程函数中处理消息
}
}
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系统没有状态栏,所以返回的矩形各位置都是0
// 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;
}
//静态成员函数,获取当前系统的语言类型
ccLanguageType CCApplication::getCurrentLanguage()
{
ccLanguageType ret = kLanguageEnglish;
LCID localeID = GetUserDefaultLCID();
unsigned short primaryLanguageID = localeID & 0xFF;
switch (primaryLanguageID)
{
case LANG_CHINESE:
ret = kLanguageChinese;
break;
case LANG_FRENCH:
ret = kLanguageFrench;
break;
case LANG_ITALIAN:
ret = kLanguageItalian;
break;
case LANG_GERMAN:
ret = kLanguageGerman;
break;
case LANG_SPANISH:
ret = kLanguageSpanish;
break;
case LANG_RUSSIAN:
ret = kLanguageRussian;
break;
}
return ret;
}
NS_CC_END;
//
// Local function
//
static void PVRFrameEnableControlWindow(bool bEnable)
{
HKEY hKey = 0;
//在注册表中写入对于PVRFrame的显示和隐藏的设置
// Open PVRFrame control key, if not exist create it.
if(ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
L"Software\\Imagination Technologies\\PVRVFRame\\STARTUP\\",
0,
0,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
0,
&hKey,
NULL))
{
return;
}
const wchar_t * wszValue = L"hide_gui";
const wchar_t * wszNewData = (bEnable) ? L"NO" : L"YES";
wchar_t wszOldData[256] = {0};
DWORD dwSize = sizeof(wszOldData);
LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, NULL, (LPBYTE)wszOldData, &dwSize);
if (ERROR_FILE_NOT_FOUND == status // the key not exist
|| (ERROR_SUCCESS == status // or the hide_gui value is exist
&& 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal
{
dwSize = sizeof(wchar_t) * (wcslen(wszNewData) + 1);
RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize);
}
//关闭注册表
RegCloseKey(hKey);
}
转载于:https://my.oschina.net/ajian2014/blog/282615