]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_crosshairs.c
c6a2b81c681ddb172aa4a4fc7de91674d64dacdd
[xonotic/darkplaces.git] / r_crosshairs.c
1 #include "quakedef.h"
2
3 cvar_t crosshair_brightness = {"crosshair_brightness", "1.0", true};
4 cvar_t crosshair_alpha = {"crosshair_alpha", "1.0", true};
5 cvar_t crosshair_flashspeed = {"crosshair_flashspeed", "2", true};
6 cvar_t crosshair_flashrange = {"crosshair_flashrange", "0.1", true};
7
8 #define NUMCROSSHAIRS 5
9
10 rtexture_t *crosshairtex[NUMCROSSHAIRS];
11
12 byte *crosshairtexdata[NUMCROSSHAIRS] =
13 {
14         "0000000000000000"
15         "0000000000000000"
16         "0000000000000000"
17         "0003300000033000"
18         "0003550000553000"
19         "0000577007750000"
20         "0000077007700000"
21         "0000000000000000"
22         "0000000000000000"
23         "0000077007700000"
24         "0000577007750000"
25         "0003550000553000"
26         "0003300000033000"
27         "0000000000000000"
28         "0000000000000000"
29         "0000000000000000"
30         ,
31         "0000000000000000"
32         "0000000000000000"
33         "0000000000000000"
34         "0003000000003000"
35         "0000500000050000"
36         "0000070000700000"
37         "0000007007000000"
38         "0000000000000000"
39         "0000000000000000"
40         "0000007007000000"
41         "0000070000700000"
42         "0000500000050000"
43         "0003000000003000"
44         "0000000000000000"
45         "0000000000000000"
46         "0000000000000000"
47         ,
48         "0000000000000000"
49         "0000000770000000"
50         "0000000770000000"
51         "0000000000000000"
52         "0000000000000000"
53         "0000000440000000"
54         "0000000440000000"
55         "0770044004400770"
56         "0770044004400770"
57         "0000000440000000"
58         "0000000440000000"
59         "0000000000000000"
60         "0000000770000000"
61         "0000000770000000"
62         "0000000000000000"
63         "0000000000000000"
64         ,
65         "0000000000000000"
66         "0000000000000000"
67         "0000000000000000"
68         "0000000000000000"
69         "0000000000000000"
70         "0000000000000000"
71         "0000000000000000"
72         "0000000000000000"
73         "0000000077777770"
74         "0000000075200000"
75         "0000000072000000"
76         "0000000070000000"
77         "0000000070000000"
78         "0000000070000000"
79         "0000000000000000"
80         "0000000000000000"
81         ,
82         "0000000000000000"
83         "0000000000000000"
84         "0000000000000000"
85         "0000000000000000"
86         "0000000000000000"
87         "0000000070000000"
88         "0000000000000000"
89         "0000000040000000"
90         "0000070404070000"
91         "0000000040000000"
92         "0000000000000000"
93         "0000000070000000"
94         "0000000000000000"
95         "0000000000000000"
96         "0000000000000000"
97         "0000000000000000"
98 };
99
100 void crosshairload(int num, byte *in)
101 {
102         int i;
103         byte data[16*16][4];
104         for (i = 0;i < 16*16;i++)
105         {
106                 data[i][0] = data[i][1] = data[i][2] = 255;
107                 data[i][3] = (in[i] - '0') * 255 / 7;
108         }
109         crosshairtex[num] = R_LoadTexture(va("crosshair%02d", num), 16, 16, &data[0][0], TEXF_ALPHA | TEXF_RGBA | TEXF_PRECACHE);
110 }
111
112 void r_crosshairs_start()
113 {
114         int i;
115         for (i = 0;i < NUMCROSSHAIRS;i++)
116                 crosshairload(i, crosshairtexdata[i]);
117 //      crosshairtex[1] = crosshairload(crosshairtex2);
118 }
119
120 void r_crosshairs_shutdown()
121 {
122 }
123
124 void R_Crosshairs_Init()
125 {
126         Cvar_RegisterVariable(&crosshair_brightness);
127         Cvar_RegisterVariable(&crosshair_alpha);
128         Cvar_RegisterVariable(&crosshair_flashspeed);
129         Cvar_RegisterVariable(&crosshair_flashrange);
130         R_RegisterModule("R_Crosshairs", r_crosshairs_start, r_crosshairs_shutdown);
131 }
132
133 void DrawCrosshair(int num)
134 {
135         byte *color;
136         float scale, base;
137 //      Draw_Character (r_refdef.vrect.x + r_refdef.vrect.width/2, r_refdef.vrect.y + r_refdef.vrect.height/2, '+');
138         if (num < 0 || num >= NUMCROSSHAIRS)
139                 num = 0;
140         if (cl.viewentity)
141         {
142                 int i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
143                 if (i >= 208 && i < 224) // blue
144                         i += 8;
145                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
146                         i += 15;
147                 color = (byte *) &d_8to24table[i];
148         }
149         else
150                 color = (byte *) &d_8to24table[15];
151         if (crosshair_flashspeed.value >= 0.01f)
152 //              scale = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value + 1.0f) * (1.0f / 255.0f);
153                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
154         else
155                 base = 0.0f;
156         scale = crosshair_brightness.value / 255.0f;
157         Draw_GenericPic(crosshairtex[num], color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value, r_refdef.vrect.x + r_refdef.vrect.width * 0.5f - 8.0f, r_refdef.vrect.y + r_refdef.vrect.height * 0.5f - 8.0f, 16.0f, 16.0f);
158 }
159