Code:
#ifndef CSTATICSPRITECONTAINER
#define CSTATICSPRITECONTAINER
#include <d3d9.h>
#include <vector>
#include "VertexTypes.h"
#include "CQuickCom.h"
class CStaticSprite
{
friend class CStaticSpriteContainer;
D3DXVECTOR3 Pos;
float ZRotation;
D3DXVECTOR3 Vel;
float Speed;
float ZRotSpeed;
//This is the transformed version of the vertexes
//These are world-coordinate vertexes
float Scale;
RECT Bounds;
D3DCOLOR Color;
public:
CStaticSprite(void):Pos(D3DXVECTOR3(0.0f,0.0f,0.0f)),
ZRotation(0.0f),
ZRotSpeed(0.0f),
Vel(D3DXVECTOR3(0.0f,0.0f,0.0f)),
Speed(0.0f) {}
virtual ~CStaticSprite(void) {}
void Create(D3DXVECTOR3 _pos,
float _zrotation,
float _zrotspeed,
D3DXVECTOR3 _vel,
float _speed,
float _scale)
{
Pos=_pos;
ZRotation=_zrotation;
Vel=_vel;
Speed=_speed;
Scale=_scale;
ZRotSpeed=_zrotspeed;
}
void SetColor(D3DCOLOR _newcolor) {Color=_newcolor;};
void SetBounds(RECT _newbounds) {Bounds=_newbounds;};
};
class CStaticSpriteContainer
{
friend class CStaticSprite;
ID3DXSprite *Sprite;
std::vector<CStaticSprite> StaticSprites;
IDirect3DTexture9 *Texture;
IDirect3DDevice9 *Device;
float MaxX,MaxY;
float Width;
float Height;
float Width2;
float Height2;
bool KillAtBorder;
D3DCOLOR Color;
//This is the original copy of the vertexes for this list
//All vertexes are centered around 0,0,0.
TexTLVertex OrigVerts[4];
public:
CStaticSpriteContainer(void) {}
~CStaticSpriteContainer(void)
{
StaticSprites.clear();
SAFE_RELEASE(Sprite);
}
void SetColor(D3DCOLOR _newcolor)
{
Color=_newcolor;
}
void Create(IDirect3DDevice9 *_device,std::string _file,float _maxx,float _maxy)
{
Device=_device;
if (FAILED(D3DXCreateSprite(Device,&Sprite)))
{
::MessageBox(0,0,"Failed to create Sprite interface",0);
return;
}
MaxX=_maxx;
MaxY=_maxy;
D3DXCreateTextureFromFile(Device,_file.c_str(),&Texture);
D3DSURFACE_DESC desc;
Texture->GetLevelDesc(0,&desc);
Width=(float)desc.Width;
Height=(float)desc.Height;
Width2=Width*.5f;
Height2=Height*.5f;
}
void SetKillAtBorder(bool _cond=true)
{
KillAtBorder=_cond;
}
unsigned int Add(D3DXVECTOR3 _pos,
float _zrotation,
float _zrotspeed,
D3DXVECTOR3 _vel,
float _speed,
float _scale,
D3DCOLOR _color)
{
CStaticSprite newSprite;
newSprite.Create(_pos,_zrotation,_zrotspeed,_vel,_speed,_scale);
newSprite.SetColor(_color);
//RECT bbox=offset_bounds;
StaticSprites.push_back(newSprite);
return (StaticSprites.size()-1);
}
void UpdateAll(void)
{
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
std::vector<CStaticSprite>::iterator CurSprite=StaticSprites.begin();
D3DXMATRIX ptrans;
D3DXMATRIX otrans;
D3DXMATRIX scale;
D3DXMATRIX rot;
D3DXMATRIX world;
D3DXMatrixIdentity(&world);
bool Increment=true;
while (CurSprite!=StaticSprites.end())
{
Increment=true;
//Update Sprite position
CurSprite->Pos+=(CurSprite->Vel*CurSprite->Speed);
CurSprite->ZRotation+=CurSprite->ZRotSpeed;
//Scroll sprites - leave left enter right, etc.
if (KillAtBorder==false)
{
if (CurSprite->Pos.x<0) CurSprite->Pos.x+=MaxX;
if (CurSprite->Pos.y<0) CurSprite->Pos.y+=MaxY;
if (CurSprite->Pos.x>MaxX) CurSprite->Pos.x-=MaxX;
if (CurSprite->Pos.y>MaxY) CurSprite->Pos.y-=-MaxY;
}
else
{
if (CurSprite->Pos.x<0 ||
CurSprite->Pos.y<0 ||
CurSprite->Pos.x>MaxX ||
CurSprite->Pos.y>MaxY)
{
CurSprite=StaticSprites.erase(CurSprite);
//Dont increment on kill
Increment=false;
}
}
//Rotate it - only need z rotation
D3DXMatrixRotationYawPitchRoll(&rot,
0.0f,
0.0f,
CurSprite->ZRotation);
//Scale it - no z scale
D3DXMatrixScaling(&scale,
CurSprite->Scale,
CurSprite->Scale,
1.0f);
//Translate it - zero out z translation
D3DXMatrixTranslation(&ptrans,
CurSprite->Pos.x,
CurSprite->Pos.y,
0.0f);
//Concatenate
world=rot*scale*ptrans;
Sprite->SetTransform(&world);
Sprite->Draw(Texture,
NULL,
NULL,
&D3DXVECTOR3(-Width2,-Height2,0.0f),
CurSprite->Color);
if (Increment) CurSprite++;
}
Sprite->End();
}
void DrawAll(void)
{
}
};
class CMasterStaticSpriteManager
{
static std::vector<CStaticSpriteContainer> StaticContainers;
public:
CMasterStaticSpriteManager() {}
~CMasterStaticSpriteManager()
{
std::vector<CStaticSpriteContainer>::iterator CList;
while (CList!=StaticContainers.end())
{
StaticContainers.erase(CList);
}
}
unsigned int AddContainer(CStaticSpriteContainer *_NewContainer)
{
StaticContainers.push_back(*_NewContainer);
}
CStaticSpriteContainer GetContainer(unsigned int _ID)
{
return (StaticContainers[_ID]);
}
};
#endif