From 363223186e33d59fa6771c45a0b890b3d7c3ef8b Mon Sep 17 00:00:00 2001 From: TimeServ Date: Fri, 14 Apr 2006 04:31:29 +0000 Subject: [PATCH] change crosshairimage/crosshair/crosshaircolor to use callbacks git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@2206 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/client/view.c | 4 +- engine/common/cvar.c | 42 ++++++++++------- engine/common/cvar.h | 6 +++ engine/gl/gl_draw.c | 110 ++++++++++++++++++++++--------------------- engine/gl/gl_rmisc.c | 14 ++++++ engine/sw/r_main.c | 8 ++++ engine/sw/sw_draw.c | 11 ++--- 7 files changed, 117 insertions(+), 78 deletions(-) diff --git a/engine/client/view.c b/engine/client/view.c index 5889e9fdd..fdac17112 100644 --- a/engine/client/view.c +++ b/engine/client/view.c @@ -74,13 +74,13 @@ cvar_t v_ipitch_level = SCVAR("v_ipitch_level", "0.3"); cvar_t v_idlescale = SCVAR("v_idlescale", "0"); cvar_t crosshair = SCVARF("crosshair", "0", CVAR_ARCHIVE); -cvar_t crosshaircolor = SCVARF("crosshaircolor", "79", CVAR_ARCHIVE); +cvar_t crosshaircolor = SCVARF("crosshaircolor", "79", CVAR_ARCHIVE | CVAR_RENDERERCALLBACK); cvar_t crosshairsize = SCVARF("crosshairsize", "8", CVAR_ARCHIVE); cvar_t cl_crossx = SCVARF("cl_crossx", "0", CVAR_ARCHIVE); cvar_t cl_crossy = SCVARF("cl_crossy", "0", CVAR_ARCHIVE); cvar_t crosshaircorrect = SCVARF("crosshaircorrect", "0", CVAR_SEMICHEAT); -cvar_t crosshairimage = SCVAR("crosshairimage", ""); +cvar_t crosshairimage = SCVARF("crosshairimage", "", CVAR_RENDERERCALLBACK); cvar_t crosshairalpha = SCVAR("crosshairalpha", "1"); cvar_t gl_cshiftpercent = SCVAR("gl_cshiftpercent", "100"); diff --git a/engine/common/cvar.c b/engine/common/cvar.c index 60086e861..40c09bc35 100644 --- a/engine/common/cvar.c +++ b/engine/common/cvar.c @@ -763,22 +763,6 @@ void Cvar_ForceCheatVars(qboolean semicheats, qboolean absolutecheats) } } -void Cvar_ApplyCallbacks(int callbackflag) -{ - cvar_group_t *grp; - cvar_t *var; - - for (grp=cvar_groups ; grp ; grp=grp->next) - for (var=grp->cvars ; var ; var=var->next) - { - if (var->flags & callbackflag) - { - if (var->callback) - var->callback(var, var->string); - } - } -} - void Cvar_ApplyLatches(int latchflag) { cvar_group_t *grp; @@ -1127,6 +1111,32 @@ void Cvar_WriteVariables (vfsfile_t *f, qboolean all) } } +void Cvar_Hook(cvar_t *cvar, void (*callback) (struct cvar_s *var, char *oldvalue)) +{ + cvar->callback = callback; +} + +void Cvar_Unhook(cvar_t *cvar) +{ + cvar->callback = NULL; +} + +void Cvar_ApplyCallbacks(int callbackflag) +{ + cvar_group_t *grp; + cvar_t *var; + + for (grp=cvar_groups ; grp ; grp=grp->next) + for (var=grp->cvars ; var ; var=var->next) + { + if (var->flags & callbackflag) + { + if (var->callback) + var->callback(var, var->string); + } + } +} + void Cvar_Shutdown(void) { cvar_t *var; diff --git a/engine/common/cvar.h b/engine/common/cvar.h index fc307381b..05b654fea 100644 --- a/engine/common/cvar.h +++ b/engine/common/cvar.h @@ -131,6 +131,12 @@ void Cvar_SetValue (cvar_t *var, float value); void Cvar_ApplyLatches(int latchflag); //sets vars to thier latched values +void Cvar_Hook(cvar_t *cvar, void (*callback) (struct cvar_s *var, char *oldvalue)); +//hook a cvar with a given callback function at runtime + +void Cvar_Unhook(cvar_t *cvar); +//unhook a cvar + void Cvar_ApplyCallbacks(int callbackflag); //forces callbacks to be ran for given flags diff --git a/engine/gl/gl_draw.c b/engine/gl/gl_draw.c index fc5c2c8e5..e5580bb40 100644 --- a/engine/gl/gl_draw.c +++ b/engine/gl/gl_draw.c @@ -90,6 +90,7 @@ float custom_char_instep, default_char_instep; //to avoid blending issues float char_instep; static unsigned cs_data[16*16]; +static int externalhair; typedef struct { @@ -1347,20 +1348,66 @@ void GLDraw_Alt_String (int x, int y, const qbyte *str) vec3_t chcolor; int chmodified; +void GLCrosshairimage_Callback(struct cvar_s *var, char *oldvalue) +{ + if (*(var->string)) + externalhair = Mod_LoadHiResTexture (var->string, "crosshairs", false, true, true); +} + +void GLCrosshair_Callback(struct cvar_s *var, char *oldvalue) +{ + unsigned int c, c2; + + if (!var->value) + return; + + c = (unsigned int)(chcolor[0] * 255) | // red + ((unsigned int)(chcolor[1] * 255) << 8) | // green + ((unsigned int)(chcolor[2] * 255) << 16) | // blue + 0xff000000; // alpha + c2 = c; + +#define Pix(x,y,c) { \ + if (y+8<0)c=0; \ + if (y+8>=16)c=0; \ + if (x+8<0)c=0; \ + if (x+8>=16)c=0; \ + \ + cs_data[(y+8)*16+(x+8)] = c; \ + } + memset(cs_data, 0, sizeof(cs_data)); + switch((int)var->value) + { + default: +#include "crosshairs.dat" + } +#undef Pix + + GL_Bind (cs_texture); + GL_Upload32(NULL, cs_data, 16, 16, 0, true); + +} + +void GLCrosshaircolor_Callback(struct cvar_s *var, char *oldvalue) +{ + SCR_StringToRGB(var->string, chcolor, 255); + + chcolor[0] = bound(0, chcolor[0], 1); + chcolor[1] = bound(0, chcolor[1], 1); + chcolor[2] = bound(0, chcolor[2], 1); + + GLCrosshair_Callback(&crosshair, ""); +} + void GLDraw_Crosshair(void) { int x, y; int sc; - static int externalhair; float x1, x2, y1, y2; float size, chc; - int c2, c, usecolor; - int chrebuild; - - usecolor = 0; - c2 = c = 0; // shut up msvc + int usecolor = 0; if (crosshair.value == 1 && !*crosshairimage.string) { @@ -1373,36 +1420,9 @@ void GLDraw_Crosshair(void) } GL_TexEnv(GL_MODULATE); - chrebuild = chmodified != crosshaircolor.modified || - crosshair.modified || - (*crosshairimage.string && crosshairimage.modified) || - crosshair.value >= FIRSTANIMATEDCROSHAIR; - - if (chrebuild) - { - SCR_StringToRGB(crosshaircolor.string, chcolor, 255); - - chcolor[0] = bound(0, chcolor[0], 1); - chcolor[1] = bound(0, chcolor[1], 1); - chcolor[2] = bound(0, chcolor[2], 1); - - c = (int)(chcolor[0] * 255) | // red - ((int)(chcolor[1] * 255) << 8) | // green - ((int)(chcolor[2] * 255) << 16) | // blue - 0xff000000; // alpha - c2 = c; - - chmodified = crosshaircolor.modified; - } - if (*crosshairimage.string) { usecolor = 1; - if (crosshairimage.modified) - { - crosshairimage.modified = false; - externalhair = Mod_LoadHiResTexture (crosshairimage.string, "crosshairs", false, true, true); - } GL_Bind (externalhair); chc = 0; @@ -1414,28 +1434,10 @@ void GLDraw_Crosshair(void) GL_Bind (cs_texture); chc = 1/16.0; - if (chrebuild) - { - crosshair.modified = false; + // force crosshair refresh with animated crosshairs + if (crosshair.value >= FIRSTANIMATEDCROSHAIR) + GLCrosshair_Callback(&crosshair, ""); -#define Pix(x,y,c) { \ - if (y+8<0)c=0; \ - if (y+8>=16)c=0; \ - if (x+8<0)c=0; \ - if (x+8>=16)c=0; \ - \ - cs_data[(y+8)*16+(x+8)] = c; \ - } - memset(cs_data, 0, sizeof(cs_data)); - switch((int)crosshair.value) - { - default: -#include "crosshairs.dat" - } - GL_Upload32(NULL, cs_data, 16, 16, 0, true); - -#undef Pix - } if (crosshairsize.value <= 16) { qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); diff --git a/engine/gl/gl_rmisc.c b/engine/gl/gl_rmisc.c index 08fb93ab5..883548b40 100644 --- a/engine/gl/gl_rmisc.c +++ b/engine/gl/gl_rmisc.c @@ -657,6 +657,12 @@ void GLR_TimeRefresh_f (void); extern cvar_t gl_bump; extern cvar_t r_stains, r_stainfadetime, r_stainfadeammount; +// callback defines +extern cvar_t crosshair, crosshairimage, crosshaircolor; +void GLCrosshairimage_Callback(struct cvar_s *var, char *oldvalue); +void GLCrosshair_Callback(struct cvar_s *var, char *oldvalue); +void GLCrosshaircolor_Callback(struct cvar_s *var, char *oldvalue); + void GLR_DeInit (void) { Cmd_RemoveCommand ("timerefresh"); @@ -665,6 +671,10 @@ void GLR_DeInit (void) Cmd_RemoveCommand ("makewad"); + Cvar_Unhook(&crosshair); + Cvar_Unhook(&crosshairimage); + Cvar_Unhook(&crosshaircolor); + GLDraw_DeInit(); GLSurf_DeInit(); @@ -677,6 +687,10 @@ void GLR_Init (void) // Cmd_AddRemCommand ("makewad", R_MakeTexWad_f); + Cvar_Hook(&crosshair, GLCrosshair_Callback); + Cvar_Hook(&crosshairimage, GLCrosshairimage_Callback); + Cvar_Hook(&crosshaircolor, GLCrosshaircolor_Callback); + R_InitBubble(); GLR_ReInit(); diff --git a/engine/sw/r_main.c b/engine/sw/r_main.c index 43de441b7..0d8918bc8 100644 --- a/engine/sw/r_main.c +++ b/engine/sw/r_main.c @@ -203,11 +203,17 @@ void SWR_InitTextures (void) } }*/ +// callback declares +extern cvar_t crosshaircolor; +void SWCrosshaircolor_Callback(struct cvar_s *var, char *oldvalue); + void SWR_DeInit (void) { Cmd_RemoveCommand ("timerefresh"); Cmd_RemoveCommand ("pointfile"); + Cvar_Unhook(&crosshaircolor); + SWDraw_Shutdown(); D_Shutdown(); } @@ -228,6 +234,8 @@ void SWR_Init (void) Cmd_AddRemCommand ("timerefresh", SWR_TimeRefresh_f); + Cvar_Hook(&crosshaircolor, SWCrosshaircolor_Callback); + if (!r_maxedges.value) Cvar_SetValue (&r_maxedges, (float)NUMSTACKEDGES); if (!r_maxsurfs.value) diff --git a/engine/sw/sw_draw.c b/engine/sw/sw_draw.c index 4f6843e8f..7269a1958 100644 --- a/engine/sw/sw_draw.c +++ b/engine/sw/sw_draw.c @@ -909,6 +909,11 @@ void SWDraw_Pixel(int x, int y, qbyte color) } } +void SWCrosshaircolor_Callback(struct cvar_s *var, char *oldvalue) +{ + sw_crosshaircolor = SCR_StringToPalIndex(var->string, 255); +} + #include "crosshairs.dat" qbyte *COM_LoadFile (char *path, int usehunk); void SWDraw_Crosshair(void) @@ -919,12 +924,6 @@ void SWDraw_Crosshair(void) qbyte c, c2; int sc; - if (crosshaircolor.modified) - { // redo color every modification to crosshaircolor - sw_crosshaircolor = SCR_StringToPalIndex(crosshaircolor.string, 255); - crosshaircolor.modified = false; - } - c2 = c = sw_crosshaircolor; for (sc = 0; sc < cl.splitclients; sc++)