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