summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2ImageObj.cpp
blob: 92f61373505bea3b7a2b3d6a34c8ba2957775a05 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "StdAfx.h"
#define ATOI_KLUDGE
#include "GlobalFunc.h"
#include "T2BitImage.h"
#include "T2ImageObj.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

T2ImageObj::T2ImageObj() {
	mCount = 0;
	mHandle = GlobalAlloc(GHND, 1);
	mData = (ObjectData *) GlobalLock(mHandle);
}

/*virtual*/ T2ImageObj::~T2ImageObj() {
	GlobalUnlock(mHandle);
	GlobalFree(mHandle);
}

void T2ImageObj::AddObject(HINSTANCE instance, unsigned int resourceID, T2BitImage* image) {
#line 36
	_ASSERT(image);

	HRSRC rsrc = FindResource(instance, MAKEINTRESOURCE(resourceID), "OBJMAP");
	if (!rsrc) {
		CString nameStr;
		nameStr.Format("%d", resourceID);

		CString error = "T2ImageObj::AddObject ERROR : " + nameStr + " @" + GetModuleName(instance) + "\n";
		OutputDebugString(error);
	}

#line 45
	_ASSERT(rsrc);

	HGLOBAL theHandle = LoadResource(instance, rsrc);
#line 47
	_ASSERT(theHandle);

	void *theRes = LockResource(theHandle);
	DWORD theResSize = SizeofResource(instance, rsrc);
	char *theData = (char *) malloc(theResSize);
	memcpy(theData, theRes, theResSize);
	UnlockResource(theHandle);
	FreeResource(theHandle);

	char *theCmd = strtok(theData, " \t\r\n\x1A");
	while (theCmd) {
		if (!_stricmp(theCmd, "End"))
			break;

		if (!_stricmp(theCmd, "DefParts")) {
			mCount++;
			mHandle = GlobalReAlloc(mHandle, mCount * sizeof(ObjectData), GHND);
			mData = (ObjectData *) GlobalLock(mHandle);
			memset(&mData[mCount - 1], 0, sizeof(ObjectData));

			mData[mCount - 1].image = image;
			strcpy(mData[mCount - 1].name, "");
			mData[mCount - 1].id = 0;
			mData[mCount - 1].pattern = -1;
			mData[mCount - 1].grade = 0;
			mData[mCount - 1].span = 0;
			mData[mCount - 1].offset = -1;
		} else if (!_stricmp(theCmd, "Name")) {
			strcpy(mData[mCount - 1].name, strtok(NULL, "\t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "ID")) {
			mData[mCount - 1].id = atoi(strtok(NULL, " \t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "Pattern")) {
			mData[mCount - 1].pattern = atoi(strtok(NULL, " \t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "Grade")) {
			mData[mCount - 1].grade = atoi(strtok(NULL, " \t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "Span")) {
			mData[mCount - 1].span = atoi(strtok(NULL, " \t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "Offset")) {
			mData[mCount - 1].offset = atoi(strtok(NULL, " \t\r\n\x1A"));
		} else if (!_stricmp(theCmd, "Roof")) {
			if (!_stricmp(strtok(NULL, " \t\r\n\x1A"), "Yes"))
				mData[mCount - 1].roof = true;
		} else if (!_stricmp(theCmd, "Floor")) {
			if (!_stricmp(strtok(NULL, " \t\r\n\x1A"), "Yes"))
				mData[mCount - 1].floor = true;
		} else if (!_stricmp(theCmd, "List")) {
			// nothing
		} else if (!_stricmp(theCmd, "Loop")) {
			mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags |= SPD_LOOP;
		} else if (!_stricmp(theCmd, "Transparent")) {
			mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags |= SPD_TRANSPARENT;
		} else if (!_stricmp(theCmd, "Halftone")) {
			mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags |= SPD_HALFTONE;
		} else if (!_stricmp(theCmd, "Rect")) {
			mData[mCount - 1].subPartCount++;
			mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags = SPD_RECT;

			char *arg = strtok(NULL, " \t\r\n\x1A");
			char *argCopy = (char *) malloc(strlen(arg) + 1);
			strcpy(argCopy, arg);

			char *p;
			while ((p = strchr(argCopy, ',')))
				*p = ' ';

			sscanf(
				argCopy,
				"%hd %hd %hd %hd",
				&mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].left,
				&mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].top,
				&mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].right,
				&mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].bottom
			);

			free(argCopy);
		} else if (!_stricmp(theCmd, "Parts")) {
			char *arg = strtok(NULL, " \t\r\n\x1A");
			if (arg[0] != '#') {
				int i;
				for (i = 0; i < (mCount - 1); i++) {
					if (!strcmp(mData[i].name, arg)) {
						mData[mCount - 1].subPartCount++;
						mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags = SPD_PARTS_BY_NAME;
						mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].partIndex = i;
						break;
					}
				}

				if (i == (mCount - 1)) {
					char buf[256];
					wsprintf(buf, "Undefined parts [%s]", arg);
					MessageBox(NULL, buf, "ERROR", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
				}
			} else {
				mData[mCount - 1].subPartCount++;
				mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].flags = SPD_PARTS_BY_ID;
				mData[mCount - 1].subParts[mData[mCount - 1].subPartCount - 1].objectID = atoi(&arg[1]);
			}
		} else if (theCmd[0] == '#') {
			// nothing
		} else {
			char buf[256];
			wsprintf(buf, "Undefined theCmd [%s]", theCmd);
			MessageBox(NULL, buf, "ERROR", MB_OK | MB_ICONWARNING | MB_TASKMODAL);
		}

        theCmd = strtok(NULL, " \t\r\n\x1A");
	}

	free(theData);
}

void T2ImageObj::AddObject(const char* name, int pattern, T2BitImage& image, const RECT* rect, BOOL transparent, BOOL halftoneMode) {
	mCount++;
	mHandle = GlobalReAlloc(mHandle, mCount * sizeof(ObjectData), GHND);
	mData = (ObjectData *) GlobalLock(mHandle);
	memset(&mData[mCount - 1], 0, sizeof(ObjectData));

	mData[mCount - 1].image = &image;
	strcpy(mData[mCount - 1].name, name);
	mData[mCount - 1].id = 0;
	mData[mCount - 1].pattern = pattern;
	mData[mCount - 1].grade = 0;
	mData[mCount - 1].span = 0;
	mData[mCount - 1].offset = -1;
	mData[mCount - 1].subPartCount = 1;
	mData[mCount - 1].subParts[0].flags =
		SPD_RECT |
		(transparent ? SPD_TRANSPARENT : 0) |
		(halftoneMode ? SPD_HALFTONE : 0);

	if (rect) {
		mData[mCount - 1].subParts[0].top = rect->top;
		mData[mCount - 1].subParts[0].left = rect->left;
		mData[mCount - 1].subParts[0].bottom = rect->bottom;
		mData[mCount - 1].subParts[0].right = rect->right;
	} else {
		mData[mCount - 1].subParts[0].top = 0;
		mData[mCount - 1].subParts[0].left = 0;
		mData[mCount - 1].subParts[0].bottom = abs(image.mBitmap.header.biHeight);
		mData[mCount - 1].subParts[0].right = abs(image.mBitmap.header.biWidth);
	}
}

void T2ImageObj::AddObject(int id, int pattern, T2BitImage& image, const RECT* rect, BOOL transparent, BOOL halftoneMode) {
	mCount++;
	mHandle = GlobalReAlloc(mHandle, mCount * sizeof(ObjectData), GHND);
	mData = (ObjectData *) GlobalLock(mHandle);
	memset(&mData[mCount - 1], 0, sizeof(ObjectData));

	mData[mCount - 1].image = &image;
	strcpy(mData[mCount - 1].name, "");
	mData[mCount - 1].id = id;
	mData[mCount - 1].pattern = pattern;
	mData[mCount - 1].grade = 0;
	mData[mCount - 1].span = 0;
	mData[mCount - 1].offset = -1;
	mData[mCount - 1].subPartCount = 1;
	mData[mCount - 1].subParts[0].flags =
		SPD_RECT |
		(transparent ? SPD_TRANSPARENT : 0) |
		(halftoneMode ? SPD_HALFTONE : 0);

	if (rect) {
		mData[mCount - 1].subParts[0].top = rect->top;
		mData[mCount - 1].subParts[0].left = rect->left;
		mData[mCount - 1].subParts[0].bottom = rect->bottom;
		mData[mCount - 1].subParts[0].right = rect->right;
	} else {
		mData[mCount - 1].subParts[0].top = 0;
		mData[mCount - 1].subParts[0].left = 0;
		mData[mCount - 1].subParts[0].bottom = abs(image.mBitmap.header.biHeight);
		mData[mCount - 1].subParts[0].right = abs(image.mBitmap.header.biWidth);
	}
}

int T2ImageObj::FindObject(int id, int pattern, int grade, int span, int offset) {
	ObjectData *obj = mData;
	int index;

	for (index = 0; index < mCount; index++, obj++) {
		if (
			(obj->id == id) &&
			(obj->pattern == pattern || obj->pattern == -1 || pattern == -1) &&
			(obj->grade == grade || obj->grade == 0 || grade == 0) &&
			(obj->span == span || obj->span == 0 || span == 0) &&
			(obj->offset == offset || obj->offset == 255 || offset == 255)
		)
			break;
	}

	if (index == mCount)
		return -1;
	else
		return index;
}

int T2ImageObj::FindObject(const char* name, int pattern, int grade, int span, int offset) {
	ObjectData *obj = mData;
	int index;

	for (index = 0; index < mCount; index++, obj++) {
		if (
			!strcmp(obj->name, name) &&
			(obj->pattern == pattern || obj->pattern == -1 || pattern == -1) &&
			(obj->grade == grade || obj->grade == 0 || grade == 0) &&
			(obj->span == span || obj->span == 0 || span == 0) &&
			(obj->offset == offset || obj->offset == 255 || offset == 255)
		)
			break;
	}

	if (index == mCount)
		return -1;
	else
		return index;
}

void T2ImageObj::EnumParts(int index, int width, PARTSLIST* outParts, int* outCount) {
#line 272
	_ASSERT(index >= 0);

	int i = 0;
	width = width * 8;

	while (width > 0 && i < mData[index].subPartCount) {
		if (mData[index].subParts[i].flags & SPD_RECT) {
			// Draw a piece of the image
			outParts[*outCount].image = mData[index].image;
			outParts[*outCount].rect.top = mData[index].subParts[i].top;
			outParts[*outCount].rect.left = mData[index].subParts[i].left;
			outParts[*outCount].rect.bottom = mData[index].subParts[i].bottom;
			outParts[*outCount].rect.right = mData[index].subParts[i].right;

			if ((mData[index].subParts[i].flags & SPD_TRANSPARENT) == 0)
				outParts[*outCount].transparent = false;
			else
				outParts[*outCount].transparent = true;

			if ((mData[index].subParts[i].flags & SPD_HALFTONE) == 0)
				outParts[*outCount].halftoneMode = false;
			else
				outParts[*outCount].halftoneMode = true;

			width -= (outParts[*outCount].rect.right - outParts[*outCount].rect.left);
			(*outCount)++;
		} else if (mData[index].subParts[i].flags & SPD_PARTS_BY_NAME) {
			// Include another part list
			int prevCount = *outCount;
			EnumParts(mData[index].subParts[i].partIndex, width, outParts, outCount);
			for (; prevCount < *outCount; prevCount++)
				width -= (outParts[prevCount].rect.right - outParts[prevCount].rect.left);
		} else if (mData[index].subParts[i].flags & SPD_PARTS_BY_ID) {
			// Search for an object and include its part list
			int prevCount = *outCount;
			EnumParts(FindObject(mData[index].subParts[i].objectID), width, outParts, outCount);
			for (; prevCount < *outCount; prevCount++)
				width -= (outParts[prevCount].rect.right - outParts[prevCount].rect.left);
		}

		if (!(mData[index].subParts[i].flags & SPD_LOOP))
			i++;
	}
}

BOOL T2ImageObj::WithRoof(int index) {
	return mData[index].roof;
}

BOOL T2ImageObj::WithFloor(int index) {
	return mData[index].floor;
}

void T2ImageObj::DrawObject(T2BitImage* image, int index, RECT rect, int factor, int foreGndColor) {
	DrawObject(image, index, rect, factor, false, foreGndColor);
}

void T2ImageObj::DrawObject(CDC* dc, int index, RECT rect, int factor, int foreGndColor) {
	DrawObject(dc, index, rect, factor, true, foreGndColor);
}

void T2ImageObj::DrawObject(void* target, int index, RECT rect, int factor, BOOL targetIsDC, int foreGndColor) {
#line 338
	_ASSERT(index >= 0);

	static PARTSLIST parts[500];
    RECT srcRect, dstRect;
	int theSize = rect.right - rect.left;
	int numOfParts = 0;
	EnumParts(index, theSize, parts, &numOfParts);

	if (numOfParts >= 500)
		MessageBox(NULL, "Too many parts", "T2ImageObj::DrawObject", MB_OK | MB_ICONERROR | MB_TASKMODAL);

	for (int n = 0; n < numOfParts; n++) {
		srcRect = parts[n].rect;
        dstRect = rect;

		if ((srcRect.right - srcRect.left) > ((dstRect.right - dstRect.left) << factor))
			srcRect.right = srcRect.left + ((dstRect.right - dstRect.left) << factor);

		if ((dstRect.right - dstRect.left) > ((srcRect.right - srcRect.left) >> factor))
            dstRect.right = dstRect.left + ((srcRect.right - srcRect.left) >> factor);

		if ((srcRect.bottom - srcRect.top) > ((dstRect.bottom - dstRect.top) << factor))
			srcRect.bottom = srcRect.top + ((dstRect.bottom - dstRect.top) << factor);

		if ((dstRect.bottom - dstRect.top) > ((srcRect.bottom - srcRect.top) >> factor))
            dstRect.bottom = dstRect.top + ((srcRect.bottom - srcRect.top) >> factor);

		if (parts[n].transparent)
			parts[n].image->SetBackGndColor(0);
		else
			parts[n].image->SetBackGndColor(-1);

		if (parts[n].halftoneMode)
			parts[n].image->SetHalftoneMode(true);
		else
			parts[n].image->SetHalftoneMode(false);

		parts[n].image->SetForeGndColor(foreGndColor);
		if (targetIsDC) {
			parts[n].image->CopyImage((CDC *) target, srcRect, dstRect, false, NULL);
		} else {
			parts[n].image->CopyImage(*((T2BitImage *) target), srcRect, dstRect, false, NULL);
		}
		parts[n].image->SetHalftoneMode(false);

		rect.left += (dstRect.right - dstRect.left);
	}
}

BOOL T2ImageObj::GetObjectSize(int index, SIZE* outSize) {
#line 383
	_ASSERT(index >= 0);

	PARTSLIST part;
	int num = 0;
	EnumParts(index, 1, &part, &num);

	if (num != 1)
		return false;

	outSize->cx = part.rect.right - part.rect.left;
	outSize->cy = part.rect.bottom - part.rect.top;
	return true;
}

T2BitImage* T2ImageObj::GetObjectImage(int index, RECT& outRect) {
#line 397
	_ASSERT(index >= 0);

	PARTSLIST part;
	int num = 0;
	EnumParts(index, 1, &part, &num);

	if (num != 1)
		return NULL;

	outRect = part.rect;
	return part.image;
}