]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/draw.qc
Improve g_freezetag_revive_speed description
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / draw.qc
1 #include "draw.qh"
2
3 #include <client/hud/hud.qh>
4
5 void drawborderlines(float thickness, vector pos, vector dim, vector color, float theAlpha, float drawflag)
6 {
7         vector line_dim = '0 0 0';
8
9         // left and right lines
10         pos.x -= thickness;
11         line_dim.x = thickness;
12         line_dim.y = dim.y;
13         drawfill(pos, line_dim, color, theAlpha, drawflag);
14         drawfill(pos + (dim.x + thickness) * '1 0 0', line_dim, color, theAlpha, drawflag);
15
16         // upper and lower lines
17         pos.y -= thickness;
18         line_dim.x = dim.x + thickness * 2; // make upper and lower lines longer
19         line_dim.y = thickness;
20         drawfill(pos, line_dim, color, theAlpha, drawflag);
21         drawfill(pos + (dim.y + thickness) * '0 1 0', line_dim, color, theAlpha, drawflag);
22 }
23
24 void drawpic_tiled(vector pos, string pic, vector sz, vector area, vector color, float theAlpha, float drawflag)
25 {
26         pos = HUD_Shift(pos);
27         sz = HUD_Scale(sz);
28         area = HUD_Scale(area);
29
30         vector current_pos = '0 0 0', end_pos, new_size = '0 0 0', ratio = '0 0 0';
31         end_pos = pos + area;
32
33         current_pos.y = pos.y;
34         while (current_pos.y < end_pos.y)
35         {
36                 current_pos.x = pos.x;
37                 while (current_pos.x < end_pos.x)
38                 {
39                         new_size.x = min(sz.x, end_pos.x - current_pos.x);
40                         new_size.y = min(sz.y, end_pos.y - current_pos.y);
41                         ratio.x = new_size.x / sz.x;
42                         ratio.y = new_size.y / sz.y;
43                         drawsubpic(current_pos, new_size, pic, '0 0 0', ratio, color, theAlpha, drawflag);
44                         current_pos.x += sz.x;
45                 }
46                 current_pos.y += sz.y;
47         }
48 }
49
50 float expandingbox_sizefactor_from_fadelerp(float fadelerp)
51 {
52         return 1.2 / (1.2 - fadelerp);
53 }
54
55 vector expandingbox_resize_centered_box_offset(float sz, vector boxsize, float boxxsizefactor)
56 {
57         boxsize.x *= boxxsizefactor; // easier interface for text
58         return boxsize * (0.5 * (1 - sz));
59 }
60
61 void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
62 {
63         float sz;
64         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
65
66         drawpic_aspect_skin(position + expandingbox_resize_centered_box_offset(sz, theScale, 1), pic, theScale * sz, rgb, theAlpha * (1 - fadelerp), flag);
67 }
68
69 void drawpic_aspect_skin_expanding_two(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
70 {
71         drawpic_aspect_skin_expanding(position, pic, theScale, rgb, theAlpha, flag, fadelerp);
72         drawpic_skin(position, pic, theScale, rgb, theAlpha * fadelerp, flag);
73 }
74
75 float stringwidth(string text, float handleColors, vector sz)
76 {
77         vector dfs = drawfontscale;
78         drawfontscale = '1 1 0';
79         float r = stringwidth_builtin(text, handleColors, sz);
80         drawfontscale = dfs;
81         return r;
82 }
83
84 #define SET_POS_AND_SZ_Y_ASPECT(allow_colors) MACRO_BEGIN \
85         float textaspect, oldsz; \
86         vector dfs = drawfontscale; \
87         drawfontscale = '1 1 0'; \
88         textaspect = stringwidth(text, allow_colors, '1 1 1' * sz.y) / sz.y; \
89         drawfontscale = dfs; \
90         if(sz.x/sz.y > textaspect) { \
91                 oldsz = sz.x; \
92                 sz.x = sz.y * textaspect; \
93                 pos.x += (oldsz - sz.x) * 0.5; \
94         } else { \
95                 oldsz = sz.y; \
96                 sz.y = sz.x / textaspect; \
97                 pos.y += (oldsz - sz.y) * 0.5; \
98         } \
99 MACRO_END
100
101 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
102 void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag) {
103         SET_POS_AND_SZ_Y_ASPECT(false);
104         drawstring(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag);
105 }
106
107 // drawstring wrapper to draw a colorcodedstring as large as possible with preserved aspect ratio into a box
108 void drawcolorcodedstring_aspect(vector pos, string text, vector sz, float theAlpha, float drawflag) {
109         SET_POS_AND_SZ_Y_ASPECT(true);
110         drawcolorcodedstring(pos, text, '1 1 0' * sz.y, theAlpha, drawflag);
111 }
112
113 void drawstring_expanding(vector position, string text, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
114 {
115         float sz;
116         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
117
118         drawfontscale = hud_scale * sz;
119         vector dfs = drawfontscale;
120         drawfontscale = sz * '1 1 0';
121         float textaspect = stringwidth_builtin(text, false, theScale * (sz / drawfontscale.x)) / (theScale.x * sz);
122         drawfontscale = dfs;
123         drawstring(position + expandingbox_resize_centered_box_offset(sz, theScale, textaspect), text, HUD_Scale(theScale * (sz / drawfontscale.x)), rgb, theAlpha * (1 - fadelerp), flag);
124         // width parameter:
125         //    (scale_x * sz / drawfontscale.x) * drawfontscale.x * SIZE1 / (scale_x * sz)
126         //    SIZE1
127         drawfontscale = hud_scale;
128 }
129
130 // drawstring wrapper to draw a string as large as possible with preserved aspect ratio into a box
131 void drawstring_aspect_expanding(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag, float fadelerp) {
132         SET_POS_AND_SZ_Y_ASPECT(false);
133         drawstring_expanding(pos, text, '1 1 0' * sz.y, color, theAlpha, drawflag, fadelerp);
134 }
135
136 void drawcolorcodedstring_expanding(vector position, string text, vector theScale, float theAlpha, float flag, float fadelerp)
137 {
138         float sz;
139         sz = expandingbox_sizefactor_from_fadelerp(fadelerp);
140
141         drawfontscale = hud_scale * sz;
142         // eventually replace with drawcolorcodedstring
143         drawcolorcodedstring(position + expandingbox_resize_centered_box_offset(sz, theScale, stringwidth_builtin(text, true, theScale * (sz / drawfontscale.x)) / (theScale.x * sz)), text, theScale * (sz / drawfontscale.x), theAlpha * (1 - fadelerp), flag);
144         drawfontscale = hud_scale;
145 }
146
147 void drawcolorcodedstring_aspect_expanding(vector pos, string text, vector sz, float theAlpha, float drawflag, float fadelerp) {
148         SET_POS_AND_SZ_Y_ASPECT(true);
149         drawcolorcodedstring_expanding(pos, text, '1 1 0' * sz.y, theAlpha, drawflag, fadelerp);
150 }
151
152 // this draws the triangles of a model DIRECTLY. Don't expect high performance, really...
153 float PolyDrawModelSurface(entity e, float i_s)
154 {
155         float i_t;
156         float n_t;
157         vector tri;
158         string tex;
159         tex = getsurfacetexture(e, i_s);
160         if (!tex)
161                 return 0; // this is beyond the last one
162         n_t = getsurfacenumtriangles(e, i_s);
163         for(i_t = 0; i_t < n_t; ++i_t)
164         {
165                 tri = getsurfacetriangle(e, i_s, i_t);
166                 R_BeginPolygon(tex, 0, false);
167                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.x), getsurfacepointattribute(e, i_s, tri.x, SPA_TEXCOORDS0), '1 1 1', 1);
168                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.y), getsurfacepointattribute(e, i_s, tri.y, SPA_TEXCOORDS0), '1 1 1', 1);
169                 R_PolygonVertex(getsurfacepoint(e, i_s, tri.z), getsurfacepointattribute(e, i_s, tri.z, SPA_TEXCOORDS0), '1 1 1', 1);
170                 R_EndPolygon();
171         }
172         return 1;
173 }
174 void PolyDrawModel(entity e)
175 {
176         float i_s;
177         for(i_s = 0; ; ++i_s)
178                 if(!PolyDrawModelSurface(e, i_s))
179                         break;
180 }
181
182 void DrawCircleClippedPic(vector centre, float radi, string pic, float f, vector rgb, float a, float drawflag)
183 {
184         vector ringsize, v, t;
185         ringsize = radi * '1 1 0';
186         centre = HUD_Shift(centre);
187         ringsize = HUD_Scale(ringsize);
188
189         if(f >= 1)
190         {
191                 // draw full rectangle
192                 R_BeginPolygon(pic, drawflag, true);
193                         v = centre;                     t = '0.5 0.5 0';
194                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
195                         R_PolygonVertex(v, t, rgb, a);
196
197                         v = centre;                     t = '0.5 0.5 0';
198                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
199                         R_PolygonVertex(v, t, rgb, a);
200
201                         v = centre;                     t = '0.5 0.5 0';
202                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
203                         R_PolygonVertex(v, t, rgb, a);
204
205                         v = centre;                     t = '0.5 0.5 0';
206                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
207                         R_PolygonVertex(v, t, rgb, a);
208                 R_EndPolygon();
209                 return;  // Complete rectangle, nothing more needed.
210         }
211
212         float co = cos(f * 2 * M_PI);
213         float si = sin(f * 2 * M_PI);
214         float q = fabs(co) + fabs(si);
215         co /= q;
216         si /= q;
217
218         if(f > 0.75)
219         {
220                 // draw upper half in full
221                 R_BeginPolygon(pic, drawflag, true);
222                         v = centre;                     t = '0.5 0.5 0';
223                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
224                         R_PolygonVertex(v, t, rgb, a);
225
226                         v = centre;                     t = '0.5 0.5 0';
227                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
228                         R_PolygonVertex(v, t, rgb, a);
229
230                         v = centre;                     t = '0.5 0.5 0';
231                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
232                         R_PolygonVertex(v, t, rgb, a);
233                 R_EndPolygon();
234                 // draw clipped lower half as a quad
235                 R_BeginPolygon(pic, drawflag, true);
236                         v = centre;                     t = '0.5 0.5 0';
237                         R_PolygonVertex(v, t, rgb, a);
238
239                         v = centre;                     t = '0.5 0.5 0';
240                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
241                         R_PolygonVertex(v, t, rgb, a);
242
243                         v = centre;                     t = '0.5 0.5 0';
244                         v.y -= 0.5 * ringsize.y;        t -= '0.5 -0.5 0';
245                         R_PolygonVertex(v, t, rgb, a);
246         }
247         else if(f > 0.5)
248         {
249                 // draw upper half in full
250                 R_BeginPolygon(pic, drawflag, true);
251                         v = centre;                     t = '0.5 0.5 0';
252                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
253                         R_PolygonVertex(v, t, rgb, a);
254
255                         v = centre;                     t = '0.5 0.5 0';
256                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
257                         R_PolygonVertex(v, t, rgb, a);
258
259                         v = centre;                     t = '0.5 0.5 0';
260                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
261                         R_PolygonVertex(v, t, rgb, a);
262                 R_EndPolygon();
263                 // draw clipped lower half as a triangle
264                 R_BeginPolygon(pic, drawflag, true);
265                         v = centre;                     t = '0.5 0.5 0';
266                         R_PolygonVertex(v, t, rgb, a);
267
268                         v = centre;                     t = '0.5 0.5 0';
269                         v.x -= 0.5 * ringsize.x;        t -= '0.5 0.5 0';
270                         R_PolygonVertex(v, t, rgb, a);
271         }
272         else if(f > 0.25)
273         {
274                 // draw clipped lower half as a quad
275                 R_BeginPolygon(pic, drawflag, true);
276                         v = centre;                     t = '0.5 0.5 0';
277                         R_PolygonVertex(v, t, rgb, a);
278
279                         v = centre;                     t = '0.5 0.5 0';
280                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
281                         R_PolygonVertex(v, t, rgb, a);
282
283                         v = centre;                     t = '0.5 0.5 0';
284                         v.y += 0.5 * ringsize.y;        t += '0.5 -0.5 0';
285                         R_PolygonVertex(v, t, rgb, a);
286         }
287         else if (f > 0)
288         {
289                 // draw clipped lower half as a triangle
290                 R_BeginPolygon(pic, drawflag, true);
291                         v = centre;                     t = '0.5 0.5 0';
292                         R_PolygonVertex(v, t, rgb, a);
293
294                         v = centre;                     t = '0.5 0.5 0';
295                         v.x += 0.5 * ringsize.x;        t += '0.5 0.5 0';
296                         R_PolygonVertex(v, t, rgb, a);
297         }
298         else
299         {
300                 // Nothing to draw.
301                 return;
302         }
303
304         // The last, moving vertex.
305                 v = centre;                     t = '0.5 0.5 0';
306                 v.x += co * 0.5 * ringsize.x;   t += co * '0.5 0.5 0';
307                 v.y += si * 0.5 * ringsize.y;   t += si * '0.5 -0.5 0';
308                 R_PolygonVertex(v, t, rgb, a);
309         R_EndPolygon();
310 }