From bce642c75ce09a153e69dc2665e34b6e52110c2b Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Tue, 2 Jul 2024 10:42:39 +0200 Subject: [PATCH] factor crosshair code out into its own source file (#1765) * factor crosshair code out into its own source file Fixes #1764 * comments * initialize the plr pointer * HU_ prefix for non-static function ColorByHealth() * check if crosshair patch lump is from a PWAD * run clang-format over hu_crosshair.\* --- src/CMakeLists.txt | 1 + src/hu_crosshair.c | 192 +++++++++++++++++++++++++++++++++++++++++++++ src/hu_crosshair.h | 55 +++++++++++++ src/hu_stuff.c | 159 +------------------------------------ src/hu_stuff.h | 24 +----- src/mn_setup.c | 1 + src/r_things.c | 2 +- 7 files changed, 255 insertions(+), 179 deletions(-) create mode 100644 src/hu_crosshair.c create mode 100644 src/hu_crosshair.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e62cd568..1346508d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ set(WOOF_SOURCES g_input.c g_input.h hu_command.c hu_command.h hu_coordinates.c hu_coordinates.h + hu_crosshair.c hu_crosshair.h hu_lib.c hu_lib.h hu_obituary.c hu_obituary.h hu_stuff.c hu_stuff.h diff --git a/src/hu_crosshair.c b/src/hu_crosshair.c new file mode 100644 index 00000000..71005c1e --- /dev/null +++ b/src/hu_crosshair.c @@ -0,0 +1,192 @@ +// +// Copyright (C) 1999 by +// id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman +// Copyright (C) 2023-2024 Fabian Greffrath +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// DESCRIPTION: Crosshair HUD component +// +//----------------------------------------------------------------------------- + +#include "hu_crosshair.h" +#include "d_items.h" +#include "doomstat.h" +#include "hu_stuff.h" +#include "m_swap.h" +#include "p_map.h" +#include "p_mobj.h" +#include "r_data.h" +#include "r_main.h" +#include "r_state.h" +#include "st_stuff.h" +#include "v_fmt.h" +#include "v_video.h" + +static player_t *plr = players; + +int hud_crosshair; + +crosstarget_t hud_crosshair_target; +boolean hud_crosshair_health; +boolean hud_crosshair_lockon; // [Alaux] Crosshair locks on target +int hud_crosshair_color; +int hud_crosshair_target_color; + +typedef struct +{ + patch_t *patch; + int w, h, x, y; + byte *cr; +} crosshair_t; + +static crosshair_t crosshair; + +const char *crosshair_lumps[HU_CROSSHAIRS] = { + NULL, "CROSS00", "CROSS01", "CROSS02", "CROSS03", + "CROSS04", "CROSS05", "CROSS06", "CROSS07", "CROSS08"}; + +const char *crosshair_strings[HU_CROSSHAIRS] = { + "Off", "Cross", "Angle", "Dot", "Big Cross", + "Circle", "Big Circle", "Chevron", "Chevrons", "Arcs"}; + +void HU_InitCrosshair(void) +{ + for (int i = 1; i < HU_CROSSHAIRS; i++) + { + int lump = W_CheckNumForName(crosshair_lumps[i]); + + if (W_IsWADLump(lump)) + { + if (R_IsPatchLump(lump)) + { + crosshair_strings[i] = crosshair_lumps[i]; + } + else + { + crosshair_lumps[i] = NULL; + } + } + } +} + +void HU_StartCrosshair(void) +{ + if (crosshair.patch) + { + Z_ChangeTag(crosshair.patch, PU_CACHE); + } + + if (crosshair_lumps[hud_crosshair]) + { + crosshair.patch = + V_CachePatchName(crosshair_lumps[hud_crosshair], PU_STATIC); + + crosshair.w = SHORT(crosshair.patch->width) / 2; + crosshair.h = SHORT(crosshair.patch->height) / 2; + } + else + { + crosshair.patch = NULL; + } +} + +mobj_t *crosshair_target; // [Alaux] Lock crosshair on target + +void HU_UpdateCrosshair(void) +{ + plr = &players[displayplayer]; + + crosshair.x = SCREENWIDTH / 2; + crosshair.y = (screenblocks <= 10) ? (SCREENHEIGHT - ST_HEIGHT) / 2 + : SCREENHEIGHT / 2; + + if (hud_crosshair_health) + { + crosshair.cr = HU_ColorByHealth(plr->health, 100, st_invul); + } + else + { + crosshair.cr = colrngs[hud_crosshair_color]; + } + + if (STRICTMODE(hud_crosshair_target || hud_crosshair_lockon)) + { + angle_t an = plr->mo->angle; + ammotype_t ammo = weaponinfo[plr->readyweapon].ammo; + fixed_t range = (ammo == am_noammo) ? MELEERANGE : 16 * 64 * FRACUNIT; + boolean intercepts_overflow_enabled = overflow[emu_intercepts].enabled; + + crosshair_target = linetarget = NULL; + + overflow[emu_intercepts].enabled = false; + P_AimLineAttack(plr->mo, an, range, CROSSHAIR_AIM); + if (!direct_vertical_aiming && (ammo == am_misl || ammo == am_cell)) + { + if (!linetarget) + { + P_AimLineAttack(plr->mo, an + (1 << 26), range, CROSSHAIR_AIM); + } + if (!linetarget) + { + P_AimLineAttack(plr->mo, an - (1 << 26), range, CROSSHAIR_AIM); + } + } + overflow[emu_intercepts].enabled = intercepts_overflow_enabled; + + if (linetarget && !(linetarget->flags & MF_SHADOW)) + { + crosshair_target = linetarget; + } + + if (hud_crosshair_target && crosshair_target) + { + // [Alaux] Color crosshair by target health + if (hud_crosshair_target == crosstarget_health) + { + crosshair.cr = HU_ColorByHealth( + crosshair_target->health, + crosshair_target->info->spawnhealth, false); + } + else + { + crosshair.cr = colrngs[hud_crosshair_target_color]; + } + } + } +} + +void HU_UpdateCrosshairLock(int x, int y) +{ + int w = (crosshair.w * video.xscale) >> FRACBITS; + int h = (crosshair.h * video.yscale) >> FRACBITS; + + x = viewwindowx + BETWEEN(w, viewwidth - w - 1, x); + y = viewwindowy + BETWEEN(h, viewheight - h - 1, y); + + crosshair.x = (x << FRACBITS) / video.xscale - video.deltaw; + crosshair.y = (y << FRACBITS) / video.yscale; +} + +void HU_DrawCrosshair(void) +{ + if (plr->playerstate != PST_LIVE || automapactive || menuactive || paused) + { + return; + } + + if (crosshair.patch) + { + V_DrawPatchTranslated(crosshair.x - crosshair.w, + crosshair.y - crosshair.h, crosshair.patch, + crosshair.cr); + } +} diff --git a/src/hu_crosshair.h b/src/hu_crosshair.h new file mode 100644 index 00000000..f1077b21 --- /dev/null +++ b/src/hu_crosshair.h @@ -0,0 +1,55 @@ +// Copyright (C) 1999 by +// id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman +// Copyright (C) 2023-2024 Fabian Greffrath +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// DESCRIPTION: Crosshair HUD component +// +//----------------------------------------------------------------------------- + +#ifndef __HU_CROSSHAIR_H__ +#define __HU_CROSSHAIR_H__ + +#include "doomtype.h" + +extern int hud_crosshair; + +// [Alaux] Lock crosshair on target +typedef enum +{ + crosstarget_off, + crosstarget_highlight, + crosstarget_health, // [Alaux] Color crosshair by target health +} crosstarget_t; + +extern crosstarget_t hud_crosshair_target; +extern struct mobj_s *crosshair_target; + +extern boolean hud_crosshair_health; +extern boolean hud_crosshair_lockon; +extern int hud_crosshair_color; +extern int hud_crosshair_target_color; + +#define HU_CROSSHAIRS 10 +extern const char *crosshair_lumps[HU_CROSSHAIRS]; +extern const char *crosshair_strings[HU_CROSSHAIRS]; + +void HU_InitCrosshair(void); + +void HU_StartCrosshair(void); + +void HU_UpdateCrosshair(void); +void HU_UpdateCrosshairLock(int x, int y); + +void HU_DrawCrosshair(void); + +#endif diff --git a/src/hu_stuff.c b/src/hu_stuff.c index 9e61647d..9c2108f1 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -25,40 +25,31 @@ #include "d_deh.h" /* Ty 03/27/98 - externalization of mapnamesx arrays */ #include "d_event.h" #include "d_items.h" -#include "d_player.h" -#include "doomdef.h" #include "doomkeys.h" #include "doomstat.h" -#include "doomtype.h" #include "dstrings.h" #include "hu_coordinates.h" +#include "hu_crosshair.h" #include "hu_lib.h" #include "hu_obituary.h" #include "hu_stuff.h" #include "i_timer.h" // time_scale #include "i_video.h" // fps #include "m_config.h" -#include "m_fixed.h" #include "m_input.h" #include "m_misc.h" #include "m_swap.h" -#include "p_map.h" // crosshair (linetarget) #include "p_mobj.h" -#include "r_data.h" -#include "r_defs.h" #include "r_main.h" #include "r_state.h" #include "r_voxel.h" #include "s_sound.h" #include "sounds.h" #include "st_stuff.h" /* jff 2/16/98 need loc of status bar */ -#include "tables.h" #include "u_mapinfo.h" #include "u_scanner.h" #include "v_fmt.h" #include "v_video.h" -#include "w_wad.h" -#include "z_zone.h" // global heads up display controls @@ -197,10 +188,6 @@ static boolean message_list; // killough 11/98: made global static int message_timer = HU_MSGTIMEOUT * (1000/TICRATE); // killough 11/98 static int chat_msg_timer = HU_MSGTIMEOUT * (1000/TICRATE); // killough 11/98 -static void HU_InitCrosshair(void); -static void HU_StartCrosshair(void); -int hud_crosshair; - // // Builtin map names. // The actual names can be found in DStrings.h. @@ -352,7 +339,7 @@ static crange_idx_e CRByHealth(int health, int maxhealth, boolean invul) return CR_BLUE; } -static byte* ColorByHealth(int health, int maxhealth, boolean invul) +byte* HU_ColorByHealth(int health, int maxhealth, boolean invul) { const crange_idx_e cr = CRByHealth(health, maxhealth, invul); @@ -852,7 +839,7 @@ static void HU_widget_build_health (void) M_snprintf(hud_stringbuffer + i, sizeof(hud_stringbuffer), "%3d", st_health); // set the display color from the amount of health posessed - w_health.cr = ColorByHealth(plr->health, 100, st_invul); + w_health.cr = HU_ColorByHealth(plr->health, 100, st_invul); // transfer the init string to the widget HUlib_add_string_to_cur_line(&w_health, hud_stringbuffer); @@ -1376,146 +1363,6 @@ static void HU_widget_build_cmd(void) HU_BuildCommandHistory(&w_cmd); } -// Crosshair - -static boolean hud_crosshair_health; -crosstarget_t hud_crosshair_target; -boolean hud_crosshair_lockon; // [Alaux] Crosshair locks on target -int hud_crosshair_color; -static int hud_crosshair_target_color; - -typedef struct -{ - patch_t *patch; - int w, h, x, y; - byte *cr; -} crosshair_t; - -static crosshair_t crosshair; - -const char *crosshair_lumps[HU_CROSSHAIRS] = -{ - NULL, - "CROSS00", "CROSS01", "CROSS02", "CROSS03", - "CROSS04", "CROSS05", "CROSS06", "CROSS07", - "CROSS08" -}; - -const char *crosshair_strings[HU_CROSSHAIRS] = -{ - "Off", - "Cross", "Angle", "Dot", "Big Cross", - "Circle", "Big Circle", "Chevron", "Chevrons", - "Arcs" -}; - -static void HU_InitCrosshair(void) -{ - for (int i = 1; i < HU_CROSSHAIRS; i++) - { - int lump = W_CheckNumForName(crosshair_lumps[i]); - - if (R_IsPatchLump(lump)) - crosshair_strings[i] = crosshair_lumps[i]; - else - crosshair_lumps[i] = NULL; - } -} - -static void HU_StartCrosshair(void) -{ - if (crosshair.patch) - Z_ChangeTag(crosshair.patch, PU_CACHE); - - if (crosshair_lumps[hud_crosshair]) - { - crosshair.patch = V_CachePatchName(crosshair_lumps[hud_crosshair], PU_STATIC); - - crosshair.w = SHORT(crosshair.patch->width)/2; - crosshair.h = SHORT(crosshair.patch->height)/2; - } - else - crosshair.patch = NULL; -} - -mobj_t *crosshair_target; // [Alaux] Lock crosshair on target - -static void HU_UpdateCrosshair(void) -{ - crosshair.x = SCREENWIDTH/2; - crosshair.y = (screenblocks <= 10) ? (SCREENHEIGHT-ST_HEIGHT)/2 : SCREENHEIGHT/2; - - if (hud_crosshair_health) - crosshair.cr = ColorByHealth(plr->health, 100, st_invul); - else - crosshair.cr = colrngs[hud_crosshair_color]; - - if (STRICTMODE(hud_crosshair_target || hud_crosshair_lockon)) - { - angle_t an = plr->mo->angle; - ammotype_t ammo = weaponinfo[plr->readyweapon].ammo; - fixed_t range = (ammo == am_noammo) ? MELEERANGE : 16*64*FRACUNIT; - boolean intercepts_overflow_enabled = overflow[emu_intercepts].enabled; - - crosshair_target = linetarget = NULL; - - overflow[emu_intercepts].enabled = false; - P_AimLineAttack(plr->mo, an, range, CROSSHAIR_AIM); - if (!direct_vertical_aiming && (ammo == am_misl || ammo == am_cell)) - { - if (!linetarget) - P_AimLineAttack(plr->mo, an + (1<<26), range, CROSSHAIR_AIM); - if (!linetarget) - P_AimLineAttack(plr->mo, an - (1<<26), range, CROSSHAIR_AIM); - } - overflow[emu_intercepts].enabled = intercepts_overflow_enabled; - - if (linetarget && !(linetarget->flags & MF_SHADOW)) - crosshair_target = linetarget; - - if (hud_crosshair_target && crosshair_target) - { - // [Alaux] Color crosshair by target health - if (hud_crosshair_target == crosstarget_health) - { - crosshair.cr = ColorByHealth(crosshair_target->health, crosshair_target->info->spawnhealth, false); - } - else - { - crosshair.cr = colrngs[hud_crosshair_target_color]; - } - } - } -} - -void HU_UpdateCrosshairLock(int x, int y) -{ - int w = (crosshair.w * video.xscale) >> FRACBITS; - int h = (crosshair.h * video.yscale) >> FRACBITS; - - x = viewwindowx + BETWEEN(w, viewwidth - w - 1, x); - y = viewwindowy + BETWEEN(h, viewheight - h - 1, y); - - crosshair.x = (x << FRACBITS) / video.xscale - video.deltaw; - crosshair.y = (y << FRACBITS) / video.yscale; -} - -void HU_DrawCrosshair(void) -{ - if (plr->playerstate != PST_LIVE || - automapactive || - menuactive || - paused) - { - return; - } - - if (crosshair.patch) - V_DrawPatchTranslated(crosshair.x - crosshair.w, - crosshair.y - crosshair.h, - crosshair.patch, crosshair.cr); -} - // [crispy] print a bar indicating demo progress at the bottom of the screen boolean HU_DemoProgressBar(boolean force) { diff --git a/src/hu_stuff.h b/src/hu_stuff.h index 97facf03..3c7d7abb 100644 --- a/src/hu_stuff.h +++ b/src/hu_stuff.h @@ -88,8 +88,6 @@ enum extern int hud_type; extern boolean draw_crispy_hud; -extern int hud_crosshair; - enum { HUD_WIDGET_OFF, @@ -99,28 +97,10 @@ enum HUD_WIDGET_ADVANCED, }; -typedef enum -{ - crosstarget_off, - crosstarget_highlight, - crosstarget_health, // [Alaux] Color crosshair by target health -} crosstarget_t; -extern crosstarget_t hud_crosshair_target; - -// [Alaux] Lock crosshair on target -extern boolean hud_crosshair_lockon; -extern struct mobj_s *crosshair_target; -void HU_UpdateCrosshairLock(int x, int y); -void HU_DrawCrosshair(void); - -extern int hud_crosshair_color; - -#define HU_CROSSHAIRS 10 -extern const char *crosshair_lumps[HU_CROSSHAIRS]; -extern const char *crosshair_strings[HU_CROSSHAIRS]; - void HU_BindHUDVariables(void); +byte* HU_ColorByHealth(int health, int maxhealth, boolean invul); + #endif //---------------------------------------------------------------------------- diff --git a/src/mn_setup.c b/src/mn_setup.c index 35e7fc65..25395b87 100644 --- a/src/mn_setup.c +++ b/src/mn_setup.c @@ -22,6 +22,7 @@ #include "doomstat.h" #include "doomtype.h" #include "g_game.h" +#include "hu_crosshair.h" #include "hu_lib.h" #include "hu_stuff.h" #include "i_gamepad.h" diff --git a/src/r_things.c b/src/r_things.c index 68f5a018..3fdfaac8 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -24,7 +24,7 @@ #include "d_player.h" #include "doomdef.h" #include "doomstat.h" -#include "hu_stuff.h" // [Alaux] Lock crosshair on target +#include "hu_crosshair.h" // [Alaux] Lock crosshair on target #include "i_printf.h" #include "i_system.h" #include "i_video.h"