From 51c703a4812c62095eb16858330a137ceae94897 Mon Sep 17 00:00:00 2001
From: Petr Bartos <petr.bartos@plus4u.net>
Date: Tue, 28 Mar 2023 23:21:28 +0200
Subject: [PATCH] Fix subtitles rendering; Fix rendering of other centered
 texts

---
 .../Android/jni/OpenJK/code/cgame/cg_draw.cpp | 22 +++++++-------
 .../jni/OpenJK/code/cgame/cg_drawtools.cpp    |  2 +-
 .../Android/jni/OpenJK/code/cgame/cg_local.h  |  2 +-
 .../Android/jni/OpenJK/code/cgame/cg_text.cpp | 30 +++++++++----------
 .../jni/OpenJK/codeJK2/cgame/cg_draw.cpp      | 26 +++++++++-------
 .../jni/OpenJK/codeJK2/cgame/cg_drawtools.cpp |  2 +-
 .../jni/OpenJK/codeJK2/cgame/cg_local.h       |  2 +-
 .../jni/OpenJK/codeJK2/cgame/cg_text.cpp      | 30 +++++++++----------
 8 files changed, 61 insertions(+), 55 deletions(-)

diff --git a/Projects/Android/jni/OpenJK/code/cgame/cg_draw.cpp b/Projects/Android/jni/OpenJK/code/cgame/cg_draw.cpp
index e1464d2..a8034da 100644
--- a/Projects/Android/jni/OpenJK/code/cgame/cg_draw.cpp
+++ b/Projects/Android/jni/OpenJK/code/cgame/cg_draw.cpp
@@ -3666,11 +3666,11 @@ static void CG_DrawAmmoWarning( void ) {
 	}
 
 	w = cgi_R_Font_StrLenPixels(text, cgs.media.qhFontSmall, FONT_SCALE);
-
-	int tempX = 320 - w/2;
+	int offset = w / 2;
+	int tempX = SCREEN_WIDTH / 2;
 	int tempY = 64;
 	CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-	cgi_R_Font_DrawString(tempX, tempY, text, colorTable[CT_LTGOLD1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+	cgi_R_Font_DrawString(tempX - offset, tempY, text, colorTable[CT_LTGOLD1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 }
 
 //---------------------------------------
@@ -4236,9 +4236,7 @@ static void CG_Draw2D( void )
 
 	CG_DrawScrollText();
 
-	if (!vr->immersive_cinematics) {
-		CG_DrawCaptionText();
-	}
+	CG_DrawCaptionText( in_camera && vr->immersive_cinematics );
 
 	if ( in_camera )
 	{//still draw the saber clash flare, but nothing else
@@ -4370,30 +4368,34 @@ static void CG_Draw2D( void )
 			cgi_SP_GetStringTextString( "SP_INGAME_NEW_OBJECTIVE_INFO", text, sizeof(text) );
 
 			int x_pos = 0;
+			int offset = 0;
 			y_pos = 20;
 			w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
-			x_pos = (SCREEN_WIDTH/2)-(w/2);
+			offset = w / 2;
+			x_pos = SCREEN_WIDTH / 2;
 
 			int tempX = x_pos;
 			int tempY = y_pos;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 		}
 	}
 
 	if (cg.weaponPickupTextTime	> cg.time )
 	{
 		int x_pos = 0;
+		int offset = 0;
 		y_pos = 5;
 		gi.Cvar_VariableStringBuffer( "cg_WeaponPickupText", text, sizeof(text) );
 
 		w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
-		x_pos = (SCREEN_WIDTH/2)-(w/2);
+		offset = w / 2;
+		x_pos = SCREEN_WIDTH / 2;
 
 		int tempX = x_pos;
 		int tempY = y_pos;
 		CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-		cgi_R_Font_DrawString(tempX, tempY, text,  colorTable[CT_WHITE], cgs.media.qhFontSmall, -1, FONT_SCALE);
+		cgi_R_Font_DrawString(tempX - offset, tempY, text,  colorTable[CT_WHITE], cgs.media.qhFontSmall, -1, FONT_SCALE);
 	}
 
 	cg.drawingHUD = CG_HUD_NORMAL;
diff --git a/Projects/Android/jni/OpenJK/code/cgame/cg_drawtools.cpp b/Projects/Android/jni/OpenJK/code/cgame/cg_drawtools.cpp
index 05567ad..dd8af8f 100644
--- a/Projects/Android/jni/OpenJK/code/cgame/cg_drawtools.cpp
+++ b/Projects/Android/jni/OpenJK/code/cgame/cg_drawtools.cpp
@@ -28,7 +28,7 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
 
 
 void CG_AdjustFrom640( float *x, float *y, float *w, float *h ) {
-	if (cg.drawingHUD && !vr->cin_camera && !vr->using_screen_layer)
+	if (cg.drawingHUD && (!vr->cin_camera || vr->immersive_cinematics) && !vr->using_screen_layer)
 	{
 		float screenXScale = 1.0f / (cg.drawingHUD == CG_HUD_SCALED ? cg_hudScale.value : 1.0f);
 		float screenYScale = 1.0f / (cg.drawingHUD == CG_HUD_SCALED ? cg_hudScale.value : 1.25f);
diff --git a/Projects/Android/jni/OpenJK/code/cgame/cg_local.h b/Projects/Android/jni/OpenJK/code/cgame/cg_local.h
index 145805e..b668497 100644
--- a/Projects/Android/jni/OpenJK/code/cgame/cg_local.h
+++ b/Projects/Android/jni/OpenJK/code/cgame/cg_local.h
@@ -820,7 +820,7 @@ void CG_CaptionTextStop( void );
 // cg_text.c
 //
 void CG_DrawScrollText( void );
-void CG_DrawCaptionText( void );
+void CG_DrawCaptionText( bool inImmersiveCamera );
 void CG_DrawCenterString( void );
 
 
diff --git a/Projects/Android/jni/OpenJK/code/cgame/cg_text.cpp b/Projects/Android/jni/OpenJK/code/cgame/cg_text.cpp
index b88f6fe..9140265 100644
--- a/Projects/Android/jni/OpenJK/code/cgame/cg_text.cpp
+++ b/Projects/Android/jni/OpenJK/code/cgame/cg_text.cpp
@@ -262,7 +262,7 @@ void CG_CaptionText( const char *str, int sound)
 	if (in_camera) {
 		cg.captionTextY = SCREEN_HEIGHT - (client_camera.bar_height_dest/2);	// ths is now a centre'd Y, not a start Y
 	} else {	//get above the hud
-		cg.captionTextY = (int) (0.88f*((float)SCREEN_HEIGHT - (float)fontHeight * 1.5f));	// do NOT move this, it has to fit in between the weapon HUD and the datapad update.
+		cg.captionTextY = (int) (0.80f*((float)SCREEN_HEIGHT - (float)fontHeight * 1.5f));	// do NOT move this, it has to fit in between the weapon HUD and the datapad update.
 	}
 	cg.captionTextCurrentLine = 0;
 
@@ -389,7 +389,7 @@ void CG_CaptionText( const char *str, int sound)
 }
 
 
-void CG_DrawCaptionText(void)
+void CG_DrawCaptionText( bool inImmersiveCamera )
 {
 	int		i;
 	int		x, y, w;
@@ -442,22 +442,24 @@ void CG_DrawCaptionText(void)
 
 	// Set Y of the first line (varies if only printing one line of text)
 	// (this all works, please don't mess with it)
-	const int fontHeight = (int) ((cgi_Language_IsAsian() ? 1.4f : 1.0f) * (float) cgi_R_Font_HeightPixels(cgs.media.qhFontSmall, fFontScale * FONT_SCALE));
+	const int fontHeight = (int) (3.0f * (float) cgi_R_Font_HeightPixels(cgs.media.qhFontSmall, fFontScale * FONT_SCALE));
 	const bool bPrinting2Lines = !!(cg.captionText[ cg.captionTextCurrentLine+1 ][0]);
 	y = cg.captionTextY - ( (float)fontHeight * (bPrinting2Lines ? 1 : 0.5f));	// captionTextY was a centered Y pos, not a top one
 	y -= cgi_Language_IsAsian() ? 0 : 4;
+	if (inImmersiveCamera) {
+		y -= 100;
+	}
 
 	for (i=	cg.captionTextCurrentLine;i< cg.captionTextCurrentLine + 2;++i)
 	{
 		w = cgi_R_Font_StrLenPixels(cg.captionText[i], cgs.media.qhFontSmall, fFontScale * FONT_SCALE);
 		if (w)
 		{
-			x = (SCREEN_WIDTH-w) / 2;
-
-			int tempX = x;
+			int offset = w / 2;
+			int tempX = SCREEN_WIDTH / 2;
 			int tempY = y;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, cg.captionText[i], textcolor_caption, cgs.media.qhFontSmall, -1, fFontScale * FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, cg.captionText[i], textcolor_caption, cgs.media.qhFontSmall, -1, fFontScale * FONT_SCALE);
 			y += fontHeight;
 		}
 	}
@@ -650,12 +652,11 @@ void CG_DrawScrollText(void)
 //		w = cgi_R_Font_StrLenPixels(cg.printText[i], cgs.media.qhFontMedium, 1.0f);
 //		if (w)
 		{
-			x = (SCREEN_WIDTH - giScrollTextPixelWidth) / 2;
-
-			int tempX = x;
+			int offset = giScrollTextPixelWidth / 2;
+			int tempX = SCREEN_WIDTH / 2;
 			int tempY = y;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, cg.printText[i], textcolor_scroll, cgs.media.qhFontSmall, -1, FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, cg.printText[i], textcolor_scroll, cgs.media.qhFontSmall, -1, FONT_SCALE);
 			y += fontHeight;
 		}
 	}
@@ -777,12 +778,11 @@ void CG_DrawCenterString( void )
 
 		w = cgi_R_Font_StrLenPixels(linebuffer, cgs.media.qhFontSmall, FONT_SCALE);
 
-		x = ( SCREEN_WIDTH - w ) / 2;
-
-		int tempX = x;
+		int offset = w / 2;
+		int tempX = SCREEN_WIDTH / 2;
 		int tempY = y;
 		CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-		cgi_R_Font_DrawString(tempX, tempY,linebuffer, textcolor_center, cgs.media.qhFontSmall, -1, FONT_SCALE);
+		cgi_R_Font_DrawString(tempX - offset, tempY,linebuffer, textcolor_center, cgs.media.qhFontSmall, -1, FONT_SCALE);
 
 		y += fontHeight;
 
diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp
index a1b8282..d1141c4 100644
--- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp
+++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_draw.cpp
@@ -2502,10 +2502,12 @@ static void CG_DrawAmmoWarning( void ) {
 		//s = "LOW AMMO WARNING";
 	}
 
-	int tempX = 320 - w/2;
+	w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
+	int offset = w / 2;
+	int tempX = SCREEN_WIDTH / 2;
 	int tempY = 64;
 	CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-	cgi_R_Font_DrawString(tempX, tempY, text, colorTable[CT_LTGOLD1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+	cgi_R_Font_DrawString(tempX - offset, tempY, text, colorTable[CT_LTGOLD1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 }
 
 //---------------------------------------
@@ -2728,9 +2730,7 @@ static void CG_Draw2D( void )
 
 	CG_DrawScrollText();
 
-	if (!vr->immersive_cinematics) {
-		CG_DrawCaptionText();
-	}
+	CG_DrawCaptionText( in_camera && vr->immersive_cinematics );
 
 	if ( in_camera )
 	{//still draw the saber clash flare, but nothing else
@@ -2842,17 +2842,19 @@ static void CG_Draw2D( void )
 			cgi_SP_GetStringTextString( "INGAME_DATAPAD_UPDATED", text, sizeof(text) );
 
 			int x_pos = 0;
+			int offset = 0;
 			y_pos = (SCREEN_HEIGHT/2)+80;
 			if ( cg_missionInfoCentered.integer )
 			{
 				w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
-				x_pos = (SCREEN_WIDTH/2)-(w/2);
+				offset = w / 2;
+				x_pos = SCREEN_WIDTH / 2;
 			}
 
 			int tempX = x_pos;
 			int tempY = y_pos;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 
 			if (cg_updatedDataPadForcePower1.integer) 
 			{
@@ -2861,13 +2863,14 @@ static void CG_Draw2D( void )
 				if ( cg_missionInfoCentered.integer )
 				{
 					w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
-					x_pos = (SCREEN_WIDTH/2)-(w/2);
+					offset = w / 2;
+					x_pos = SCREEN_WIDTH / 2;
 				}
 
 				tempX = x_pos;
 				tempY = y_pos;
 				CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-				cgi_R_Font_DrawString(tempX, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+				cgi_R_Font_DrawString(tempX - offset, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 			}
 
 			if (cg_updatedDataPadObjective.integer) 
@@ -2877,13 +2880,14 @@ static void CG_Draw2D( void )
 				if ( cg_missionInfoCentered.integer )
 				{
 					w = cgi_R_Font_StrLenPixels(text,cgs.media.qhFontSmall, FONT_SCALE);
-					x_pos = (SCREEN_WIDTH/2)-(w/2);
+					offset = w / 2;
+					x_pos = SCREEN_WIDTH / 2;
 				}
 
 				tempX = x_pos;
 				tempY = y_pos;
 				CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-				cgi_R_Font_DrawString(tempX, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
+				cgi_R_Font_DrawString(tempX - offset, tempY, text,  colorTable[CT_LTRED1], cgs.media.qhFontSmall, -1, FONT_SCALE);
 			}
 
 	//		if (cent->gent->client->sess.missionObjectivesShown<3)
diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_drawtools.cpp b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_drawtools.cpp
index cc227ae..865d057 100644
--- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_drawtools.cpp
+++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_drawtools.cpp
@@ -27,7 +27,7 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.
 
 
 void CG_AdjustFrom640( float *x, float *y, float *w, float *h ) {
-	if (cg.drawingHUD && !vr->cin_camera && !vr->using_screen_layer)
+	if (cg.drawingHUD && (!vr->cin_camera || vr->immersive_cinematics) && !vr->using_screen_layer)
 	{
 		float screenXScale = 1.0f / (cg.drawingHUD == CG_HUD_SCALED ? cg_hudScale.value : 1.0f);
 		float screenYScale = 1.0f / (cg.drawingHUD == CG_HUD_SCALED ? cg_hudScale.value : 1.25f);
diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_local.h b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_local.h
index 2699444..5e1b85a 100644
--- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_local.h
+++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_local.h
@@ -801,7 +801,7 @@ void CG_GameText(int y );
 // cg_text.c
 //
 void CG_DrawScrollText( void );
-void CG_DrawCaptionText( void );
+void CG_DrawCaptionText( bool inImmersiveCamera );
 void CG_DrawCenterString( void ); 
 
 
diff --git a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_text.cpp b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_text.cpp
index 8873650..f6b767e 100644
--- a/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_text.cpp
+++ b/Projects/Android/jni/OpenJK/codeJK2/cgame/cg_text.cpp
@@ -259,7 +259,7 @@ void CG_CaptionText( const char *str, int sound)
 	if (in_camera) {
 		cg.captionTextY = SCREEN_HEIGHT - (client_camera.bar_height_dest/2);	// ths is now a centre'd Y, not a start Y
 	} else {	//get above the hud
-		cg.captionTextY = (int) (0.88f*((float)SCREEN_HEIGHT - (float)fontHeight * 1.5f));	// do NOT move this, it has to fit in between the weapon HUD and the datapad update.
+		cg.captionTextY = (int) (0.84f*((float)SCREEN_HEIGHT - (float)fontHeight * 1.5f));	// do NOT move this, it has to fit in between the weapon HUD and the datapad update.
 	}
 	cg.captionTextCurrentLine = 0;
 
@@ -385,7 +385,7 @@ void CG_CaptionText( const char *str, int sound)
 }
 
 
-void CG_DrawCaptionText(void)
+void CG_DrawCaptionText( bool inImmersiveCamera )
 {
 	int		i;
 	int		x, y, w;
@@ -438,22 +438,24 @@ void CG_DrawCaptionText(void)
 
 	// Set Y of the first line (varies if only printing one line of text)
 	// (this all works, please don't mess with it)
-	const int fontHeight = (int) ((cgi_Language_IsAsian() ? 1.4f : 1.0f) * (float) cgi_R_Font_HeightPixels(cgs.media.qhFontSmall, fFontScale * FONT_SCALE));
+	const int fontHeight = (int) (3.0f * (float) cgi_R_Font_HeightPixels(cgs.media.qhFontSmall, fFontScale * FONT_SCALE));
 	const bool bPrinting2Lines = !!(cg.captionText[ cg.captionTextCurrentLine+1 ][0]);
 	y = cg.captionTextY - ( (float)fontHeight * (bPrinting2Lines ? 1 : 0.5f));	// captionTextY was a centered Y pos, not a top one
 	y -= cgi_Language_IsAsian() ? 0 : 4;
+	if (inImmersiveCamera) {
+		y -= 100;
+	}
 
 	for (i=	cg.captionTextCurrentLine;i< cg.captionTextCurrentLine + 2;++i)
 	{
 		w = cgi_R_Font_StrLenPixels(cg.captionText[i], cgs.media.qhFontSmall, fFontScale * FONT_SCALE);
 		if (w)
 		{
-			x = (SCREEN_WIDTH-w) / 2;
-
-			int tempX = x;
+			int offset = w / 2;
+			int tempX = SCREEN_WIDTH / 2;
 			int tempY = y;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, cg.captionText[i], textcolor_caption, cgs.media.qhFontSmall, -1, fFontScale * FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, cg.captionText[i], textcolor_caption, cgs.media.qhFontSmall, -1, fFontScale * FONT_SCALE);
 			y += fontHeight;
 		}
 	}
@@ -644,12 +646,11 @@ void CG_DrawScrollText(void)
 //		w = cgi_R_Font_StrLenPixels(cg.printText[i], cgs.media.qhFontMedium, 1.0f);
 //		if (w)
 		{
-			x = (SCREEN_WIDTH - giScrollTextPixelWidth) / 2;
-
-			int tempX = x;
+			int offset = giScrollTextPixelWidth / 2;
+			int tempX = SCREEN_WIDTH / 2;
 			int tempY = y;
 			CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-			cgi_R_Font_DrawString(tempX, tempY, cg.printText[i], textcolor_scroll, cgs.media.qhFontSmall, -1, FONT_SCALE);
+			cgi_R_Font_DrawString(tempX - offset, tempY, cg.printText[i], textcolor_scroll, cgs.media.qhFontSmall, -1, FONT_SCALE);
 			y += fontHeight;
 		}
 	}
@@ -769,12 +770,11 @@ void CG_DrawCenterString( void )
 
 		w = cgi_R_Font_StrLenPixels(linebuffer, cgs.media.qhFontSmall, FONT_SCALE);
 
-		x = ( SCREEN_WIDTH - w ) / 2;
-
-		int tempX = x;
+		int offset = w / 2;
+		int tempX = SCREEN_WIDTH / 2;
 		int tempY = y;
 		CG_AdjustFrom640Int( &tempX, &tempY, NULL, NULL );
-		cgi_R_Font_DrawString(tempX, tempY,linebuffer, textcolor_center, cgs.media.qhFontSmall, -1, FONT_SCALE);
+		cgi_R_Font_DrawString(tempX - offset, tempY,linebuffer, textcolor_center, cgs.media.qhFontSmall, -1, FONT_SCALE);
 
 		y += fontHeight;