[Scummvm-git-logs] scummvm master -> 97c163d35183e606335fc99fc8e0c034d7fbbc38

mgerhardy martin.gerhardy at gmail.com
Sat Feb 20 17:23:44 UTC 2021


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

Summary:
41067b2f04 TWINE: minor optimzations in the renderer code
da2b660f6c TWINE: minor optimizations in computeBoundingBox
e609ac4930 TWINE: minor optimization in renderer code
b8dc840cc7 TWINE: render loop optimization for renderPolygonsDither
d3b5ee1d08 TWINE: render loop optimization for renderPolygonsGouraud
2faca6fac1 TWINE: less casting in computeBoundingBox
7b4ca38336 TWINE: optimizations by reducing calls to AnimData::loadFromBuffer
6bf6c93cc4 TWINE: optimized renderPolygonsCopper
2c94ef1f0a TWINE: optimized renderPolygonsBopper
c05b3a8ac9 TWINE: optimized renderPolygonsFlat
33e983651a TWINE: optimized renderPolygonTrame
51932eec27 TWINE: extract to local variable
97c163d351 TWINE: rewrite loop


Commit: 41067b2f04a1eddd2ddaf1cb93583adb2db81e67
    https://github.com/scummvm/scummvm/commit/41067b2f04a1eddd2ddaf1cb93583adb2db81e67
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: minor optimzations in the renderer code

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 95110429bc..309ecb839e 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -394,6 +394,11 @@ void Renderer::computePolygons(int16 polyRenderType, const Vertex *vertices, int
 	uint8 vertexParam1 = vertices[numVertices - 1].colorIndex;
 	int16 currentVertexX = vertices[numVertices - 1].x;
 	int16 currentVertexY = vertices[numVertices - 1].y;
+	const int16 *polyTabBegin = _polyTab;
+	const int16 *polyTabEnd = &_polyTab[_polyTabSize - 1];
+	const int16 *polyTab2Begin = _polyTab2;
+	const int16 *polyTab2End = &_polyTab2[_polyTabSize - 1];
+	const int screenHeight = _engine->height();
 
 	for (int32 nVertex = 0; nVertex < numVertices; nVertex++) {
 		const int16 oldVertexY = currentVertexY;
@@ -433,17 +438,15 @@ void Renderer::computePolygons(int16 polyRenderType, const Vertex *vertices, int
 			cvalue = (oldVertexParam * 256) + ((vertexParam2 - oldVertexParam) * 256) % vsize;
 			cdelta = ((vertexParam2 - oldVertexParam) * 256) / vsize;
 		}
-		const int32 polyTabIndex = ypos + (up ? _engine->height() : 0);
+		const int32 polyTabIndex = ypos + (up ? screenHeight : 0);
 		int16 *outPtr = &_polyTab[polyTabIndex]; // outPtr is the output ptr in the renderTab
 
 		float slope = (float)hsize / (float)vsize;
 		slope = up ? -slope : slope;
 
-		for (int32 i = 0; i < vsize + 2; i++) {
-			if (outPtr - _polyTab < _polyTabSize) {
-				if (outPtr - _polyTab > 0) {
-					*outPtr = xpos;
-				}
+		for (int16 i = 0; i < vsize + 2; i++) {
+			if (outPtr >= polyTabBegin && outPtr <= polyTabEnd) {
+				*outPtr = xpos;
 			}
 			outPtr += direction;
 			xpos += slope;
@@ -452,11 +455,9 @@ void Renderer::computePolygons(int16 polyRenderType, const Vertex *vertices, int
 		if (polyRenderType >= POLYGONTYPE_GOURAUD) { // we must compute the color progression
 			int16 *outPtr2 = &_polyTab2[polyTabIndex];
 
-			for (int32 i = 0; i < vsize + 2; i++) {
-				if (outPtr2 - _polyTab2 < _polyTabSize) {
-					if (outPtr2 - _polyTab2 > 0) {
-						*outPtr2 = cvalue;
-					}
+			for (int16 i = 0; i < vsize + 2; i++) {
+				if (outPtr2 >= polyTab2Begin && outPtr2 <= polyTab2End) {
+					*outPtr2 = cvalue;
 				}
 				outPtr2 += direction;
 				cvalue += cdelta;
@@ -468,10 +469,13 @@ void Renderer::computePolygons(int16 polyRenderType, const Vertex *vertices, int
 void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	int32 currentLine = vtop;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
+
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
+		if (currentLine >= 0 && currentLine < screenHeight) {
 			int16 start = ptr1[0];
-			int16 stop = ptr1[_engine->height()];
+			int16 stop = ptr1[screenHeight];
 
 			ptr1++;
 			int32 hsize = stop - start;
@@ -489,7 +493,7 @@ void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 col
 					start += mask;
 					start = (start & 0xFF00) | ((start & 0xFF) & (uint8)(dx / 256));
 					start = (start & 0xFF00) | ((start & 0xFF) + (dx & 0xFF));
-					if (j >= 0 && j < _engine->width()) {
+					if (j >= 0 && j < screenWidth) {
 						out[j] = start & 0xFF;
 					}
 					mask = (mask * 4) | (mask / SCENE_SIZE_HALF);
@@ -497,7 +501,7 @@ void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 col
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--vsize);
 }
@@ -505,25 +509,28 @@ void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 col
 void Renderer::renderPolygonsBopper(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	int32 currentLine = vtop;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
+
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
+		if (currentLine >= 0 && currentLine < screenHeight) {
 			int16 start = ptr1[0];
-			int16 stop = ptr1[_engine->height()];
+			int16 stop = ptr1[screenHeight];
 			ptr1++;
 			int32 hsize = stop - start;
 
 			if (hsize >= 0) {
 				hsize++;
 				for (int32 j = start; j < hsize + start; j++) {
-					if ((start + (vtop % 1)) & 1) {
-						if (j >= 0 && j < _engine->width()) {
+					if (start & 1) {
+						if (j >= 0 && j < screenWidth) {
 							out[j] = color;
 						}
 					}
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--vsize);
 }
@@ -531,23 +538,26 @@ void Renderer::renderPolygonsBopper(uint8 *out, int vtop, int32 vsize, int32 col
 void Renderer::renderPolygonsFlat(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	int32 currentLine = vtop;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
+
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
+		if (currentLine >= 0 && currentLine < screenHeight) {
 			int16 start = ptr1[0];
-			int16 stop = ptr1[_engine->height()];
+			int16 stop = ptr1[screenHeight];
 			ptr1++;
 			int32 hsize = stop - start;
 
 			if (hsize >= 0) {
 				hsize++;
 				for (int32 j = start; j < hsize + start; j++) {
-					if (j >= 0 && j < _engine->width()) {
+					if (j >= 0 && j < screenWidth) {
 						out[j] = color;
 					}
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--vsize);
 }
@@ -555,6 +565,9 @@ void Renderer::renderPolygonsFlat(uint8 *out, int vtop, int32 vsize, int32 color
 void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	int bx = (uint16)color << 16;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
+
 	int32 renderLoop = vsize;
 	do {
 		int16 start;
@@ -562,7 +575,7 @@ void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color
 		int32 hsize;
 		while (1) {
 			start = ptr1[0];
-			stop = ptr1[_engine->height()];
+			stop = ptr1[screenHeight];
 			ptr1++;
 			hsize = stop - start;
 
@@ -575,7 +588,7 @@ void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color
 
 			color = *(out2 + 1);
 
-			out += _engine->width();
+			out += screenWidth;
 
 			--renderLoop;
 			if (!renderLoop) {
@@ -626,7 +639,7 @@ void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color
 			}
 		}
 
-		out += _engine->width();
+		out += screenWidth;
 		--renderLoop;
 
 	} while (renderLoop);
@@ -635,11 +648,14 @@ void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color
 // FIXME: buggy
 void Renderer::renderPolygonsTras(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
+
 	do {
 		unsigned short int bx;
 
 		int16 start = ptr1[0];
-		int16 stop = ptr1[_engine->height()];
+		int16 stop = ptr1[screenHeight];
 
 		ptr1++;
 		int32 hsize = stop - start;
@@ -660,7 +676,7 @@ void Renderer::renderPolygonsTras(uint8 *out, int vtop, int32 vsize, int32 color
 				out2++;
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 	} while (--vsize);
 }
 
@@ -668,12 +684,14 @@ void Renderer::renderPolygonsTras(uint8 *out, int vtop, int32 vsize, int32 color
 void Renderer::renderPolygonTrame(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	unsigned char bh = 0;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
 
 	int32 currentLine = vtop;
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
+		if (currentLine >= 0 && currentLine < screenHeight) {
 			int16 start = ptr1[0];
-			int16 stop = ptr1[_engine->height()];
+			int16 stop = ptr1[screenHeight];
 			ptr1++;
 			int32 hsize = stop - start;
 
@@ -698,7 +716,7 @@ void Renderer::renderPolygonTrame(uint8 *out, int vtop, int32 vsize, int32 color
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--vsize);
 }


Commit: da2b660f6c0b87454b3c97eabdbcc8ebe8e66a1f
    https://github.com/scummvm/scummvm/commit/da2b660f6c0b87454b3c97eabdbcc8ebe8e66a1f
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: minor optimizations in computeBoundingBox

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 309ecb839e..edd1ed1296 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -377,16 +377,17 @@ FORCEINLINE int16 clamp(int16 x, int16 a, int16 b) {
 void Renderer::computeBoundingBox(Vertex *vertices, int32 numVertices, int &vleft, int &vright, int &vtop, int &vbottom) const {
 	vleft = vtop = SCENE_SIZE_MAX;
 	vright = vbottom = SCENE_SIZE_MIN;
+	const int maxWidth = _engine->width() - 1;
+	const int maxHeight = _engine->height() - 1;
 
 	for (int32 i = 0; i < numVertices; i++) {
-		vertices[i].x = clamp(vertices[i].x, 0, _engine->width() - 1);
-		vertices[i].y = clamp(vertices[i].y, 0, _engine->height() - 1);
-		const int vertexX = vertices[i].x;
-		vleft = MIN(vleft, vertexX);
-		vright = MAX(vright, vertexX);
-		const int vertexY = vertices[i].y;
-		vtop = MIN(vtop, vertexY);
-		vbottom = MAX(vbottom, vertexY);
+		vertices[i].x = clamp(vertices[i].x, 0, maxWidth);
+		vleft = MIN<int>(vleft, vertices[i].x);
+		vright = MAX<int>(vright, vertices[i].x);
+
+		vertices[i].y = clamp(vertices[i].y, 0, maxHeight);
+		vtop = MIN<int>(vtop, vertices[i].y);
+		vbottom = MAX<int>(vbottom, vertices[i].y);
 	}
 }
 


Commit: e609ac49307569f97b1949978f82d57f36dfde56
    https://github.com/scummvm/scummvm/commit/e609ac49307569f97b1949978f82d57f36dfde56
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: minor optimization in renderer code

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index edd1ed1296..883e332a4a 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -727,21 +727,23 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 	const int16 *ptr2 = &_polyTab2[vtop];
 	int32 renderLoop = vsize;
 	int32 currentLine = vtop;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
+		if (currentLine >= 0 && currentLine < screenHeight) {
 			uint16 startColor = ptr2[0];
-			uint16 stopColor = ptr2[_engine->height()];
+			uint16 stopColor = ptr2[screenHeight];
 
 			int16 colorSize = stopColor - startColor;
 
-			int16 stop = ptr1[_engine->height()]; // stop
+			int16 stop = ptr1[screenHeight]; // stop
 			int16 start = ptr1[0];            // start
 
 			ptr1++;
 			uint8 *out2 = start + out;
 			int32 hsize = stop - start;
 
-			//varf2 = ptr2[_engine->height()];
+			//varf2 = ptr2[screenHeight];
 			//varf3 = ptr2[0];
 
 			ptr2++;
@@ -749,28 +751,28 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 			//varf4 = (float)((int32)varf2 - (int32)varf3);
 
 			if (hsize == 0) {
-				if (start >= 0 && start < _engine->width()) {
+				if (start >= 0 && start < screenWidth) {
 					*out2 = ((startColor + stopColor) / 2) / 256; // moyenne des 2 couleurs
 				}
 			} else if (hsize > 0) {
 				if (hsize == 1) {
-					if (start >= -1 && start < _engine->width() - 1) {
+					if (start >= -1 && start < screenWidth - 1) {
 						*(out2 + 1) = stopColor / 256;
 					}
 
-					if (start >= 0 && start < _engine->width()) {
+					if (start >= 0 && start < screenWidth) {
 						*(out2) = startColor / 256;
 					}
 				} else if (hsize == 2) {
-					if (start >= -2 && start < _engine->width() - 2) {
+					if (start >= -2 && start < screenWidth - 2) {
 						*(out2 + 2) = stopColor / 256;
 					}
 
-					if (start >= -1 && start < _engine->width() - 1) {
+					if (start >= -1 && start < screenWidth - 1) {
 						*(out2 + 1) = ((startColor + stopColor) / 2) / 256;
 					}
 
-					if (start >= 0 && start < _engine->width()) {
+					if (start >= 0 && start < screenWidth) {
 						*(out2) = startColor / 256;
 					}
 				} else {
@@ -780,7 +782,7 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 
 					if (hsize % 2) {
 						hsize /= 2;
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2) = startColor / 256;
 						}
 						out2++;
@@ -791,14 +793,14 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 					}
 
 					do {
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2) = startColor / 256;
 						}
 
 						currentXPos++;
 						startColor += colorSize;
 
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2 + 1) = startColor / 256;
 						}
 
@@ -809,7 +811,7 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--renderLoop);
 }
@@ -818,25 +820,26 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 	const int16 *ptr1 = &_polyTab[vtop];
 	const int16 *ptr2 = &_polyTab2[vtop];
 	int32 renderLoop = vsize;
-
 	int32 currentLine = vtop;
+	const int screenWidth = _engine->width();
+	const int screenHeight = _engine->height();
 	do {
-		if (currentLine >= 0 && currentLine < _engine->height()) {
-			int16 stop = ptr1[_engine->height()]; // stop
+		if (currentLine >= 0 && currentLine < screenHeight) {
+			int16 stop = ptr1[screenHeight]; // stop
 			int16 start = ptr1[0];            // start
 			ptr1++;
 			int32 hsize = stop - start;
 
 			if (hsize >= 0) {
 				uint16 startColor = ptr2[0];
-				uint16 stopColor = ptr2[_engine->height()];
+				uint16 stopColor = ptr2[screenHeight];
 				int32 currentXPos = start;
 
 				uint8 *out2 = start + out;
 				ptr2++;
 
 				if (hsize == 0) {
-					if (currentXPos >= 0 && currentXPos < _engine->width()) {
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
 						*(out2) = (uint8)(((startColor + stopColor) / 2) / 256);
 					}
 				} else {
@@ -848,7 +851,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 
 						currentColor &= 0xFF;
 						currentColor += startColor;
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2) = currentColor / 256;
 						}
 
@@ -858,7 +861,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 						currentColor += startColor;
 
 						currentXPos++;
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2 + 1) = currentColor / 256;
 						}
 					} else if (hsize == 2) {
@@ -870,7 +873,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 						colorSize /= 2;
 						currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
 						currentColor += startColor;
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2) = currentColor / 256;
 						}
 
@@ -881,7 +884,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 						currentColor &= 0xFF;
 						currentColor += startColor;
 
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2) = currentColor / 256;
 						}
 
@@ -891,7 +894,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 						currentColor += startColor;
 
 						currentXPos++;
-						if (currentXPos >= 0 && currentXPos < _engine->width()) {
+						if (currentXPos >= 0 && currentXPos < screenWidth) {
 							*(out2 + 1) = currentColor / 256;
 						}
 					} else {
@@ -904,7 +907,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 							currentColor &= 0xFF;
 							currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
 							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < _engine->width()) {
+							if (currentXPos >= 0 && currentXPos < screenWidth) {
 								*(out2) = currentColor / 256;
 							}
 							out2++;
@@ -916,7 +919,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 						do {
 							currentColor &= 0xFF;
 							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < _engine->width()) {
+							if (currentXPos >= 0 && currentXPos < screenWidth) {
 								*(out2) = currentColor / 256;
 							}
 							currentXPos++;
@@ -924,7 +927,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 							startColor += colorSize;
 							currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
 							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < _engine->width()) {
+							if (currentXPos >= 0 && currentXPos < screenWidth) {
 								*(out2 + 1) = currentColor / 256;
 							}
 							currentXPos++;
@@ -935,7 +938,7 @@ void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 col
 				}
 			}
 		}
-		out += _engine->width();
+		out += screenWidth;
 		currentLine++;
 	} while (--renderLoop);
 }


Commit: b8dc840cc7cd856e7ecc03081a3fffa6f5a92d94
    https://github.com/scummvm/scummvm/commit/b8dc840cc7cd856e7ecc03081a3fffa6f5a92d94
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: render loop optimization for renderPolygonsDither

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 883e332a4a..38da06120f 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -819,128 +819,133 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	const int16 *ptr2 = &_polyTab2[vtop];
-	int32 renderLoop = vsize;
-	int32 currentLine = vtop;
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			int16 stop = ptr1[screenHeight]; // stop
-			int16 start = ptr1[0];            // start
-			ptr1++;
-			int32 hsize = stop - start;
 
-			if (hsize >= 0) {
-				uint16 startColor = ptr2[0];
-				uint16 stopColor = ptr2[screenHeight];
-				int32 currentXPos = start;
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		int16 stop = ptr1[screenHeight];  // stop
+		int16 start = ptr1[0];            // start
+		ptr1++;
+		int32 hsize = stop - start;
+		if (hsize < 0) {
+			out += screenWidth;
+			continue;
+		}
+		uint16 startColor = ptr2[0];
+		uint16 stopColor = ptr2[screenHeight];
+		int32 currentXPos = start;
 
-				uint8 *out2 = start + out;
-				ptr2++;
+		uint8 *out2 = start + out;
+		ptr2++;
 
-				if (hsize == 0) {
-					if (currentXPos >= 0 && currentXPos < screenWidth) {
-						*(out2) = (uint8)(((startColor + stopColor) / 2) / 256);
-					}
-				} else {
-					int16 colorSize = stopColor - startColor;
-					if (hsize == 1) {
-						uint16 currentColor = startColor;
-						hsize++;
-						hsize /= 2;
+		if (hsize == 0) {
+			if (currentXPos >= 0 && currentXPos < screenWidth) {
+				*(out2) = (uint8)(((startColor + stopColor) / 2) / 256);
+			}
+		} else {
+			int16 colorSize = stopColor - startColor;
+			if (hsize == 1) {
+				uint16 currentColor = startColor;
+				hsize++;
+				hsize /= 2;
 
-						currentColor &= 0xFF;
-						currentColor += startColor;
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2) = currentColor / 256;
-						}
+				currentColor &= 0xFF;
+				currentColor += startColor;
+				if (currentXPos >= 0 && currentXPos < screenWidth) {
+					*(out2) = currentColor / 256;
+				}
 
-						currentColor &= 0xFF;
-						startColor += colorSize;
-						currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
-						currentColor += startColor;
+				currentColor &= 0xFF;
+				startColor += colorSize;
+				currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
+				currentColor += startColor;
 
-						currentXPos++;
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2 + 1) = currentColor / 256;
-						}
-					} else if (hsize == 2) {
-						uint16 currentColor = startColor;
-						hsize++;
-						hsize /= 2;
+				currentXPos++;
+				if (currentXPos >= 0 && currentXPos < screenWidth) {
+					*(out2 + 1) = currentColor / 256;
+				}
+			} else if (hsize == 2) {
+				uint16 currentColor = startColor;
+				hsize++;
+				hsize /= 2;
 
-						currentColor &= 0xFF;
-						colorSize /= 2;
-						currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
-						currentColor += startColor;
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2) = currentColor / 256;
-						}
+				currentColor &= 0xFF;
+				colorSize /= 2;
+				currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
+				currentColor += startColor;
+				if (currentXPos >= 0 && currentXPos < screenWidth) {
+					*(out2) = currentColor / 256;
+				}
 
-						out2++;
-						currentXPos++;
-						startColor += colorSize;
+				out2++;
+				currentXPos++;
+				startColor += colorSize;
 
-						currentColor &= 0xFF;
-						currentColor += startColor;
+				currentColor &= 0xFF;
+				currentColor += startColor;
 
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2) = currentColor / 256;
-						}
+				if (currentXPos >= 0 && currentXPos < screenWidth) {
+					*(out2) = currentColor / 256;
+				}
 
-						currentColor &= 0xFF;
-						startColor += colorSize;
-						currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
-						currentColor += startColor;
+				currentColor &= 0xFF;
+				startColor += colorSize;
+				currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
+				currentColor += startColor;
 
-						currentXPos++;
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2 + 1) = currentColor / 256;
-						}
-					} else {
-						uint16 currentColor = startColor;
-						colorSize /= hsize;
-						hsize++;
-
-						if (hsize % 2) {
-							hsize /= 2;
-							currentColor &= 0xFF;
-							currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
-							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < screenWidth) {
-								*(out2) = currentColor / 256;
-							}
-							out2++;
-							currentXPos++;
-						} else {
-							hsize /= 2;
-						}
+				currentXPos++;
+				if (currentXPos >= 0 && currentXPos < screenWidth) {
+					*(out2 + 1) = currentColor / 256;
+				}
+			} else {
+				uint16 currentColor = startColor;
+				colorSize /= hsize;
+				hsize++;
 
-						do {
-							currentColor &= 0xFF;
-							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < screenWidth) {
-								*(out2) = currentColor / 256;
-							}
-							currentXPos++;
-							currentColor &= 0xFF;
-							startColor += colorSize;
-							currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
-							currentColor += startColor;
-							if (currentXPos >= 0 && currentXPos < screenWidth) {
-								*(out2 + 1) = currentColor / 256;
-							}
-							currentXPos++;
-							out2 += 2;
-							startColor += colorSize;
-						} while (--hsize);
+				if (hsize % 2) {
+					hsize /= 2;
+					currentColor &= 0xFF;
+					currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
+					currentColor += startColor;
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
+						*(out2) = currentColor / 256;
 					}
+					out2++;
+					currentXPos++;
+				} else {
+					hsize /= 2;
 				}
+
+				do {
+					currentColor &= 0xFF;
+					currentColor += startColor;
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
+						*(out2) = currentColor / 256;
+					}
+					currentXPos++;
+					currentColor &= 0xFF;
+					startColor += colorSize;
+					currentColor = ((currentColor & (0xFF00)) | ((((currentColor & 0xFF) << (hsize & 0xFF))) & 0xFF));
+					currentColor += startColor;
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
+						*(out2 + 1) = currentColor / 256;
+					}
+					currentXPos++;
+					out2 += 2;
+					startColor += colorSize;
+				} while (--hsize);
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--renderLoop);
+	}
 }
 
 void Renderer::renderPolygonsMarble(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: d3b5ee1d0852a031444a6a81b80a7c40e8f9b111
    https://github.com/scummvm/scummvm/commit/d3b5ee1d0852a031444a6a81b80a7c40e8f9b111
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: render loop optimization for renderPolygonsGouraud

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 38da06120f..9da4c76445 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -725,95 +725,98 @@ void Renderer::renderPolygonTrame(uint8 *out, int vtop, int32 vsize, int32 color
 void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
 	const int16 *ptr2 = &_polyTab2[vtop];
-	int32 renderLoop = vsize;
-	int32 currentLine = vtop;
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			uint16 startColor = ptr2[0];
-			uint16 stopColor = ptr2[screenHeight];
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		uint16 startColor = ptr2[0];
+		uint16 stopColor = ptr2[screenHeight];
 
-			int16 colorSize = stopColor - startColor;
+		int16 colorSize = stopColor - startColor;
 
-			int16 stop = ptr1[screenHeight]; // stop
-			int16 start = ptr1[0];            // start
+		int16 stop = ptr1[screenHeight]; // stop
+		int16 start = ptr1[0];            // start
 
-			ptr1++;
-			uint8 *out2 = start + out;
-			int32 hsize = stop - start;
+		ptr1++;
+		uint8 *out2 = start + out;
+		int32 hsize = stop - start;
 
-			//varf2 = ptr2[screenHeight];
-			//varf3 = ptr2[0];
+		//varf2 = ptr2[screenHeight];
+		//varf3 = ptr2[0];
 
-			ptr2++;
+		ptr2++;
+
+		//varf4 = (float)((int32)varf2 - (int32)varf3);
 
-			//varf4 = (float)((int32)varf2 - (int32)varf3);
+		if (hsize == 0) {
+			if (start >= 0 && start < screenWidth) {
+				*out2 = ((startColor + stopColor) / 2) / 256; // moyenne des 2 couleurs
+			}
+		} else if (hsize > 0) {
+			if (hsize == 1) {
+				if (start >= -1 && start < screenWidth - 1) {
+					*(out2 + 1) = stopColor / 256;
+				}
 
-			if (hsize == 0) {
 				if (start >= 0 && start < screenWidth) {
-					*out2 = ((startColor + stopColor) / 2) / 256; // moyenne des 2 couleurs
+					*(out2) = startColor / 256;
+				}
+			} else if (hsize == 2) {
+				if (start >= -2 && start < screenWidth - 2) {
+					*(out2 + 2) = stopColor / 256;
 				}
-			} else if (hsize > 0) {
-				if (hsize == 1) {
-					if (start >= -1 && start < screenWidth - 1) {
-						*(out2 + 1) = stopColor / 256;
-					}
 
-					if (start >= 0 && start < screenWidth) {
-						*(out2) = startColor / 256;
-					}
-				} else if (hsize == 2) {
-					if (start >= -2 && start < screenWidth - 2) {
-						*(out2 + 2) = stopColor / 256;
-					}
+				if (start >= -1 && start < screenWidth - 1) {
+					*(out2 + 1) = ((startColor + stopColor) / 2) / 256;
+				}
 
-					if (start >= -1 && start < screenWidth - 1) {
-						*(out2 + 1) = ((startColor + stopColor) / 2) / 256;
-					}
+				if (start >= 0 && start < screenWidth) {
+					*(out2) = startColor / 256;
+				}
+			} else {
+				int32 currentXPos = start;
+				colorSize /= hsize;
+				hsize++;
 
-					if (start >= 0 && start < screenWidth) {
+				if (hsize % 2) {
+					hsize /= 2;
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
 						*(out2) = startColor / 256;
 					}
+					out2++;
+					currentXPos++;
+					startColor += colorSize;
 				} else {
-					int32 currentXPos = start;
-					colorSize /= hsize;
-					hsize++;
-
-					if (hsize % 2) {
-						hsize /= 2;
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2) = startColor / 256;
-						}
-						out2++;
-						currentXPos++;
-						startColor += colorSize;
-					} else {
-						hsize /= 2;
-					}
+					hsize /= 2;
+				}
 
-					do {
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2) = startColor / 256;
-						}
+				do {
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
+						*(out2) = startColor / 256;
+					}
 
-						currentXPos++;
-						startColor += colorSize;
+					currentXPos++;
+					startColor += colorSize;
 
-						if (currentXPos >= 0 && currentXPos < screenWidth) {
-							*(out2 + 1) = startColor / 256;
-						}
+					if (currentXPos >= 0 && currentXPos < screenWidth) {
+						*(out2 + 1) = startColor / 256;
+					}
 
-						currentXPos++;
-						out2 += 2;
-						startColor += colorSize;
-					} while (--hsize);
-				}
+					currentXPos++;
+					out2 += 2;
+					startColor += colorSize;
+				} while (--hsize);
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--renderLoop);
+	}
 }
 
 void Renderer::renderPolygonsDither(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: 2faca6fac108644898adb3dd9afa82b04cb9cfc0
    https://github.com/scummvm/scummvm/commit/2faca6fac108644898adb3dd9afa82b04cb9cfc0
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:45+01:00

Commit Message:
TWINE: less casting in computeBoundingBox

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 9da4c76445..dc09d60fe9 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -377,8 +377,8 @@ FORCEINLINE int16 clamp(int16 x, int16 a, int16 b) {
 void Renderer::computeBoundingBox(Vertex *vertices, int32 numVertices, int &vleft, int &vright, int &vtop, int &vbottom) const {
 	vleft = vtop = SCENE_SIZE_MAX;
 	vright = vbottom = SCENE_SIZE_MIN;
-	const int maxWidth = _engine->width() - 1;
-	const int maxHeight = _engine->height() - 1;
+	const int16 maxWidth = _engine->width() - 1;
+	const int16 maxHeight = _engine->height() - 1;
 
 	for (int32 i = 0; i < numVertices; i++) {
 		vertices[i].x = clamp(vertices[i].x, 0, maxWidth);


Commit: 7b4ca383360642a506a8c5441b926e5e529fd033
    https://github.com/scummvm/scummvm/commit/7b4ca383360642a506a8c5441b926e5e529fd033
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: optimizations by reducing calls to AnimData::loadFromBuffer

Changed paths:
    engines/twine/holomap.cpp
    engines/twine/menu/menu.cpp
    engines/twine/renderer/redraw.cpp
    engines/twine/scene/animations.cpp
    engines/twine/scene/animations.h
    engines/twine/scene/gamestate.cpp


diff --git a/engines/twine/holomap.cpp b/engines/twine/holomap.cpp
index dc2ff09bdb..e89b7d2533 100644
--- a/engines/twine/holomap.cpp
+++ b/engines/twine/holomap.cpp
@@ -27,6 +27,7 @@
 #include "common/types.h"
 #include "twine/audio/sound.h"
 #include "twine/menu/interface.h"
+#include "twine/parser/anim.h"
 #include "twine/renderer/redraw.h"
 #include "twine/renderer/renderer.h"
 #include "twine/renderer/screens.h"
@@ -338,9 +339,11 @@ void Holomap::drawHolomapTrajectory(int32 trajectoryIndex) {
 
 	_engine->flip();
 	ActorMoveStruct move;
-	AnimTimerDataStruct animData;
+	AnimTimerDataStruct animTimerData;
 	uint8 *animPtr = nullptr;
-	HQR::getAllocEntry(&animPtr, Resources::HQR_RESS_FILE, data.getAnimation());
+	const int32 animSize = HQR::getAllocEntry(&animPtr, Resources::HQR_RESS_FILE, data.getAnimation());
+	AnimData animData;
+	animData.loadFromBuffer(animPtr, animSize);
 	uint8 *modelPtr = nullptr;
 	HQR::getAllocEntry(&modelPtr, Resources::HQR_RESS_FILE, data.getModel());
 	Renderer::prepareIsoModel(modelPtr);
@@ -370,7 +373,7 @@ void Holomap::drawHolomapTrajectory(int32 trajectoryIndex) {
 			_engine->_movements->setActorAngleSafe(ANGLE_0, -ANGLE_90, 500, &move);
 		}
 
-		if (_engine->_animations->setModelAnimation(frameNumber, animPtr, modelPtr, &animData)) {
+		if (_engine->_animations->setModelAnimation(frameNumber, animData, animPtr, modelPtr, &animTimerData)) {
 			frameNumber++;
 			if (frameNumber >= _engine->_animations->getNumKeyframes(animPtr)) {
 				frameNumber = _engine->_animations->getStartKeyframe(animPtr);
diff --git a/engines/twine/menu/menu.cpp b/engines/twine/menu/menu.cpp
index 68c90cf738..fbe9a75d87 100644
--- a/engines/twine/menu/menu.cpp
+++ b/engines/twine/menu/menu.cpp
@@ -893,10 +893,13 @@ bool Menu::isBehaviourHovered(HeroBehaviourType behaviour) const {
 void Menu::drawBehaviour(HeroBehaviourType behaviour, int32 angle, bool cantDrawBox, Common::Rect &dirtyRect) {
 	const Common::Rect &boxRect = calcBehaviourRect(behaviour);
 
-	const uint8 *currentAnim = _engine->_resources->animTable[_engine->_actor->heroAnimIdx[(byte)behaviour]];
+	const int animIdx = _engine->_actor->heroAnimIdx[(byte)behaviour];
+	const uint8 *currentAnim = _engine->_resources->animTable[animIdx];
+	const AnimData &currentAnimData = _engine->_resources->animData[animIdx];
+
 	int16 currentAnimState = behaviourAnimState[(byte)behaviour];
 
-	if (_engine->_animations->setModelAnimation(currentAnimState, currentAnim, behaviourEntity, &behaviourAnimData[(byte)behaviour])) {
+	if (_engine->_animations->setModelAnimation(currentAnimState, currentAnimData, currentAnim, behaviourEntity, &behaviourAnimData[(byte)behaviour])) {
 		currentAnimState++; // keyframe
 		if (currentAnimState >= _engine->_animations->getNumKeyframes(currentAnim)) {
 			currentAnimState = _engine->_animations->getStartKeyframe(currentAnim);
diff --git a/engines/twine/renderer/redraw.cpp b/engines/twine/renderer/redraw.cpp
index 100e2d0402..25096685c6 100644
--- a/engines/twine/renderer/redraw.cpp
+++ b/engines/twine/renderer/redraw.cpp
@@ -345,7 +345,9 @@ void Redraw::processDrawListShadows(const DrawListStruct &drawCmd) {
 void Redraw::processDrawListActors(const DrawListStruct &drawCmd, bool bgRedraw) {
 	const int32 actorIdx = drawCmd.actorIdx;
 	ActorStruct *actor = _engine->_scene->getActor(actorIdx);
-	_engine->_animations->setModelAnimation(actor->animPosition, _engine->_resources->animTable[actor->previousAnimIdx], _engine->_actor->bodyTable[actor->entity], &actor->animTimerData);
+	const uint8 *animPtr = _engine->_resources->animTable[actor->previousAnimIdx];
+	const AnimData &animData = _engine->_resources->animData[actor->previousAnimIdx];
+	_engine->_animations->setModelAnimation(actor->animPosition, animData, animPtr, _engine->_actor->bodyTable[actor->entity], &actor->animTimerData);
 
 	const int32 x = actor->x - _engine->_grid->cameraX;
 	const int32 y = actor->y - _engine->_grid->cameraY;
diff --git a/engines/twine/scene/animations.cpp b/engines/twine/scene/animations.cpp
index 8618b8e093..16e7116516 100644
--- a/engines/twine/scene/animations.cpp
+++ b/engines/twine/scene/animations.cpp
@@ -127,12 +127,10 @@ int32 Animations::getAnimMode(uint8 *ptr, const uint8 *keyFramePtr) {
 	return opcode;
 }
 
-bool Animations::setModelAnimation(int32 keyframeIdx, const uint8 *animPtr, uint8 *const bodyPtr, AnimTimerDataStruct *animTimerDataPtr) {
+bool Animations::setModelAnimation(int32 keyframeIdx, const AnimData &animData, const uint8 *animPtr, uint8 *const bodyPtr, AnimTimerDataStruct *animTimerDataPtr) {
 	if (!Model::isAnimated(bodyPtr)) {
 		return false;
 	}
-	AnimData animData;
-	animData.loadFromBuffer(animPtr, 100000);
 	const KeyFrame *keyFrame = animData.getKeyframe(keyframeIdx);
 
 	currentStepX = keyFrame->x;
diff --git a/engines/twine/scene/animations.h b/engines/twine/scene/animations.h
index c9e451948c..eef3157c5f 100644
--- a/engines/twine/scene/animations.h
+++ b/engines/twine/scene/animations.h
@@ -101,7 +101,7 @@ public:
 	 * @param bodyPtr Body model poitner
 	 * @param animTimerDataPtr Animation time data
 	 */
-	bool setModelAnimation(int32 keyframeIdx, const uint8 *animPtr, uint8 *const bodyPtr, AnimTimerDataStruct *animTimerDataPtr);
+	bool setModelAnimation(int32 keyframeIdx, const AnimData &animData, const uint8 *animPtr, uint8 *const bodyPtr, AnimTimerDataStruct *animTimerDataPtr);
 
 	/**
 	 * Get entity anim index (This is taken from File3D entities)
diff --git a/engines/twine/scene/gamestate.cpp b/engines/twine/scene/gamestate.cpp
index 46019d73fb..1407e5a989 100644
--- a/engines/twine/scene/gamestate.cpp
+++ b/engines/twine/scene/gamestate.cpp
@@ -358,7 +358,8 @@ void GameState::processFoundItem(int32 item) {
 	_engine->_text->initVoxToPlay(item);
 
 	const int32 bodyAnimIdx = _engine->_animations->getBodyAnimIndex(AnimationTypes::kFoundItem);
-	uint8 *currentAnim = _engine->_resources->animTable[bodyAnimIdx];
+	const uint8 *currentAnim = _engine->_resources->animTable[bodyAnimIdx];
+	const AnimData &currentAnimData = _engine->_resources->animData[bodyAnimIdx];
 
 	AnimTimerDataStruct tmpAnimTimer = _engine->_scene->sceneHero->animTimerData;
 
@@ -388,7 +389,7 @@ void GameState::processFoundItem(int32 item) {
 		_engine->_interface->resetClip();
 		initEngineProjections();
 
-		if (_engine->_animations->setModelAnimation(currentAnimState, currentAnim, _engine->_actor->bodyTable[_engine->_scene->sceneHero->entity], &_engine->_scene->sceneHero->animTimerData)) {
+		if (_engine->_animations->setModelAnimation(currentAnimState, currentAnimData, currentAnim, _engine->_actor->bodyTable[_engine->_scene->sceneHero->entity], &_engine->_scene->sceneHero->animTimerData)) {
 			currentAnimState++; // keyframe
 			if (currentAnimState >= _engine->_animations->getNumKeyframes(currentAnim)) {
 				currentAnimState = _engine->_animations->getStartKeyframe(currentAnim);


Commit: 6bf6c93cc4003b7a2d81e1b4312b7e0c60ad101f
    https://github.com/scummvm/scummvm/commit/6bf6c93cc4003b7a2d81e1b4312b7e0c60ad101f
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: optimized renderPolygonsCopper

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index dc09d60fe9..5b12b567b5 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -469,42 +469,46 @@ void Renderer::computePolygons(int16 polyRenderType, const Vertex *vertices, int
 
 void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
-	int32 currentLine = vtop;
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
 
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			int16 start = ptr1[0];
-			int16 stop = ptr1[screenHeight];
-
-			ptr1++;
-			int32 hsize = stop - start;
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		int16 start = ptr1[0];
+		int16 stop = ptr1[screenHeight];
 
-			if (hsize >= 0) {
-				uint16 mask = 0x43DB;
+		ptr1++;
+		int32 hsize = stop - start;
 
-				uint16 dx = (uint8)color;
-				dx |= 0x300;
+		if (hsize >= 0) {
+			uint16 mask = 0x43DB;
 
-				hsize++;
-				const int32 startCopy = start;
+			uint16 dx = (uint8)color;
+			dx |= 0x300;
 
-				for (int32 j = startCopy; j < hsize + startCopy; j++) {
-					start += mask;
-					start = (start & 0xFF00) | ((start & 0xFF) & (uint8)(dx / 256));
-					start = (start & 0xFF00) | ((start & 0xFF) + (dx & 0xFF));
-					if (j >= 0 && j < screenWidth) {
-						out[j] = start & 0xFF;
-					}
-					mask = (mask * 4) | (mask / SCENE_SIZE_HALF);
-					mask++;
+			hsize++;
+			const int32 startCopy = start;
+
+			for (int32 j = startCopy; j < hsize + startCopy; j++) {
+				start += mask;
+				start = (start & 0xFF00) | ((start & 0xFF) & (uint8)(dx / 256));
+				start = (start & 0xFF00) | ((start & 0xFF) + (dx & 0xFF));
+				if (j >= 0 && j < screenWidth) {
+					out[j] = start & 0xFF;
 				}
+				mask = (mask * 4) | (mask / SCENE_SIZE_HALF);
+				mask++;
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--vsize);
+	}
 }
 
 void Renderer::renderPolygonsBopper(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: 2c94ef1f0a96759e18c58c86709fc094f08cb70f
    https://github.com/scummvm/scummvm/commit/2c94ef1f0a96759e18c58c86709fc094f08cb70f
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: optimized renderPolygonsBopper

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 5b12b567b5..5735fb07f3 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -513,31 +513,34 @@ void Renderer::renderPolygonsCopper(uint8 *out, int vtop, int32 vsize, int32 col
 
 void Renderer::renderPolygonsBopper(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
-	int32 currentLine = vtop;
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		int16 start = ptr1[0];
+		int16 stop = ptr1[screenHeight];
+		ptr1++;
+		int32 hsize = stop - start;
 
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			int16 start = ptr1[0];
-			int16 stop = ptr1[screenHeight];
-			ptr1++;
-			int32 hsize = stop - start;
-
+		if (start & 1) {
 			if (hsize >= 0) {
 				hsize++;
 				for (int32 j = start; j < hsize + start; j++) {
-					if (start & 1) {
-						if (j >= 0 && j < screenWidth) {
-							out[j] = color;
-						}
+					if (j >= 0 && j < screenWidth) {
+						out[j] = color;
 					}
 				}
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--vsize);
+	}
 }
 
 void Renderer::renderPolygonsFlat(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: c05b3a8ac99457de624832d786bbad37ca835781
    https://github.com/scummvm/scummvm/commit/c05b3a8ac99457de624832d786bbad37ca835781
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: optimized renderPolygonsFlat

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 5735fb07f3..ed34eafb41 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -545,29 +545,32 @@ void Renderer::renderPolygonsBopper(uint8 *out, int vtop, int32 vsize, int32 col
 
 void Renderer::renderPolygonsFlat(uint8 *out, int vtop, int32 vsize, int32 color) const {
 	const int16 *ptr1 = &_polyTab[vtop];
-	int32 currentLine = vtop;
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		int16 start = ptr1[0];
+		int16 stop = ptr1[screenHeight];
+		ptr1++;
+		int32 hsize = stop - start;
 
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			int16 start = ptr1[0];
-			int16 stop = ptr1[screenHeight];
-			ptr1++;
-			int32 hsize = stop - start;
-
-			if (hsize >= 0) {
-				hsize++;
-				for (int32 j = start; j < hsize + start; j++) {
-					if (j >= 0 && j < screenWidth) {
-						out[j] = color;
-					}
+		if (hsize >= 0) {
+			hsize++;
+			for (int32 j = start; j < hsize + start; j++) {
+				if (j >= 0 && j < screenWidth) {
+					out[j] = color;
 				}
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--vsize);
+	}
 }
 
 void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: 33e983651a204dbe5d5a5da828ba27a7ab4a8638
    https://github.com/scummvm/scummvm/commit/33e983651a204dbe5d5a5da828ba27a7ab4a8638
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: optimized renderPolygonTrame

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index ed34eafb41..193576c1f8 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -698,38 +698,42 @@ void Renderer::renderPolygonTrame(uint8 *out, int vtop, int32 vsize, int32 color
 	const int screenWidth = _engine->width();
 	const int screenHeight = _engine->height();
 
-	int32 currentLine = vtop;
-	do {
-		if (currentLine >= 0 && currentLine < screenHeight) {
-			int16 start = ptr1[0];
-			int16 stop = ptr1[screenHeight];
-			ptr1++;
-			int32 hsize = stop - start;
+	int32 renderLoop = vsize;
+	if (vtop < 0) {
+		out += screenWidth * ABS(vtop);
+		renderLoop -= ABS(vtop);
+	}
+	if (renderLoop > screenHeight) {
+		renderLoop = screenHeight;
+	}
+	for (int32 currentLine = 0; currentLine < renderLoop; ++currentLine) {
+		int16 start = ptr1[0];
+		int16 stop = ptr1[screenHeight];
+		ptr1++;
+		int32 hsize = stop - start;
 
-			if (hsize >= 0) {
-				hsize++;
-				uint8 *out2 = start + out;
+		if (hsize >= 0) {
+			hsize++;
+			uint8 *out2 = start + out;
 
-				hsize /= 2;
-				if (hsize > 1) {
-					uint16 ax;
-					bh ^= 1;
-					ax = (uint16)(*out2);
-					ax &= 1;
-					if (ax ^ bh) {
-						out2++;
-					}
+			hsize /= 2;
+			if (hsize > 1) {
+				uint16 ax;
+				bh ^= 1;
+				ax = (uint16)(*out2);
+				ax &= 1;
+				if (ax ^ bh) {
+					out2++;
+				}
 
-					for (int32 j = 0; j < hsize; j++) {
-						*(out2) = (uint8)color;
-						out2 += 2;
-					}
+				for (int32 j = 0; j < hsize; j++) {
+					*(out2) = (uint8)color;
+					out2 += 2;
 				}
 			}
 		}
 		out += screenWidth;
-		currentLine++;
-	} while (--vsize);
+	}
 }
 
 void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 color) const {


Commit: 51932eec27504c130fe7a9889036a773b09c8afc
    https://github.com/scummvm/scummvm/commit/51932eec27504c130fe7a9889036a773b09c8afc
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: extract to local variable

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 193576c1f8..499fb5cbc2 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -799,10 +799,12 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 				colorSize /= hsize;
 				hsize++;
 
+				const uint8 startColorByte = startColor / 256;
+
 				if (hsize % 2) {
 					hsize /= 2;
 					if (currentXPos >= 0 && currentXPos < screenWidth) {
-						*(out2) = startColor / 256;
+						*(out2) = startColorByte;
 					}
 					out2++;
 					currentXPos++;
@@ -813,14 +815,14 @@ void Renderer::renderPolygonsGouraud(uint8 *out, int vtop, int32 vsize, int32 co
 
 				do {
 					if (currentXPos >= 0 && currentXPos < screenWidth) {
-						*(out2) = startColor / 256;
+						*(out2) = startColorByte;
 					}
 
 					currentXPos++;
 					startColor += colorSize;
 
 					if (currentXPos >= 0 && currentXPos < screenWidth) {
-						*(out2 + 1) = startColor / 256;
+						*(out2 + 1) = startColorByte;
 					}
 
 					currentXPos++;


Commit: 97c163d35183e606335fc99fc8e0c034d7fbbc38
    https://github.com/scummvm/scummvm/commit/97c163d35183e606335fc99fc8e0c034d7fbbc38
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-02-20T18:22:46+01:00

Commit Message:
TWINE: rewrite loop

Changed paths:
    engines/twine/renderer/renderer.cpp


diff --git a/engines/twine/renderer/renderer.cpp b/engines/twine/renderer/renderer.cpp
index 499fb5cbc2..ff3e9fae7a 100644
--- a/engines/twine/renderer/renderer.cpp
+++ b/engines/twine/renderer/renderer.cpp
@@ -634,17 +634,10 @@ void Renderer::renderPolygonsTele(uint8 *out, int vtop, int32 vsize, int32 color
 				ax = 0; // not sure about this
 			}
 
-			int32 j = hsize >> 1;
-
-			while (1) {
+			for (int32 j = hsize >> 1; j > 0; --j) {
 				*(out2++) = ax & 0x0F;
 				ax += dx;
 
-				--j;
-				if (!j) {
-					break;
-				}
-
 				*(out2++) = ax & 0x0F;
 				ax += dx;
 			}




More information about the Scummvm-git-logs mailing list