UFO: Alien Invasion
Doxygen documentation generating
ui_render.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2023 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "ui_main.h"
26 #include "ui_font.h"
27 #include "ui_render.h"
28 #include "../client.h"
29 #include "../cl_video.h"
30 #include "../renderer/r_draw.h"
31 #include "../renderer/r_misc.h"
32 #include "../renderer/r_state.h"
33 
37 void UI_DrawFill (int x, int y, int w, int h, const vec4_t color)
38 {
39  R_DrawFill(x, y, w, h, color);
40 }
41 
42 void UI_DrawRect (int x, int y, int w, int h, const vec4_t color, float lineWidth, int pattern)
43 {
44  R_DrawRect(x, y, w, h, color, lineWidth, pattern);
45 }
46 
47 void UI_PushClipRect (int x, int y, int width, int height)
48 {
49  R_PushClipRect(x, y, width, height);
50 }
51 
52 void UI_PopClipRect (void)
53 {
54  R_PopClipRect();
55 }
56 
68 void UI_Transform (const vec3_t transform, const vec3_t rotate, const vec3_t scale)
69 {
70  vec3_t pos;
71 
72  if (transform != nullptr) {
73  R_PushMatrix();
74  VectorCopy(transform, pos);
75  pos[0] *= viddef.rx;
76  pos[1] *= viddef.ry;
77 
78  R_Transform(pos, rotate, scale);
79  } else {
80  R_PopMatrix();
81  }
82 }
83 
91 const struct image_s* UI_LoadImage (const char* name)
92 {
93  const struct image_s* image = R_FindImage(va("pics/%s", name), it_pic);
94  if (image == r_noTexture)
95  return nullptr;
96  return image;
97 }
98 
106 const struct image_s* UI_LoadWrappedImage (const char* name)
107 {
108  const struct image_s* image = R_FindImage(va("pics/%s", name), it_wrappic);
109  if (image == r_noTexture)
110  return nullptr;
111  return image;
112 }
113 
126 void UI_DrawNormImage (bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const image_t* image)
127 {
128  float nw, nh, x1, x2, x3, x4, y1, y2, y3, y4;
129 
130  if (!image)
131  return;
132 
133  /* normalize to the screen resolution */
134  x1 = x * viddef.rx;
135  y1 = y * viddef.ry;
136 
137  /* provided width and height (if any) take precedence */
138  if (w)
139  nw = w * viddef.rx;
140  else
141  nw = 0;
142 
143  if (h)
144  nh = h * viddef.ry;
145  else
146  nh = 0;
147 
148  /* horizontal texture mapping */
149  if (sh) {
150  if (!w)
151  nw = (sh - sl) * viddef.rx;
152  sh /= image->width;
153  } else {
154  if (!w)
155  nw = ((float)image->width - sl) * viddef.rx;
156  sh = 1.0f;
157  }
158  sl /= image->width;
159 
160  /* vertical texture mapping */
161  if (th) {
162  if (!h)
163  nh = (th - tl) * viddef.ry;
164  th /= image->height;
165  } else {
166  if (!h)
167  nh = ((float)image->height - tl) * viddef.ry;
168  th = 1.0f;
169  }
170  tl /= image->height;
171 
172  /* fill the rest of the coordinates to make a rectangle */
173  x4 = x1;
174  x3 = x2 = x1 + nw;
175  y2 = y1;
176  y4 = y3 = y1 + nh;
177 
178  if (flip) {
179  const float tmp = sl;
180  sl = sh;
181  sh = tmp;
182  }
183  const vec2_t imageTexcoords[4] = {{sl, tl}, {sh, tl}, {sh, th}, {sl, th}};
184  const vec2_t imageVerts[4] = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}};
185  R_DrawImageArray(imageTexcoords, imageVerts, image);
186 }
187 
203 const image_t* UI_DrawNormImageByName (bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const char* name)
204 {
205  const struct image_s* image;
206 
207  image = UI_LoadImage(name);
208  if (!image) {
209  Com_Printf("Can't find pic: %s\n", name);
210  return nullptr;
211  }
212 
213  UI_DrawNormImage(flip, x, y, w, h, sh, th, sl, tl, image);
214  return image;
215 }
216 
230 void UI_DrawPanel (const vec2_t pos, const vec2_t size, const char* texture, int texX, int texY, const int panelDef[7])
231 {
232  const image_t* image = UI_LoadImage(texture);
233  if (!image)
234  return;
235 
236  const int leftWidth = panelDef[0];
237  const int midWidth = panelDef[1];
238  const int rightWidth = panelDef[2];
239  const int topHeight = panelDef[3];
240  const int midHeight = panelDef[4];
241  const int bottomHeight = panelDef[5];
242  const int marge = panelDef[6];
243 
245  const int firstPos = 0;
246  const int secondPos = firstPos + leftWidth + marge;
247  const int thirdPos = secondPos + midWidth + marge;
248  const int firstPosY = 0;
249  const int secondPosY = firstPosY + topHeight + marge;
250  const int thirdPosY = secondPosY + midHeight + marge;
251 
252  /* draw top (from left to right) */
253  UI_DrawNormImage(false, pos[0], pos[1], leftWidth, topHeight, texX + firstPos + leftWidth, texY + firstPosY + topHeight,
254  texX + firstPos, texY + firstPosY, image);
255  UI_DrawNormImage(false, pos[0] + leftWidth, pos[1], size[0] - leftWidth - rightWidth, topHeight, texX + secondPos + midWidth, texY + firstPosY + topHeight,
256  texX + secondPos, texY + firstPosY, image);
257  UI_DrawNormImage(false, pos[0] + size[0] - rightWidth, pos[1], rightWidth, topHeight, texX + thirdPos + rightWidth, texY + firstPosY + topHeight,
258  texX + thirdPos, texY + firstPosY, image);
259 
260  /* draw middle (from left to right) */
261  const int yMiddle = pos[1] + topHeight;
262  const int hMiddle = size[1] - topHeight - bottomHeight; /* height of middle */
263  UI_DrawNormImage(false, pos[0], yMiddle, leftWidth, hMiddle, texX + firstPos + leftWidth, texY + secondPosY + midHeight,
264  texX + firstPos, texY + secondPosY, image);
265  UI_DrawNormImage(false, pos[0] + leftWidth, yMiddle, size[0] - leftWidth - rightWidth, hMiddle, texX + secondPos + midWidth, texY + secondPosY + midHeight,
266  texX + secondPos, texY + secondPosY, image);
267  UI_DrawNormImage(false, pos[0] + size[0] - rightWidth, yMiddle, rightWidth, hMiddle, texX + thirdPos + rightWidth, texY + secondPosY + midHeight,
268  texX + thirdPos, texY + secondPosY, image);
269 
270  /* draw bottom (from left to right) */
271  const int yBottom = pos[1] + size[1] - bottomHeight;
272  UI_DrawNormImage(false, pos[0], yBottom, leftWidth, bottomHeight, texX + firstPos + leftWidth, texY + thirdPosY + bottomHeight,
273  texX + firstPos, texY + thirdPosY, image);
274  UI_DrawNormImage(false, pos[0] + leftWidth, yBottom, size[0] - leftWidth - rightWidth, bottomHeight, texX + secondPos + midWidth, texY + thirdPosY + bottomHeight,
275  texX + secondPos, texY + thirdPosY, image);
276  UI_DrawNormImage(false, pos[0] + size[0] - bottomHeight, yBottom, rightWidth, bottomHeight, texX + thirdPos + rightWidth, texY + thirdPosY + bottomHeight,
277  texX + thirdPos, texY + thirdPosY, image);
278 }
279 
293 void UI_DrawBorderedPanel (const vec2_t pos, const vec2_t size, const char* texture, int texX, int texY, int texW, int texH, int border)
294 {
295  const image_t* image = UI_LoadImage(texture);
296  if (!image)
297  return;
298 
299  const int leftWidth = border;
300  const int midWidth = texW - 2 * border;
301  const int rightWidth = border;
302  const int topHeight = border;
303  const int midHeight = texH - 2 * border;
304  const int bottomHeight = border;
305  const int marge = 0;
306 
307  const int firstPos = texX;
308  const int secondPos = firstPos + leftWidth + marge;
309  const int thirdPos = secondPos + midWidth + marge;
310  const int firstPosY = texY;
311  const int secondPosY = firstPosY + topHeight + marge;
312  const int thirdPosY = secondPosY + midHeight + marge;
313 
314  /* draw top (from left to right) */
315  UI_DrawNormImage(false, pos[0], pos[1], leftWidth, topHeight, firstPos + leftWidth, firstPosY + topHeight,
316  firstPos, firstPosY, image);
317  UI_DrawNormImage(false, pos[0] + leftWidth, pos[1], size[0] - leftWidth - rightWidth, topHeight, secondPos + midWidth, firstPosY + topHeight,
318  secondPos, firstPosY, image);
319  UI_DrawNormImage(false, pos[0] + size[0] - rightWidth, pos[1], rightWidth, topHeight, thirdPos + rightWidth, firstPosY + topHeight,
320  thirdPos, firstPosY, image);
321 
322  /* draw middle (from left to right) */
323  const int yMiddle = pos[1] + topHeight;
324  const int hMiddle = size[1] - topHeight - bottomHeight; /* height of middle */
325  UI_DrawNormImage(false, pos[0], yMiddle, leftWidth, hMiddle, firstPos + leftWidth, secondPosY + midHeight,
326  firstPos, secondPosY, image);
327  UI_DrawNormImage(false, pos[0] + leftWidth, yMiddle, size[0] - leftWidth - rightWidth, hMiddle, secondPos + midWidth, secondPosY + midHeight,
328  secondPos, secondPosY, image);
329  UI_DrawNormImage(false, pos[0] + size[0] - rightWidth, yMiddle, rightWidth, hMiddle, thirdPos + rightWidth, secondPosY + midHeight,
330  thirdPos, secondPosY, image);
331 
332  /* draw bottom (from left to right) */
333  const int yBottom = pos[1] + size[1] - bottomHeight;
334  UI_DrawNormImage(false, pos[0], yBottom, leftWidth, bottomHeight, firstPos + leftWidth, thirdPosY + bottomHeight,
335  firstPos, thirdPosY, image);
336  UI_DrawNormImage(false, pos[0] + leftWidth, yBottom, size[0] - leftWidth - rightWidth, bottomHeight, secondPos + midWidth, thirdPosY + bottomHeight,
337  secondPos, thirdPosY, image);
338  UI_DrawNormImage(false, pos[0] + size[0] - bottomHeight, yBottom, rightWidth, bottomHeight, thirdPos + rightWidth, thirdPosY + bottomHeight,
339  thirdPos, thirdPosY, image);
340 }
341 
359 int UI_DrawStringInBox (const char* fontID, align_t align, int x, int y, int width, int height, const char* text, longlines_t method)
360 {
361  const align_t horizontalAlign = (align_t)(align % 3); /* left, center, right */
362  const align_t verticalAlign = (align_t)(align / 3); /* top, center, bottom */
363 
364  /* position of the text for UI_DrawString */
365  const int xx = x + ((width * horizontalAlign) >> 1);
366  const int yy = y + ((height * verticalAlign) >> 1);
367 
368  return UI_DrawString(fontID, align, xx, yy, xx, width, 0, text, 0, 0, nullptr, false, method);
369 }
370 
371 int UI_DrawString (const char* fontID, align_t align, int x, int y, int absX, int maxWidth,
372  int lineHeight, const char* c, int boxHeight, int scrollPos, int* curLine, bool increaseLine, longlines_t method)
373 {
374  const uiFont_t* font = UI_GetFontByID(fontID);
375  const align_t verticalAlign = (align_t)(align / 3); /* top, center, bottom */
376  int lines;
377 
378  if (!font)
379  Com_Error(ERR_FATAL, "Could not find font with id: '%s'", fontID);
380 
381  if (lineHeight <= 0)
382  lineHeight = UI_FontGetHeight(font->name);
383 
384  /* vertical alignment makes only a single-line adjustment in this
385  * function. That means that ALIGN_Lx values will not show more than
386  * one line in any case. */
387  if (verticalAlign == 1)
388  y += -(lineHeight / 2);
389  else if (verticalAlign == 2)
390  y += -lineHeight;
391 
392  lines = R_FontDrawString(fontID, align, x, y, absX, maxWidth, lineHeight,
393  c, boxHeight, scrollPos, curLine, method);
394 
395  if (curLine && increaseLine)
396  *curLine += lines;
397 
398  return lines * lineHeight;
399 }
400 
406 void UI_EnableFlashing (const vec4_t flashingColor, float speed)
407 {
408  vec4_t color;
409  Vector4Copy(flashingColor, color);
410  color[3] = sin(M_PI * cls.realtime * speed / 500.0f) * 0.5f + 0.5f;
411  R_TexOverride(color);
412 }
413 
418 {
419  R_TexOverride(nullptr);
420 }
#define VectorCopy(src, dest)
Definition: vector.h:51
int UI_DrawStringInBox(const char *fontID, align_t align, int x, int y, int width, int height, const char *text, longlines_t method)
draw a line into a bounding box
Definition: ui_render.cpp:359
void UI_PopClipRect(void)
Definition: ui_render.cpp:52
void R_Transform(const vec3_t transform, const vec3_t rotate, const vec3_t scale)
Perform translate, rotate and scale operations on the current matrix.
Definition: r_misc.cpp:220
const image_t * R_DrawImageArray(const vec2_t texcoords[4], const vec2_t verts[4], const image_t *image)
Definition: r_draw.cpp:357
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don&#39;t need to have varargs versions of all text functi...
Definition: shared.cpp:410
int R_FontDrawString(const char *fontId, align_t align, int x, int y, int absX, int maxWidth, int lineHeight, const char *c, int boxHeight, int scrollPos, int *curLine, longlines_t method)
Definition: r_font.cpp:687
static const vec3_t scale
float rx
Definition: cl_video.h:71
voidpf void uLong size
Definition: ioapi.h:42
viddef_t viddef
Definition: cl_video.cpp:34
void UI_DrawPanel(const vec2_t pos, const vec2_t size, const char *texture, int texX, int texY, const int panelDef[7])
draw a panel from a texture as we can see on the image
Definition: ui_render.cpp:230
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
image_t * r_noTexture
Definition: r_main.cpp:51
void UI_DisableFlashing(void)
Disables flashing effect for UI nodes.
Definition: ui_render.cpp:417
Definition: r_image.h:45
int height
Definition: r_image.h:64
#define ERR_FATAL
Definition: common.h:210
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
image_t * R_FindImage(const char *pname, imagetype_t type)
Finds or loads the given image.
Definition: r_image.cpp:603
client_static_t cls
Definition: cl_main.cpp:83
int realtime
Definition: client.h:58
align_t
We need this here for checking the boundaries from script values.
Definition: scripts.h:89
void R_PopClipRect(void)
Definition: r_draw.cpp:579
char * name
Definition: ui_font.h:30
const image_t * UI_DrawNormImageByName(bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const char *name)
Draws an image or parts of it.
Definition: ui_render.cpp:203
#define M_PI
Definition: mathlib.h:34
const struct image_s * UI_LoadImage(const char *name)
Searches for an image in the image array.
Definition: ui_render.cpp:91
void UI_DrawNormImage(bool flip, float x, float y, float w, float h, float sh, float th, float sl, float tl, const image_t *image)
Draw a normalized (to the screen) image.
Definition: ui_render.cpp:126
const uiFont_t * UI_GetFontByID(const char *name)
Return the font for a specific id.
Definition: ui_font.cpp:157
void UI_Transform(const vec3_t transform, const vec3_t rotate, const vec3_t scale)
Pushes a new matrix, normalize to current resolution and move, rotate and scale the matrix to the giv...
Definition: ui_render.cpp:68
void UI_EnableFlashing(const vec4_t flashingColor, float speed)
Enables flashing effect for UI nodes.
Definition: ui_render.cpp:406
void R_PopMatrix(void)
Removes the current matrix from the stack.
Definition: r_misc.cpp:248
int width
Definition: r_image.h:64
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
int UI_FontGetHeight(const char *fontID)
Definition: ui_font.cpp:166
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec_t vec2_t[2]
Definition: ufotypes.h:38
void UI_PushClipRect(int x, int y, int width, int height)
Definition: ui_render.cpp:47
void R_DrawRect(int x, int y, int w, int h, const vec4_t color, float lineWidth, int pattern)
Draws a rect to the screen. Also has support for stippled rendering of the rect.
Definition: r_draw.cpp:390
int UI_DrawString(const char *fontID, align_t align, int x, int y, int absX, int maxWidth, int lineHeight, const char *c, int boxHeight, int scrollPos, int *curLine, bool increaseLine, longlines_t method)
Definition: ui_render.cpp:371
void UI_DrawRect(int x, int y, int w, int h, const vec4_t color, float lineWidth, int pattern)
Definition: ui_render.cpp:42
const struct image_s * UI_LoadWrappedImage(const char *name)
Searches for a wrapped image in the image array.
Definition: ui_render.cpp:106
void UI_DrawBorderedPanel(const vec2_t pos, const vec2_t size, const char *texture, int texX, int texY, int texW, int texH, int border)
draw a panel from a texture as we can see on the image
Definition: ui_render.cpp:293
void R_DrawFill(int x, int y, int w, int h, const vec4_t color)
Fills a box of pixels with a single color.
Definition: r_draw.cpp:188
void R_PushMatrix(void)
Push a new matrix to the stack.
Definition: r_misc.cpp:240
void UI_DrawFill(int x, int y, int w, int h, const vec4_t color)
Fills a box of pixels with a single color.
Definition: ui_render.cpp:37
float ry
Definition: cl_video.h:72
void R_TexOverride(vec4_t rgba)
Sets special texture environment mode to override texture color; don&#39;t forget to call R_TexOverride(n...
Definition: r_state.cpp:968
#define Vector4Copy(src, dest)
Definition: vector.h:53
longlines_t
Definition: cl_renderer.h:217
void R_PushClipRect(int x, int y, int width, int height)
Force to draw only on a rect.
Definition: r_draw.cpp:550
vec_t vec4_t[4]
Definition: ufotypes.h:40