[Scummvm-git-logs] scummvm master -> 00e72a17004d393d8f44e4c54aa51c0a7efe75cc

sev- noreply at scummvm.org
Wed Nov 15 15:15:23 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:
e774d8ba96 SLUDGE: Add Z-Buffers
00e72a1700 SLUDGE: Use a byte array instead of a double array for the Z-Buffer


Commit: e774d8ba968a7f011ab919cb5f6a66fab5e4854c
    https://github.com/scummvm/scummvm/commit/e774d8ba968a7f011ab919cb5f6a66fab5e4854c
Author: polyesterswing (kurianjojo2004 at gmail.com)
Date: 2023-11-15T16:15:18+01:00

Commit Message:
SLUDGE: Add Z-Buffers

This commit removes the "SpriteLayer" logic previously used in favour of
a Z-Buffer implementation

Changed paths:
    engines/sludge/freeze.cpp
    engines/sludge/freeze.h
    engines/sludge/graphics.cpp
    engines/sludge/graphics.h
    engines/sludge/sludger.cpp
    engines/sludge/sprites.cpp
    engines/sludge/sprites.h
    engines/sludge/zbuffer.cpp
    engines/sludge/zbuffer.h


diff --git a/engines/sludge/freeze.cpp b/engines/sludge/freeze.cpp
index f373063344b..8d9d6d9d942 100644
--- a/engines/sludge/freeze.cpp
+++ b/engines/sludge/freeze.cpp
@@ -65,10 +65,10 @@ bool GraphicsManager::freeze() {
 	newFreezer->parallaxLayers = _parallaxLayers;
 	_parallaxLayers = NULL;
 
-	newFreezer->zBufferSprites = _zBuffer->sprites;
+	newFreezer->zBufferTex = _zBuffer->tex;
 	newFreezer->zBufferNumber = _zBuffer->originalNum;
 	newFreezer->zPanels = _zBuffer->numPanels;
-	_zBuffer->sprites = NULL;
+	_zBuffer->tex = NULL;
 	// resizeBackdrop kills parallax stuff, light map, z-buffer...
 	if (!killResizeBackdrop(_winWidth, _winHeight))
 		return fatal("Can't create new temporary backdrop buffer");
@@ -138,7 +138,7 @@ void GraphicsManager::unfreeze(bool killImage) {
 	_backdropSurface.copyFrom(_frozenStuff->backdropSurface);
 	_backdropExists = true;
 
-	_zBuffer->sprites = _frozenStuff->zBufferSprites;
+	_zBuffer->tex = _frozenStuff->zBufferTex;
 	killZBuffer();
 	_zBuffer->originalNum = _frozenStuff->zBufferNumber;
 	_zBuffer->numPanels = _frozenStuff->zPanels;
diff --git a/engines/sludge/freeze.h b/engines/sludge/freeze.h
index 351ba15bc8c..0dfc2af6c2f 100644
--- a/engines/sludge/freeze.h
+++ b/engines/sludge/freeze.h
@@ -43,7 +43,7 @@ struct FrozenStuffStruct {
 	ScreenRegionList *allScreenRegions;
 	Graphics::Surface backdropSurface;
 	Graphics::Surface lightMapSurface;
-	Graphics::Surface *zBufferSprites;
+	uint8 *zBufferTex;
 	int zPanels;
 	ParallaxLayers *parallaxLayers;
 	int lightMapNumber, zBufferNumber;
diff --git a/engines/sludge/graphics.cpp b/engines/sludge/graphics.cpp
index 3df902bf588..cd1c45abbbb 100644
--- a/engines/sludge/graphics.cpp
+++ b/engines/sludge/graphics.cpp
@@ -63,17 +63,14 @@ void GraphicsManager::init() {
 	// Back drop
 	_backdropExists = false;
 
-	// Sprites
-	_spriteLayers = new SpriteLayers;
-	_spriteLayers->numLayers = 0;
-
 	// Sprite Bank
 	_allLoadedBanks.clear();
 
 	// ZBuffer
 	_zBuffer = new ZBufferData;
 	_zBuffer->originalNum = -1;
-	_zBuffer->sprites = nullptr;
+	_zBuffer->tex = nullptr;
+	_zBufferSurface = nullptr;
 
 	// Colors
 	_currentBlankColour = _renderSurface.format.ARGBToColor(0xff, 0, 0, 0);
@@ -108,13 +105,6 @@ void GraphicsManager::kill() {
 		killMe = _frozenStuff;
 	}
 
-	// kill sprite layers
-	if (_spriteLayers) {
-		killSpriteLayers();
-		delete _spriteLayers;
-		_spriteLayers = nullptr;
-	}
-
 	// kill sprite banks
 	LoadedSpriteBanks::iterator it;
 	for (it = _allLoadedBanks.begin(); it != _allLoadedBanks.end(); ++it) {
@@ -134,6 +124,11 @@ void GraphicsManager::kill() {
 	if (_renderSurface.getPixels())
 		_renderSurface.free();
 
+	if (_zBufferSurface) {
+		delete[] _zBufferSurface;
+		_zBufferSurface = nullptr;
+	}
+
 	if (_snapshotSurface.getPixels())
 		_snapshotSurface.free();
 
@@ -154,6 +149,8 @@ bool GraphicsManager::initGfx() {
 	initGraphics(_winWidth, _winHeight, _vm->getScreenPixelFormat());
 	_renderSurface.create(_winWidth, _winHeight, *_vm->getScreenPixelFormat());
 
+	_zBufferSurface = new double[_winWidth * _winHeight];
+
 	if (!killResizeBackdrop(_winWidth, _winHeight))
 		return fatal("Couldn't allocate memory for backdrop");
 
diff --git a/engines/sludge/graphics.h b/engines/sludge/graphics.h
index de510b6ba19..5afd070b749 100644
--- a/engines/sludge/graphics.h
+++ b/engines/sludge/graphics.h
@@ -41,7 +41,6 @@ struct LoadedSpriteBank;
 struct OnScreenPerson;
 struct SpriteBank;
 struct Sprite;
-struct SpriteLayers;
 struct VariableStack;
 struct ZBufferData;
 
@@ -149,11 +148,6 @@ public:
 	bool reserveSpritePal(SpritePalette &sP, int n);
 	void burnSpriteToBackDrop(int x1, int y1, Sprite &single, const SpritePalette &fontPal);
 
-	void resetSpriteLayers(ZBufferData *ptrZBuffer, int x, int y, bool upsidedown);
-	void addSpriteDepth(Graphics::Surface *ptr, int depth, int x, int y, Graphics::FLIP_FLAGS flip, int width = -1, int height = -1, bool disposeAfterUse = false, byte trans = 255);
-	void displaySpriteLayers();
-	void killSpriteLayers();
-
 	// Sprite Bank
 	LoadedSpriteBank *loadBankForAnim(int ID);
 
@@ -164,6 +158,9 @@ public:
 	void saveZBuffer(Common::WriteStream *stream);
 	bool loadZBuffer(Common::SeekableReadStream *stream);
 
+	void drawSpriteToZBuffer(int x, int y, double depth, const Graphics::Surface &surface);
+	void fillZBuffer(double d);
+
 	// Colors
 	void setBlankColor(int r, int g, int b) { _currentBlankColour = _renderSurface.format.RGBToColor(r & 255, g & 255, b & 255);};
 	void setBurnColor(int r, int g, int b) {
@@ -211,6 +208,9 @@ private:
 	// renderSurface
 	Graphics::Surface _renderSurface;
 
+	// Z Buffer Surface
+	double *_zBufferSurface = nullptr;
+
 	// Snapshot
 	Graphics::Surface _snapshotSurface;
 
@@ -234,7 +234,6 @@ private:
 	bool reserveBackdrop();
 
 	// Sprites
-	SpriteLayers *_spriteLayers;
 	void fontSprite(bool flip, int x, int y, Sprite &single, const SpritePalette &fontPal);
 	Graphics::Surface *duplicateSurface(Graphics::Surface *surface);
 	void blendColor(Graphics::Surface * surface, uint32 color, Graphics::TSpriteBlendMode mode);
diff --git a/engines/sludge/sludger.cpp b/engines/sludge/sludger.cpp
index 1290ddb8ded..bd0729089bc 100644
--- a/engines/sludge/sludger.cpp
+++ b/engines/sludge/sludger.cpp
@@ -293,7 +293,6 @@ void displayBase() {
 	g_sludge->_gfxMan->drawBackDrop();// Draw Backdrop
 	g_sludge->_gfxMan->drawZBuffer(g_sludge->_gfxMan->getCamX(), g_sludge->_gfxMan->getCamY(), false);
 	g_sludge->_peopleMan->drawPeople();// Then add any moving characters...
-	g_sludge->_gfxMan->displaySpriteLayers();
 }
 
 void sludgeDisplay() {
diff --git a/engines/sludge/sprites.cpp b/engines/sludge/sprites.cpp
index 52bb72295e0..e42f7aad7ee 100644
--- a/engines/sludge/sprites.cpp
+++ b/engines/sludge/sprites.cpp
@@ -271,7 +271,7 @@ void GraphicsManager::pasteSpriteToBackDrop(int x1, int y1, Sprite &single, cons
 	}
 
 	// kill zBuffer
-	if (_zBuffer->originalNum >= 0 && _zBuffer->sprites) {
+	if (_zBuffer->originalNum >= 0 && _zBuffer->tex) {
 		int num = _zBuffer->originalNum;
 		killZBuffer();
 		_zBuffer->originalNum = num;
@@ -294,7 +294,7 @@ void GraphicsManager::burnSpriteToBackDrop(int x1, int y1, Sprite &single, const
 	}
 
 	// kill zBuffer
-	if (_zBuffer->originalNum >= 0 && _zBuffer->sprites) {
+	if (_zBuffer->originalNum >= 0 && _zBuffer->tex) {
 		int num = _zBuffer->originalNum;
 		killZBuffer();
 		_zBuffer->originalNum = num;
@@ -462,6 +462,21 @@ bool GraphicsManager::scaleSprite(Sprite &single, const SpritePalette &fontPal,
 		y2 = y1 + diffY;
 	}
 
+	float z;
+
+	if (useZB && _zBuffer->numPanels) {
+		int i;
+		for (i = 1; i < _zBuffer->numPanels; i++) {
+			if (_zBuffer->panel[i] >= y + _cameraY) {
+				i--;
+				break;
+			}
+		}
+		z = 0.999 - (double)i * (1.0 / 128.0);
+	} else {
+		z = -0.5;
+	}
+
 	Graphics::Surface *blitted = &single.surface;
 	Graphics::Surface *ptr = applyLightmapToSprite(blitted, thisPerson, mirror, x, y, x1, y1, diffX, diffY);
 
@@ -476,8 +491,14 @@ bool GraphicsManager::scaleSprite(Sprite &single, const SpritePalette &fontPal,
 			ptr = nullptr;
 		}
 	} else {
-		int d = useZB ? y + _cameraY : (y + _cameraY > _sceneHeight * 0.6 ? _sceneHeight + 1 : 0);
-		addSpriteDepth(blitted, d, x1, y1, (mirror ? Graphics::FLIP_H : Graphics::FLIP_NONE), diffX, diffY, ptr, 255 - thisPerson->transparency);
+
+		// TODO: you dont need to copy the whole render surface, just the part to which the sprite may be drawn
+		Graphics::ManagedSurface scaled(&_renderSurface, DisposeAfterUse::NO);
+
+		Graphics::ManagedSurface tmp(blitted, DisposeAfterUse::NO);
+		tmp.blendBlitTo(scaled, x1, y1, (mirror ? Graphics::FLIP_H : Graphics::FLIP_NONE), nullptr, MS_ARGB(255 - thisPerson->transparency, 255, 255, 255), diffX, diffY);
+
+		drawSpriteToZBuffer(0, 0, z, scaled.rawSurface());
 	}
 
 	// Are we pointing at the sprite?
@@ -498,61 +519,6 @@ bool GraphicsManager::scaleSprite(Sprite &single, const SpritePalette &fontPal,
 	return false;
 }
 
-void GraphicsManager::resetSpriteLayers(ZBufferData *pz, int x, int y, bool upsidedown) {
-	if (_spriteLayers->numLayers > 0)
-		killSpriteLayers();
-	_spriteLayers->numLayers = pz->numPanels;
-	debugC(3, kSludgeDebugZBuffer, "%i zBuffer layers", _spriteLayers->numLayers);
-	for (int i = 0; i < _spriteLayers->numLayers; ++i) {
-		SpriteDisplay *node = new SpriteDisplay(x, y, (upsidedown ? Graphics::FLIP_V : Graphics::FLIP_NONE), &pz->sprites[i], pz->sprites[i].w, pz->sprites[i].h);
-		_spriteLayers->layer[i].push_back(node);
-		debugC(3, kSludgeDebugZBuffer, "Layer %i is of depth %i", i, pz->panel[i]);
-	}
-}
-
-void GraphicsManager::addSpriteDepth(Graphics::Surface *ptr, int depth, int x, int y, Graphics::FLIP_FLAGS flip, int width, int height, bool freeAfterUse, byte trans) {
-	int i;
-	for (i = 1; i < _zBuffer->numPanels; ++i) {
-		if (_zBuffer->panel[i] >= depth) {
-			break;
-		}
-	}
-	--i;
-	debugC(3, kSludgeDebugZBuffer, "Add sprite of Y-value : %i in layer %i trans: %02x", depth, i, trans);
-
-	SpriteDisplay *node = new SpriteDisplay(x, y, flip, ptr, width, height, freeAfterUse, trans);
-	_spriteLayers->layer[i].push_back(node);
-}
-
-void GraphicsManager::displaySpriteLayers() {
-	for (int i = 0; i < _spriteLayers->numLayers; ++i) {
-		debugC(3, kSludgeDebugGraphics, "Display layer %i with %i sprites", i, _spriteLayers->layer[i].size());
-		SpriteLayer::iterator it;
-		for (it = _spriteLayers->layer[i].begin(); it != _spriteLayers->layer[i].end(); ++it) {
-			Graphics::ManagedSurface tmp((*it)->surface, DisposeAfterUse::NO);
-			tmp.blendBlitTo(_renderSurface, (*it)->x, (*it)->y, (*it)->flip, nullptr, MS_ARGB((*it)->transparency, 255, 255, 255), (*it)->width, (*it)->height);
-		}
-	}
-	killSpriteLayers();
-}
-
-void GraphicsManager::killSpriteLayers() {
-	for (int i = 0; i < _spriteLayers->numLayers; ++i) {
-		SpriteLayer::iterator it;
-		for (it = _spriteLayers->layer[i].begin(); it != _spriteLayers->layer[i].end(); ++it) {
-			if ((*it)->freeAfterUse) {
-				(*it)->surface->free();
-				delete (*it)->surface;
-				(*it)->surface = nullptr;
-			}
-			delete (*it);
-			(*it) = nullptr;
-		}
-		_spriteLayers->layer[i].clear();
-	}
-	_spriteLayers->numLayers = 0;
-}
-
 // Paste a scaled sprite onto the backdrop
 void GraphicsManager::fixScaleSprite(int x, int y, Sprite &single, const SpritePalette &fontPal, OnScreenPerson *thisPerson, int camX, int camY, bool mirror) {
 
@@ -571,6 +537,21 @@ void GraphicsManager::fixScaleSprite(int x, int y, Sprite &single, const SpriteP
 		x1 = x - (int)((mirror ? (float)(single.surface.w - (single.xhot + 1)) : (float)single.xhot) * scale);
 	int y1 = y - (int)((single.yhot - thisPerson->floaty) * scale);
 
+	float z;
+
+	if (useZB && _zBuffer->numPanels) {
+		int i;
+		for (i = 1; i < _zBuffer->numPanels; i++) {
+			if (_zBuffer->panel[i] >= y + _cameraY) {
+				i--;
+				break;
+			}
+		}
+		z = 0.999 - (double)i * (1.0 / 128.0);
+	} else {
+		z = -0.5;
+	}
+
 	Graphics::Surface *blitted = &single.surface;
 	Graphics::Surface *ptr = applyLightmapToSprite(blitted, thisPerson, mirror, x, y, x1, y1, diffX, diffY);
 
@@ -593,12 +574,14 @@ void GraphicsManager::fixScaleSprite(int x, int y, Sprite &single, const SpriteP
 			ptr = nullptr;
 		}
 	} else {
-		int d = useZB ? y + _cameraY : (y + _cameraY > _sceneHeight * 0.6 ? _sceneHeight + 1 : 0);
-		addSpriteDepth(&single.surface, d, x1, y1, (mirror ? Graphics::FLIP_H : Graphics::FLIP_NONE), diffX, diffY, ptr, 255 - thisPerson->transparency);
-	}
+		Graphics::ManagedSurface scaled(&_renderSurface, DisposeAfterUse::NO);
+
+		Graphics::ManagedSurface tmp(blitted, DisposeAfterUse::NO);
+		tmp.blendBlitTo(scaled, x1, y1, (mirror ? Graphics::FLIP_H : Graphics::FLIP_NONE), nullptr, MS_ARGB(255 - thisPerson->transparency, 255, 255, 255), diffX, diffY);
 
-	// draw all
-	displaySpriteLayers();
+		drawSpriteToZBuffer(0, 0, z, scaled.rawSurface());
+
+	}
 
 	// copy screen to backdrop
 	_backdropSurface.copyFrom(_renderSurface);
diff --git a/engines/sludge/sprites.h b/engines/sludge/sprites.h
index 105998139a7..e2e3341ceae 100644
--- a/engines/sludge/sprites.h
+++ b/engines/sludge/sprites.h
@@ -97,13 +97,6 @@ struct SpriteDisplay {
 	}
 };
 
-// All sprites are sorted into different "layers" (up to 16) according to their relative y position to z-buffer zones
-typedef Common::List<SpriteDisplay *> SpriteLayer;
-struct SpriteLayers {
-	int numLayers;
-	SpriteLayer layer[16];
-};
-
 } // End of namespace Sludge
 
 #endif
diff --git a/engines/sludge/zbuffer.cpp b/engines/sludge/zbuffer.cpp
index 8786ae68833..09e919bf61a 100644
--- a/engines/sludge/zbuffer.cpp
+++ b/engines/sludge/zbuffer.cpp
@@ -27,16 +27,17 @@
 #include "sludge/sludge.h"
 #include "sludge/zbuffer.h"
 
+#include "graphics/managed_surface.h"
+
 namespace Sludge {
 
 void GraphicsManager::killZBuffer() {
-	if (_zBuffer->sprites) {
-		for (int i = 0; i < _zBuffer->numPanels; ++i) {
-			_zBuffer->sprites[i].free();
-		}
-		delete []_zBuffer->sprites;
-		_zBuffer->sprites = nullptr;
+
+	if (_zBuffer->tex) {
+		delete[] _zBuffer->tex;
+		_zBuffer->tex = nullptr;
 	}
+
 	_zBuffer->numPanels = 0;
 	_zBuffer->originalNum = -1;
 }
@@ -71,7 +72,7 @@ bool GraphicsManager::setZBuffer(int num) {
 
 	debug (kSludgeDebugGraphics, "Setting zBuffer");
 	uint32 stillToGo = 0;
-	int yPalette[16], sorted[16];
+	int yPalette[16], sorted[16], sortback[16];
 
 	killZBuffer();
 
@@ -93,23 +94,22 @@ bool GraphicsManager::setZBuffer(int num) {
 	if (readStream->readByte() != 'b')
 		return fatal("Not a Z-buffer file");
 
-	uint width, height;
 	switch (readStream->readByte()) {
 		case 0:
-			width = 640;
-			height = 480;
+			_zBuffer->width = 640;
+			_zBuffer->height = 480;
 			break;
 
 		case 1:
-			width = readStream->readUint16BE();
-			height = readStream->readUint16BE();
+			_zBuffer->width = readStream->readUint16BE();
+			_zBuffer->height = readStream->readUint16BE();
 			break;
 
 		default:
 			return fatal("Extended Z-buffer format not supported in this version of the SLUDGE engine");
 	}
-	if (width != _sceneWidth || height != _sceneHeight) {
-		Common::String tmp = Common::String::format("Z-w: %d Z-h:%d w: %d, h:%d", width, height, _sceneWidth, _sceneHeight);
+	if (_zBuffer->width != _sceneWidth || _zBuffer->height != _sceneHeight) {
+		Common::String tmp = Common::String::format("Z-w: %d Z-h:%d w: %d, h:%d", _zBuffer->width, _zBuffer->height, _sceneWidth, _sceneHeight);
 		return fatal("Z-buffer width and height don't match scene width and height", tmp);
 	}
 
@@ -121,18 +121,15 @@ bool GraphicsManager::setZBuffer(int num) {
 	sortZPal(yPalette, sorted, _zBuffer->numPanels);
 	for (int y = 0; y < _zBuffer->numPanels; y++) {
 		_zBuffer->panel[y] = yPalette[sorted[y]];
+		sortback[sorted[y]] = y;
 		debugC(2, kSludgeDebugZBuffer, "Y-value : %i", _zBuffer->panel[y]);
 	}
 
 	int picWidth = _sceneWidth;
 	int picHeight = _sceneHeight;
 
-	_zBuffer->sprites = nullptr;
-	_zBuffer->sprites = new Graphics::Surface[_zBuffer->numPanels];
-
-	for (int i = 0; i < _zBuffer->numPanels; ++i) {
-		_zBuffer->sprites[i].create(picWidth, picHeight, *g_sludge->getScreenPixelFormat());
-	}
+	_zBuffer->tex = nullptr;
+	_zBuffer->tex = new uint8[picHeight * picWidth];
 
 	int n = 0;
 
@@ -148,21 +145,8 @@ bool GraphicsManager::setZBuffer(int num) {
 				n &= 15;
 			}
 
-			for (int i = 0; i < _zBuffer->numPanels; ++i) {
-				byte *target = (byte *)_zBuffer->sprites[i].getBasePtr(x, y);
-				if (sorted[i] == n || i == 0) {
-					byte *source = (byte *)_backdropSurface.getBasePtr(x, y);
-					target[0] = source[0];
-					target[1] = source[1];
-					target[2] = source[2];
-					target[3] = source[3];
-				} else {
-					target[0] = 0;
-					target[1] = 0;
-					target[2] = 0;
-					target[3] = 0;
-				}
-			}
+			_zBuffer->tex[y*picWidth + x] = sortback[n];
+
 			stillToGo--;
 		}
 	}
@@ -170,29 +154,67 @@ bool GraphicsManager::setZBuffer(int num) {
 	g_sludge->_resMan->finishAccess();
 	setResourceForFatal(-1);
 
-	if (!g_sludge->_dumpScripts)
-		return true;
+	return true;
+}
 
-	// Debug code to output light map image
+void GraphicsManager::fillZBuffer(double d) {
+	for (uint y = 0; y < _winHeight; y++) {
+		for (uint x = 0; x < _winWidth; x++) {
+			_zBufferSurface[y*_winWidth + x] = d;
+		}
+	}
+}
 
-	for (int i = 0; i < _zBuffer->numPanels; ++i) {
-		Common::DumpFile *outFile = new Common::DumpFile();
+void GraphicsManager::drawSpriteToZBuffer(int x, int y, double depth, const Graphics::Surface &surface) {
 
-		outFile->open(Common::String::format("dumps/zbuffer%04d-%d.png", num, i));
-		Image::writePNG(*outFile, _zBuffer->sprites[i]);
-		outFile->finalize();
-		outFile->close();
-		delete outFile;
-	}
+	for (uint y1 = 0; y1 < (uint)surface.h; y1++) {
+		for (uint x1 = 0; x1 < (uint)surface.w; x1++) {
+			if (x1 + x >= _sceneWidth || y1 + y >= _sceneHeight) {
+				continue;
+			}
 
-	return true;
+			byte *target = (byte *)_renderSurface.getBasePtr(x1 + x, y1 + y);
+			const byte *source = (const byte *)surface.getBasePtr(x1, y1);
+
+			if (depth <= _zBufferSurface[(y1 + y) * _sceneWidth + (x1 + x)]) {
+
+				if (source[0] == 0xff) {
+					// Completely opaque, so copy RGB values over
+					target[0] = 0xff;
+					target[1] = source[1];
+					target[2] = source[2];
+					target[3] = source[3];
+
+				}
+			}
+		}
+	}
 }
 
 void GraphicsManager::drawZBuffer(int x, int y, bool upsidedown) {
-	if (!_zBuffer->numPanels || !_zBuffer->sprites)
+	if (!_zBuffer->numPanels || !_zBuffer->tex)
 		return;
 
-	g_sludge->_gfxMan->resetSpriteLayers(_zBuffer, x, y, upsidedown);
+	fillZBuffer(1.0);
+	int i;
+
+	for (uint y1 = y; y1 < _zBuffer->height + y; y1++) {
+		for (uint x1 = x; x1 < _zBuffer->width + x; x1++) {
+
+			double z = 1.0f;
+
+			if (upsidedown) {
+				z = 1.0 - (double)_zBuffer->tex[(_zBuffer->height - y1) * _zBuffer->width + x1] * (1.0 / 128.0);
+			} else {
+				z = 1.0 - (double)_zBuffer->tex[y1 * _zBuffer->width + x1] * (1.0 / 128.0);
+			}
+
+			if ( z < _zBufferSurface[y1 * _winWidth + x1]) {
+				_zBufferSurface[y1 * _winWidth + x1] = z;
+			}
+
+		}
+	}
 }
 
 void GraphicsManager::saveZBuffer(Common::WriteStream *stream) {
diff --git a/engines/sludge/zbuffer.h b/engines/sludge/zbuffer.h
index 24634eeacfa..5aa14795f5e 100644
--- a/engines/sludge/zbuffer.h
+++ b/engines/sludge/zbuffer.h
@@ -25,9 +25,12 @@ namespace Sludge {
 
 struct ZBufferData {
 //	bool loaded;
+	uint width, height;
 	int numPanels;
 	int panel[16];
 	int originalNum;
+
+	uint8 *tex;
 	Graphics::Surface *sprites;
 };
 


Commit: 00e72a17004d393d8f44e4c54aa51c0a7efe75cc
    https://github.com/scummvm/scummvm/commit/00e72a17004d393d8f44e4c54aa51c0a7efe75cc
Author: polyesterswing (kurianjojo2004 at gmail.com)
Date: 2023-11-15T16:15:18+01:00

Commit Message:
SLUDGE: Use a byte array instead of a double array for the Z-Buffer

Changed paths:
    engines/sludge/graphics.cpp
    engines/sludge/graphics.h
    engines/sludge/sprites.cpp
    engines/sludge/zbuffer.cpp


diff --git a/engines/sludge/graphics.cpp b/engines/sludge/graphics.cpp
index cd1c45abbbb..ace5e3bce3a 100644
--- a/engines/sludge/graphics.cpp
+++ b/engines/sludge/graphics.cpp
@@ -149,7 +149,7 @@ bool GraphicsManager::initGfx() {
 	initGraphics(_winWidth, _winHeight, _vm->getScreenPixelFormat());
 	_renderSurface.create(_winWidth, _winHeight, *_vm->getScreenPixelFormat());
 
-	_zBufferSurface = new double[_winWidth * _winHeight];
+	_zBufferSurface = new uint8[_winWidth * _winHeight];
 
 	if (!killResizeBackdrop(_winWidth, _winHeight))
 		return fatal("Couldn't allocate memory for backdrop");
diff --git a/engines/sludge/graphics.h b/engines/sludge/graphics.h
index 5afd070b749..f7c773a6c97 100644
--- a/engines/sludge/graphics.h
+++ b/engines/sludge/graphics.h
@@ -158,8 +158,8 @@ public:
 	void saveZBuffer(Common::WriteStream *stream);
 	bool loadZBuffer(Common::SeekableReadStream *stream);
 
-	void drawSpriteToZBuffer(int x, int y, double depth, const Graphics::Surface &surface);
-	void fillZBuffer(double d);
+	void drawSpriteToZBuffer(int x, int y, uint8 depth, const Graphics::Surface &surface);
+	void fillZBuffer(uint8 d);
 
 	// Colors
 	void setBlankColor(int r, int g, int b) { _currentBlankColour = _renderSurface.format.RGBToColor(r & 255, g & 255, b & 255);};
@@ -209,7 +209,7 @@ private:
 	Graphics::Surface _renderSurface;
 
 	// Z Buffer Surface
-	double *_zBufferSurface = nullptr;
+	uint8 *_zBufferSurface = nullptr;
 
 	// Snapshot
 	Graphics::Surface _snapshotSurface;
diff --git a/engines/sludge/sprites.cpp b/engines/sludge/sprites.cpp
index e42f7aad7ee..79d112fcbda 100644
--- a/engines/sludge/sprites.cpp
+++ b/engines/sludge/sprites.cpp
@@ -462,7 +462,7 @@ bool GraphicsManager::scaleSprite(Sprite &single, const SpritePalette &fontPal,
 		y2 = y1 + diffY;
 	}
 
-	float z;
+	uint8 z;
 
 	if (useZB && _zBuffer->numPanels) {
 		int i;
@@ -472,9 +472,9 @@ bool GraphicsManager::scaleSprite(Sprite &single, const SpritePalette &fontPal,
 				break;
 			}
 		}
-		z = 0.999 - (double)i * (1.0 / 128.0);
+		z = ((i + 1) * 2) + 1;
 	} else {
-		z = -0.5;
+		z = 0xFF;
 	}
 
 	Graphics::Surface *blitted = &single.surface;
@@ -537,7 +537,7 @@ void GraphicsManager::fixScaleSprite(int x, int y, Sprite &single, const SpriteP
 		x1 = x - (int)((mirror ? (float)(single.surface.w - (single.xhot + 1)) : (float)single.xhot) * scale);
 	int y1 = y - (int)((single.yhot - thisPerson->floaty) * scale);
 
-	float z;
+	uint8 z;
 
 	if (useZB && _zBuffer->numPanels) {
 		int i;
@@ -547,9 +547,9 @@ void GraphicsManager::fixScaleSprite(int x, int y, Sprite &single, const SpriteP
 				break;
 			}
 		}
-		z = 0.999 - (double)i * (1.0 / 128.0);
+		z = ((i + 1) * 2) + 1;
 	} else {
-		z = -0.5;
+		z = 0xFF;
 	}
 
 	Graphics::Surface *blitted = &single.surface;
diff --git a/engines/sludge/zbuffer.cpp b/engines/sludge/zbuffer.cpp
index 09e919bf61a..d36d9c03ccb 100644
--- a/engines/sludge/zbuffer.cpp
+++ b/engines/sludge/zbuffer.cpp
@@ -157,15 +157,11 @@ bool GraphicsManager::setZBuffer(int num) {
 	return true;
 }
 
-void GraphicsManager::fillZBuffer(double d) {
-	for (uint y = 0; y < _winHeight; y++) {
-		for (uint x = 0; x < _winWidth; x++) {
-			_zBufferSurface[y*_winWidth + x] = d;
-		}
-	}
+void GraphicsManager::fillZBuffer(uint8 d) {
+	memset(_zBufferSurface, d, _winHeight * _winWidth);
 }
 
-void GraphicsManager::drawSpriteToZBuffer(int x, int y, double depth, const Graphics::Surface &surface) {
+void GraphicsManager::drawSpriteToZBuffer(int x, int y, uint8 depth, const Graphics::Surface &surface) {
 
 	for (uint y1 = 0; y1 < (uint)surface.h; y1++) {
 		for (uint x1 = 0; x1 < (uint)surface.w; x1++) {
@@ -176,7 +172,7 @@ void GraphicsManager::drawSpriteToZBuffer(int x, int y, double depth, const Grap
 			byte *target = (byte *)_renderSurface.getBasePtr(x1 + x, y1 + y);
 			const byte *source = (const byte *)surface.getBasePtr(x1, y1);
 
-			if (depth <= _zBufferSurface[(y1 + y) * _sceneWidth + (x1 + x)]) {
+			if (depth > _zBufferSurface[(y1 + y) * _sceneWidth + (x1 + x)]) {
 
 				if (source[0] == 0xff) {
 					// Completely opaque, so copy RGB values over
@@ -195,23 +191,22 @@ void GraphicsManager::drawZBuffer(int x, int y, bool upsidedown) {
 	if (!_zBuffer->numPanels || !_zBuffer->tex)
 		return;
 
-	fillZBuffer(1.0);
-	int i;
+	fillZBuffer(0);
 
 	for (uint y1 = y; y1 < _zBuffer->height + y; y1++) {
 		for (uint x1 = x; x1 < _zBuffer->width + x; x1++) {
 
-			double z = 1.0f;
+			uint8 z = 0;
 
 			if (upsidedown) {
-				z = 1.0 - (double)_zBuffer->tex[(_zBuffer->height - y1) * _zBuffer->width + x1] * (1.0 / 128.0);
+				z = (_zBuffer->tex[(_zBuffer->height - y1) * _zBuffer->width + x1] + 1) * 2;
 			} else {
-				z = 1.0 - (double)_zBuffer->tex[y1 * _zBuffer->width + x1] * (1.0 / 128.0);
+				z = (_zBuffer->tex[y1 * _zBuffer->width + x1] + 1) * 2;
 			}
 
-			if ( z < _zBufferSurface[y1 * _winWidth + x1]) {
+
+			if ( z > _zBufferSurface[y1 * _winWidth + x1])
 				_zBufferSurface[y1 * _winWidth + x1] = z;
-			}
 
 		}
 	}




More information about the Scummvm-git-logs mailing list