📄 asl_utils.h
字号:
//-----------------------------------------------------------------------------
//
// ____ Azure Star Game Engine 蓝星游戏引擎 ____
//
// Copyright (c) 2006, 蓝星工作室
// All rights reserved.
//
// 文件名称: asl_utils.h
// 摘 要: ASL库实用组件, 任何本库文件必须包含本文件
//
// 当前版本: 1.0
// 作 者: 汤 祺
// 创建日期: 2006-7-15
//
//-----------------------------------------------------------------------------
#ifndef ASL_UTILITY_INCLUDE
#define ASL_UTILITY_INCLUDE
#pragma once
// 用户态下引用库
#ifndef ASL_LIB
#ifdef _DEBUG
#pragma comment(lib, "ASLD.lib")
#else
#pragma comment(lib, "ASL.lib")
#endif
#endif
// 消除VC不支持标准异常规范的警告
#pragma warning( disable : 4290 )
// 用STRICT方式编译所有文件
#ifndef STRICT
#define STRICT
#endif
// 包含标准头文件
#include <windows.h>
#include <stdio.h>
// 包含调试文件
#ifdef _DEBUG
#include <crtdbg.h>
#endif
//-----------------------------------------------------------------------------
namespace ASL
{
//-----------------------------------------------------------------------------
// 断言定义
//-----------------------------------------------------------------------------
#ifndef ASSERT
#ifdef _DEBUG
#define ASSERT(expr) _ASSERT(expr)
#else
#define ASSERT(expr)
#endif
#endif
//-----------------------------------------------------------------------------
// 智能delete, releas宏
//-----------------------------------------------------------------------------
// 删除一个对象
#define SAFE_DELETE(p) { if ((p) != NULL) { delete (p); (p) = NULL; } }
// 删除一个数组
#define SAFE_DELETE_ARRAY(p) { if ((p) != NULL) { delete [](p); (p) = NULL; } }
// 删除一个GDI对象
#define SAFE_DELETE_GDIOBJ(p) { if ((p) != NULL) { DeleteObject(p); (p) = NULL; } }
// 释放DirectX对象
#define SAFE_RELEASE(p) { if ((p) != NULL) { (p)->Release(); (p) = NULL; } }
//-----------------------------------------------------------------------------
// 全局实用函数
//-----------------------------------------------------------------------------
// 打开Debug版的内存泄漏检测(Release版无作用)
void EnableMemoryLeakCheck();
// 检测DirectX版本是否在8以上
bool CheckDXVersion8(void);
// 取应用程序路径
LPCSTR GetAppPath(void);
// 取应用程序目录下某子目录的绝对路径
LPCSTR GetAbsPath(LPCSTR szDir);
// 检测CPU是否支持MMX指令
bool IsMMX();
// 快速创建字体
HFONT CreateFontFast(LPCSTR face, int height, bool bold = false,
bool italic = false, bool underline = false);
// 将16位数扩展为64位(拷贝4份)
__int64 Bit16To64(WORD val);
//-----------------------------------------------------------------------------
// 日志记录工具, 定义DISABLE_LOG则无效
//-----------------------------------------------------------------------------
// 日志类仅供Log()函数使用
class ASLLog
{
friend void Log(LPCSTR format, ...);
private:
static ASLLog& Instance(void) { static ASLLog instance; return instance; }
void WriteLog(LPCSTR msg) { fputs(msg, m_pFile); }
private:
ASLLog(void) { m_pFile = fopen("Log.txt", "wt"); }
~ASLLog(void) { fclose(m_pFile); }
private:
FILE *m_pFile;
};
// 记录日志的全局函数
#ifndef DISABLE_LOG
inline void Log(LPCSTR format, ...)
{
va_list va;
char str[256];
// 处理格式化字符串
va_start(va, format);
vsprintf(str, format, va);
va_end(va);
ASLLog::Instance().WriteLog(str);
}
#else
inline void Log(LPCSTR format, ...) {}
#endif
//-----------------------------------------------------------------------------
// 异常类
//-----------------------------------------------------------------------------
// 异常基类
class ASLException
{
public:
virtual LPCSTR GetErrorMessage(void) const
{ static char szGeneric[] = "发生严重错误"; return szGeneric; }
};
// 简单异常
class ASLSimpleException : public ASLException
{
public:
ASLSimpleException(LPCSTR szMessage)
{ strcpy(m_szMessage, szMessage); }
virtual LPCSTR GetErrorMessage(void) const
{ return m_szMessage; }
private:
char m_szMessage[128];
};
// Ini异常
class ASLIniException : public ASLException
{
public:
ASLIniException(LPCSTR szFileName, LPCSTR szKey)
{ sprintf(m_szMessage, "Ini文件 %s 键值 %s 读取失败", szFileName, szKey); }
virtual LPCSTR GetErrorMessage(void) const
{ return m_szMessage; }
private:
char m_szMessage[128];
};
// 文件异常
class ASLFileException : public ASLException
{
public:
enum Cause
{
Generic,
OpenFailed,
WriteFailed,
BadFormat
};
ASLFileException(LPCSTR szFileName, Cause cause = ASLFileException::Generic)
{ strcpy(m_szFileName, szFileName); m_Cause = cause; }
virtual LPCSTR GetErrorMessage(void) const;
inline Cause GetCause(void) const { return m_Cause; }
private:
char m_szFileName[128];
Cause m_Cause;
};
// DirectX异常
class ASLDirectXException : public ASLException
{
public:
enum DirectType
{
DirectX,
DirectDraw,
DirectMusic,
DirectSound,
DirectInput
};
ASLDirectXException(LPCSTR szDesc, HRESULT hr,
DirectType dt = ASLDirectXException::DirectX)
{ strcpy(m_szDesc, szDesc); m_hr = hr; m_DirectType = dt; }
virtual LPCSTR GetErrorMessage(void) const;
inline DirectType GetDirectType(void) const { return m_DirectType; }
private:
DirectType m_DirectType;
char m_szDesc[128];
HRESULT m_hr;
};
// DirectDraw异常
class ASLDirectDrawException : public ASLDirectXException
{
public:
ASLDirectDrawException(LPCSTR szDesc, HRESULT hr)
: ASLDirectXException(szDesc, hr, ASLDirectXException::DirectDraw) {}
};
// DirectMusic异常
class ASLDirectMusicException : public ASLDirectXException
{
public:
ASLDirectMusicException(LPCSTR szDesc, HRESULT hr)
: ASLDirectXException(szDesc, hr, ASLDirectXException::DirectMusic) {}
};
// DirectSound异常
class ASLDirectSoundException : public ASLDirectXException
{
public:
ASLDirectSoundException(LPCSTR szDesc, HRESULT hr)
: ASLDirectXException(szDesc, hr, ASLDirectXException::DirectSound) {}
};
// DirectInput异常
class ASLDirectInputException : public ASLDirectXException
{
public:
ASLDirectInputException(LPCSTR szDesc, HRESULT hr)
: ASLDirectXException(szDesc, hr, ASLDirectXException::DirectInput) {}
};
} // namespace ASL
#endif // ASL_UTILITY_INCLUDE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -