// Author: Gabor Szauer
// http://gaborszauer.com
// Last Updated: July 3, 2009
// Changelog:
// http://pastebin.com/f125cefeb
// http://pastebin.com/f5f35c041
// Dependencies:
// http://pastebin.com/f19b4b3d8
// http://pastebin.com/f23b75420
#ifndef H_SPRITE
#define H_SPRITE
#include "Bitmap.h"
#include "Vector.h"
#define _SECONDS(x) (x*1000)
class Sprite : public Bitmap {
private:
Sprite(const Sprite& rSprite) {}
protected:
int nWidth; // Width of view window
int nHeight; // Height of view window
unsigned int nTickUpdateCount; // Update every x milliseconds
int nAnim; // What animation group to display
int nFrame; // Current Frame
POINT pPos; // Position of the view window
DWORD dwLastTick; // Keep track of the animation timer
Vector<Vector<RECT>*> vAnimation;
void SetToNull() {
Bitmap::SetToNull();
nWidth = 0;
nHeight = 0;
pPos.x = 0;
pPos.y = 0;
nFrame = 0;
nAnim = -1;
nTickUpdateCount = 0;
nFrame = 0;
}
bool IsNull() {
return (
Bitmap::IsNull() &&
nWidth == 0 &&
nHeight == 0 &&
pPos.x == 0 &&
pPos.y == 0 &&
nFrame == 0 &&
nAnim == -1 &&
nTickUpdateCount == 0 &&
nFrame == 0
);
}
public:
HWND hWnd;
void Destroy() {
for (int i = 0; i < vAnimation.Size(); ++i) {
(*vAnimation[i]).Clear();
delete vAnimation[i];
}
vAnimation.Clear();
Bitmap::Destroy();
SetToNull();
}
~Sprite() {
if (!IsNull()) {
this->Destroy();
}
}
Sprite() {
SetToNull();
Sync();
}
void Sync() {
dwLastTick = GetTickCount();
}
void SetAnimation(int rnAnim = 0) {
nAnim = rnAnim;
nFrame = 0;
}
void SetUpdateCount(int rnCount) {
nTickUpdateCount = rnCount;
}
void SetWidth(int rnWidth) {
nWidth = rnWidth;
}
void SetHeight(int rnHeight) {
nHeight = rnHeight;
}
void SetDimentions(int rnWidth, int rnHeight) {
nWidth = rnWidth;
nHeight = rnHeight;
}
void SetPosition(int rnX, int rnY) {
pPos.x = rnX;
pPos.y = rnY;
}
int CreateAnimation() {
Vector<RECT>* vNewAnimation = new Vector<RECT>;
vAnimation.Append(vNewAnimation);
return vAnimation.Size() - 1;
}
void SetAnimatedRegion(int nAnimIndex, RECT &rRect) {
(*vAnimation[nAnimIndex]).Append(rRect);
}
void Flush(HDC hdcDisplay, int rnX, int rnY) {
UpdateAnimation();
BitBlt(hdcDisplay, rnX, rnY, nWidth, nHeight, hdcBitmap, pPos.x, pPos.y, SRCCOPY);
}
void TransparentFlush(HDC hdcDisplay, int rnX, int rnY, UINT uiColorToExclude = RGB(255, 0, 255)) {
UpdateAnimation();
TransparentBlt(hdcDisplay, rnX, rnY, nWidth, nHeight, hdcBitmap, pPos.x, pPos.y, nWidth, nHeight, uiColorToExclude);
}
void UpdateAnimation() {
if (nAnim == -1)
return;
if (GetTickCount() - dwLastTick >= nTickUpdateCount) {
if (++nFrame == (*vAnimation[nAnim]).Size()) {
nFrame = 0;
}
nWidth = (*vAnimation[nAnim])[nFrame].right - (*vAnimation[nAnim])[nFrame].left;
nHeight = (*vAnimation[nAnim])[nFrame].bottom - (*vAnimation[nAnim])[nFrame].top;
pPos.x = (*vAnimation[nAnim])[nFrame].left;
pPos.y = (*vAnimation[nAnim])[nFrame].top;
dwLastTick = GetTickCount();
}
}
};
#endif