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