Game Coding Complete 精选内容

初始化检测:

1、检查是否已经运行:

bool IsOnlyInstance(LPCTSTR gameTitle)
{
	HANDLE handle = CreateMutex(NULL, TRUE, gameTitle);
	if (GetLastError()!=ERROR_SUCCESS)
	{
		//Retrieves a handle to the top-level window whose 
		//class name and window name match the specified strings. 
		//This function does not search child windows. This function 
		//does not perform a case-sensitive search.
		//The class name or a class atom created by a previous call 
		//to the RegisterClass or RegisterClassEx function.
		HWND hWnd = FindWindow(gameTitle, NULL);
		if (hWnd)
		{
			ShowWindow(hWnd, SW_SHOWNORMAL);
			SetFocus(hWnd);
			//Brings the thread that created the specified window into the foreground and activates the window.
			SetForegroundWindow(hWnd);
			SetActiveWindow(hWnd);
			return false;
		}
	}
	return true;
}
相关MSDN: createMutexgetlasterrorfindwindowshwowindowsetfocussetforegroundwindowsetactivewindow

2、检查磁盘空间

bool CheckStorage(const DWORDLONG diskSpaceNeeded)
{
	int const drive = _getdrive();
	struct _diskfree_t diskfree;
	_getdiskfree(drive, &diskfree);
	unsigned __int64 const neededClusters = 
		diskSpaceNeeded / (diskfree.sectors_per_cluster * diskfree.bytes_per_sector);
	if (diskfree.avail_clusters < neededClusters)
	{
		KE_ERROR("CheckStorage failure: not enough disk space");
		return false;
	}
	return true;
}
MSDN: _getdrive_getdiskfree

备注:这里得到的是可用簇,需要进行计算。

3、检查物理内存和虚拟内存

bool CheckMemory(const DWORDLONG physicalRAMNeeded, const DWORDLONG virtualRAMNeeded)
{
	MEMORYSTATUSEX status;
	GlobalMemoryStatusEx(&status);

	if (status.ullTotalPhys < physicalRAMNeeded)
	{
		KE_ERROR("CheckMemory failure: not enough physical memory");
		return false;
	}

	if (status.ullAvailVirtual < virtualRAMNeeded)
	{
		KE_ERROR("CheckMemory failure: not enough virtual memory");
		return false;
	}
	char *buff = KE_NEW char[(unsigned int)virtualRAMNeeded];
	if (buff)
	{
		delete[] buff;
	}else{
		KE_ERROR("CheckMemory failure: not enough continugous available memory.");
		return false;
	}
	return true;
}
GlobalMemoryStatusEx

4、获取CPU主频

DWORD ReadCPUSpeed()
{
	DWORD BufSize = sizeof(DWORD);
	DWORD dwMHz = 0;
	DWORD type = REG_DWORD;
	HKEY hKey;

	// open the key where the proc speed is hidden:
	long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
        0, KEY_READ, &hKey);
    
	if(lError == ERROR_SUCCESS)
	{
		// query the key:
		RegQueryValueEx(hKey, L"~MHz", NULL, &type, (LPBYTE) &dwMHz, &BufSize);
	}
	return dwMHz;
}
RegOpenKeyExRegQueryValueEx


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值