5872d5697a287416338b054c717eae911e6c7410
[xonotic/darkplaces.git] / r_crosshairs.c
1 #include "quakedef.h"
2
3 cvar_t crosshair_brightness = {CVAR_SAVE, "crosshair_brightness", "1.0"};
4 cvar_t crosshair_alpha = {CVAR_SAVE, "crosshair_alpha", "1.0"};
5 cvar_t crosshair_flashspeed = {CVAR_SAVE, "crosshair_flashspeed", "2"};
6 cvar_t crosshair_flashrange = {CVAR_SAVE, "crosshair_flashrange", "0.1"};
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(void)
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(void)
121 {
122 }
123
124 void r_crosshairs_newmap(void)
125 {
126 }
127
128 void R_Crosshairs_Init(void)
129 {
130         Cvar_RegisterVariable(&crosshair_brightness);
131         Cvar_RegisterVariable(&crosshair_alpha);
132         Cvar_RegisterVariable(&crosshair_flashspeed);
133         Cvar_RegisterVariable(&crosshair_flashrange);
134         R_RegisterModule("R_Crosshairs", r_crosshairs_start, r_crosshairs_shutdown, r_crosshairs_newmap);
135 }
136
137 void DrawCrosshair(int num)
138 {
139         byte *color;
140         float scale, base;
141         if (num < 0 || num >= NUMCROSSHAIRS)
142                 num = 0;
143         if (cl.viewentity)
144         {
145                 int i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
146                 if (i >= 208 && i < 224) // blue
147                         i += 8;
148                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
149                         i += 15;
150                 color = (byte *) &d_8to24table[i];
151         }
152         else
153                 color = (byte *) &d_8to24table[15];
154         if (crosshair_flashspeed.value >= 0.01f)
155 //              scale = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value + 1.0f) * (1.0f / 255.0f);
156                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
157         else
158                 base = 0.0f;
159         scale = crosshair_brightness.value / 255.0f;
160         Draw_GenericPic(crosshairtex[num], color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value, vid.conwidth * 0.5f - 8.0f, vid.conheight * 0.5f - 8.0f, 16.0f, 16.0f);
161 }
162