summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2Sprite.cpp
diff options
context:
space:
mode:
authorAsh Wolf <ninji@wuffs.org>2023-06-14 00:50:34 +0100
committerAsh Wolf <ninji@wuffs.org>2023-06-14 00:50:34 +0100
commit37e364b2c6cc7487a1c888d256a73e5337bb7189 (patch)
treeeaf6e857382eef16c2dd940eb4125536fbe068bd /src/T2DLL/T2Sprite.cpp
downloadt2win-37e364b2c6cc7487a1c888d256a73e5337bb7189.tar.gz
t2win-37e364b2c6cc7487a1c888d256a73e5337bb7189.zip
initial commit
Diffstat (limited to 'src/T2DLL/T2Sprite.cpp')
-rw-r--r--src/T2DLL/T2Sprite.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/T2DLL/T2Sprite.cpp b/src/T2DLL/T2Sprite.cpp
new file mode 100644
index 0000000..6f04e24
--- /dev/null
+++ b/src/T2DLL/T2Sprite.cpp
@@ -0,0 +1,157 @@
+#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 *obj = new T2SpriteObj;
+ obj->mImageObj = &imageObj;
+ obj->mName = name;
+
+ int index;
+ for (index = 0; ; index++) {
+ if (!FindObj(index))
+ break;
+ }
+ obj->mIndex = index;
+ obj->mLayer = layer;
+
+ int objIndex = imageObj.FindObject(name, -1, 0, 0, 255);
+ imageObj.GetObjectSize(objIndex, &obj->mSize);
+ AddTail(obj);
+ 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, 0, 0, 255);
+ if (objectID < 0)
+ return;
+
+ obj->mImageObj->DrawObject(dest, objectID, rect, factor, -1);
+ }
+}
+
+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);
+}