#include "StdAfx.h" #include "T2ImageObj.h" #include "T2Sprite.h" #include "T2SpriteObj.h" T2Sprite::T2Sprite() { SetRectEmpty(&mUpdateRect); } /*virtual*/ T2Sprite::~T2Sprite() { AllClear(); } void T2Sprite::AllClear() { POSITION p = GetHeadPosition(); while (p) { T2SpriteObj *obj = GetNext(p); delete obj; } RemoveAll(); } int T2Sprite::NewSprite(T2ImageObj& imageObj, const CString& name, int layer) { T2SpriteObj *theSpriteObj = new T2SpriteObj; theSpriteObj->mImageObj = &imageObj; theSpriteObj->mName = name; int index; for (index = 0; ; index++) { if (!FindObj(index)) break; } theSpriteObj->mIndex = index; theSpriteObj->mLayer = layer; int imageObjInd = imageObj.FindObject(name); imageObj.GetObjectSize(imageObjInd, &theSpriteObj->mSize); AddTail(theSpriteObj); return index; } void T2Sprite::DeleteSprite(int index) { POSITION p = GetHeadPosition(); while (p) { POSITION prev = p; T2SpriteObj *obj = GetNext(p); if (obj->mIndex == index) { RemoveAt(prev); delete obj; break; } } } T2SpriteObj* T2Sprite::FindObj(int index) { POSITION p = GetHeadPosition(); while (p) { T2SpriteObj *obj = GetNext(p); if (obj->mIndex == index) return obj; } return NULL; } void T2Sprite::ShowSprite(int index, BOOL show) { T2SpriteObj *obj = FindObj(index); if (obj) { if (show) obj->mShowCount++; else obj->mShowCount--; AddUpdateRect(obj, obj->mPosition); } } void T2Sprite::ChangePattern(int index, int pattern) { T2SpriteObj *obj = FindObj(index); obj->mPattern = pattern; if (obj->mShowCount > 0) AddUpdateRect(obj, obj->mPosition); } void T2Sprite::MoveSprite(int index, POINT pt) { T2SpriteObj *obj = FindObj(index); if (obj) { if (obj->mShowCount > 0) AddUpdateRect(obj, obj->mPosition); obj->mPosition = pt; if (obj->mShowCount > 0) AddUpdateRect(obj, obj->mPosition); } } void T2Sprite::UpdateSprite(int index) { T2SpriteObj *obj = FindObj(index); AddUpdateRect(obj, obj->mPosition); } void T2Sprite::GetSpriteRect(int index, RECT& outRect) { T2SpriteObj *obj = FindObj(index); outRect.left = obj->mPosition.x; outRect.top = obj->mPosition.y; outRect.right = obj->mPosition.x + obj->mSize.cx; outRect.bottom = obj->mPosition.y + obj->mSize.cy; } void T2Sprite::GetUpdateRect(RECT* outRect, int factor) { *outRect = mUpdateRect; int divisor = 1 << factor; outRect->top = (outRect->top >= 0) ? (outRect->top / divisor) : ((outRect->top - (divisor - 1)) / divisor); outRect->left = (outRect->left >= 0) ? (outRect->left / divisor) : ((outRect->left - (divisor - 1)) / divisor); outRect->bottom = (outRect->bottom >= 0) ? (outRect->bottom / divisor) : ((outRect->bottom - (divisor - 1)) / divisor); outRect->right = (outRect->right >= 0) ? (outRect->right / divisor) : ((outRect->right - (divisor - 1)) / divisor); SetRectEmpty(&mUpdateRect); } void T2Sprite::DrawLayer(T2BitImage* dest, int layer, int factor) { POSITION p = GetHeadPosition(); while (p) { T2SpriteObj *obj = GetNext(p); if (obj->mLayer == layer) DrawSprite(dest, obj, factor); } } void T2Sprite::DrawSprite(T2BitImage* dest, T2SpriteObj* obj, int factor) { if (obj->mShowCount > 0) { RECT rect; SetRect( &rect, obj->mPosition.x, obj->mPosition.y, obj->mPosition.x + obj->mSize.cx, obj->mPosition.y + obj->mSize.cy ); rect.top = rect.top / (1 << factor); rect.left = rect.left / (1 << factor); rect.bottom = rect.bottom / (1 << factor); rect.right = rect.right / (1 << factor); int objectID = obj->mImageObj->FindObject(obj->mName, obj->mPattern); if (objectID < 0) return; obj->mImageObj->DrawObject(dest, objectID, rect, factor); } } void T2Sprite::AddUpdateRect(T2SpriteObj* obj, POINT pt) { RECT rect; SetRect(&rect, pt.x, pt.y, pt.x + obj->mSize.cx, pt.y + obj->mSize.cy); UnionRect(&mUpdateRect, &mUpdateRect, &rect); }