summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2Sprite.cpp
blob: d15cd7b28c6df34d2bd1e27ea0c8e3c71e6eb3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#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);
}