[Scummvm-git-logs] scummvm master -> 214b3bd8c1eb43f953aeaca7670f26ca91b19b92

sev- noreply at scummvm.org
Mon Dec 18 17:17:19 UTC 2023


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
821666e210 KINGDOM: Avoid loading entire files into memory
214b3bd8c1 KINGDOM: Free allocated memory on exit


Commit: 821666e210ec905e2e83cd64b4256a13ce89f74e
    https://github.com/scummvm/scummvm/commit/821666e210ec905e2e83cd64b4256a13ce89f74e
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-12-18T18:17:15+01:00

Commit Message:
KINGDOM: Avoid loading entire files into memory

Changed paths:
    engines/kingdom/kingdom.cpp
    engines/kingdom/kingdom.h


diff --git a/engines/kingdom/kingdom.cpp b/engines/kingdom/kingdom.cpp
index f145d804697..330220dec28 100644
--- a/engines/kingdom/kingdom.cpp
+++ b/engines/kingdom/kingdom.cpp
@@ -130,10 +130,6 @@ void KingdomGame::initVariables() {
 	_daelonCntr = 0;
 	_sound = false;
 	_asMode = false;
-	for (int i = 0; i < 510; i++) {
-		_rezPointers[i] = nullptr;
-		_rezSize[i] = 0;
-	}
 	_mouseDebound = false;
 	_mouseButton = 0;
 	_cursorDrawn = false;
@@ -341,8 +337,7 @@ void KingdomGame::fadeToBlack2() {
 }
 
 void KingdomGame::loadKingArt() {
-	loadAResource(0x97);
-	Common::SeekableReadStream *kingartStream = _rezPointers[0x97];
+	Common::SeekableReadStream *kingartStream = loadAResource(0x97);
 	int val = kingartStream->readUint32LE();
 	int size = val / 4;
 	uint32 *kingartIdx = new uint32[size + 1];
@@ -363,43 +358,39 @@ void KingdomGame::loadKingArt() {
 		kingartStream->read(_kingartEntries[i]._data, chunkSize - 2);
 	}
 
+	delete kingartStream;
 	delete[] kingartIdx;
 }
 
-void KingdomGame::loadAResource(int reznum) {
+Common::SeekableReadStream *KingdomGame::loadAResource(int reznum) {
 	Common::String path = Common::String(_rezNames[reznum]);
 	path.toUppercase();
 
 	debug("Loading resource: %i (%s)\n", reznum, path.c_str());
 
-	if(!_rezSize[reznum]) {
-		Common::File *file = new Common::File();
-		if(!file->open(path))
-			warning("Failed to open %s", path.c_str());
-		else {
-			_rezSize[reznum] = file->size();
-			file->seek(0, SEEK_SET);
-			_rezPointers[reznum] = file->readStream(_rezSize[reznum]);
-			file->close();
-		}
-		delete file;
-	}
-}
-
-void KingdomGame::releaseAResource(int reznum) {
-	if (_rezSize[reznum]) {
-		delete _rezPointers[reznum];
-		_rezSize[reznum] = 0;
+	Common::File *file = new Common::File();
+	if(!file->open(path)) {
+		warning("Failed to open %s", path.c_str());
+		return nullptr;
+	} else {
+		return file;
 	}
 }
 
 void KingdomGame::showPic(int reznum) {
 	eraseCursor();
 
-	loadAResource(reznum);
+	Common::SeekableReadStream *stream = loadAResource(reznum);
+	if (!stream)
+		return;
+
 	Image::IFFDecoder decoder;
-	if (!_rezPointers[reznum] || !decoder.loadStream(*_rezPointers[reznum]))
+	if (!decoder.loadStream(*stream)) {
+		delete stream;
 		return;
+	}
+
+	delete stream;
 
 	const byte *palette = decoder.getPalette();
 	int paletteColorCount = decoder.getPaletteColorCount();
@@ -419,8 +410,6 @@ void KingdomGame::showPic(int reznum) {
 	}
 	g_system->unlockScreen();
 	g_system->updateScreen();
-
-	releaseAResource(reznum);
 }
 
 void KingdomGame::fShowPic(int reznum) {
@@ -922,7 +911,6 @@ void KingdomGame::playSound(int idx) {
 	// Stop Sound
 	if (_mixer->isSoundHandleActive(_soundHandle)) {
 		_mixer->stopHandle(_soundHandle);
-		releaseAResource(idx);
 	}
 
 	_soundNumber = idx;
@@ -931,10 +919,9 @@ void KingdomGame::playSound(int idx) {
 
 	int realIdx = _soundNumber + 200; // Or +250, depending in the original on the sound card
 	debug("PlaySound %d : %s", idx, _rezNames[realIdx]);
-	loadAResource(realIdx);
 
-	Common::SeekableReadStream *soundStream = _rezPointers[realIdx];
-	Audio::RewindableAudioStream *rewindableStream = Audio::makeRawStream(soundStream, 22050, Audio::FLAG_UNSIGNED | Audio::FLAG_LITTLE_ENDIAN, DisposeAfterUse::NO);
+	Common::SeekableReadStream *soundStream = loadAResource(realIdx);
+	Audio::RewindableAudioStream *rewindableStream = Audio::makeRawStream(soundStream, 22050, Audio::FLAG_UNSIGNED | Audio::FLAG_LITTLE_ENDIAN, DisposeAfterUse::YES);
 	_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, Audio::Mixer::kMaxMixerVolume);
 	_mixer->playStream(Audio::Mixer::kMusicSoundType, &_soundHandle, rewindableStream);
 //  In the original, there's an array describing whether a sound should loop or not.
@@ -1086,11 +1073,18 @@ void KingdomGame::processMapInput(int mapNum) {
 
 void KingdomGame::drawPic(int reznum) {
 	eraseCursor();
-	loadAResource(reznum);
+
+	Common::SeekableReadStream *stream = loadAResource(reznum);
+	if (!stream)
+		return;
 
 	Image::IFFDecoder decoder;
-	if (!decoder.loadStream(*_rezPointers[reznum]))
+	if (!decoder.loadStream(*stream)) {
+		delete stream;
 		return;
+	}
+
+	delete stream;
 
 	const Graphics::Surface *surface = decoder.getSurface();
 
@@ -1106,8 +1100,6 @@ void KingdomGame::drawPic(int reznum) {
 	}
 	g_system->unlockScreen();
 	g_system->updateScreen();
-
-	releaseAResource(reznum);
 }
 
 void KingdomGame::displayIcon(int reznum) {
diff --git a/engines/kingdom/kingdom.h b/engines/kingdom/kingdom.h
index 192c346a793..b1508450b78 100644
--- a/engines/kingdom/kingdom.h
+++ b/engines/kingdom/kingdom.h
@@ -174,8 +174,6 @@ namespace Kingdom {
 		int _tickCount;
 		uint32 _oldTime;
 
-		Common::SeekableReadStream *_rezPointers[510];
-		int _rezSize[510];
 		int _iconPic[7];
 		uint16 _userInput;
 		uint16 _mouseButton;
@@ -188,8 +186,7 @@ namespace Kingdom {
 		void initHelp();
 		void fadeToBlack1();
 		void fadeToBlack2();
-		void loadAResource(int reznum);
-		void releaseAResource(int reznum);
+		Common::SeekableReadStream *loadAResource(int reznum);
 		void showPic(int reznum);
 		void fShowPic(int reznum);
 		void initCursor();


Commit: 214b3bd8c1eb43f953aeaca7670f26ca91b19b92
    https://github.com/scummvm/scummvm/commit/214b3bd8c1eb43f953aeaca7670f26ca91b19b92
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2023-12-18T18:17:15+01:00

Commit Message:
KINGDOM: Free allocated memory on exit

Changed paths:
    engines/kingdom/kingdom.cpp
    engines/kingdom/kingdom.h


diff --git a/engines/kingdom/kingdom.cpp b/engines/kingdom/kingdom.cpp
index 330220dec28..0316878723f 100644
--- a/engines/kingdom/kingdom.cpp
+++ b/engines/kingdom/kingdom.cpp
@@ -57,6 +57,7 @@ KingdomGame::KingdomGame(OSystem *syst, const ADGameDescription *gameDesc) : Eng
 	_quit = false;
 	_demoMovieSkipped = false;
 	_kingartEntries = nullptr;
+	_kingartCount = 0;
 
 	_tickCount = 0;
 	_oldTime = g_system->getMillis();
@@ -141,6 +142,9 @@ void KingdomGame::initVariables() {
 }
 
 KingdomGame::~KingdomGame() {
+	unloadKingArt();
+
+	delete[] _asPtr;
 	delete _logic;
 	delete _rnd;
 }
@@ -342,6 +346,7 @@ void KingdomGame::loadKingArt() {
 	int size = val / 4;
 	uint32 *kingartIdx = new uint32[size + 1];
 	_kingartEntries = new KingArtEntry[size];
+	_kingartCount = size;
 	kingartIdx[0] = val;
 	for (int i = 1; i < size; i++)
 		kingartIdx[i] = kingartStream->readUint32LE();
@@ -362,6 +367,19 @@ void KingdomGame::loadKingArt() {
 	delete[] kingartIdx;
 }
 
+void KingdomGame::unloadKingArt() {
+	if (!_kingartEntries)
+		return;
+
+	for (uint32 i = 0; i < _kingartCount; i++) {
+		delete[] _kingartEntries[i]._data;
+	}
+
+	delete[] _kingartEntries;
+	_kingartEntries = 0;
+	_kingartCount = 0;
+}
+
 Common::SeekableReadStream *KingdomGame::loadAResource(int reznum) {
 	Common::String path = Common::String(_rezNames[reznum]);
 	path.toUppercase();
diff --git a/engines/kingdom/kingdom.h b/engines/kingdom/kingdom.h
index b1508450b78..37ea2f7ac8b 100644
--- a/engines/kingdom/kingdom.h
+++ b/engines/kingdom/kingdom.h
@@ -104,6 +104,8 @@ namespace Kingdom {
 		Logic *_logic;
 
 		KingArtEntry *_kingartEntries;
+		uint32 _kingartCount;
+
 		void displayDebugHotSpots();
 
 	public:
@@ -220,6 +222,7 @@ namespace Kingdom {
 		void drawCursor();
 		void cursorType();
 		void loadKingArt();
+		void unloadKingArt();
 		void setCursor(int cursor);
 		int getAKey();
 		int checkMouseMapAS();




More information about the Scummvm-git-logs mailing list