From ba04b045e68252cec7b87a788ec253631a6aae27 Mon Sep 17 00:00:00 2001
From: Hannu Hanhi <hhanhipublic@gmail.com>
Date: Tue, 25 Jan 2022 04:53:09 +0200
Subject: [PATCH] Fix portals

- Resetting portalcullsector fixes the major visual glitches

- Using 32 bits for nummasks and i fixes crashes when rendering lots of portals
---
 src/r_main.c   | 3 +--
 src/r_portal.c | 1 +
 src/r_things.c | 4 ++--
 src/r_things.h | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/r_main.c b/src/r_main.c
index 8729b5dcb3..e25d257c61 100644
--- a/src/r_main.c
+++ b/src/r_main.c
@@ -1450,7 +1450,7 @@ static void Mask_Post (maskcount_t* m)
 
 void R_RenderPlayerView(player_t *player)
 {
-	UINT8			nummasks	= 1;
+	INT32			nummasks	= 1;
 	maskcount_t*	masks		= malloc(sizeof(maskcount_t));
 
 	if (cv_homremoval.value && player == &players[displayplayer]) // if this is display player 1
@@ -1515,7 +1515,6 @@ void R_RenderPlayerView(player_t *player)
 	R_ClipSprites(drawsegs, NULL);
 	PS_STOP_TIMING(ps_sw_spritecliptime);
 
-
 	// Add skybox portals caused by sky visplanes.
 	if (cv_skybox.value && skyboxmo[0])
 		Portal_AddSkyboxPortals();
diff --git a/src/r_portal.c b/src/r_portal.c
index 3026f4e4c0..cba98db051 100644
--- a/src/r_portal.c
+++ b/src/r_portal.c
@@ -132,6 +132,7 @@ static portal_t* Portal_Add (const INT16 x1, const INT16 x2)
 
 void Portal_Remove (portal_t* portal)
 {
+	portalcullsector = NULL;
 	portal_base = portal->next;
 	Z_Free(portal->ceilingclip);
 	Z_Free(portal->floorclip);
diff --git a/src/r_things.c b/src/r_things.c
index accd1e2b3c..520f426885 100644
--- a/src/r_things.c
+++ b/src/r_things.c
@@ -3168,10 +3168,10 @@ static void R_DrawMaskedList (drawnode_t* head)
 	}
 }
 
-void R_DrawMasked(maskcount_t* masks, UINT8 nummasks)
+void R_DrawMasked(maskcount_t* masks, INT32 nummasks)
 {
 	drawnode_t *heads;	/**< Drawnode lists; as many as number of views/portals. */
-	SINT8 i;
+	INT32 i;
 
 	heads = calloc(nummasks, sizeof(drawnode_t));
 
diff --git a/src/r_things.h b/src/r_things.h
index b1ff32b1ee..b4676fbbd0 100644
--- a/src/r_things.h
+++ b/src/r_things.h
@@ -100,7 +100,7 @@ typedef struct
 	sector_t* viewsector;
 } maskcount_t;
 
-void R_DrawMasked(maskcount_t* masks, UINT8 nummasks);
+void R_DrawMasked(maskcount_t* masks, INT32 nummasks);
 
 // ----------
 // VISSPRITES
-- 
GitLab