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