Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
draw.qc
Go to the documentation of this file.
1#include "draw.qh"
2#include <common/util.qh>
3#include <common/constants.qh>
4
8
9void draw_setMousePointer(string pic, vector theSize, vector theOffset)
10{
11 draw_mousepointer = strzone(draw_UseSkinFor(pic));
12 draw_mousepointer_size = theSize;
13 draw_mousepointer_offset = eX * (theOffset.x * theSize.x) + eY * (theOffset.y * theSize.y);
14}
15
20
21void draw_reset(float cw, float ch, float ox, float oy)
22{
23 draw_shift = '1 0 0' * ox + '0 1 0' * oy;
24 draw_scale = '1 0 0' * cw + '0 1 0' * ch;
25 draw_alpha = 1;
26 draw_fontscale = '1 1 0';
28}
29
30vector globalToBox(vector v, vector theOrigin, vector theScale)
31{
32 v -= theOrigin;
33 v.x /= theScale.x;
34 v.y /= theScale.y;
35 return v;
36}
37
39{
40 v.x /= theScale.x;
41 v.y /= theScale.y;
42 return v;
43}
44
45vector boxToGlobal(vector v, vector theOrigin, vector theScale)
46{
47 v.x *= theScale.x;
48 v.y *= theScale.y;
49 v += theOrigin;
50 return v;
51}
52
54{
55 v.x *= theScale.x;
56 v.y *= theScale.y;
57 return v;
58}
59
60string draw_PreloadPicture(string pic)
61{
62 pic = draw_UseSkinFor(pic);
63 return precache_pic(pic);
64}
65
66string draw_PreloadPictureWithFlags(string pic, float f)
67{
68 pic = draw_UseSkinFor(pic);
69 return precache_pic(pic, f);
70}
71
72void draw_Picture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
73{
74 if (theSize.x == 0 || theSize.y <= 0) // no default sizing please
75 return;
76 pic = draw_UseSkinFor(pic);
77 drawpic(boxToGlobal(theOrigin, draw_shift, draw_scale), pic, boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
78}
79
81{
82 pic = draw_UseSkinFor(pic);
83 return drawgetimagesize(pic);
84}
85
86bool draw_PictureExists(string pic)
87{
88 pic = draw_UseSkinFor(pic);
89 if (fexists(strcat(pic, ".tga"))) return true;
90 if (fexists(strcat(pic, ".png"))) return true;
91 if (fexists(strcat(pic, ".jpg"))) return true;
92 if (fexists(strcat(pic, ".pcx"))) return true;
93
94 return false;
95}
96
97void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
98{
99 drawfill(boxToGlobal(theOrigin, draw_shift, draw_scale), boxToGlobalSize(theSize, draw_scale), theColor, theAlpha * draw_alpha, 0);
100}
101
102// a button picture is a texture containing three parts:
103// 1/4 width: left part
104// 1/2 width: middle part (stretched)
105// 1/4 width: right part
106// it is assumed to be 4x as wide as high for aspect ratio purposes, which
107// means, the parts are a square, two squares and a square.
108void draw_ButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
109{
110 vector square;
111 vector width, height;
112 vector bW;
113 pic = draw_UseSkinFor(pic);
114 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
115 theSize = boxToGlobalSize(theSize, draw_scale);
116 theAlpha *= draw_alpha;
117 width = eX * theSize.x;
118 height = eY * theSize.y;
119 if (theSize.x <= theSize.y * 2)
120 {
121 // button not wide enough
122 // draw just left and right part then
123 square = eX * theSize.x * 0.5;
124 bW = eX * (0.25 * theSize.x / (theSize.y * 2));
125 drawsubpic(theOrigin, square + height, pic, '0 0 0', eY + bW, theColor, theAlpha, 0);
126 drawsubpic(theOrigin + square, square + height, pic, eX - bW, eY + bW, theColor, theAlpha, 0);
127 }
128 else
129 {
130 square = eX * theSize.y;
131 drawsubpic(theOrigin, height + square, pic, '0 0 0', '0.25 1 0', theColor, theAlpha, 0);
132 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0.25 0 0', '0.5 1 0', theColor, theAlpha, 0);
133 drawsubpic(theOrigin + width - square, height + square, pic, '0.75 0 0', '0.25 1 0', theColor, theAlpha, 0);
134 }
135}
136
137// a vertical button picture is a texture containing three parts:
138// 1/4 height: left part
139// 1/2 height: middle part (stretched)
140// 1/4 height: right part
141// it is assumed to be 4x as high as wide for aspect ratio purposes, which
142// means, the parts are a square, two squares and a square.
143void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
144{
145 vector square;
146 vector width, height;
147 vector bH;
148 pic = draw_UseSkinFor(pic);
149 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
150 theSize = boxToGlobalSize(theSize, draw_scale);
151 theAlpha *= draw_alpha;
152 width = eX * theSize.x;
153 height = eY * theSize.y;
154 if (theSize.y <= theSize.x * 2)
155 {
156 // button not high enough
157 // draw just upper and lower part then
158 square = eY * theSize.y * 0.5;
159 bH = eY * (0.25 * theSize.y / (theSize.x * 2));
160 drawsubpic(theOrigin, square + width, pic, '0 0 0', eX + bH, theColor, theAlpha, 0);
161 drawsubpic(theOrigin + square, square + width, pic, eY - bH, eX + bH, theColor, theAlpha, 0);
162 }
163 else
164 {
165 square = eY * theSize.x;
166 drawsubpic(theOrigin, width + square, pic, '0 0 0', '1 0.25 0', theColor, theAlpha, 0);
167 drawsubpic(theOrigin + square, theSize - 2 * square, pic, '0 0.25 0', '1 0.5 0', theColor, theAlpha, 0);
168 drawsubpic(theOrigin + height - square, width + square, pic, '0 0.75 0', '1 0.25 0', theColor, theAlpha, 0);
169 }
170}
171
172// a border picture is a texture containing nine parts:
173// 1/4 width: left part
174// 1/2 width: middle part (stretched)
175// 1/4 width: right part
176// divided into
177// 1/4 height: top part
178// 1/2 height: middle part (stretched)
179// 1/4 height: bottom part
180void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
181{
182 vector dX, dY;
183 vector bW, bH;
184 pic = draw_UseSkinFor(pic);
185 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
186 theSize = boxToGlobalSize(theSize, draw_scale);
187 theBorderSize = boxToGlobalSize(theBorderSize, draw_scale);
188 theAlpha *= draw_alpha;
189 vector width = eX * theSize.x;
190 vector height = eY * theSize.y;
191
192 // zero size? bail out, we cannot handle this
193 if (theSize.x <= 0 || theSize.y <= 0)
194 return;
195
196 if (theBorderSize.x <= 0) // no x border
197 {
198 if (theBorderSize.y <= 0)
199 drawsubpic(theOrigin, width + height, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
200 else if (theSize.y <= theBorderSize.y * 2)
201 {
202 // not high enough... draw just top and bottom then
203 bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
204 drawsubpic(theOrigin, width + height * 0.5, pic, '0.25 0 0', '0.5 0 0' + bH, theColor, theAlpha, 0);
205 drawsubpic(theOrigin + height * 0.5, width + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5 0 0' + bH, theColor, theAlpha, 0);
206 }
207 else
208 {
209 dY = theBorderSize.y * eY;
210 drawsubpic(theOrigin, width + dY, pic, '0.25 0 0', '0.5 0.25 0', theColor, theAlpha, 0);
211 drawsubpic(theOrigin + dY, width + height - 2 * dY, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
212 drawsubpic(theOrigin + height - dY, width + dY, pic, '0.25 0.75 0', '0.5 0.25 0', theColor, theAlpha, 0);
213 }
214 }
215 else if (theSize.x <= theBorderSize.x * 2)
216 {
217 // not wide enough... draw just left and right then
218 bW = eX * (0.25 * theSize.x / (theBorderSize.x * 2));
219 if (theBorderSize.y <= 0)
220 {
221 drawsubpic(theOrigin, width * 0.5 + height, pic, '0 0.25 0', '0 0.5 0' + bW, theColor, theAlpha, 0);
222 drawsubpic(theOrigin + width * 0.5, width * 0.5 + height, pic, '0 0.25 0' + eX - bW, '0 0.5 0' + bW, theColor, theAlpha, 0);
223 }
224 else if (theSize.y <= theBorderSize.y * 2)
225 {
226 // not high enough... draw just corners
227 bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
228 drawsubpic(theOrigin, width * 0.5 + height * 0.5, pic, '0 0 0', bW + bH, theColor, theAlpha, 0);
229 drawsubpic(theOrigin + width * 0.5, width * 0.5 + height * 0.5, pic, eX - bW, bW + bH, theColor, theAlpha, 0);
230 drawsubpic(theOrigin + height * 0.5, width * 0.5 + height * 0.5, pic, eY - bH, bW + bH, theColor, theAlpha, 0);
231 drawsubpic(theOrigin + theSize * 0.5, width * 0.5 + height * 0.5, pic, eX + eY - bW - bH, bW + bH, theColor, theAlpha, 0);
232 }
233 else
234 {
235 dY = theBorderSize.y * eY;
236 drawsubpic(theOrigin, width * 0.5 + dY, pic, '0 0 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
237 drawsubpic(theOrigin + width * 0.5, width * 0.5 + dY, pic, '0 0 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
238 drawsubpic(theOrigin + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0', '0 0.5 0' + bW, theColor, theAlpha, 0);
239 drawsubpic(theOrigin + width * 0.5 + dY, width * 0.5 + height - 2 * dY, pic, '0 0.25 0' + eX - bW, '0 0.5 0' + bW, theColor, theAlpha, 0);
240 drawsubpic(theOrigin + height - dY, width * 0.5 + dY, pic, '0 0.75 0', '0 0.25 0' + bW, theColor, theAlpha, 0);
241 drawsubpic(theOrigin + width * 0.5 + height - dY, width * 0.5 + dY, pic, '0 0.75 0' + eX - bW, '0 0.25 0' + bW, theColor, theAlpha, 0);
242 }
243 }
244 else
245 {
246 if (theBorderSize.y <= 0)
247 {
248 dX = theBorderSize.x * eX;
249 drawsubpic(theOrigin, dX + height, pic, '0 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
250 drawsubpic(theOrigin + dX, width - 2 * dX + height, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
251 drawsubpic(theOrigin + width - dX, dX + height, pic, '0.75 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
252 }
253 else if (theSize.y <= theBorderSize.y * 2)
254 {
255 // not high enough... draw just top and bottom then
256 bH = eY * (0.25 * theSize.y / (theBorderSize.y * 2));
257 dX = theBorderSize.x * eX;
258 drawsubpic(theOrigin, dX + height * 0.5, pic, '0 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
259 drawsubpic(theOrigin + dX, width - 2 * dX + height * 0.5, pic, '0.25 0 0', '0.5 0 0' + bH, theColor, theAlpha, 0);
260 drawsubpic(theOrigin + width - dX, dX + height * 0.5, pic, '0.75 0 0', '0.25 0 0' + bH, theColor, theAlpha, 0);
261 drawsubpic(theOrigin + height * 0.5, dX + height * 0.5, pic, '0 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
262 drawsubpic(theOrigin + dX + height * 0.5, width - 2 * dX + height * 0.5, pic, '0.25 0 0' + eY - bH, '0.5 0 0' + bH, theColor, theAlpha, 0);
263 drawsubpic(theOrigin + width - dX + height * 0.5, dX + height * 0.5, pic, '0.75 0 0' + eY - bH, '0.25 0 0' + bH, theColor, theAlpha, 0);
264 }
265 else
266 {
267 dX = theBorderSize.x * eX;
268 dY = theBorderSize.y * eY;
269 drawsubpic(theOrigin, dX + dY, pic, '0 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
270 drawsubpic(theOrigin + dX, width - 2 * dX + dY, pic, '0.25 0 0', '0.5 0.25 0', theColor, theAlpha, 0);
271 drawsubpic(theOrigin + width - dX, dX + dY, pic, '0.75 0 0', '0.25 0.25 0', theColor, theAlpha, 0);
272 drawsubpic(theOrigin + dY, dX + height - 2 * dY, pic, '0 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
273 drawsubpic(theOrigin + dY + dX, width - 2 * dX + height - 2 * dY, pic, '0.25 0.25 0', '0.5 0.5 0', theColor, theAlpha, 0);
274 drawsubpic(theOrigin + dY + width - dX, dX + height - 2 * dY, pic, '0.75 0.25 0', '0.25 0.5 0', theColor, theAlpha, 0);
275 drawsubpic(theOrigin + height - dY, dX + dY, pic, '0 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
276 drawsubpic(theOrigin + height - dY + dX, width - 2 * dX + dY, pic, '0.25 0.75 0', '0.5 0.25 0', theColor, theAlpha, 0);
277 drawsubpic(theOrigin + height - dY + width - dX, dX + dY, pic, '0.75 0.75 0', '0.25 0.25 0', theColor, theAlpha, 0);
278 }
279 }
280}
281void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
282{
283 if (theSize.x <= 0 || theSize.y <= 0)
284 {
285 LOG_TRACE("Drawing zero size text?");
286 return;
287 }
288
289 theOrigin = boxToGlobal(theOrigin, draw_shift, draw_scale);
291 if (allowColorCodes)
292 drawcolorcodedstring(theOrigin, theText, theSize, theAlpha * draw_alpha, 0);
293 else
294 drawstring(theOrigin, theText, theSize, theColor, theAlpha * draw_alpha, 0);
295}
296void draw_CenterText(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
297{
298 //dprint(strcat("orig = ", vtos(theOrigin) ," tx = ", ftos(draw_TextWidth(theText, allowColorCodes, theSize)), "\n"));
299 draw_Text(theOrigin - eX * 0.5 * draw_TextWidth(theText, allowColorCodes, theSize), theText, theSize, theColor, theAlpha, allowColorCodes);
300}
301
302float draw_TextWidth(string theText, bool allowColorCodes, vector SizeThxBye)
303{
304 vector v = '0 0 0';
305 v.x = stringwidth(theText, allowColorCodes, globalToBoxSize(boxToGlobalSize(SizeThxBye, draw_scale), draw_fontscale));
307 return v.x;
308}
309
310float draw_CondensedFontFactor(string theText, bool allowColorCodes, vector SizeThxBye, float maxWidth)
311{
312 float w = draw_TextWidth(theText, allowColorCodes, SizeThxBye);
313 if (w > maxWidth)
314 {
315 //dprintf("NOTE: label text %s too wide for label, condensed by factor %f\n", theText, maxWidth / w);
316 return maxWidth / w;
317 }
318 return 1;
319}
320
327
329{
331 if (prev)
332 {
333 o.x = bound(prev.clip_shift.x, o.x, prev.clip_shift.x + prev.clip_scale.x);
334 o.y = bound(prev.clip_shift.y, o.y, prev.clip_shift.y + prev.clip_scale.y);
335 s.x = bound(0, s.x, prev.clip_scale.x - (o.x - prev.clip_shift.x));
336 s.y = bound(0, s.y, prev.clip_scale.y - (o.y - prev.clip_shift.y));
337 }
339 e.clip_shift = o;
340 e.clip_scale = s;
341 IL_PUSH(draw_clip, e);
342 drawsetcliparea(o.x, o.y, s.x, s.y);
343}
344
349
350void draw_SetClipRect(vector theOrigin, vector theScale)
351{
353}
354
356{
357 if (IL_EMPTY(draw_clip))
358 LOG_FATAL("Not clipping, can't clear it then");
359
360 entity currentSettings = IL_PEEK(draw_clip);
361 IL_REMOVE(draw_clip, currentSettings);
362 delete(currentSettings);
365 if (e)
367}
368
369string draw_TextShortenToWidth(string theText, float maxWidth, bool allowColorCodes, vector SizeThxBye)
370{
371 if (allowColorCodes)
372 return textShortenToWidth(theText, maxWidth, SizeThxBye, draw_TextWidth_WithColors);
373 else
374 return textShortenToWidth(theText, maxWidth, SizeThxBye, draw_TextWidth_WithoutColors);
375}
376
377float draw_TextWidth_WithColors(string s, vector theFontSize)
378{
379 return draw_TextWidth(s, true, theFontSize);
380}
381
382float draw_TextWidth_WithoutColors(string s, vector theFontSize)
383{
384 return draw_TextWidth(s, false, theFontSize);
385}
386
387float draw_TextLengthUpToWidth(string theText, float maxWidth, bool allowColorCodes, vector theFontSize)
388{
389 if (allowColorCodes)
390 return textLengthUpToWidth(theText, maxWidth, theFontSize, draw_TextWidth_WithColors);
391 else
392 return textLengthUpToWidth(theText, maxWidth, theFontSize, draw_TextWidth_WithoutColors);
393}
float height
Definition bobbing.qc:3
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
vector clip_scale
Definition draw.qc:325
vector clip_shift
Definition draw.qc:324
limitations: NULL cannot be present elements can only be present once a maximum of IL_MAX lists can e...
#define drawsetcliparea(xposition, yposition, w, h)
Definition draw.qh:39
#define drawcolorcodedstring(position, text, scale, alpha, flag)
Definition draw.qh:30
#define drawstring(position, text, scale, rgb, alpha, flag)
Definition draw.qh:27
#define drawpic(position, pic, size, rgb, alpha, flag)
Definition draw.qh:21
#define drawfill(position, size, rgb, alpha, flag)
Definition draw.qh:36
#define draw_endBoldFont()
Definition draw.qh:5
string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1173
float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
Definition util.qc:884
float drawsubpic(vector position, vector size, string pic, vector srcPosition, vector srcSize, vector rgb, float alpha, float flag)
vector drawgetimagesize(string pic)
#define stringwidth
ERASEABLE bool fexists(string f)
Definition file.qh:4
prev
Definition all.qh:53
ERASEABLE void IL_REMOVE(IntrusiveList this, entity it)
Remove any element, anywhere in the list.
ERASEABLE entity IL_PUSH(IntrusiveList this, entity it)
Push to tail.
#define IL_NEW()
#define IL_PEEK(this)
#define IL_EMPTY(this)
#define LOG_TRACE(...)
Definition log.qh:74
#define LOG_FATAL(...)
Definition log.qh:50
vector globalToBox(vector v, vector theOrigin, vector theScale)
Definition draw.qc:30
float draw_TextLengthUpToWidth(string theText, float maxWidth, bool allowColorCodes, vector theFontSize)
Definition draw.qc:387
string draw_PreloadPictureWithFlags(string pic, float f)
Definition draw.qc:66
void draw_reset(float cw, float ch, float ox, float oy)
Definition draw.qc:21
bool draw_PictureExists(string pic)
Definition draw.qc:86
vector draw_mousepointer_size
Definition draw.qc:7
float draw_TextWidth_WithoutColors(string s, vector theFontSize)
Definition draw.qc:382
void draw_Picture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:72
string draw_mousepointer
Definition draw.qc:5
vector draw_mousepointer_offset
Definition draw.qc:6
void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:143
void draw_setMousePointer(string pic, vector theSize, vector theOffset)
Definition draw.qc:9
vector draw_PictureSize(string pic)
Definition draw.qc:80
void draw_SetClip()
Definition draw.qc:345
void draw_ButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:108
void _draw_SetClip(vector o, vector s)
Definition draw.qc:328
vector globalToBoxSize(vector v, vector theScale)
Definition draw.qc:38
void draw_drawMousePointer(vector where)
Definition draw.qc:16
vector boxToGlobalSize(vector v, vector theScale)
Definition draw.qc:53
IntrusiveList draw_clip
Definition draw.qc:321
float draw_TextWidth(string theText, bool allowColorCodes, vector SizeThxBye)
Definition draw.qc:302
string draw_TextShortenToWidth(string theText, float maxWidth, bool allowColorCodes, vector SizeThxBye)
Definition draw.qc:369
void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
Definition draw.qc:281
float draw_TextWidth_WithColors(string s, vector theFontSize)
Definition draw.qc:377
vector boxToGlobal(vector v, vector theOrigin, vector theScale)
Definition draw.qc:45
void draw_CenterText(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
Definition draw.qc:296
void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:97
void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
Definition draw.qc:180
float draw_CondensedFontFactor(string theText, bool allowColorCodes, vector SizeThxBye, float maxWidth)
Definition draw.qc:310
void draw_ClearClip()
Definition draw.qc:355
string draw_PreloadPicture(string pic)
Definition draw.qc:60
void draw_SetClipRect(vector theOrigin, vector theScale)
Definition draw.qc:350
vector draw_shift
Definition draw.qh:7
vector draw_scale
Definition draw.qh:8
#define draw_fontscale
Definition draw.qh:5
float draw_alpha
Definition draw.qh:9
void drawresetcliparea(void)
float bound(float min, float value, float max)
string precache_pic(string name,...)
string strzone(string s)
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NEW(cname,...)
Definition oo.qh:120
#define CLASS(...)
Definition oo.qh:149
#define ENDCLASS(cname)
Definition oo.qh:286
#define ATTRIB(...)
Definition oo.qh:152
vector
Definition self.qh:96
#define STATIC_INIT(func)
during worldspawn
Definition static.qh:33
const vector eY
Definition vector.qh:44
const vector eX
Definition vector.qh:43