3 #include "cl_collision.h"
5 cvar_t crosshair_brightness = {CVAR_SAVE, "crosshair_brightness", "1", "how bright the crosshair should be"};
6 cvar_t crosshair_alpha = {CVAR_SAVE, "crosshair_alpha", "1", "how opaque the crosshair should be"};
7 cvar_t crosshair_flashspeed = {CVAR_SAVE, "crosshair_flashspeed", "2", "speed at which the crosshair flashes"};
8 cvar_t crosshair_flashrange = {CVAR_SAVE, "crosshair_flashrange", "0.1", "how much the crosshair flashes"};
9 cvar_t crosshair_size = {CVAR_SAVE, "crosshair_size", "1", "adjusts size of the crosshair on the screen"};
10 cvar_t crosshair_static = {CVAR_SAVE, "crosshair_static", "1", "if 1 the crosshair is a 2D overlay, if 0 it is a sprite in the world indicating where your weapon will hit in standard quake mods (if the mod has the weapon somewhere else this won't be accurate)"};
12 // must match NUMCROSSHAIRS in gl_draw.c
13 #define NUMCROSSHAIRS 6
15 void R_Crosshairs_Init(void)
17 Cvar_RegisterVariable(&crosshair_brightness);
18 Cvar_RegisterVariable(&crosshair_alpha);
19 Cvar_RegisterVariable(&crosshair_flashspeed);
20 Cvar_RegisterVariable(&crosshair_flashrange);
21 Cvar_RegisterVariable(&crosshair_size);
22 Cvar_RegisterVariable(&crosshair_static);
25 void R_GetCrosshairColor(float *out)
30 if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
32 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
33 if (i >= 208 && i < 224) // blue
35 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
40 color = (unsigned char *) &palette_complete[i];
41 if (crosshair_flashspeed.value >= 0.01f)
42 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
45 scale = crosshair_brightness.value * (1.0f / 255.0f);
46 out[0] = color[0] * scale + base;
47 out[1] = color[1] * scale + base;
48 out[2] = color[2] * scale + base;
49 out[3] = crosshair_alpha.value;
51 // clamp the colors and alpha
52 out[0] = bound(0, out[0], 1);
53 out[1] = bound(0, out[1], 1);
54 out[2] = bound(0, out[2], 1);
55 out[3] = bound(0, out[3], 1.0f);
58 void R_DrawWorldCrosshair(void)
62 vec3_t v1, v2, spriteorigin;
66 if (r_letterbox.value)
68 if (crosshair_static.integer)
70 num = crosshair.integer;
71 if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
73 if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
75 pic = Draw_CachePic(va("gfx/crosshair%i", num), true);
78 R_GetCrosshairColor(color);
80 // trace the shot path up to a certain distance
81 Matrix4x4_OriginFromMatrix(&cl_entities[cl.viewentity].render.matrix, v1);
82 v1[2] += 16; // HACK: this depends on the QC
84 // get the forward vector for the gun (not the view)
85 AngleVectors(cl.viewangles, v2, NULL, NULL);
86 //VectorCopy(r_vieworigin, v1);
87 VectorMA(v1, 8192, v2, v2);
88 trace = CL_TraceBox(v1, vec3_origin, vec3_origin, v2, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY, false);
89 spritescale = trace.fraction * (8192.0f / 40.0f) * crosshair_size.value;
90 VectorCopy(trace.endpos, spriteorigin);
93 R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, pic->tex, NULL, true, spriteorigin, r_viewright, r_viewup, spritescale, -spritescale, -spritescale, spritescale, color[0], color[1], color[2], color[3]);
96 void R_Draw2DCrosshair(void)
101 if (r_letterbox.value)
103 if (!crosshair_static.integer)
105 num = crosshair.integer;
106 if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
108 if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
110 pic = Draw_CachePic(va("gfx/crosshair%i", num), true);
113 R_GetCrosshairColor(color);
114 DrawQ_Pic((vid_conwidth.integer - pic->width * crosshair_size.value) * 0.5f, (vid_conheight.integer - pic->height * crosshair_size.value) * 0.5f, pic->name, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0], color[1], color[2], color[3], 0);