added crosshair_useteamcolor, crosshair_color_red, crosshair_color_green, and crossha...
[divverent/darkplaces.git] / r_crosshairs.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t crosshair_useteamcolor = {CVAR_SAVE, "crosshair_useteamcolor", "0", "uses team color instead of customizable crosshair color"};
6 cvar_t crosshair_color_red = {CVAR_SAVE, "crosshair_color_red", "1", "customizable crosshair color"};
7 cvar_t crosshair_color_green = {CVAR_SAVE, "crosshair_color_green", "0", "customizable crosshair color"};
8 cvar_t crosshair_color_blue = {CVAR_SAVE, "crosshair_color_blue", "0", "customizable crosshair color"};
9 cvar_t crosshair_brightness = {CVAR_SAVE, "crosshair_brightness", "1", "how bright the crosshair should be"};
10 cvar_t crosshair_alpha = {CVAR_SAVE, "crosshair_alpha", "1", "how opaque the crosshair should be"};
11 cvar_t crosshair_flashspeed = {CVAR_SAVE, "crosshair_flashspeed", "2", "speed at which the crosshair flashes"};
12 cvar_t crosshair_flashrange = {CVAR_SAVE, "crosshair_flashrange", "0.1", "how much the crosshair flashes"};
13 cvar_t crosshair_size = {CVAR_SAVE, "crosshair_size", "1", "adjusts size of the crosshair on the screen"};
14 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)"};
15
16 void R_Crosshairs_Init(void)
17 {
18         Cvar_RegisterVariable(&crosshair_useteamcolor);
19         Cvar_RegisterVariable(&crosshair_color_red);
20         Cvar_RegisterVariable(&crosshair_color_green);
21         Cvar_RegisterVariable(&crosshair_color_blue);
22         Cvar_RegisterVariable(&crosshair_brightness);
23         Cvar_RegisterVariable(&crosshair_alpha);
24         Cvar_RegisterVariable(&crosshair_flashspeed);
25         Cvar_RegisterVariable(&crosshair_flashrange);
26         Cvar_RegisterVariable(&crosshair_size);
27         Cvar_RegisterVariable(&crosshair_static);
28 }
29
30 void R_GetCrosshairColor(float *out)
31 {
32         int i;
33         vec3_t color;
34         float scale, base;
35         if (crosshair_useteamcolor.integer && cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
36         {
37                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
38                 if (i >= 208 && i < 224) // blue
39                         i += 8;
40                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
41                         i += 15;
42                 VectorScale((unsigned char *) &palette_complete[i], (1.0f / 255.0f), color);
43         }
44         else
45                 VectorSet(color, crosshair_color_red.value, crosshair_color_green.value, crosshair_color_blue.value);
46         if (crosshair_flashspeed.value >= 0.01f)
47                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
48         else
49                 base = 0.0f;
50         scale = crosshair_brightness.value;
51         out[0] = color[0] * scale + base;
52         out[1] = color[1] * scale + base;
53         out[2] = color[2] * scale + base;
54         out[3] = crosshair_alpha.value;
55
56         // clamp the colors and alpha
57         out[0] = bound(0, out[0], 1);
58         out[1] = bound(0, out[1], 1);
59         out[2] = bound(0, out[2], 1);
60         out[3] = bound(0, out[3], 1.0f);
61 }
62
63 void R_DrawWorldCrosshair(void)
64 {
65         int num;
66         cachepic_t *pic;
67         vec3_t v1, v2, spriteorigin;
68         vec_t spritescale;
69         vec4_t color;
70         trace_t trace;
71         if (r_letterbox.value)
72                 return;
73         if (crosshair_static.integer)
74                 return;
75         num = crosshair.integer;
76         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
77                 return;
78         if (!cl.viewentity || !cl.entities[cl.viewentity].state_current.active)
79                 return;
80         pic = r_crosshairs[num];
81         if (!pic)
82                 return;
83         R_GetCrosshairColor(color);
84
85         // trace the shot path up to a certain distance
86         Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, v1);
87         v1[2] += 16; // HACK: this depends on the QC
88
89         // get the forward vector for the gun (not the view)
90         AngleVectors(cl.viewangles, v2, NULL, NULL);
91         //VectorCopy(r_view.origin, v1);
92         VectorMA(v1, 8192, v2, v2);
93         trace = CL_TraceBox(v1, vec3_origin, vec3_origin, v2, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_SKY, false);
94         spritescale = trace.fraction * (8192.0f / 40.0f) * crosshair_size.value;
95         VectorCopy(trace.endpos, spriteorigin);
96
97         // draw the sprite
98         R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, pic->tex, NULL, true, spriteorigin, r_view.right, r_view.up, spritescale, -spritescale, -spritescale, spritescale, color[0], color[1], color[2], color[3]);
99 }
100
101 void R_Draw2DCrosshair(void)
102 {
103         int num;
104         cachepic_t *pic;
105         vec4_t color;
106         if (r_letterbox.value)
107                 return;
108         if (!crosshair_static.integer)
109                 return;
110         num = crosshair.integer;
111         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
112                 return;
113         if (!cl.viewentity || !cl.entities[cl.viewentity].state_current.active)
114                 return;
115         pic = r_crosshairs[num];
116         if (pic)
117         {
118                 R_GetCrosshairColor(color);
119                 DrawQ_Pic((vid_conwidth.integer - pic->width * crosshair_size.value) * 0.5f, (vid_conheight.integer - pic->height * crosshair_size.value) * 0.5f, pic, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0], color[1], color[2], color[3], 0);
120         }
121 }
122
123
124
125