]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_crosshairs.c
-Added the parameter 'persistent'(naming?) to Draw_CachePic, which decides
[xonotic/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_GetCrosshairColor(float *out)
26 {
27         int i;
28         qbyte *color;
29         float scale, base;
30         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
31         {
32                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
33                 if (i >= 208 && i < 224) // blue
34                         i += 8;
35                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
36                         i += 15;
37         }
38         else
39                 i = 15;
40         color = (qbyte *) &palette_complete[i];
41         if (crosshair_flashspeed.value >= 0.01f)
42                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
43         else
44                 base = 0.0f;
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;
50
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);
56 }
57
58 void R_DrawWorldCrosshair(void)
59 {
60         int num;
61         cachepic_t *pic;
62         vec3_t v1, v2, spriteorigin;
63         vec_t spritescale;
64         vec4_t color;
65         if (r_letterbox.value)
66                 return;
67         if (crosshair_static.integer)
68                 return;
69         num = crosshair.integer;
70         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
71                 return;
72         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
73                 return;
74         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num), true);
75         if (!pic)
76                 return;
77         R_GetCrosshairColor(color);
78
79         // trace the shot path up to a certain distance
80         VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
81         v1[2] += 16; // HACK: this depends on the QC
82
83         // get the forward vector for the gun (not the view)
84         AngleVectors(cl.viewangles, v2, NULL, NULL);
85         //VectorCopy(r_vieworigin, v1);
86         VectorMA(v1, 8192, v2, v2);
87         spritescale = CL_TraceLine(v1, v2, spriteorigin, NULL, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY) * (8192.0f / 40.0f) * crosshair_size.value;
88
89         // draw the sprite
90         R_DrawSprite(GL_SRC_ALPHA, GL_ONE, pic->tex, true, spriteorigin, r_viewright, r_viewup, spritescale, -spritescale, -spritescale, spritescale, color[0], color[1], color[2], color[3]);
91 }
92
93 void R_Draw2DCrosshair(void)
94 {
95         int num;
96         cachepic_t *pic;
97         vec4_t color;
98         if (r_letterbox.value)
99                 return;
100         if (!crosshair_static.integer)
101                 return;
102         num = crosshair.integer;
103         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
104                 return;
105         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
106                 return;
107         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num), true);
108         if (pic)
109         {
110                 R_GetCrosshairColor(color);
111                 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);
112         }
113 }
114
115
116
117