From 314870bdc28068579a5d82c90e9373f3a1ed413a Mon Sep 17 00:00:00 2001
From: toasterbabe <rollerorbital@gmail.com>
Date: Mon, 17 Apr 2017 22:21:16 +0100
Subject: [PATCH] Support captions for player 2 in 2-player. (You can't tell
 which screen the sounds are coming from without visual cues normally, so
 there isn't two seperate caption lists.)

---
 src/s_sound.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/screen.c  | 15 ++++++++-----
 2 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/src/s_sound.c b/src/s_sound.c
index 181ca5a675..3993d7f981 100644
--- a/src/s_sound.c
+++ b/src/s_sound.c
@@ -531,6 +531,64 @@ void S_StartSoundAtVolume(const void *origin_p, sfxenum_t sfx_id, INT32 volume)
 			sep = (~sep) & 255;
 #endif
 
+		// Handle closed caption input.
+		if (cv_closedcaptioning.value && sfx->caption[0] != '/')
+		{
+			UINT8 i, set = NUMCAPTIONS-1, moveup = 255;
+			boolean same = false;
+			for (i = 0; i < set; i++)
+			{
+				same = ((sfx == closedcaptions[i].s) || (closedcaptions[i].s && fastcmp(sfx->caption, closedcaptions[i].s->caption)));
+				if (same)
+				{
+					set = i;
+					break;
+				}
+			}
+
+			if (!same)
+			{
+				for (i = 0; i < set; i++)
+				{
+					if (!(closedcaptions[i].c || closedcaptions[i].s) || (sfx->priority >= closedcaptions[i].s->priority))
+					{
+						set = i;
+						if (closedcaptions[i].s && (sfx->priority >= closedcaptions[i].s->priority))
+							moveup = i;
+						break;
+					}
+				}
+				for (i = NUMCAPTIONS-1; i > set; i--)
+				{
+					if (sfx == closedcaptions[i].s)
+					{
+						closedcaptions[i].c = NULL;
+						closedcaptions[i].s = NULL;
+						closedcaptions[i].t = 0;
+					}
+				}
+			}
+
+			if (moveup != 255)
+			{
+				for (i = moveup; i < NUMCAPTIONS-1; i++)
+				{
+					if (!(closedcaptions[i].c || closedcaptions[i].s))
+						break;
+				}
+				for (; i > set; i--)
+				{
+					closedcaptions[i].c = closedcaptions[i-1].c;
+					closedcaptions[i].s = closedcaptions[i-1].s;
+					closedcaptions[i].t = closedcaptions[i-1].t;
+				}
+			}
+
+			closedcaptions[set].c = &channels[cnum];
+			closedcaptions[set].s = sfx;
+			closedcaptions[set].t = TICRATE+2;
+		}
+
 		// Assigns the handle to one of the channels in the
 		// mix/output buffer.
 		channels[cnum].handle = I_StartSound(sfx_id, volume, sep, pitch, priority);
diff --git a/src/screen.c b/src/screen.c
index 12bb9c4bd5..ca1e6b6594 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -478,11 +478,16 @@ void SCR_ClosedCaptions(void)
 				const mobj_t *o = (const mobj_t *)closedcaptions[i].c->origin;
 				if (o)
 				{
-					angle_t angle = R_PointToAngle(o->x, o->y) - localangle;
-					if (angle > ANGLE_45 && angle < ANGLE_135)
-						dir = '\x1C';
-					else if (angle > ANGLE_225 && angle < ANGLE_315)
-						dir = '\x1D';
+					if (!splitscreen)
+					{
+						angle_t angle = R_PointToAngle(o->x, o->y) - localangle;
+						if (angle > ANGLE_45 && angle < ANGLE_135)
+							dir = '\x1C';
+						else if (angle > ANGLE_225 && angle < ANGLE_315)
+							dir = '\x1D';
+						else
+							dir = '\x1E';
+					}
 					else
 						dir = '\x1E';
 				}
-- 
GitLab