From 8fa6167a57b942ef5826874d896ac45a1a5cf8b5 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 14 Nov 2022 19:39:55 +0900 Subject: [PATCH] [vid] Add a function to set the palette and colormap Mostly for qwaq as it uses the default VGA palette but I need to do some testing with the quake palette. --- include/QF/vid.h | 1 + include/vid_internal.h | 1 + libs/video/targets/vid.c | 18 ++++++++++++++++-- libs/video/targets/vid_win.c | 22 +++++++++++++++------- libs/video/targets/vid_x11.c | 22 +++++++++++++++------- ruamoko/qwaq/builtins/graphics.c | 9 +++++++++ 6 files changed, 57 insertions(+), 16 deletions(-) diff --git a/include/QF/vid.h b/include/QF/vid.h index 6987ebfc8..2f78d7945 100644 --- a/include/QF/vid.h +++ b/include/QF/vid.h @@ -71,6 +71,7 @@ void VID_Init_Cvars (void); // the palette data will go away after the call, so it must be copied off if // the video driver will need it again void VID_Init (byte *palette, byte *colormap); +void VID_SetPalette (byte *palette, byte *colormap); void VID_SetCaption (const char *text); void VID_ClearMemory (void); diff --git a/include/vid_internal.h b/include/vid_internal.h index e79fa9a9b..967c4eacc 100644 --- a/include/vid_internal.h +++ b/include/vid_internal.h @@ -6,6 +6,7 @@ typedef struct vid_system_s { void (*init) (byte *palette, byte *colormap); + void (*set_palette) (byte *palette, byte *colormap); void (*init_cvars) (void); void (*update_fullscreen) (int fullscreen); } vid_system_t; diff --git a/libs/video/targets/vid.c b/libs/video/targets/vid.c index 51c235440..77cc55f30 100644 --- a/libs/video/targets/vid.c +++ b/libs/video/targets/vid.c @@ -246,7 +246,11 @@ VID_InitGamma (const byte *pal) { int i; double gamma = 1.0; + static int cvar_initialized; + free (viddef.gammatable); + free (viddef.palette); + free (viddef.palette32); viddef.gammatable = malloc (256); viddef.basepal = pal; viddef.palette = malloc (256 * 3); @@ -256,9 +260,13 @@ VID_InitGamma (const byte *pal) } gamma = bound (0.1, gamma, 9.9); - Cvar_Register (&vid_gamma_cvar, vid_gamma_f, 0); + if (!cvar_initialized) { + cvar_initialized = 1; + Cvar_Register (&vid_gamma_cvar, vid_gamma_f, 0); + } - VID_BuildGammaTable (vid_gamma); + //VID_BuildGammaTable (vid_gamma); + VID_UpdateGamma (); if (viddef.onPaletteChanged) { LISTENER_INVOKE (viddef.onPaletteChanged, &viddef); @@ -315,6 +323,12 @@ VID_Init (byte *palette, byte *colormap) vid_system.init (palette, colormap); } +VISIBLE void +VID_SetPalette (byte *palette, byte *colormap) +{ + vid_system.set_palette (palette, colormap); +} + static void vid_fullscreen_f (void *data, const cvar_t *var) { diff --git a/libs/video/targets/vid_win.c b/libs/video/targets/vid_win.c index 124a971c3..6b8de7445 100644 --- a/libs/video/targets/vid_win.c +++ b/libs/video/targets/vid_win.c @@ -117,6 +117,19 @@ VID_shutdown (void *data) Win_CloseDisplay (); } +static v oid +Win_VID_SetPalette (byte *palette, byte *colormap) +{ + viddef.colormap8 = colormap; + viddef.fullbright = 256 - viddef.colormap8[256 * VID_GRADES]; + if (vid_internal.set_colormap) { + vid_internal.set_colormap (vid_internal.data, colormap); + } + + VID_InitGamma (palette); + viddef.vid_internal->set_palette (win_sw_context, palette); +} + static void Win_VID_Init (byte *palette, byte *colormap) { @@ -131,11 +144,6 @@ Win_VID_Init (byte *palette, byte *colormap) R_LoadModule (&vid_internal); viddef.numpages = 1; - viddef.colormap8 = colormap; - viddef.fullbright = 256 - viddef.colormap8[256 * VID_GRADES]; - if (vid_internal.set_colormap) { - vid_internal.set_colormap (vid_internal.data, colormap); - } VID_GetWindowSize (640, 480); Win_OpenDisplay (); @@ -144,8 +152,7 @@ Win_VID_Init (byte *palette, byte *colormap) Win_CreateWindow (viddef.width, viddef.height); vid_internal.create_context (win_sw_context); - VID_InitGamma (palette); - viddef.vid_internal->set_palette (win_sw_context, palette); + Win_VID_SetPalette (palette, colormap); Sys_MaskPrintf (SYS_vid, "Video mode %dx%d initialized.\n", viddef.width, viddef.height); @@ -166,6 +173,7 @@ Win_VID_Init_Cvars (void) vid_system_t vid_system = { .init = Win_VID_Init, + .set_palette = Win_VID_SetPalette, .init_cvars = Win_VID_Init_Cvars, }; diff --git a/libs/video/targets/vid_x11.c b/libs/video/targets/vid_x11.c index e1b674e20..566953940 100644 --- a/libs/video/targets/vid_x11.c +++ b/libs/video/targets/vid_x11.c @@ -89,6 +89,19 @@ VID_shutdown (void *data) X11_CloseDisplay (); } +static void +X11_VID_SetPalette (byte *palette, byte *colormap) +{ + viddef.colormap8 = colormap; + viddef.fullbright = 256 - viddef.colormap8[256 * VID_GRADES]; + if (vid_internal.set_colormap) { + vid_internal.set_colormap (vid_internal.data, colormap); + } + + VID_InitGamma (palette); + vid_internal.set_palette (vid_internal.data, viddef.palette); +} + /* Set up color translation tables and the window. Takes a 256-color 8-bit palette. Palette data will go away after the call, so copy it if you'll @@ -108,11 +121,6 @@ X11_VID_Init (byte *palette, byte *colormap) R_LoadModule (&vid_internal); viddef.numpages = 2; - viddef.colormap8 = colormap; - viddef.fullbright = 256 - viddef.colormap8[256 * VID_GRADES]; - if (vid_internal.set_colormap) { - vid_internal.set_colormap (vid_internal.data, colormap); - } srandom (getpid ()); @@ -124,8 +132,7 @@ X11_VID_Init (byte *palette, byte *colormap) X11_CreateNullCursor (); // hide mouse pointer vid_internal.create_context (vid_internal.data); - VID_InitGamma (palette); - vid_internal.set_palette (vid_internal.data, viddef.palette); + X11_VID_SetPalette (palette, colormap); Sys_MaskPrintf (SYS_vid, "Video mode %dx%d initialized.\n", viddef.width, viddef.height); @@ -149,6 +156,7 @@ vid_system_t vid_system = { .init = X11_VID_Init, .init_cvars = X11_VID_Init_Cvars, .update_fullscreen = X11_UpdateFullscreen, + .set_palette = X11_VID_SetPalette, }; #if 0 diff --git a/ruamoko/qwaq/builtins/graphics.c b/ruamoko/qwaq/builtins/graphics.c index 7aa6d3037..a241e2826 100644 --- a/ruamoko/qwaq/builtins/graphics.c +++ b/ruamoko/qwaq/builtins/graphics.c @@ -124,6 +124,14 @@ bi_refresh_2d (progs_t *pr, void *_res) qc2d = P_FUNCTION (pr, 0); } +static void +bi_setpalette (progs_t *pr, void *_res) +{ + byte *palette = (byte *) P_GPOINTER (pr, 0); + byte *colormap = (byte *) P_GPOINTER (pr, 1); + VID_SetPalette (palette, colormap); +} + static void bi_shutdown (progs_t *pr, void *_res) { @@ -136,6 +144,7 @@ static builtin_t builtins[] = { bi(newscene, -1, 1, p(long)), bi(refresh, -1, 0), bi(refresh_2d, -1, 1, p(func)), + bi(setpalette, -1, 2, p(ptr), p(ptr)), bi(shutdown, -1, 0), {0} };