From c0bf79ad8e8230bb315035f6be7d93d137d3d176 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Sun, 9 Sep 2018 22:48:09 +0100
Subject: [PATCH] R_CreateColormap2 and R_MakeColormaps have been made
 obsolete, it's just R_CreateColormap now, like it used to be!

With that, I moved R_CreateColormap2's exclusive software colormap malloc code to R_CreateColormap, and merged the two software-only blocks of code into one. I also disabled any unneeded variables and fixed a preprocessor-related goofup
---
 src/p_setup.c |  2 +-
 src/r_data.c  | 86 +++++++++++++++++++++++++++++++++++++++------------
 src/r_data.h  |  4 +--
 3 files changed, 70 insertions(+), 22 deletions(-)

diff --git a/src/p_setup.c b/src/p_setup.c
index d8c913e0f0..58170e2627 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -2673,7 +2673,7 @@ boolean P_SetupLevel(boolean skipprecip)
 		P_CreateBlockMap(); // Graue 02-29-2004
 	P_LoadSideDefs2(lastloadedmaplumpnum + ML_SIDEDEFS);
 
-	R_MakeColormaps();
+	//R_MakeColormaps();
 	P_LoadLineDefs2();
 	P_LoadSubsectors(lastloadedmaplumpnum + ML_SSECTORS);
 	P_LoadNodes(lastloadedmaplumpnum + ML_NODES);
diff --git a/src/r_data.c b/src/r_data.c
index e1d4b8935d..2551a2946d 100644
--- a/src/r_data.c
+++ b/src/r_data.c
@@ -1038,8 +1038,8 @@ void R_ReInitColormaps(UINT16 num)
 
 static lumpnum_t foundcolormaps[MAXCOLORMAPS];
 
-static char colormapFixingArray[MAXCOLORMAPS][3][9];
-static size_t carrayindex;
+//static char colormapFixingArray[MAXCOLORMAPS][3][9];
+//static size_t carrayindex;
 
 //
 // R_ClearColormaps
@@ -1052,7 +1052,7 @@ void R_ClearColormaps(void)
 
 	num_extra_colormaps = 0;
 
-	carrayindex = 0;
+	//carrayindex = 0;
 
 	for (i = 0; i < MAXCOLORMAPS; i++)
 		foundcolormaps[i] = LUMPERROR;
@@ -1110,7 +1110,7 @@ static int RoundUp(double number);
 INT32 R_CreateColormap(char *p1, char *p2, char *p3)
 {
 	double cmaskr, cmaskg, cmaskb, cdestr, cdestg, cdestb;
-	double r, g, b, cbrightness, maskamt = 0, othermask = 0;
+	double maskamt = 0, othermask = 0;
 	int mask, fog = 0;
 	size_t mapnum = num_extra_colormaps;
 	size_t i;
@@ -1163,7 +1163,7 @@ INT32 R_CreateColormap(char *p1, char *p2, char *p3)
 		fadedist = fadeend - fadestart;
 		fog = NUMFROMCHAR(p2[1]);
 	}
-#undef getnum
+#undef NUMFROMCHAR
 
 	if (p3[0] == '#')
 	{
@@ -1194,14 +1194,35 @@ INT32 R_CreateColormap(char *p1, char *p2, char *p3)
 	if (num_extra_colormaps == MAXCOLORMAPS)
 		I_Error("R_CreateColormap: Too many colormaps! the limit is %d\n", MAXCOLORMAPS);
 
-	strncpy(colormapFixingArray[num_extra_colormaps][0], p1, 8);
-	strncpy(colormapFixingArray[num_extra_colormaps][1], p2, 8);
-	strncpy(colormapFixingArray[num_extra_colormaps][2], p3, 8);
+	//strncpy(colormapFixingArray[num_extra_colormaps][0], p1, 8);
+	//strncpy(colormapFixingArray[num_extra_colormaps][1], p2, 8);
+	//strncpy(colormapFixingArray[num_extra_colormaps][2], p3, 8);
 
 	num_extra_colormaps++;
 
+	foundcolormaps[mapnum] = LUMPERROR;
+
+	// aligned on 8 bit for asm code
+	extra_colormaps[mapnum].colormap = NULL;
+	extra_colormaps[mapnum].maskcolor = (UINT16)maskcolor;
+	extra_colormaps[mapnum].fadecolor = (UINT16)fadecolor;
+	extra_colormaps[mapnum].maskamt = maskamt;
+	extra_colormaps[mapnum].fadestart = (UINT16)fadestart;
+	extra_colormaps[mapnum].fadeend = (UINT16)fadeend;
+	extra_colormaps[mapnum].fog = fog;
+
+	// This code creates the colormap array used by software renderer
 	if (rendermode == render_soft)
 	{
+		double r, g, b, cbrightness;
+		int p;
+		char *colormap_p;
+
+		// Initialise the map and delta arrays
+		// map[i] stores an RGB color (as double) for index i,
+		//  which is then converted to SRB2's palette later
+		// deltas[i] stores a corresponding fade delta between the RGB color and the final fade color;
+		//  map[i]'s values are decremented by after each use
 		for (i = 0; i < 256; i++)
 		{
 			r = pLocalPalette[i].s.red;
@@ -1224,22 +1245,48 @@ INT32 R_CreateColormap(char *p1, char *p2, char *p3)
 				map[i][2] = 255.0l;
 			deltas[i][2] = (map[i][2] - cdestb) / (double)fadedist;
 		}
-	}
 
-	foundcolormaps[mapnum] = LUMPERROR;
+		// Now allocate memory for the actual colormap array itself!
+		colormap_p = Z_MallocAlign((256 * 34) + 10, PU_LEVEL, NULL, 8);
+		extra_colormaps[mapnum].colormap = (UINT8 *)colormap_p;
 
-	// aligned on 8 bit for asm code
-	extra_colormaps[mapnum].colormap = NULL;
-	extra_colormaps[mapnum].maskcolor = (UINT16)maskcolor;
-	extra_colormaps[mapnum].fadecolor = (UINT16)fadecolor;
-	extra_colormaps[mapnum].maskamt = maskamt;
-	extra_colormaps[mapnum].fadestart = (UINT16)fadestart;
-	extra_colormaps[mapnum].fadeend = (UINT16)fadeend;
-	extra_colormaps[mapnum].fog = fog;
+		// Calculate the palette index for each palette index, for each light level
+		// (as well as the two unused colormap lines we inherited from Doom)
+		for (p = 0; p < 34; p++)
+		{
+			for (i = 0; i < 256; i++)
+			{
+				*colormap_p = NearestColor((UINT8)RoundUp(map[i][0]),
+					(UINT8)RoundUp(map[i][1]),
+					(UINT8)RoundUp(map[i][2]));
+				colormap_p++;
+
+				if ((UINT32)p < fadestart)
+					continue;
+#define ABS2(x) ((x) < 0 ? -(x) : (x))
+				if (ABS2(map[i][0] - cdestr) > ABS2(deltas[i][0]))
+					map[i][0] -= deltas[i][0];
+				else
+					map[i][0] = cdestr;
+
+				if (ABS2(map[i][1] - cdestg) > ABS2(deltas[i][1]))
+					map[i][1] -= deltas[i][1];
+				else
+					map[i][1] = cdestg;
+
+				if (ABS2(map[i][2] - cdestb) > ABS2(deltas[i][1]))
+					map[i][2] -= deltas[i][2];
+				else
+					map[i][2] = cdestb;
+#undef ABS2
+			}
+		}
+	}
 
 	return (INT32)mapnum;
 }
 
+/*
 void R_MakeColormaps(void)
 {
 	size_t i;
@@ -1310,7 +1357,7 @@ void R_CreateColormap2(char *p1, char *p2, char *p3)
 		fadedist = fadeend - fadestart;
 		fog = NUMFROMCHAR(p2[1]);
 	}
-#undef getnum
+#undef NUMFROMCHAR
 
 	if (p3[0] == '#')
 	{
@@ -1419,6 +1466,7 @@ void R_CreateColormap2(char *p1, char *p2, char *p3)
 
 	return;
 }
+*/
 
 // Thanks to quake2 source!
 // utils3/qdata/images.c
diff --git a/src/r_data.h b/src/r_data.h
index 1e9e0eb5ed..c1f2a73da6 100644
--- a/src/r_data.h
+++ b/src/r_data.h
@@ -93,8 +93,8 @@ void R_ReInitColormaps(UINT16 num);
 void R_ClearColormaps(void);
 INT32 R_ColormapNumForName(char *name);
 INT32 R_CreateColormap(char *p1, char *p2, char *p3);
-void R_CreateColormap2(char *p1, char *p2, char *p3);
-void R_MakeColormaps(void);
+//void R_CreateColormap2(char *p1, char *p2, char *p3);
+//void R_MakeColormaps(void);
 const char *R_ColormapNameForNum(INT32 num);
 
 extern INT32 numtextures;
-- 
GitLab