+static float cachegamma, cachebrightness, cachecontrast, cacheblack[3], cachegrey[3], cachewhite[3];
+static int cachecolorenable, cachehwgamma;
+#define BOUNDCVAR(cvar, m1, m2) c = &(cvar);f = bound(m1, c->value, m2);if (c->value != f) Cvar_SetValueQuick(c, f);
+void VID_UpdateGamma(qboolean force)
+{
+ cvar_t *c;
+ float f;
+ static int forcenextframe = false;
+
+ // LordHavoc: don't mess with gamma tables if running dedicated
+ if (cls.state == ca_dedicated)
+ return;
+
+ if (!force
+ && !forcenextframe
+ && !v_psycho.integer
+ && vid_usinghwgamma == (vid_activewindow && v_hwgamma.integer)
+ && v_gamma.value == cachegamma
+ && v_contrast.value == cachecontrast
+ && v_brightness.value == cachebrightness
+ && cachecolorenable == v_color_enable.integer
+ && cacheblack[0] == v_color_black_r.value
+ && cacheblack[1] == v_color_black_g.value
+ && cacheblack[2] == v_color_black_b.value
+ && cachegrey[0] == v_color_grey_r.value
+ && cachegrey[1] == v_color_grey_g.value
+ && cachegrey[2] == v_color_grey_b.value
+ && cachewhite[0] == v_color_white_r.value
+ && cachewhite[1] == v_color_white_g.value
+ && cachewhite[2] == v_color_white_b.value)
+ return;
+
+ forcenextframe = false;
+
+ if (vid_activewindow && v_hwgamma.integer)
+ {
+ if (!vid_usinghwgamma)
+ {
+ vid_usinghwgamma = true;
+ Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_GetGamma(vid_systemgammaramps));
+ }
+
+ BOUNDCVAR(v_gamma, 0.1, 5);cachegamma = v_gamma.value;
+ BOUNDCVAR(v_contrast, 1, 5);cachecontrast = v_contrast.value;
+ BOUNDCVAR(v_brightness, 0, 0.8);cachebrightness = v_brightness.value;
+ BOUNDCVAR(v_color_black_r, 0, 0.8);cacheblack[0] = v_color_black_r.value;
+ BOUNDCVAR(v_color_black_g, 0, 0.8);cacheblack[1] = v_color_black_g.value;
+ BOUNDCVAR(v_color_black_b, 0, 0.8);cacheblack[2] = v_color_black_b.value;
+ BOUNDCVAR(v_color_grey_r, 0, 0.95);cachegrey[0] = v_color_grey_r.value;
+ BOUNDCVAR(v_color_grey_g, 0, 0.95);cachegrey[1] = v_color_grey_g.value;
+ BOUNDCVAR(v_color_grey_b, 0, 0.95);cachegrey[2] = v_color_grey_b.value;
+ BOUNDCVAR(v_color_white_r, 1, 5);cachewhite[0] = v_color_white_r.value;
+ BOUNDCVAR(v_color_white_g, 1, 5);cachewhite[1] = v_color_white_g.value;
+ BOUNDCVAR(v_color_white_b, 1, 5);cachewhite[2] = v_color_white_b.value;
+ cachecolorenable = v_color_enable.integer;
+ cachehwgamma = v_hwgamma.integer;
+
+ if (cachecolorenable)
+ {
+ BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[0]), cachewhite[0], cacheblack[0], vid_gammaramps);
+ BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[1]), cachewhite[1], cacheblack[1], vid_gammaramps + 256);
+ BuildGammaTable16(1.0f, invpow(0.5, 1 - cachegrey[2]), cachewhite[2], cacheblack[2], vid_gammaramps + 512);
+ }
+ else
+ {
+ BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps);
+ BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 256);
+ BuildGammaTable16(1.0f, cachegamma, cachecontrast, cachebrightness, vid_gammaramps + 512);
+ }
+
+ // LordHavoc: this code came from Ben Winslow and Zinx Verituse, I have
+ // immensely butchered it to work with variable framerates and fit in with
+ // the rest of darkplaces.
+ if (v_psycho.integer)
+ {
+ int x, y;
+ float t;
+ static float n[3], nd[3], nt[3];
+ static int init = true;
+ unsigned short *ramp;
+ forcenextframe = true;
+ if (init)
+ {
+ init = false;
+ for (x = 0;x < 3;x++)
+ {
+ n[x] = lhrandom(0, 1);
+ nd[x] = (rand()&1)?-0.25:0.25;
+ nt[x] = lhrandom(1, 8.2);
+ }
+ }
+
+ for (x = 0;x < 3;x++)
+ {
+ nt[x] -= host_realframetime;
+ if (nt[x] < 0)
+ {
+ nd[x] = -nd[x];
+ nt[x] += lhrandom(1, 8.2);
+ }
+ n[x] += nd[x] * host_realframetime;
+ n[x] -= floor(n[x]);
+ }
+
+ for (x = 0, ramp = vid_gammaramps;x < 3;x++)
+ for (y = 0, t = n[x] - 0.75f;y < 256;y++, t += 0.75f * (2.0f / 256.0f))
+ *ramp++ = cos(t*(M_PI*2.0)) * 32767.0f + 32767.0f;
+ }
+
+ Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_gammaramps));
+ }
+ else
+ {
+ if (vid_usinghwgamma)
+ {
+ vid_usinghwgamma = false;
+ Cvar_SetValueQuick(&vid_hardwaregammasupported, VID_SetGamma(vid_systemgammaramps));
+ }
+ }
+}
+
+void VID_RestoreSystemGamma(void)