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

📄 instsprite.h

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


#include <instTypes.h>
#include <instDefines.h>
#include <instISurf.h>
#include <instList.h>

namespace inst
{


class ILayer:virtual public IDynamic
{
public:
	virtual ~ILayer(){}
	virtual Bool IsA(const CStr &name)const{ return name==L"inst::ILayer" || IDynamic::IsA(name); }
	virtual POS GetX()const=0;
	virtual void SetX(POS x)=0;
	virtual POS GetY()const=0;
	virtual void SetY(POS y)=0;
	virtual POS GetW()const=0;
	virtual POS GetH()const=0;
	ABSTRACT_IS_SET_FUNC(Visible);
	virtual void Render(ISurf *surf,POS vpdestx,POS vpdesty,POS vpx,POS vpy,POS vpw,POS vph)=0;
};

class CLayerMgr:virtual public IDynamic
{
	IS_SET(BkTransparent);
private:
	IList<ILayer *> *m_pLayers;
	struct
	{
		POS x,y,w,h;
	}m_vp;
public:
	virtual void *GetAddr()const{ return (void *)this; }
	virtual CStr GetClass()const{ return L"inst::CLayerMgr"; }
	virtual Bool IsA(const CStr &name)const{ return name==L"inst::CLayerMgr" || IDynamic::IsA(name); }
	CLayerMgr();
	~CLayerMgr(){ SafeDel(m_pLayers); }
	void AddLayer(ILayer *layer);
	void InsertLayer(Int32 index,ILayer *layer); //参见IList::Insert的文档!
	void RemoveLayer(ILayer *layer);
	void RemoveAllLayers(){ if(m_pLayers)m_pLayers->Clear(); }
	ILayer *GetLayer(Int32 index){ return m_pLayers->Get(index); }
	Int32 GetNumLayers()const{ if(!m_pLayers)return 0; else return m_pLayers->Count(); }
	void PutLayerTop(ILayer *layer){ RemoveLayer(layer); AddLayer(layer); }
	void PutLayerBottom(ILayer *layer){ RemoveLayer(layer); InsertLayer(0,layer); } //参见IList::Insert的文档!
	void GetViewPort(POS *vpx,POS *vpy,POS *vpw,POS *vph){ if(vpx)*vpx=m_vp.x; if(vpy)*vpy=m_vp.y; if(vpw)*vpw=m_vp.w; if(vph)*vph=m_vp.h; }
	void SetViewPort(POS vpx,POS vpy,POS vpw,POS vph){ m_vp.x=vpx; m_vp.y=vpy; m_vp.w=vpw; m_vp.h=vph; }
	void Render(ISurf *surf,POS vpdestx,POS vpdesty);
};

class CTiledLayer:virtual public ILayer
{
	VIRTUAL_IS_SET(Visible);
private:
	POS m_x,m_y,m_w,m_h;
	POS m_TileW,m_TileH;
	Int32 m_numCols,m_numRows;
	Int16 **m_ppCells;
	ISurf *m_pImage;
public:
	void ToStdVector(POS *x,POS *y,Int32 col,Int32 row);
	void ToStdVector(POS *x,POS *y,Float col,Float row);
	void ToTileVector(Int32 *col,Int32 *row,POS x,POS y);
	void ToTileVector(Float *col,Float *row,POS x,POS y);
	void ToStdCoord(POS *x,POS *y,Int32 col,Int32 row);
	void ToStdCoord(POS *x,POS *y,Float col,Float row);
	void ToTileCoord(Int32 *col,Int32 *row,POS x,POS y);
	void ToTileCoord(Float *col,Float *row,POS x,POS y);
public:
	virtual void *GetAddr()const{ return (void *)this; }
	virtual CStr GetClass()const{ return L"inst::CTiledLayer"; }
	virtual Bool IsA(const CStr &name)const{ return name==L"inst::CTiledLayer" || ILayer::IsA(name); }
	CTiledLayer(Int32 numcols,Int32 numrows,ISurf *image,POS TileW,POS TileH);
	virtual ~CTiledLayer();

	VIRTUAL_GET_SET_FUNC(POS,X,m_x);
	VIRTUAL_GET_SET_FUNC(POS,Y,m_y);
	virtual POS GetW()const{ return m_w; }
	virtual POS GetH()const{ return m_h; }

	ISurf *GetTileImage()const{ return m_pImage; }
	void SetTileImage(ISurf *image){ m_pImage=image; }
	POS GetTileW()const{ return m_TileW; }
	POS GetTileH()const{ return m_TileH; }
	Int32 GetNumCols()const{ return m_numCols; }
	Int32 GetNumRows()const{ return m_numRows; }

	Int16 GetCell(Int32 col,Int32 row)const{ if(col<0 || col>=m_numCols || row<0 || row>=m_numRows)return 0;
											 if(m_ppCells)return m_ppCells[row][col]; else return 0; }
	void SetCell(Int32 col,Int32 row,Int16 TileIndex){ if(col<0 || col>=m_numCols || row<0 || row>=m_numRows)return;
													   if(m_ppCells)m_ppCells[row][col]=TileIndex; }
	void FillCells(Int32 col,Int32 row,Int32 numcols,Int32 numrows,Int16 TileIndex);

//	Int32 CreateAnimatedTile(Int32 StaticTileIndex);
//	Int32 GetAnimatedTile(Int32 AnimatedTileIndex);
//	void SetAnimatedTile(Int32 AnimatedTileIndex,Int32 StaticTileIndex);

	virtual void Render(ISurf *surf,POS vpdestx,POS vpdesty,POS vpx,POS vpy,POS vpw,POS vph);
};

class CSprite:virtual public ILayer
{
	VIRTUAL_IS_SET(Visible);
	GET_SET(ALPHA,Alpha);
	IS_SET(Stretch);
	GET_SET(POS,SrcW);
	GET_SET(POS,SrcH);
private:
	POS m_x,m_y,m_w,m_h;
	POS m_offx,m_offy;
	ISurf *m_pImage;
	struct
	{
		POS x,y,w,h; //把w,h设为零以使用精灵自身的m_w,m_h
	}m_CollisionRect;
	UInt32 *m_pSeq;
	UInt32 m_SeqLen;
	UInt32 m_CrrtFrame;
public:
	virtual void *GetAddr()const{ return (void *)this; }
	virtual CStr GetClass()const{ return L"inst::CSprite"; }
	virtual Bool IsA(const CStr &name)const{ return name==L"inst::CSprite" || ILayer::IsA(name); }
	CSprite(ISurf *image,POS w,POS h);
	virtual ~CSprite(){ return; }

	VIRTUAL_GET_SET_FUNC(POS,X,m_x);
	VIRTUAL_GET_SET_FUNC(POS,Y,m_y);
	virtual POS GetW()const{ return m_w; }
	virtual POS GetH()const{ return m_h; }

	void SetCollisionRect(POS x,POS y,POS w,POS h){ m_CollisionRect.x=x; m_CollisionRect.y=y;
													m_CollisionRect.w=w; m_CollisionRect.h=h; }
	Bool CollidesWith(ISurf *surf,POS x,POS y);
	Bool CollidesWith(CSprite *sprite);
	Bool CollidesWith(CTiledLayer *tiledlayer);

	void GetHotPoint(POS *x,POS *y)const{ if(x)*x=m_offx; if(y)*y=m_offy; }
	void SetHotPoint(POS x,POS y){ m_offx=x; m_offy=y; }
	POS GetXHot()const{ return m_x+m_offx; }
	POS GetYHot()const{ return m_y+m_offy; }
	void SetXHot(POS xhot){ m_x=xhot-m_offx; }
	void SetYHot(POS yhot){ m_y=yhot-m_offy; }

	UInt32 GetFrame()const{ return m_CrrtFrame; }
	void SetFrame(UInt32 SequenceIndex){ if(SequenceIndex<0 || SequenceIndex>=GetFrameSequenceLength())return; 
										 m_CrrtFrame=SequenceIndex; }
	void NextFrame(){ if((m_CrrtFrame++)>=GetFrameSequenceLength())m_CrrtFrame=0; }
	void PrevFrame(){ if(m_CrrtFrame>0)m_CrrtFrame--; else m_CrrtFrame=GetFrameSequenceLength();}
	UInt32 GetRawFrame()const{ return GetRawFrameInSequence(m_CrrtFrame); }
	void SetFrameSequence(UInt32 *pSequence,UInt32 SequenceLength){ m_pSeq=pSequence; m_SeqLen=SequenceLength; }
	UInt32 GetFrameSequenceLength()const{ if(!m_pSeq)return GetRawFrameCount(); else return m_SeqLen; }
	UInt32 GetRawFrameInSequence(UInt32 SequenceIndex)const{ if(!m_pSeq)return SequenceIndex;
															 if(SequenceIndex<0 || SequenceIndex>=m_SeqLen)return 0;
															 return m_pSeq[SequenceIndex]; }
	UInt32 GetRawFrameCount()const;

	ISurf *GetImage()const{ return m_pImage; }
	void SetImage(ISurf *surf){ m_pImage=surf; }
	void setw(POS w){ m_w=w; }
	void seth(POS h){ m_h=h; }
	virtual void Render(ISurf *surf,POS vpdestx,POS vpdesty,POS vpx,POS vpy,POS vpw,POS vph);
};


}// end of namespace inst


#endif

⌨️ 快捷键说明

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