File: CDefHandler.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 10,264 kB
  • ctags: 16,826
  • sloc: cpp: 121,945; objc: 248; sh: 193; makefile: 28; python: 13; ansic: 9
file content (380 lines) | stat: -rw-r--r-- 9,048 bytes parent folder | download
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
#include "StdInc.h"
#include "SDL.h"
#include "CDefHandler.h"

#include "../lib/filesystem/Filesystem.h"
#include "../lib/VCMI_Lib.h"
#include "CBitmapHandler.h"
#include "gui/SDL_Extensions.h"
/*
 * CDefHandler.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

#ifdef unused
static long long pow(long long a, int b)
{
	if (!b) return 1;
	long c = a;
	while (--b)
		a*=c;
	return a;
}
#endif

CDefHandler::CDefHandler()
{
	notFreeImgs = false;
}
CDefHandler::~CDefHandler()
{
	if (notFreeImgs)
		return;
	for (auto & elem : ourImages)
	{
		if (elem.bitmap)
		{
			SDL_FreeSurface(elem.bitmap);
			elem.bitmap=nullptr;
		}
	}
}
CDefEssential::~CDefEssential()
{
	for(auto & elem : ourImages)
		SDL_FreeSurface(elem.bitmap);
}

void CDefHandler::openFromMemory(ui8 *table, const std::string & name)
{
	SDL_Color palette[256];
	SDefEntry &de = * reinterpret_cast<SDefEntry *>(table);
	ui8 *p;

	defName = name;
	DEFType = read_le_u32(&de.DEFType);
	width = read_le_u32(&de.width);
	height = read_le_u32(&de.height);
	ui32 totalBlocks = read_le_u32(&de.totalBlocks);

	for (ui32 it=0;it<256;it++)
	{
		palette[it].r = de.palette[it].R;
		palette[it].g = de.palette[it].G;
		palette[it].b = de.palette[it].B;
		palette[it].a = SDL_ALPHA_OPAQUE;	
	}

	// The SDefEntryBlock starts just after the SDefEntry
	p = reinterpret_cast<ui8 *>(&de);
	p += sizeof(de);

	int totalEntries=0;
	for (ui32 z=0; z<totalBlocks; z++)
	{
		SDefEntryBlock &block = * reinterpret_cast<SDefEntryBlock *>(p);
		ui32 totalInBlock;

		totalInBlock = read_le_u32(&block.totalInBlock);

		for (ui32 j=SEntries.size(); j<totalEntries+totalInBlock; j++)
			SEntries.push_back(SEntry());

		p = block.data;
		for (ui32 j=0; j<totalInBlock; j++)
		{
			char Buffer[13];
			memcpy(Buffer, p, 12);
			Buffer[12] = 0;
			SEntries[totalEntries+j].name=Buffer;
			p += 13;
		}
		for (ui32 j=0; j<totalInBlock; j++)
		{
			SEntries[totalEntries+j].offset = read_le_u32(p);
			p += 4;
		}
		//totalEntries+=totalInBlock;
		for(ui32 hh=0; hh<totalInBlock; ++hh)
		{
			SEntries[totalEntries].group = z;
			++totalEntries;
		}
	}

	for(auto & elem : SEntries)
	{
		elem.name = elem.name.substr(0, elem.name.find('.')+4);
	}
	//RWEntries = new ui32[height];
	for(ui32 i=0; i < SEntries.size(); ++i)
	{
		Cimage nimg;
		nimg.bitmap = getSprite(i, table, palette);
		nimg.imName = SEntries[i].name;
		nimg.groupNumber = SEntries[i].group;
		ourImages.push_back(nimg);
	}
}

SDL_Surface * CDefHandler::getSprite (int SIndex, const ui8 * FDef, const SDL_Color * palette) const
{
	SDL_Surface * ret=nullptr;

	ui32 BaseOffset,
		SpriteWidth, SpriteHeight, //format of sprite
		TotalRowLength,			// length of read segment
		add, FullHeight,FullWidth,
		RowAdd,
		defType2;
	int LeftMargin, RightMargin, TopMargin, BottomMargin;


	ui8 SegmentType;

	BaseOffset = SEntries[SIndex].offset;
	SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef + BaseOffset);

	defType2 = read_le_u32(&sd.defType2);
	FullWidth = read_le_u32(&sd.FullWidth);
	FullHeight = read_le_u32(&sd.FullHeight);
	SpriteWidth = read_le_u32(&sd.SpriteWidth);
	SpriteHeight = read_le_u32(&sd.SpriteHeight);
	LeftMargin = read_le_u32(&sd.LeftMargin);
	TopMargin = read_le_u32(&sd.TopMargin);
	RightMargin = FullWidth - SpriteWidth - LeftMargin;
	BottomMargin = FullHeight - SpriteHeight - TopMargin;

	//if(LeftMargin + RightMargin < 0)
	//	SpriteWidth += LeftMargin + RightMargin; //ugly construction... TODO: check how to do it nicer
	if(LeftMargin<0)
		SpriteWidth+=LeftMargin;
	if(RightMargin<0)
		SpriteWidth+=RightMargin;

	// Note: this looks bogus because we allocate only FullWidth, not FullWidth+add
	add = 4 - FullWidth%4;
	if (add==4)
		add=0;

	ret = SDL_CreateRGBSurface(SDL_SWSURFACE, FullWidth, FullHeight, 8, 0, 0, 0, 0);
	
	if(nullptr == ret)
	{
		logGlobal->errorStream() << __FUNCTION__ <<": Unable to create surface";
		logGlobal->errorStream() << FullWidth << "X" << FullHeight;
		logGlobal->errorStream() << SDL_GetError();
		throw std::runtime_error("Unable to create surface");		
	}

	BaseOffset += sizeof(SSpriteDef);
	int BaseOffsetor = BaseOffset;
	
	SDL_Palette * p = SDL_AllocPalette(256);	
	SDL_SetPaletteColors(p, palette, 0, 256);
	SDL_SetSurfacePalette(ret, p);
	SDL_FreePalette(p);	

	int ftcp=0;

	// If there's a margin anywhere, just blank out the whole surface.
	if (TopMargin > 0 || BottomMargin > 0 || LeftMargin > 0 || RightMargin > 0) {
		memset( reinterpret_cast<char*>(ret->pixels), 0, FullHeight*FullWidth);
	}

	// Skip top margin
	if (TopMargin > 0)
		ftcp += TopMargin*(FullWidth+add);

	switch(defType2)
	{
	case 0:
	{
		for (ui32 i=0;i<SpriteHeight;i++)
		{
			if (LeftMargin>0)
				ftcp += LeftMargin;

			memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], SpriteWidth);
			ftcp += SpriteWidth;
			BaseOffset += SpriteWidth;

			if (RightMargin>0)
				ftcp += RightMargin;
		}
	}
	break;

	case 1:
	{
		const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+BaseOffset);
		BaseOffset += sizeof(int) * SpriteHeight;
		for (ui32 i=0;i<SpriteHeight;i++)
		{
			BaseOffset=BaseOffsetor + read_le_u32(RWEntriesLoc + i);
			if (LeftMargin>0)
				ftcp += LeftMargin;

			TotalRowLength=0;
			do
			{
				ui32 SegmentLength;

				SegmentType=FDef[BaseOffset++];
				SegmentLength=FDef[BaseOffset++] + 1;

				if (SegmentType==0xFF)
				{
					memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, FDef + BaseOffset, SegmentLength);
					BaseOffset+=SegmentLength;
				}
				else
				{
					memset(reinterpret_cast<char*>(ret->pixels)+ftcp, SegmentType, SegmentLength);
				}
				ftcp += SegmentLength;
				TotalRowLength += SegmentLength;
			}while(TotalRowLength<SpriteWidth);

			RowAdd=SpriteWidth-TotalRowLength;

			if (RightMargin>0)
				ftcp += RightMargin;

			if (add>0)
				ftcp += add+RowAdd;
		}
	}
	break;

	case 2:
	{
		BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor);

		for (ui32 i=0;i<SpriteHeight;i++)
		{
			//BaseOffset = BaseOffsetor+RWEntries[i];
			if (LeftMargin>0)
				ftcp += LeftMargin;

			TotalRowLength=0;

			do
			{
				SegmentType=FDef[BaseOffset++];
				ui8 code = SegmentType / 32;
				ui8 value = (SegmentType & 31) + 1;
				if(code==7)
				{
					memcpy(reinterpret_cast<char*>(ret->pixels)+ftcp, &FDef[BaseOffset], value);
					ftcp += value;
					BaseOffset += value;
				}
				else
				{
					memset(reinterpret_cast<char*>(ret->pixels)+ftcp, code, value);
					ftcp += value;
				}
				TotalRowLength+=value;
			} while(TotalRowLength<SpriteWidth);

			if (RightMargin>0)
				ftcp += RightMargin;

			RowAdd=SpriteWidth-TotalRowLength;

			if (add>0)
				ftcp += add+RowAdd;
		}
	}
	break;

	case 3:
	{
		for (ui32 i=0;i<SpriteHeight;i++)
		{
			BaseOffset = BaseOffsetor + read_le_u16(FDef + BaseOffsetor+i*2*(SpriteWidth/32));
			if (LeftMargin>0)
				ftcp += LeftMargin;

			TotalRowLength=0;

			do
			{
				SegmentType=FDef[BaseOffset++];
				ui8 code = SegmentType / 32;
				ui8 value = (SegmentType & 31) + 1;

				int len = std::min<ui32>(value, SpriteWidth - TotalRowLength) - std::max(0, -LeftMargin);
				vstd::amax(len, 0);

				if(code==7)
				{
					memcpy((ui8*)ret->pixels + ftcp, FDef + BaseOffset, len);
					ftcp += len;
					BaseOffset += len;
				}
				else
				{
					memset((ui8*)ret->pixels + ftcp, code, len);
					ftcp += len;
				}
				TotalRowLength+=( LeftMargin>=0 ? value : value+LeftMargin );
			}while(TotalRowLength<SpriteWidth);

			if (RightMargin>0)
				ftcp += RightMargin;

			RowAdd=SpriteWidth-TotalRowLength;

			if (add>0)
				ftcp += add+RowAdd;
		}
	}
	break;

	default:
		throw std::runtime_error("Unknown sprite format.");
		break;
	}

	SDL_Color ttcol = ret->format->palette->colors[0];
	Uint32 keycol = SDL_MapRGBA(ret->format, ttcol.r, ttcol.b, ttcol.g, ttcol.a);	
	SDL_SetColorKey(ret, SDL_TRUE, keycol);	

	return ret;
}

CDefEssential * CDefHandler::essentialize()
{
	auto   ret = new CDefEssential;
	ret->ourImages = ourImages;
	notFreeImgs = true;
	return ret;
}

CDefHandler * CDefHandler::giveDef(const std::string & defName)
{
	ResourceID resID(std::string("SPRITES/") + defName, EResType::ANIMATION);

	auto data = CResourceHandler::get()->load(resID)->readAll().first;
	if(!data)
		throw std::runtime_error("bad def name!");
	auto   nh = new CDefHandler();
	nh->openFromMemory(data.get(), defName);
	return nh;
}
CDefEssential * CDefHandler::giveDefEss(const std::string & defName)
{
	CDefEssential * ret;
	CDefHandler * temp = giveDef(defName);
	ret = temp->essentialize();
	delete temp;
	return ret;
}