⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instdefines.h

📁 跨平台2D引擎
💻 H
字号:
//instDefines.h
#ifndef INST_DEFINES_H
#define INST_DEFINES_H


#ifdef NULL
#undef NULL
#endif
#define NULL 0

#ifdef null
#undef null
#endif
#define null 0

#ifdef Null
#undef Null
#endif
#define Null 0


#ifdef SAFEDEL
#undef SAFEDEL
#endif
#define SAFEDEL(x) if(x) delete x

#ifdef safedel
#undef safedel
#endif
#define safedel(x) if(x) delete x

#ifdef SafeDel
#undef SafeDel
#endif
#define SafeDel(x) if(x) delete x


#ifdef SAFEDELARRAY
#undef SAFEDELARRAY	
#endif
#define SAFEDELARRAY(x) if(x) delete[] x

#ifdef safedelarray
#undef safedelarray
#endif
#define safedelarray(x) if(x) delete[] x

#ifdef SafeDelArray
#undef SafeDelArray
#endif
#define SafeDelArray(x) if(x) delete[] x


#ifdef SAFERELEASE
#undef SAFERELEASE
#endif
#define SAFERELEASE(x) if(x) x->Release()

#ifdef saferelease
#undef saferelease
#endif
#define saferelease(x) if(x) x->Release()

#ifdef SafeRelease
#undef SafeRelease
#endif
#define SafeRelease(x) if(x) x->Release()


#ifdef MIN
#undef MIN
#endif
#define MIN(x,y)	((x)<(y)?(x):(y))

#ifdef min
#undef min
#endif
#define min(x,y)	((x)<(y)?(x):(y))

#ifdef Min
#undef Min
#endif
#define Min(x,y)	((x)<(y)?(x):(y))


#ifdef MAX
#undef MAX
#endif
#define MAX(x,y)	((x)>(y)?(x):(y))

#ifdef max
#undef max
#endif
#define max(x,y)	((x)>(y)?(x):(y))

#ifdef Max
#undef Max
#endif
#define Max(x,y)	((x)>(y)?(x):(y))


#ifdef GET_SET
#undef GET_SET
#endif

#define GET_SET(type,x)\
private: type m_##x;\
public: type Get##x()const{return m_##x;}\
public: void Set##x(type val){m_##x=val;}


#ifdef IS_SET
#undef IS_SET
#endif

#define IS_SET(x)\
private: Bool m_b##x;\
public: Bool Is##x()const{return m_b##x;}\
public: void Set##x(Bool val){m_b##x=val;}


#ifdef GET_SET_FUNC
#undef GET_SET_FUNC
#endif

#define GET_SET_FUNC(type,x,m)\
type Get##x()const{return (m);}\
void Set##x(type val){(val)=m;}


#ifdef IS_SET_FUNC
#undef IS_SET_FUNC
#endif

#define IS_SET_FUNC(x,m)\
Bool Is##x()const{return (m);}\
void Set##x(Bool val){(val)=m;}



#ifdef VIRTUAL_GET_SET
#undef VIRTUAL_GET_SET
#endif

#define VIRTUAL_GET_SET(type,x)\
private: type m_##x;\
public: virtual type Get##x()const{return m_##x;}\
public: virtual void Set##x(type val){m_##x=val;}


#ifdef VIRTUAL_IS_SET
#undef VIRTUAL_IS_SET
#endif

#define VIRTUAL_IS_SET(x)\
private: Bool m_b##x;\
public: virtual Bool Is##x()const{return m_b##x;}\
public: virtual void Set##x(Bool val){m_b##x=val;}


#ifdef VIRTUAL_GET_SET_FUNC
#undef VIRTUAL_GET_SET_FUNC
#endif

#define VIRTUAL_GET_SET_FUNC(type,x,m)\
virtual type Get##x()const{return (m);}\
virtual void Set##x(type val){(val)=m;}


#ifdef VIRTUAL_IS_SET_FUNC
#undef VIRTUAL_IS_SET_FUNC
#endif

#define VIRTUAL_IS_SET_FUNC(x,m)\
virtual Bool Is##x()const=0;\
virtual void Set##x(Bool m)=0;


#ifdef ABSTRACT_GET_SET
#undef ABSTRACT_GET_SET_FUNC
#endif

#define ABSTRACT_GET_SET_FUNC(type,x)\
virtual type Get##x()const=0;\
virtual void Set##x(type val)=0


#ifdef ABSTRACT_IS_SET_FUNC
#undef ABSTRACT_IS_SET_FUNC
#endif

#define ABSTRACT_IS_SET_FUNC(x)\
virtual Bool Is##x()const=0;\
virtual void Set##x(Bool val)=0


#ifndef COUT
#define COUT(x) cout<<#x" : "<<(x)
#endif

#ifndef COUTLN
#define COUTLN(x) COUT(x)<<"\n"
#endif


template <class dest, class src> struct _UNIONCAST
{
	union
	{
		dest d;
		src s;
	};
};

template <class dest, class src> dest union_cast(src s)
{
	_UNIONCAST<dest,src> tmp;
	tmp.s=s;
	return tmp.d;
}


#include <instTypes.h>
#include <instStr.h>

namespace inst
{


class __obj{};

typedef Int32 POS;
typedef UInt32 TIME;

typedef UInt32 COLOR;

__forceinline COLOR ARgb(UByte a,UByte r,UByte g,UByte b)
{
	return a<<24 | r<<16 | g<<8 | b;
}

__forceinline COLOR Rgb(UByte r,UByte g,UByte b)
{
	return ARgb(0xff,r,g,b);
}

__forceinline UByte GetA(COLOR color){ return (UByte)( (color&0xff000000) >> 24 ); }
__forceinline UByte GetR(COLOR color){ return (UByte)( (color&0x00ff0000) >> 16 ); }
__forceinline UByte GetG(COLOR color){ return (UByte)( (color&0x0000ff00) >> 8 ); }
__forceinline UByte GetB(COLOR color){ return (UByte)( (color&0x000000ff) ); }

typedef struct _FLOATCOLOR
{
    Float r;
    Float g;
    Float b;
    Float a;
	_FLOATCOLOR()
	{
		r=0.0f;
		g=0.0f;
		b=0.0f;
		a=0.0f;
	}
	_FLOATCOLOR(const _FLOATCOLOR &fc)
	{
		r=fc.r;
		g=fc.g;
		b=fc.b;
		a=fc.a;
	}
	_FLOATCOLOR(Float fr,Float fg,Float fb,Float fa=1.0f)
	{
		r=fr;
		g=fg;
		b=fb;
		a=fa;
	}
	_FLOATCOLOR(COLOR color)
	{
		r=(Float)GetR(color)/255.0f;
		g=(Float)GetG(color)/255.0f;
		b=(Float)GetB(color)/255.0f;
		a=(Float)GetA(color)/255.0f;
	}
	_FLOATCOLOR(UByte r,UByte g,UByte b,UByte a=0xff)
	{
		_FLOATCOLOR::r=(Float)r/255.0f;
		_FLOATCOLOR::g=(Float)g/255.0f;
		_FLOATCOLOR::b=(Float)b/255.0f;
		_FLOATCOLOR::a=(Float)a/255.0f;
	}
}FLOATCOLOR;


// 开始 - 包装器类型 (不需要继承IDynamic)

template <class T> class Optional
{
private:
	Bool m_bInited;
	T m_Val;
	static Bool CompareMemory(void *a,void *b,UInt32 size)
	{
		for(UInt32 i=0;i<size;i++)
			if(((UByte *)a)[i]!=((UByte *)b)[i])return False;
		return True;
	}
public:
	Optional(){ m_bInited=False; }
	void Invalidate(){ m_bInited=False; }
	Bool IsInited()const{ return m_bInited; }
	const T &GetValue()const{ return m_Val; }
	const T &operator =(const T & val){ m_bInited=True; m_Val=val; return m_Val; }
	Bool operator ==(const T & val)const{ return m_bInited & CompareMemory((void *)&m_Val,(void *)&val,sizeof(T)); }
	Bool operator !=(const T & val)const{ return (!m_bInited) || (!CompareMemory((void *)&m_Val,(void *)&val,sizeof(T))); }
};

template <class T> class AutoPtrBase
{
protected:
	AutoPtrBase(){} //无法创建本类对象
	~AutoPtrBase(){} // 既然无法创建本类对象,也就不必要virtual析构函数,以保证性能

	T *m_p;
//	virtual void SafeDestroy()=0; // 仅仅为了子类重用代码而利用多态,会造成浪费!
public:
	T *GetPtr(){ return m_p; }

	T *SetPtr(T *p){ m_p=p; }

//	T *operator =(T *p){ this->SafeDestroy(m_p); return SetPtr(p); } // 仅仅为了子类重用代码而利用多态,会造成浪费!

	T *operator ->()const{ return GetPtr(); }

	Bool operator !(){ return null==m_p; }

	Bool operator ==(const T *p){ return m_p==p; }
	Bool operator !=(const T *p){ return m_p!=p; }

};

template <class T> class AutoPtr:public AutoPtrBase<T>
{
public:
	AutoPtr(){ m_p=null; }
	AutoPtr(T *p){ m_p=p; }
	~AutoPtr(){ SafeDel(m_p); }

	T *operator =(T *p){ SafeDel(m_p); return SetPtr(p); }
};

template <class T> class AutoArrayPtr:public AutoPtrBase<T>
{
public:
	AutoArrayPtr(){ m_p=null; }
	AutoArrayPtr(T *p){ m_p=p; }
	~AutoArrayPtr(){ SafeDelArray(m_p); }

	T *operator =(T *p){ SafeDelArray(m_p); return SetPtr(p); }
};

template <class T> class AutoComPtr:public AutoPtrBase<T>
{
public:
	AutoComPtr(){ m_p=null; }
	AutoComPtr(T *p){ m_p=p; }
	~AutoComPtr(){ SafeRelease(m_p); }

	T *operator =(T *p){ SafeRelease(m_p); return SetPtr(p); }
};

// 结束 - 包装器类型


class IDynamic // 除了CEvent,CStr,包装器,数据结构,所有类和接口必须虚拟继承IDynamic
{
public:
	virtual ~IDynamic(){}
	virtual void *GetAddr()const=0;
	virtual CStr GetClass()const=0;
	// 使用GetAddr(),可以绝对正确地比较对象的实际首地址
	// 但如果是相同接口指针的比较,完全没有必要这么做
	// 在不同接口指针作比较的时候可以用之
	// ☆注意☆:实体类指针和接口指针相比较,必须用GetAddr()!
	// 此外,GetAddr()等于是正确向下转型为实际动态类型
	// 然后,可以正确地随便向上转型!
	// 例: (BaseXX) (MyClass *) (p->GetAddr())
	// 如果不能确定实际动态类型,可以利用GetClass()取得实际类名
	virtual Bool IsA(const CStr &name)const{ return name==L"inst::IDynamic"; }
};


}// end of namespace inst


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -