Debian Codegraph @

ui/UIDraw.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ui/UIDraw.cpp $
21 * $Revision: 16 $
22 * $Date: 4/14/99 1:52a $
23 * $Author: Jeff $
24 *
25 * UI Library internal code.
26 *
27 * $Log: /DescentIII/Main/ui/UIDraw.cpp $
28 *
29 * 16 4/14/99 1:52a Jeff
30 * fixed case mismatched #includes
31 *
32 * 15 10/22/98 2:43p Samir
33 * ui_DrawStiring uses grtext_Puts.
34 *
35 * 14 10/12/98 9:28a Jason
36 * fixed some rendering bugs
37 *
38 * 13 9/30/98 4:32p Samir
39 * alpha is set to UICharAlpha when drawing a string.
40 *
41 * 12 9/08/98 10:27a Samir
42 * added function to get text height.
43 *
44 * 11 5/07/98 2:59p Samir
45 * modified ui_StartDraw.
46 *
47 * 10 3/18/98 6:25p Samir
48 * Don't turn Z-buffering back on.
49 *
50 * 9 3/10/98 1:08p Samir
51 * Fixed ui_DrawBitmap to always draw properly by setting states.
52 *
53 * 8 3/10/98 12:49p Samir
54 * Added text saturation support.
55 *
56 * 7 2/26/98 4:56p Samir
57 * Fixed some line drawing probs.
58 *
59 * 6 2/10/98 4:56p Samir
60 * Fixed bug in ui_DrawRect.
61 *
62 * 5 1/30/98 7:06p Samir
63 * Added text clipping.
64 *
65 * 4 1/18/98 4:22p Samir
66 * Implemented new UIItem system.
67 *
68 * 3 1/05/98 4:36p Samir
69 * Moved centering and fittext control flags to UIGadget class.
70 *
71 * 2 1/02/98 12:42p Samir
72 * Added ui_GetTextWidth.
73 *
74 * 1 12/30/97 4:36p Samir
75 * Initial revision
76 *
77 * $NoKeywords: $
78 */
79
80 #include "UIlib.h"
81 #include "3d.h"
82 #include "bitmap.h"
83 #include "grtext.h"
84 #include "ddvid.h"
85 #include "renderer.h"
86
87 //////////////////////////////////////////////////////////////////////////////
88 // VARIABLES
89 static int m_UIDrawLeft;
90 static int m_UIDrawTop;
91 static int m_UIDrawRight;
92 static int m_UIDrawBottom;
93 static uint8_t m_UIDrawAlpha, m_UICharAlpha;
94 static int m_UITextFlags = 0;
95
96 //////////////////////////////////////////////////////////////////////////////
97 // FUNCTIONS
98
99 //////////////////////////////////////////////////////////////////////////////
100 // DRAWING PRIMATIVE FUNCTIONS
101
102 void ui_StartDraw(int left, int top, int right, int bottom) {
103 m_UIDrawLeft = left;
104 m_UIDrawTop = top;
105 m_UIDrawRight = right;
106 m_UIDrawBottom = bottom;
107 m_UIDrawAlpha = m_UICharAlpha = 255;
108
109 rend_StartFrame(m_UIDrawLeft, m_UIDrawTop, m_UIDrawRight, m_UIDrawBottom);
110 grtext_SetParameters(0, 0, right - left + 1, bottom - top + 1);
111 }
112
113 void ui_EndDraw() {
114 grtext_Flush();
115 rend_EndFrame();
116 }
117
118 // sets text clipping within the startdraw region
119 void ui_SetTextClip(int left, int top, int right, int bottom) { grtext_SetParameters(left, top, right, bottom); }
120
121 void ui_ResetTextClip() { grtext_SetParameters(m_UIDrawLeft, m_UIDrawTop, m_UIDrawRight, m_UIDrawBottom); }
122
123 // draw primatives
124 void ui_DrawSetAlpha(uint8_t alpha) { m_UIDrawAlpha = alpha; }
125
126 void ui_DrawLine(ddgr_color color, int x1, int y1, int x2, int y2) {
127 if (color == TRANSPARENT_COLOR32)
128 return;
129
130 rend_SetFlatColor(color);
131 rend_DrawLine(x1, y1, x2, y2);
132 }
133
134 void ui_DrawBox(ddgr_color color, int l, int t, int r, int b) {
135 if (color == TRANSPARENT_COLOR32)
136 return;
137 rend_SetFlatColor(color);
138 rend_DrawLine(l, t, r - 1, t);
139 rend_DrawLine(r - 1, t, r - 1, b - 1);
140 rend_DrawLine(l, b - 1, r - 1, b - 1);
141 rend_DrawLine(l, t, l, b - 1);
142 }
143
144 void ui_DrawLTBox(ddgr_color lt_col, ddgr_color rb_col, int l, int t, int r, int b) {
145 if (lt_col != TRANSPARENT_COLOR32) {
146 rend_SetFlatColor(lt_col);
147 rend_DrawLine(l, t, r - 1, t);
148 rend_DrawLine(l, t, l, b - 1);
149 }
150 if (rb_col != TRANSPARENT_COLOR32) {
151 rend_SetFlatColor(rb_col);
152 rend_DrawLine(l, b - 1, r - 1, b - 1);
153 rend_DrawLine(r - 1, t + 1, r - 1, b - 1);
154 }
155 }
156
157 void ui_DrawRect(ddgr_color color, int l, int t, int r, int b) {
158 if (color == TRANSPARENT_COLOR32)
159 return;
160
161 g3Point *pntlist[4], points[4];
162 int i;
163
164 // Set our four corners to cover the screen
165 points[0].p3_sx = l;
166 points[0].p3_sy = t;
167 points[1].p3_sx = r;
168 points[1].p3_sy = t;
169 points[2].p3_sx = r;
170 points[2].p3_sy = b;
171 points[3].p3_sx = l;
172 points[3].p3_sy = b;
173
174 for (i = 0; i < 4; i++) {
175 points[i].p3_z = 0;
176 points[i].p3_flags = PF_PROJECTED;
177 pntlist[i] = &points[i];
178 }
179
180 rend_SetZBufferState(0);
181 rend_SetTextureType(TT_FLAT);
182 rend_SetAlphaType(AT_CONSTANT);
183 rend_SetLighting(LS_NONE);
184 rend_SetFlatColor(color);
185 rend_SetAlphaValue(m_UIDrawAlpha);
186 rend_DrawPolygon2D(0, pntlist, 4);
187 }
188
189 // draws all textures derived from ui_bm_handle.
190 void ui_DrawBitmap(UIBitmapItem *bi, int x, int y, uint8_t alpha) {
191 if (bi->is_chunked()) {
192 chunked_bitmap *chunk = bi->get_chunked_bitmap();
193 rend_DrawChunkedBitmap(chunk, x, y, alpha);
194 } else {
195 int bmp_w = bm_w(bi->get_bitmap(), 0);
196 int bmp_h = bm_h(bi->get_bitmap(), 0);
197 float u1, v1;
198
199 if ((x + bmp_w) > m_UIDrawRight) {
200 u1 = ((float)(m_UIDrawRight - x)) / ((float)bmp_w);
201 bmp_w = m_UIDrawRight - x;
202 } else
203 u1 = 1.0f;
204
205 if ((y + bmp_h) > m_UIDrawBottom) {
206 v1 = ((float)(m_UIDrawBottom - y)) / ((float)bmp_h);
207 bmp_h = m_UIDrawBottom - y;
208 } else
209 v1 = 1.0f;
210
211 rend_SetOverlayType(OT_NONE);
212 rend_SetLighting(LS_NONE);
213 rend_SetColorModel(CM_MONO);
214 rend_SetZBufferState(0);
215 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
216 rend_SetAlphaValue(alpha);
217 rend_SetWrapType(WT_CLAMP);
218
219 rend_DrawScaledBitmap(x, y, x + bmp_w, y + bmp_h, bi->get_bitmap(), 0, 0, u1, v1);
220 }
221 }
222
223 int ui_GetBitmapWidth(UIBitmapItem *bi) {
224 if (bi->is_chunked()) {
225 chunked_bitmap *chunk = bi->get_chunked_bitmap();
226 return chunk->pw;
227 } else {
228 return bm_w(bi->get_bitmap(), 0);
229 }
230 }
231
232 int ui_GetBitmapHeight(UIBitmapItem *bi) {
233 if (bi->is_chunked()) {
234 chunked_bitmap *chunk = bi->get_chunked_bitmap();
235 return chunk->ph;
236 } else {
237 return bm_h(bi->get_bitmap(), 0);
238 }
239 }
240
241 // sets font for text drawing.
242 void ui_DrawSetFont(int handle) { grtext_SetFont(handle); }
243
244 int ui_DrawGetFont() { return grtext_GetFont(); }
245
246 int ui_GetFontHeight() { return grfont_GetHeight(grtext_GetFont()); }
247
248 void ui_SetCharAlpha(uint8_t alpha) { m_UICharAlpha = alpha; }
249
250 int ui_GetTextWidth(const char *text) { return grtext_GetLineWidth((char *)text); }
251
252 int ui_GetTextHeight(const char *text) { return grtext_GetHeight(text); }
253
254 void ui_DrawSetTextType(int type) {
255 switch (type) {
256 case UI_TEXTTYPE_SHADOW:
257 m_UITextFlags = GRTEXTFLAG_SHADOW;
258 break;
259 case UI_TEXTTYPE_SATURATE:
260 m_UITextFlags = GRTEXTFLAG_SATURATE;
261 break;
262 default:
263 m_UITextFlags = 0;
264 }
265 }
266
267 // draws a line of text.
268 void ui_DrawString(ddgr_color col, int x, int y, const char *str) {
269 grtext_SetFlags(m_UITextFlags);
270 grtext_SetAlpha(m_UICharAlpha);
271 grtext_SetColor(col);
272 grtext_Puts(x, y, str);
273 grtext_Flush();
274 }
275
276 void ui_DrawCenteredString(ddgr_color col, int xoff, int y, const char *str) {
277 grtext_SetFlags(m_UITextFlags);
278 grtext_SetAlpha(m_UICharAlpha);
279 grtext_SetColor(col);
280 grtext_CenteredPrintf(xoff, y, str);
281 grtext_Flush();
282 }