Static/floating crosshair is now an option controled by the cvar "crosshair_static"
[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 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
16 {
17         "................"
18         "................"
19         "................"
20         "...33......33..."
21         "...355....553..."
22         "....577..775...."
23         ".....77..77....."
24         "................"
25         "................"
26         ".....77..77....."
27         "....577..775...."
28         "...355....553..."
29         "...33......33..."
30         "................"
31         "................"
32         "................"
33         ,
34         "................"
35         "................"
36         "................"
37         "...3........3..."
38         "....5......5...."
39         ".....7....7....."
40         "......7..7......"
41         "................"
42         "................"
43         "......7..7......"
44         ".....7....7....."
45         "....5......5...."
46         "...3........3..."
47         "................"
48         "................"
49         "................"
50         ,
51         "................"
52         ".......77......."
53         ".......77......."
54         "................"
55         "................"
56         ".......44......."
57         ".......44......."
58         ".77..44..44..77."
59         ".77..44..44..77."
60         ".......44......."
61         ".......44......."
62         "................"
63         ".......77......."
64         ".......77......."
65         "................"
66         "................"
67         ,
68         "................"
69         "................"
70         "................"
71         "................"
72         "................"
73         "................"
74         "................"
75         "................"
76         "........7777777."
77         "........752....."
78         "........72......"
79         "........7......."
80         "........7......."
81         "........7......."
82         "................"
83         "................"
84         ,
85         "................"
86         "................"
87         "................"
88         "................"
89         "................"
90         "........7......."
91         "................"
92         "........4......."
93         ".....7.4.4.7...."
94         "........4......."
95         "................"
96         "........7......."
97         "................"
98         "................"
99         "................"
100         "................"
101 };
102
103 rtexturepool_t *crosshairtexturepool;
104 rtexture_t *crosshairtexture[NUMCROSSHAIRS];
105
106 void r_crosshairs_start(void)
107 {
108         int num;
109         int i;
110         char *in;
111         qbyte data[16*16][4];
112         crosshairtexturepool = R_AllocTexturePool();
113         for (num = 0;num < 5;num++)
114         {
115                 in = crosshairtexdata[num];
116                 for (i = 0;i < 16*16;i++)
117                 {
118                         if (in[i] == '.')
119                         {
120                                 data[i][0] = 255;
121                                 data[i][1] = 255;
122                                 data[i][2] = 255;
123                                 data[i][3] = 0;
124                         }
125                         else
126                         {
127                                 data[i][0] = 255;
128                                 data[i][1] = 255;
129                                 data[i][2] = 255;
130                                 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
131                         }
132                 }
133                 crosshairtexture[num] = R_LoadTexture(crosshairtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
134         }
135 }
136
137 void r_crosshairs_shutdown(void)
138 {
139         R_FreeTexturePool(&crosshairtexturepool);
140 }
141
142 void r_crosshairs_newmap(void)
143 {
144 }
145
146 void R_Crosshairs_Init(void)
147 {
148         Cvar_RegisterVariable(&crosshair_brightness);
149         Cvar_RegisterVariable(&crosshair_alpha);
150         Cvar_RegisterVariable(&crosshair_flashspeed);
151         Cvar_RegisterVariable(&crosshair_flashrange);
152         Cvar_RegisterVariable(&crosshair_size);
153         Cvar_RegisterVariable(&crosshair_static);
154         R_RegisterModule("R_Crosshair", r_crosshairs_start, r_crosshairs_shutdown, r_crosshairs_newmap);
155 }
156
157 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
158 {
159         rmeshbufferinfo_t m;
160         float diff[3];
161
162         if (fogenabled)
163         {
164                 VectorSubtract(origin, r_origin, diff);
165                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
166         }
167
168         memset(&m, 0, sizeof(m));
169         m.transparent = false;
170         m.blendfunc1 = GL_SRC_ALPHA;
171         m.blendfunc2 = GL_ONE;
172         m.depthdisable = true;
173         m.numtriangles = 2;
174         m.numverts = 4;
175         m.tex[0] = R_GetTexture(texture);
176         if (R_Mesh_Draw_GetBuffer(&m))
177         {
178                 m.index[0] = 0;
179                 m.index[1] = 1;
180                 m.index[2] = 2;
181                 m.index[3] = 0;
182                 m.index[4] = 2;
183                 m.index[5] = 3;
184                 m.color[0] = m.color[4] = m.color[8] = m.color[12] = cr * m.colorscale;
185                 m.color[1] = m.color[5] = m.color[9] = m.color[13] = cg * m.colorscale;
186                 m.color[2] = m.color[6] = m.color[10] = m.color[14] = cb * m.colorscale;
187                 m.color[3] = m.color[7] = m.color[11] = m.color[15] = ca * m.colorscale;
188                 m.texcoords[0][0] = 0;
189                 m.texcoords[0][1] = 0;
190                 m.texcoords[0][2] = 0;
191                 m.texcoords[0][3] = 1;
192                 m.texcoords[0][4] = 1;
193                 m.texcoords[0][5] = 1;
194                 m.texcoords[0][6] = 1;
195                 m.texcoords[0][7] = 0;
196                 m.vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
197                 m.vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
198                 m.vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
199                 m.vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
200                 m.vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
201                 m.vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
202                 m.vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
203                 m.vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
204                 m.vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
205                 m.vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
206                 m.vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
207                 m.vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
208         }
209 }
210
211 void R_DrawCrosshair(void)
212 {
213         int i, num;
214         qbyte *color;
215         float scale, base;
216         vec3_t v1, v2, spriteorigin;
217         vec_t spritescale;
218         float cr, cg, cb, ca;
219         num = crosshair.integer - 1;
220         if (num < 0)
221                 return;
222         if (num >= NUMCROSSHAIRS)
223                 num = 0;
224         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
225         {
226                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
227                 if (i >= 208 && i < 224) // blue
228                         i += 8;
229                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
230                         i += 15;
231         }
232         else
233                 i = 15;
234         color = (qbyte *) &d_8to24table[i];
235         if (crosshair_flashspeed.value >= 0.01f)
236                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
237         else
238                 base = 0.0f;
239         scale = crosshair_brightness.value * (1.0f / 255.0f);
240
241         if (crosshair_static.integer)
242         {
243                 char *picname;
244                 cachepic_t *pic;
245
246                 picname = va("gfx/crosshair%i.tga", num + 1);
247                 pic = Draw_CachePic(picname);
248                 if (pic)
249                         DrawQ_Pic((vid.conwidth - pic->width * crosshair_size.value) * 0.5f, (vid.conheight - pic->height * crosshair_size.value) * 0.5f, picname, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value, 0);
250         }
251         else
252         {
253                 // trace the shot path up to a certain distance
254                 VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
255                 v1[2] += 16; // HACK: this depends on the QC
256                 // get the forward vector for the gun (not the view)
257                 AngleVectors(cl.viewangles, v2, NULL, NULL);
258                 //VectorCopy(r_origin, v1);
259                 VectorMA(v1, 8192, v2, v2);
260                 spritescale = 4.0f + (CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * 8192.0f) * (1.0f / 48.0f);
261                 spritescale = bound(0.0f, spritescale, 32.0f);
262                 //VectorMA(spriteorigin, -4, vpn, spriteorigin);
263
264                 cr = color[0] * scale + base;
265                 cg = color[1] * scale + base;
266                 cb = color[2] * scale + base;
267                 ca = crosshair_alpha.value;
268
269                 // clamp the colors so they don't go negative
270                 cr = max(0, cr);
271                 cg = max(0, cg);
272                 cb = max(0, cb);
273                 // might as well clamp the alpha
274                 ca = bound(0, ca, 1.0f);
275
276                 // finally draw the sprite
277                 R_DrawCrosshairSprite(crosshairtexture[num], spriteorigin, spritescale, cr, cg, cb, ca);
278         }
279 }
280
281