File: ImageManager.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (359 lines) | stat: -rw-r--r-- 10,754 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Copyright (C) 2006-2010 - Frictional Games
 *
 * This file is part of HPL1 Engine.
 */

#include "hpl1/engine/resources/ImageManager.h"
#include "hpl1/engine/graphics/LowLevelGraphics.h"
#include "hpl1/engine/resources/FrameBitmap.h"
#include "hpl1/engine/resources/FrameTexture.h"
#include "hpl1/engine/resources/ResourceImage.h"
#include "hpl1/engine/resources/low_level_resources.h"
#include "hpl1/engine/system/String.h"
#include "hpl1/engine/system/low_level_system.h"

namespace hpl {

//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cImageManager::cImageManager(cFileSearcher *apFileSearcher, iLowLevelGraphics *apLowLevelGraphics,
							 LowLevelResources *apLowLevelResources, LowLevelSystem *apLowLevelSystem)
	: iResourceManager(apFileSearcher, apLowLevelResources, apLowLevelSystem) {
	mpLowLevelGraphics = apLowLevelGraphics;

	mpLowLevelResources->getSupportedImageFormats(mlstFileFormats);

	mvFrameSize = cVector2l(512, 512);
	mlFrameHandle = 0;
}

cImageManager::~cImageManager() {
	// DeleteAllBitmapFrames();
	DestroyAll();

	Log(" Done with images\n");
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

iResourceBase *cImageManager::Create(const tString &asName) {
	return CreateInFrame(asName, -1);
}
//-----------------------------------------------------------------------

iResourceBase *cImageManager::CreateInFrame(const tString &asName, int alFrameHandle) {
	cResourceImage *pImage = NULL;
	tString sPath;

	BeginLoad(asName);

	pImage = FindImage(asName, sPath);
	if (!pImage) {
		if (sPath != "") {
			Bitmap2D *pBmp;
			pBmp = mpLowLevelResources->loadBitmap2D(sPath);
			if (pBmp == NULL) {
				Error("Imagemanager Couldn't load bitmap '%s'\n", sPath.c_str());
				EndLoad();
				return NULL;
			}

			pImage = AddToFrame(pBmp, alFrameHandle);

			hplDelete(pBmp);

			if (pImage == NULL) {
				Error("Imagemanager couldn't create image '%s'\n", asName.c_str());
			}

			if (pImage)
				AddResource(pImage);
		}
	} else {
		// Log("Found '%s' in stock!\n",asName.c_str());
	}

	if (pImage)
		pImage->IncUserCount();
	else
		Error("Couldn't load image '%s'\n", asName.c_str());

	// Log("Loaded image %s, it has %d users!\n", pImage->GetName().c_str(),pImage->GetUserCount());
	// Log(" frame has %d pics\n", pImage->GetFrameTexture()->GetPicCount());

	EndLoad();
	return pImage;
}

//-----------------------------------------------------------------------

cResourceImage *cImageManager::CreateImage(const tString &asName, int alFrameHandle) {
	iResourceBase *pRes = CreateInFrame(asName, alFrameHandle);
	cResourceImage *pImage = static_cast<cResourceImage *>(pRes);

	return pImage;
}

//-----------------------------------------------------------------------

cResourceImage *cImageManager::CreateFromBitmap(const tString &asName, Bitmap2D *apBmp, int alFrameHandle) {
	if (apBmp == NULL)
		return NULL;

	cResourceImage *pImage = AddToFrame(apBmp, alFrameHandle);

	if (pImage) {
		AddResource(pImage, false);
		pImage->IncUserCount();
	}

	return pImage;
}

//-----------------------------------------------------------------------

void cImageManager::Unload(iResourceBase *apResource) {
}

void cImageManager::Destroy(iResourceBase *apResource) {
	// Lower the user num for the the resource. If it is 0 then lower the
	// user num for the TextureFrame and delete the resource. If the Texture
	// frame reaches 0 it is deleted as well.
	cResourceImage *pImage = static_cast<cResourceImage *>(apResource);
	cFrameTexture *pFrame = pImage->GetFrameTexture();
	cFrameBitmap *pBmpFrame = pImage->GetFrameBitmap();

	// pImage->GetFrameBitmap()->FlushToTexture(); Not needed?

	// Log("Users Before: %d\n",pImage->GetUserCount());
	// Log("Framepics Before: %d\n",pFrame->GetPicCount());

	pImage->DecUserCount(); // dec frame count as well.. is that ok?

	// Log("---\n");
	// Log("Destroyed Image: '%s' Users: %d\n",pImage->GetName().c_str(),pImage->GetUserCount());
	// Log("Frame %d has left Pics: %d\n",pFrame,pFrame->GetPicCount());

	if (pImage->HasUsers() == false) {
		pFrame->DecPicCount(); // Doing it here now instead.
		pBmpFrame->DecPicCount();
		RemoveResource(apResource);
		hplDelete(apResource);

		// Log("deleting image and dec fram to %d images!\n",pFrame->GetPicCount());
	}

	if (pFrame->IsEmpty()) {
		// Log(" Deleting frame...");

		// Delete the bitmap frame that has this this frame.
		for (tFrameBitmapListIt it = mlstBitmapFrames.begin(); it != mlstBitmapFrames.end(); ++it) {
			cFrameBitmap *pBmpFrame2 = *it;
			if (pBmpFrame2->GetFrameTexture() == pFrame) {
				// Log("and bitmap...");
				hplDelete(pBmpFrame2);
				mlstBitmapFrames.erase(it);
				break;
			}
		}

		// delete from list
		m_mapTextureFrames.erase(pFrame->GetHandle());
		hplDelete(pFrame);
		// Log(" Deleted frame!\n");
	}
	// Log("---\n");
}

//-----------------------------------------------------------------------

void cImageManager::DeleteAllBitmapFrames() {
	FlushAll();
	for (tFrameBitmapListIt it = mlstBitmapFrames.begin(); it != mlstBitmapFrames.end();) {
		hplDelete(*it);
		it = mlstBitmapFrames.erase(it);
	}
}

//-----------------------------------------------------------------------

int cImageManager::FlushAll() {
	// Log("Flushing...");
	int lNum = 0;
	for (tFrameBitmapListIt it = mlstBitmapFrames.begin(); it != mlstBitmapFrames.end();) {
		if ((*it)->FlushToTexture())
			lNum++;

		if ((*it)->IsFull()) {
			// Do not delete all, probably this struct needs to be here for easy access :S
			// hplDelete(*it);
			// it = mlstBitmapFrames.erase(it);
			it++;
		} else {
			it++;
		}
	}

	// Log("Done!\n");

	return lNum;
}

//-----------------------------------------------------------------------

int cImageManager::CreateFrame(cVector2l avSize) {
	cFrameBitmap *pBFrame = CreateBitmapFrame(avSize);

	if (pBFrame == NULL)
		return -1;

	return pBFrame->GetHandle();
}

//-----------------------------------------------------------------------

void cImageManager::SetFrameLocked(int alHandle, bool abLocked) {
	tFrameBitmapListIt it = mlstBitmapFrames.begin();
	while (it != mlstBitmapFrames.end()) {
		if ((*it)->GetHandle() == alHandle) {
			(*it)->SetLocked(abLocked);
			break;
		}
		it++;
	}
}

//-----------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
//////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------

cResourceImage *cImageManager::FindImage(const tString &asName, tString &asFilePath) {
	cResourceImage *pImage = NULL;

	if (cString::GetFileExt(asName) == "") {
		for (tStringListIt it = mlstFileFormats.begin(); it != mlstFileFormats.end(); ++it) {
			tString sNewName = cString::SetFileExt(asName, *it);
			pImage = static_cast<cResourceImage *>(FindLoadedResource(sNewName, asFilePath));

			if ((pImage == NULL && asFilePath != "") || pImage != NULL)
				break;
		}
	} else {
		pImage = static_cast<cResourceImage *>(FindLoadedResource(asName, asFilePath));
	}

	return pImage;
}

//-----------------------------------------------------------------------

cResourceImage *cImageManager::AddToFrame(Bitmap2D *apBmp, int alFrameHandle) {
	bool bFound = false;
	cResourceImage *pImage = NULL;

	if (mlstBitmapFrames.size() == 0) {
		CreateBitmapFrame(mvFrameSize);
	}

	if (alFrameHandle < 0) {
		// Search the frames til one is find that fits the bitmap
		for (tFrameBitmapListIt it = mlstBitmapFrames.begin(); it != mlstBitmapFrames.end(); it++) {
			if (!(*it)->IsFull() && !(*it)->IsLocked()) {
				pImage = (*it)->AddBitmap(apBmp);
				if (pImage != NULL) {
					bFound = true;
					break;
				}
			}
		}

		// If it fitted in none of the frames, create a new and put it in that.
		if (!bFound) {
			// Log("No fit!\n");
			// not 100% it fits in this one...if so maybe the bitmap size of the frame
			// should be changed?

			// pImage = CreateBitmapFrame(mvFrameSize)->AddBitmap(apBmp);
			cFrameBitmap *pFrame = CreateBitmapFrame(mvFrameSize);
			if (pFrame) {
				pImage = pFrame->AddBitmap(apBmp);
				if (pImage == NULL) {
					Log("No fit in new frame!\n");
				}
			}
		}
	} else {
		tFrameBitmapListIt it = mlstBitmapFrames.begin();
		while (it != mlstBitmapFrames.end()) {
			if ((*it)->GetHandle() == alFrameHandle) {
				pImage = (*it)->AddBitmap(apBmp);
				break;
			}
			it++;
		}
		if (pImage == NULL)
			Error("Image didn't fit frame %d!\n", alFrameHandle);
	}

	return pImage;
}

//-----------------------------------------------------------------------

cFrameBitmap *cImageManager::CreateBitmapFrame(cVector2l avSize) {
	iTexture *pTex = mpLowLevelGraphics->CreateTexture(false, eTextureType_Normal, eTextureTarget_2D);
	cFrameTexture *pTFrame = hplNew(cFrameTexture, (pTex, mlFrameHandle));
	Bitmap2D *pBmp = mpLowLevelGraphics->CreateBitmap2D(avSize);
	cFrameBitmap *pBFrame = hplNew(cFrameBitmap, (pBmp, pTFrame, mlFrameHandle));

	mlstBitmapFrames.push_back(pBFrame);

	auto ret = m_mapTextureFrames.insert(tFrameTextureMap::value_type(mlFrameHandle, pTFrame));
	if (ret.second == false) {
		Error("Could not add texture frame %d with handle %d! Handle already exist!\n", pTFrame, mlFrameHandle);
	} else {
		// Log("Added texture frame: %d\n",pTFrame);
	}

	mlFrameHandle++;
	return pBFrame;
}

//-----------------------------------------------------------------------
} // namespace hpl