From 8467e0d7d0a3c963e587e659bb7a39fa04ba0f04 Mon Sep 17 00:00:00 2001 From: Josh Braendel Date: Sat, 17 Dec 2022 12:13:30 -0800 Subject: [PATCH 1/3] Add Speedometer - Introduced speedometer similar to JoeQuake, useful for speedrunners - Updated character draws to allow size specification - Added helpful calls -- find best matching color in palette; find hud size for offsetting --- Quake/draw.h | 1 + Quake/gl_draw.c | 27 +++++++++++++++++++-------- Quake/gl_screen.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ Quake/gl_texmgr.c | 27 +++++++++++++++++++++++++++ Quake/gl_texmgr.h | 1 + Quake/sbar.c | 17 +++++++++++++++++ Quake/sbar.h | 2 ++ 7 files changed, 112 insertions(+), 8 deletions(-) diff --git a/Quake/draw.h b/Quake/draw.h index 28276b88e..3caa70a69 100644 --- a/Quake/draw.h +++ b/Quake/draw.h @@ -39,6 +39,7 @@ void Draw_Fill (cb_context_t *cbx, int x, int y, int w, int h, int c, float alph void Draw_FadeScreen (cb_context_t *cbx); void Draw_String (cb_context_t *cbx, int x, int y, const char *str); void Draw_String_3D (cb_context_t *cbx, vec3_t coords, float size, const char *str); +void Draw_String_WithSize (cb_context_t *cbx, int x, int y, const char *str, float size); qpic_t *Draw_PicFromWad2 (const char *name, unsigned int texflags); qpic_t *Draw_PicFromWad (const char *name); qpic_t *Draw_CachePic (const char *path); diff --git a/Quake/gl_draw.c b/Quake/gl_draw.c index afdef7866..dfc5300a6 100644 --- a/Quake/gl_draw.c +++ b/Quake/gl_draw.c @@ -454,7 +454,7 @@ void Draw_Init (void) Draw_FillCharacterQuad ================ */ -static void Draw_FillCharacterQuad (int x, int y, char num, basicvertex_t *output, int rotation) +static void Draw_FillCharacterQuad_WithSize (int x, int y, char num, basicvertex_t *output, int rotation, float textSize) { int row, col; float frow, fcol, size; @@ -471,9 +471,9 @@ static void Draw_FillCharacterQuad (int x, int y, char num, basicvertex_t *outpu float texcoords[4][2] = { {x, y}, - {x + 8, y}, - {x + 8, y + 8}, - {x, y + 8}, + {x + textSize, y}, + {x + textSize, y + textSize}, + {x, y + textSize}, }; corner_verts[0].position[0] = texcoords[(rotation + 0) % 4][0]; @@ -508,6 +508,12 @@ static void Draw_FillCharacterQuad (int x, int y, char num, basicvertex_t *outpu output[5] = corner_verts[0]; } + +static void Draw_FillCharacterQuad (int x, int y, char num, basicvertex_t *output, int rotation) +{ + Draw_FillCharacterQuad_WithSize (x, y, num, output, rotation, 8.f); +} + /* ================ Draw_Character @@ -542,13 +548,13 @@ void Draw_Character (cb_context_t *cbx, int x, int y, int num) Draw_String ================ */ -void Draw_String (cb_context_t *cbx, int x, int y, const char *str) +void Draw_String_WithSize (cb_context_t *cbx, int x, int y, const char *str, float size) { int num_verts = 0; int i; const char *tmp; - if (y <= -8) + if (y <= -size) return; // totally off screen for (tmp = str; *tmp != 0; ++tmp) @@ -563,10 +569,10 @@ void Draw_String (cb_context_t *cbx, int x, int y, const char *str) { if (*str != 32) { - Draw_FillCharacterQuad (x, y, *str, vertices + i * 6, 0); + Draw_FillCharacterQuad_WithSize (x, y, *str, vertices + i * 6, 0, size); i++; } - x += 8; + x += size; } vulkan_globals.vk_cmd_bind_vertex_buffers (cbx->cb, 0, 1, &buffer, &buffer_offset); @@ -576,6 +582,11 @@ void Draw_String (cb_context_t *cbx, int x, int y, const char *str) vulkan_globals.vk_cmd_draw (cbx->cb, num_verts, 1, 0, 0); } +void Draw_String (cb_context_t *cbx, int x, int y, const char *str) +{ + Draw_String_WithSize (cbx, x, y, str, 8.f); +} + /* ============= Draw_Pic -- johnfitz -- modified diff --git a/Quake/gl_screen.c b/Quake/gl_screen.c index 123284acc..6edf8a137 100644 --- a/Quake/gl_screen.c +++ b/Quake/gl_screen.c @@ -89,6 +89,7 @@ cvar_t scr_conscale = {"scr_conscale", "1", CVAR_ARCHIVE}; cvar_t scr_crosshairscale = {"scr_crosshairscale", "1", CVAR_ARCHIVE}; cvar_t scr_showfps = {"scr_showfps", "0", CVAR_NONE}; cvar_t scr_clock = {"scr_clock", "0", CVAR_NONE}; +cvar_t scr_showspeed = { "scr_showspeed", "1", CVAR_NONE}; // johnfitz cvar_t scr_usekfont = {"scr_usekfont", "0", CVAR_NONE}; // 2021 re-release @@ -545,6 +546,7 @@ void SCR_Init (void) Cvar_RegisterVariable (&scr_crosshairscale); Cvar_RegisterVariable (&scr_showfps); Cvar_RegisterVariable (&scr_clock); + Cvar_RegisterVariable (&scr_showspeed); // johnfitz Cvar_RegisterVariable (&scr_usekfont); // 2021 re-release Cvar_SetCallback (&scr_fov, SCR_Callback_refdef); @@ -698,6 +700,48 @@ void SCR_DrawClock (cb_context_t *cbx) } } + +/* +============== +SCR_DrawSpeed +============== +*/ +void SCR_DrawSpeed (cb_context_t *cbx) +{ + if (cl.intermission || !scr_showspeed.value) + return; + + float maxSpeedFillWidth = 600; // if speed >= val, then speedometer is completely filled + float scale = scr_sbarscale.value; + float width = 140.f * scale; + float height = 12.f * scale; + float x = ((float)vid.width / 2.f) - (width / 2.f); + float y = (float)vid.height - height - ((float)Sbar_HudHeight() * scale); + + float textLeftOffset = 10.f * scale; + float textHeight = 8.f * scale; + float textY = y + (height / 2) - (textHeight / 2); + float textX = x + textLeftOffset; + + char st[4]; + float speed = VectorLength (cl.velocity); + sprintf (st, "%-3d", (int)speed); + int speedWidth = (int)(MIN(1.f, speed / maxSpeedFillWidth) * width); + + static int bgColor = -1; + static int fillColor = -1; + if (bgColor == -1) + { + bgColor = TexMgr_NearestColor(20, 20, 0); + fillColor = TexMgr_NearestColor(40, 30, 15); + } + + GL_SetCanvas (cbx, CANVAS_DEFAULT); + Draw_Fill (cbx, x, y, width, height, bgColor, 1.f); // entire speedometer + Draw_Fill (cbx, x, y, speedWidth, height, fillColor, 1.f); // speed fill on speedometer + Draw_String_WithSize (cbx, textX, textY, st, textHeight); +} + /* ============== SCR_DrawDevStats @@ -1142,6 +1186,7 @@ static void SCR_DrawGUI (void *unused) SCR_DrawFPS (cbx); // johnfitz SCR_DrawClock (cbx); // johnfitz SCR_DrawConsole (cbx); + SCR_DrawSpeed (cbx); M_Draw (cbx); } diff --git a/Quake/gl_texmgr.c b/Quake/gl_texmgr.c index 593b19098..a881d2964 100644 --- a/Quake/gl_texmgr.c +++ b/Quake/gl_texmgr.c @@ -566,6 +566,33 @@ void TexMgr_LoadPalette (void) ((byte *)&d_8to24table_conchars[0])[3] = 0; } +/* +================ +TexMgr_NearestColor +================ +*/ +int TexMgr_NearestColor (int r, int g, int b) +{ + byte *curColor = (byte *)d_8to24table; + int nearestColor = 0; + int nearestDist = INT_MAX; + int cursor = 0; + + while (curColor[3] != 0) // last color is transparent + { + int colorDist = (r - curColor[0]) * (r - curColor[0]) + (g - curColor[1]) * (g - curColor[1]) + (b - curColor[2]) * (b - curColor[2]); + if (colorDist < nearestDist) + { + nearestColor = cursor; + nearestDist = colorDist; + } + curColor += 4; + cursor += 1; + } + + return nearestColor; +} + /* ================ TexMgr_NewGame diff --git a/Quake/gl_texmgr.h b/Quake/gl_texmgr.h index 9adf6bb7d..8c321235a 100644 --- a/Quake/gl_texmgr.h +++ b/Quake/gl_texmgr.h @@ -110,6 +110,7 @@ void TexMgr_Init (void); void TexMgr_DeleteTextureObjects (void); void TexMgr_CollectGarbage (void); void TexMgr_LoadPalette (void); +int TexMgr_NearestColor (int r, int g, int b); // IMAGE LOADING gltexture_t *TexMgr_LoadImage ( diff --git a/Quake/sbar.c b/Quake/sbar.c index f1accbdd6..c40664373 100644 --- a/Quake/sbar.c +++ b/Quake/sbar.c @@ -1384,3 +1384,20 @@ void Sbar_FinaleOverlay (cb_context_t *cbx) pic = Draw_CachePic ("gfx/finale.lmp"); Draw_Pic (cbx, (320 - pic->width) / 2, 16, pic, 1.0f, false); // johnfitz -- stretched menus } + + +/* +================== +Sbar_HudHeight +================== +*/ +int Sbar_HudHeight (void) +{ + int height = 0; + if (scr_viewsize.value <= 110 && sb_sbar) + height += sb_sbar->height; + if (scr_viewsize.value <= 100 && sb_ibar) + height += sb_ibar->height; + + return height; +} \ No newline at end of file diff --git a/Quake/sbar.h b/Quake/sbar.h index 3155bb7a0..1ec48dcef 100644 --- a/Quake/sbar.h +++ b/Quake/sbar.h @@ -39,4 +39,6 @@ void Sbar_IntermissionOverlay (cb_context_t *cbx); void Sbar_FinaleOverlay (cb_context_t *cbx); +int Sbar_HudHeight (void); + #endif /* _QUAKE_SBAR_H */ From a39d63c630934c280cd52fdeaa9877747aa39b7b Mon Sep 17 00:00:00 2001 From: Josh Braendel Date: Sat, 17 Dec 2022 15:28:20 -0800 Subject: [PATCH 2/3] fix for msvc compile --- Quake/gl_screen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Quake/gl_screen.c b/Quake/gl_screen.c index 6edf8a137..e40c4c603 100644 --- a/Quake/gl_screen.c +++ b/Quake/gl_screen.c @@ -726,7 +726,7 @@ void SCR_DrawSpeed (cb_context_t *cbx) char st[4]; float speed = VectorLength (cl.velocity); sprintf (st, "%-3d", (int)speed); - int speedWidth = (int)(MIN(1.f, speed / maxSpeedFillWidth) * width); + int speedWidth = (int)(fmin(1.f, speed / maxSpeedFillWidth) * width); static int bgColor = -1; static int fillColor = -1; From 7a4f5b0eaf3b64bb91d1ca0c9c36aae2fea5fa42 Mon Sep 17 00:00:00 2001 From: Josh Braendel Date: Sat, 17 Dec 2022 20:38:23 -0800 Subject: [PATCH 3/3] Linting Fixing clang format issues --- Quake/draw.h | 2 +- Quake/gl_draw.c | 1 - Quake/gl_screen.c | 15 +++++++-------- Quake/gl_texmgr.c | 6 +++--- Quake/gl_texmgr.h | 2 +- Quake/sbar.c | 1 - 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Quake/draw.h b/Quake/draw.h index 3caa70a69..392cc6197 100644 --- a/Quake/draw.h +++ b/Quake/draw.h @@ -39,7 +39,7 @@ void Draw_Fill (cb_context_t *cbx, int x, int y, int w, int h, int c, float alph void Draw_FadeScreen (cb_context_t *cbx); void Draw_String (cb_context_t *cbx, int x, int y, const char *str); void Draw_String_3D (cb_context_t *cbx, vec3_t coords, float size, const char *str); -void Draw_String_WithSize (cb_context_t *cbx, int x, int y, const char *str, float size); +void Draw_String_WithSize (cb_context_t *cbx, int x, int y, const char *str, float size); qpic_t *Draw_PicFromWad2 (const char *name, unsigned int texflags); qpic_t *Draw_PicFromWad (const char *name); qpic_t *Draw_CachePic (const char *path); diff --git a/Quake/gl_draw.c b/Quake/gl_draw.c index dfc5300a6..e70e5d442 100644 --- a/Quake/gl_draw.c +++ b/Quake/gl_draw.c @@ -508,7 +508,6 @@ static void Draw_FillCharacterQuad_WithSize (int x, int y, char num, basicvertex output[5] = corner_verts[0]; } - static void Draw_FillCharacterQuad (int x, int y, char num, basicvertex_t *output, int rotation) { Draw_FillCharacterQuad_WithSize (x, y, num, output, rotation, 8.f); diff --git a/Quake/gl_screen.c b/Quake/gl_screen.c index e40c4c603..de054ada5 100644 --- a/Quake/gl_screen.c +++ b/Quake/gl_screen.c @@ -89,7 +89,7 @@ cvar_t scr_conscale = {"scr_conscale", "1", CVAR_ARCHIVE}; cvar_t scr_crosshairscale = {"scr_crosshairscale", "1", CVAR_ARCHIVE}; cvar_t scr_showfps = {"scr_showfps", "0", CVAR_NONE}; cvar_t scr_clock = {"scr_clock", "0", CVAR_NONE}; -cvar_t scr_showspeed = { "scr_showspeed", "1", CVAR_NONE}; +cvar_t scr_showspeed = {"scr_showspeed", "1", CVAR_NONE}; // johnfitz cvar_t scr_usekfont = {"scr_usekfont", "0", CVAR_NONE}; // 2021 re-release @@ -700,7 +700,6 @@ void SCR_DrawClock (cb_context_t *cbx) } } - /* ============== SCR_DrawSpeed @@ -716,28 +715,28 @@ void SCR_DrawSpeed (cb_context_t *cbx) float width = 140.f * scale; float height = 12.f * scale; float x = ((float)vid.width / 2.f) - (width / 2.f); - float y = (float)vid.height - height - ((float)Sbar_HudHeight() * scale); + float y = (float)vid.height - height - ((float)Sbar_HudHeight () * scale); float textLeftOffset = 10.f * scale; float textHeight = 8.f * scale; float textY = y + (height / 2) - (textHeight / 2); float textX = x + textLeftOffset; - char st[4]; + char st[4]; float speed = VectorLength (cl.velocity); sprintf (st, "%-3d", (int)speed); - int speedWidth = (int)(fmin(1.f, speed / maxSpeedFillWidth) * width); + int speedWidth = (int)(fmin (1.f, speed / maxSpeedFillWidth) * width); static int bgColor = -1; static int fillColor = -1; if (bgColor == -1) { - bgColor = TexMgr_NearestColor(20, 20, 0); - fillColor = TexMgr_NearestColor(40, 30, 15); + bgColor = TexMgr_NearestColor (20, 20, 0); + fillColor = TexMgr_NearestColor (40, 30, 15); } GL_SetCanvas (cbx, CANVAS_DEFAULT); - Draw_Fill (cbx, x, y, width, height, bgColor, 1.f); // entire speedometer + Draw_Fill (cbx, x, y, width, height, bgColor, 1.f); // entire speedometer Draw_Fill (cbx, x, y, speedWidth, height, fillColor, 1.f); // speed fill on speedometer Draw_String_WithSize (cbx, textX, textY, st, textHeight); } diff --git a/Quake/gl_texmgr.c b/Quake/gl_texmgr.c index a881d2964..2ae3e7c75 100644 --- a/Quake/gl_texmgr.c +++ b/Quake/gl_texmgr.c @@ -574,9 +574,9 @@ TexMgr_NearestColor int TexMgr_NearestColor (int r, int g, int b) { byte *curColor = (byte *)d_8to24table; - int nearestColor = 0; - int nearestDist = INT_MAX; - int cursor = 0; + int nearestColor = 0; + int nearestDist = INT_MAX; + int cursor = 0; while (curColor[3] != 0) // last color is transparent { diff --git a/Quake/gl_texmgr.h b/Quake/gl_texmgr.h index 8c321235a..cfdae1ba8 100644 --- a/Quake/gl_texmgr.h +++ b/Quake/gl_texmgr.h @@ -110,7 +110,7 @@ void TexMgr_Init (void); void TexMgr_DeleteTextureObjects (void); void TexMgr_CollectGarbage (void); void TexMgr_LoadPalette (void); -int TexMgr_NearestColor (int r, int g, int b); +int TexMgr_NearestColor (int r, int g, int b); // IMAGE LOADING gltexture_t *TexMgr_LoadImage ( diff --git a/Quake/sbar.c b/Quake/sbar.c index c40664373..b3a5ba271 100644 --- a/Quake/sbar.c +++ b/Quake/sbar.c @@ -1385,7 +1385,6 @@ void Sbar_FinaleOverlay (cb_context_t *cbx) Draw_Pic (cbx, (320 - pic->width) / 2, 16, pic, 1.0f, false); // johnfitz -- stretched menus } - /* ================== Sbar_HudHeight