most of Q2's keyboard handling ported over - what this means: keypad is now separatel...
[divverent/darkplaces.git] / r_crosshairs.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t crosshair_brightness = {CVAR_SAVE, "crosshair_brightness", "1"};
6 cvar_t crosshair_alpha = {CVAR_SAVE, "crosshair_alpha", "1"};
7 cvar_t crosshair_flashspeed = {CVAR_SAVE, "crosshair_flashspeed", "2"};
8 cvar_t crosshair_flashrange = {CVAR_SAVE, "crosshair_flashrange", "0.1"};
9 cvar_t crosshair_size = {CVAR_SAVE, "crosshair_size", "1"};
10 cvar_t crosshair_static = {CVAR_SAVE, "crosshair_static", "0"};
11
12 // must match NUMCROSSHAIRS in gl_draw.c
13 #define NUMCROSSHAIRS 5
14
15 void R_Crosshairs_Init(void)
16 {
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);
23 }
24
25 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
26 {
27         rmeshstate_t m;
28         float diff[3];
29
30         if (fogenabled)
31         {
32                 VectorSubtract(origin, r_origin, diff);
33                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
34         }
35
36         memset(&m, 0, sizeof(m));
37         m.blendfunc1 = GL_SRC_ALPHA;
38         m.blendfunc2 = GL_ONE;
39         m.depthdisable = true;
40         m.tex[0] = R_GetTexture(texture);
41         R_Mesh_Matrix(&r_identitymatrix);
42         R_Mesh_State(&m);
43
44         GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
45         varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
46         varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
47         varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
48         varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
49         varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
50         varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
51         varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
52         varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
53         varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
54         varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
55         varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
56         varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
57         varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
58         varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
59         varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
60         varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
61         R_Mesh_Draw(4, 2, polygonelements);
62 }
63
64 void R_GetCrosshairColor(float *out)
65 {
66         int i;
67         qbyte *color;
68         float scale, base;
69         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
70         {
71                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
72                 if (i >= 208 && i < 224) // blue
73                         i += 8;
74                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
75                         i += 15;
76         }
77         else
78                 i = 15;
79         color = (qbyte *) &palette_complete[i];
80         if (crosshair_flashspeed.value >= 0.01f)
81                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
82         else
83                 base = 0.0f;
84         scale = crosshair_brightness.value * (1.0f / 255.0f);
85         out[0] = color[0] * scale + base;
86         out[1] = color[1] * scale + base;
87         out[2] = color[2] * scale + base;
88         out[3] = crosshair_alpha.value;
89
90         // clamp the colors and alpha
91         out[0] = bound(0, out[0], 1);
92         out[1] = bound(0, out[1], 1);
93         out[2] = bound(0, out[2], 1);
94         out[3] = bound(0, out[3], 1.0f);
95 }
96
97 void R_DrawWorldCrosshair(void)
98 {
99         int num;
100         cachepic_t *pic;
101         vec3_t v1, v2, spriteorigin;
102         vec_t spritescale;
103         vec4_t color;
104         if (crosshair_static.integer)
105                 return;
106         num = crosshair.integer;
107         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
108                 return;
109         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
110                 return;
111         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num));
112         if (!pic)
113                 return;
114         R_GetCrosshairColor(color);
115
116         // trace the shot path up to a certain distance
117         VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
118         v1[2] += 16; // HACK: this depends on the QC
119
120         // get the forward vector for the gun (not the view)
121         AngleVectors(cl.viewangles, v2, NULL, NULL);
122         //VectorCopy(r_origin, v1);
123         VectorMA(v1, 8192, v2, v2);
124         spritescale = CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true, NULL) * (8192.0f / 40.0f) * crosshair_size.value;
125
126         // draw the sprite
127         R_DrawCrosshairSprite(pic->tex, spriteorigin, spritescale, color[0], color[1], color[2], color[3]);
128 }
129
130 void R_Draw2DCrosshair(void)
131 {
132         int num;
133         cachepic_t *pic;
134         vec4_t color;
135         if (!crosshair_static.integer)
136                 return;
137         num = crosshair.integer;
138         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
139                 return;
140         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
141                 return;
142         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num));
143         if (pic)
144         {
145                 R_GetCrosshairColor(color);
146                 DrawQ_Pic((vid.conwidth - pic->width * crosshair_size.value) * 0.5f, (vid.conheight - 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);
147         }
148 }
149
150
151
152