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

📄 guidefine.h

📁 HGE 2D游戏引擎制作的一个演示
💻 H
字号:
#ifndef CORE_GUIDEFINE_H
#define CORE_GUIDEFINE_H

#pragma once

#include "define.h"
#include <math.h>


// double <--> float
inline float	_d2f (double value)	{ return static_cast<float>(value); }
inline double	_f2d (float value)	{ return static_cast<double>(value); }

// float <--> int
inline float	_i2f (int value)	{ return static_cast<float>(value); }
inline int		_f2i (float value)	{ return static_cast<int>(value); }

// float <--> long
inline float	_l2f (long value)	{ return static_cast<float>(value); }
inline long		_f2l (float value)	{ return static_cast<long>(value); }

// float <--> BYTE
inline float	_b2f (BYTE value)	{ return static_cast<float>(value); }
inline BYTE		_f2b (float value)	{ return static_cast<BYTE>(value); }

// float <--> WORD
inline float	_w2f (WORD value)	{ return static_cast<float>(value); }
inline WORD		_f2w (float value)	{ return static_cast<WORD>(value); }

// int <--> BYTE
inline int		_b2i (BYTE value)	{ return static_cast<int>(value); }
inline BYTE		_i2b (int value)	{ return static_cast<BYTE>(value); }

// int <--> WORD
inline int		_w2i (WORD value)	{ return static_cast<int>(value); }
inline WORD		_i2w (int value)	{ return static_cast<WORD>(value); }

// scale 计算ci是size的百分比
inline int _scale(int ci,int size)	{ return _f2i(floor( _i2f(ci) / _i2f(size) * 100.0F)); }

class GUISize
{
public:
	float Width;
	float Height;
	GUISize() {Width=0;Height=0;}
	GUISize(float w, float h) {Width=w; Height=h;}
	~GUISize() {}
};

class GUIPoint
{
public:
	float x;
	float y;
	GUIPoint(){x=0;y=0;};
	GUIPoint(float fx,float fy){x=fx;y=fy;};
	~GUIPoint(){};
};

//矩形
class GUIRect
{
public:
	float	x1, y1, x2, y2;

	GUIRect(float _x1, float _y1, float _x2, float _y2) {x1=_x1; y1=_y1; x2=_x2; y2=_y2; bClean=false; }
	GUIRect() {bClean=true;}

	void    Clear() {bClean=true;}
	bool    IsClean() const {return bClean;}
	void	Set(float _x1, float _y1, float _x2, float _y2) { x1=_x1; x2=_x2; y1=_y1; y2=_y2; bClean=false; }
	void	SetRadius(float x, float y, float r) { x1=x-r; x2=x+r; y1=y-r; y2=y+r; bClean=false; }
	void	Encapsulate(float x, float y)
	{
		if(bClean)
		{
			x1=x2=x;
			y1=y2=y;
			bClean=false;
		}
		else
		{
			if(x<x1) x1=x;
			if(x>x2) x2=x;
			if(y<y1) y1=y;
			if(y>y2) y2=y;
		}
	}
	bool	TestPoint(float x, float y) const
	{
		if(x>=x1 && x<x2 && y>=y1 && y<y2)
			return true;
		return false;
	}
	bool	Intersect(const GUIRect *rect) const
	{
		if(fabs(x1+x2 - rect->x1-rect->x2) < (x2-x1 + rect->x2-rect->x1))
		{
			if(fabs(y1+y2 - rect->y1-rect->y2) < (y2-y1 + rect->y2-rect->y1))
				return true;
		}
		return false;
	}

private:
	bool	bClean;
};


//颜色
inline void ColorClamp(float &x) { if(x<0.0f) x=0.0f; if(x>1.0f) x=1.0f; }

class GUIColor
{
public:
	float		r,g,b,a;

	GUIColor(float _r, float _g, float _b, float _a) { r=_r; g=_g; b=_b; a=_a; }
	GUIColor(DWORD col) { SetHWColor(col); }
	GUIColor() { r=g=b=a=0; }

	GUIColor	operator- (const GUIColor &c) { return GUIColor(r-c.r, g-c.g, b-c.b, a-c.a); }
	GUIColor	operator+ (const GUIColor &c) { return GUIColor(r+c.r, g+c.g, b+c.b, a+c.a); }
	GUIColor	operator* (float scalar) { return GUIColor(r*scalar, g*scalar, b*scalar, a*scalar); }
	GUIColor&	operator-= (const GUIColor &c) { r-=c.r; g-=c.g; b-=c.b; a-=c.a; return *this; }
	GUIColor&	operator+= (const GUIColor &c) { r+=c.r; g+=c.g; b+=c.b; a+=c.a; return *this; }
	GUIColor&	operator*= (float scalar) { r*=scalar; g*=scalar; b*=scalar; a*=scalar; return *this; }
	bool		operator== (const GUIColor &c) { return (r==c.r && g==c.g && b==c.b && a==c.a); }
	bool		operator!= (const GUIColor &c) { return (r!=c.r || g!=c.g || b!=c.b || a!=c.a); }

	void		Clamp() { ColorClamp(r); ColorClamp(g); ColorClamp(b); ColorClamp(a); }
	void		SetHWColor(DWORD col) { a=(col>>24)/255.0f; r=((col>>16) & 0xFF)/255.0f; g=((col>>8) & 0xFF)/255.0f; b=(col & 0xFF)/255.0f; }
	DWORD		GetHWColor() const { return (DWORD(a*255.0f)<<24) + (DWORD(r*255.0f)<<16) + (DWORD(g*255.0f)<<8) + DWORD(b*255.0f); }
};


inline GUIColor operator* (const GUIColor &c, float s) { return GUIColor(s*c.r, s*c.g, s*c.b, s*c.a); };
inline GUIColor operator* (float s, const GUIColor &c) { return GUIColor(s*c.r, s*c.g, s*c.b, s*c.a); };

//矢量
inline float InvSqrt(float x)
{
	union
	{
		int intPart;
		float floatPart;
	} convertor;

	convertor.floatPart = x;
	convertor.intPart = 0x5f3759df - (convertor.intPart >> 1);
	return convertor.floatPart*(1.5f - 0.4999f*x*convertor.floatPart*convertor.floatPart);
}

class GUIVector
{
public:
	float	x,y;

	GUIVector(float _x, float _y)	{ x=_x; y=_y; }
	GUIVector()						{ x=0; y=0; }

	GUIVector	operator- (const GUIVector &v) { return GUIVector(x-v.x, y-v.y); }
	GUIVector	operator+ (const GUIVector &v) { return GUIVector(x+v.x, y+v.y); }
	GUIVector	operator* (float scalar) { return GUIVector(x*scalar, y*scalar); }
	GUIVector&	operator-= (const GUIVector &v) { x-=v.x; y-=v.y; return *this; }
	GUIVector&	operator+= (const GUIVector &v) { x+=v.x; y+=v.y; return *this; }
	GUIVector&	operator*= (float scalar) { x*=scalar; y*=scalar; return *this; }
	GUIVector	operator- () { return GUIVector(-x, -y); }
	bool		operator== (const GUIVector &v) { return (x==v.x && y==v.y); }
	bool		operator!= (const GUIVector &v) { return (x!=v.x || y!=v.y); }

	float		Dot(const GUIVector *v) const { return x*v->x + y*v->y; }
	GUIVector*	Normalize() { float rc=InvSqrt(Dot(this)); x*=rc; y*=rc; return this; }
	float		Length() const { return sqrtf(Dot(this)); }
	float		Angle(const GUIVector *v = 0) const
	{
		if(v)
		{
			GUIVector s=*this, t=*v;

			s.Normalize(); t.Normalize();
			return acosf(s.Dot(&t));
		}
		else
			return atan2f(y, x);
	}
	GUIVector*	Rotate(float a)
	{
		GUIVector v;

		v.x=x*cosf(a) - y*sinf(a);
		v.y=x*sinf(a) + y*cosf(a);

		x=v.x; y=v.y;

		return this;
	}
};

inline GUIVector operator* (const GUIVector &v, float s) { return GUIVector(s*v.x, s*v.y); }
inline GUIVector operator* (float s, const GUIVector &v) { return GUIVector(s*v.x, s*v.y); }
inline float operator^ (const GUIVector &v, const GUIVector &u) { return v.Angle(&u); }
inline float operator% (const GUIVector &v, const GUIVector &u) { return v.Dot(&u); }


#endif //CORE_GUIDEFINE_H

⌨️ 快捷键说明

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