From cf391fb346c7f227d8598c9adaadcb8e57db1fa5 Mon Sep 17 00:00:00 2001 From: Alug Date: Tue, 22 Apr 2025 02:52:48 +0200 Subject: [PATCH] add cv_palette and cv_palettenum so palettes can be changed midgame note: subpalettes do not work properly in opengl with paletterendering disabled, so palettenum will NOT work there --- src/d_netcmd.c | 4 ++++ src/p_setup.c | 3 ++- src/v_video.c | 60 +++++++++++++++++++++++++++++++++++++------------- src/v_video.h | 12 +++++----- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/d_netcmd.c b/src/d_netcmd.c index ebbf91736..c0a7bfb93 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -39,6 +39,7 @@ #include "d_main.h" #include "m_random.h" #include "f_finale.h" +#include "v_video.h" #include "filesrch.h" #include "mserv.h" #include "md5.h" @@ -956,6 +957,9 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_audbuffersize); + CV_RegisterVar(&cv_palette); + CV_RegisterVar(&cv_palettenum); + // m_menu.c CV_RegisterVar(&cv_chatheight); CV_RegisterVar(&cv_chatwidth); diff --git a/src/p_setup.c b/src/p_setup.c index dfa7870e5..e6db8c94e 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -3014,7 +3014,7 @@ boolean P_SetupLevel(boolean fromnetsave, boolean reloadinggamestate) // Reset the palette now all fades have been done if (rendermode != render_none) - V_SetPaletteLump(GetPalette()); // Set the level palette + V_ReloadPalette(); // Set the level palette // Print "SPEEDING OFF TO [ZONE] [ACT 1]..." /*if (rendermode != render_none) @@ -3300,6 +3300,7 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, boolean local) { name = lumpinfo->name; lumpnum_t lumpnum = i|(wadnum<<16); + if (name[0] == 'D') { if (name[1] == 'S') for (j = 1; j < NUMSFX; j++) diff --git a/src/v_video.c b/src/v_video.c index ce54fa53c..6a7e2c30c 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -48,6 +48,9 @@ consvar_t cv_ticrate = {"showfps", "No", CV_SAVE, fps_cons_t, NULL, 0, NULL, NUL static void CV_palette_OnChange(void); +consvar_t cv_palette = {"palette", "", CV_CALL|CV_NOINIT, NULL, CV_palette_OnChange, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_palettenum = {"palettenum", "0", CV_CALL|CV_NOINIT, CV_Unsigned, CV_palette_OnChange, 0, NULL, NULL, 0, 0, NULL}; + static CV_PossibleValue_t gamma_cons_t[] = {{-15, "MIN"}, {4, "MAX"}, {0, NULL}}; consvar_t cv_globalgamma = {"gamma", "0", CV_SAVE|CV_CALL, gamma_cons_t, CV_palette_OnChange, 0, NULL, NULL, 0, 0, NULL}; @@ -91,6 +94,8 @@ consvar_t cv_menucaps = {"menucaps", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NUL // local copy of the palette for V_GetColor() RGBA_t *pLocalPalette = NULL; +static size_t currentPaletteSize; + /* The following was an extremely helpful resource when developing my Colour Cube LUT. http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter24.html @@ -411,13 +416,19 @@ const UINT8 gammatable[5][256] = // keep a copy of the palette so that we can get the RGB value for a color index at any time. static void LoadPalette(const char *lumpname) { + UINT8 *pal; + size_t i, palsize; + lumpnum_t lumpnum; + #ifdef BACKWARDSCOMPATCORRECTION const UINT8 *usegamma = gammatable[min(max(cv_globalgamma.value, 0), 4)]; #endif Cubeapply = InitCube(); - lumpnum_t lumpnum = W_GetNumForName(lumpname); - size_t i, palsize = W_LumpLength(lumpnum)/3; - UINT8 *pal; + + lumpnum = W_GetNumForName(lumpname); + + currentPaletteSize = W_LumpLength(lumpnum); + palsize = currentPaletteSize / 3; Z_Free(pLocalPalette); @@ -505,8 +516,23 @@ const char *R_GetPalname(UINT16 num) const char *GetPalette(void) { + const char *user = cv_palette.string; + + if (user && user[0]) + { + if (W_CheckNumForName(user) == LUMPERROR) + { + CONS_Alert(CONS_WARNING, "cv_palette %s lump does not exist\n", user); + } + else + { + return cv_palette.string; + } + } + if (gamestate == GS_LEVEL) return R_GetPalname((encoremode ? mapheaderinfo[gamemap-1]->encorepal : mapheaderinfo[gamemap-1]->palette)); + return "PLAYPAL"; } @@ -524,6 +550,20 @@ void V_SetPalette(INT32 palettenum) if (!pLocalPalette) V_ReloadPalette(); + if (rendermode == render_soft || (rendermode == render_opengl && HWR_ShouldUsePaletteRendering())) // opengl without paletterendering hates subpalettes + { + if (palettenum == 0) + { + palettenum = cv_palettenum.value; + + if (palettenum * 256U > currentPaletteSize - 256) + { + CONS_Alert(CONS_WARNING, "cv_palettenum %d out of range\n", palettenum); + palettenum = 0; + } + } + } + #ifdef HWRENDER if (rendermode == render_opengl) HWR_SetPalette(&pLocalPalette[palettenum*256]); @@ -538,18 +578,7 @@ void V_SetPalette(INT32 palettenum) void V_SetPaletteLump(const char *pal) { LoadPalette(pal); -#ifdef HWRENDER - if (rendermode == render_opengl) - HWR_SetPalette(pLocalPalette); -#if defined (__unix__) || defined (UNIXCOMMON) || defined (HAVE_SDL) - else -#endif -#endif - if (rendermode != render_none) - I_SetPalette(pLocalPalette); -#ifdef HASINVERT - R_MakeInvertmap(); -#endif + V_SetPalette(0); } static void CV_palette_OnChange(void) @@ -557,6 +586,7 @@ static void CV_palette_OnChange(void) if (loaded_config == false) return; // reload palette + // recalculate Color Cube V_ReloadPalette(); V_SetPalette(0); } diff --git a/src/v_video.h b/src/v_video.h index 85338751c..c8eca1069 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -28,12 +28,12 @@ extern UINT8 *screens[5]; -extern consvar_t cv_ticrate, cv_accuratefps, cv_allcaps, cv_constextsize, cv_menucaps,\ -cv_globalgamma, cv_globalsaturation,\ -cv_rhue, cv_yhue, cv_ghue, cv_chue, cv_bhue, cv_mhue,\ -cv_rgamma, cv_ygamma, cv_ggamma, cv_cgamma, cv_bgamma, cv_mgamma, \ -cv_rsaturation, cv_ysaturation, cv_gsaturation, cv_csaturation, cv_bsaturation, cv_msaturation; - +extern consvar_t cv_ticrate, cv_accuratefps, cv_allcaps, cv_constextsize, cv_menucaps, +cv_globalgamma, cv_globalsaturation, +cv_rhue, cv_yhue, cv_ghue, cv_chue, cv_bhue, cv_mhue, +cv_rgamma, cv_ygamma, cv_ggamma, cv_cgamma, cv_bgamma, cv_mgamma, +cv_rsaturation, cv_ysaturation, cv_gsaturation, cv_csaturation, cv_bsaturation, cv_msaturation, +cv_palette, cv_palettenum; // Allocates buffer screens, call before R_Init. void V_Init(void);