Debian Codegraph @

ui/UIConsole.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/UIConsole.cpp $
21 * $Revision: 8 $
22 * $Date: 8/11/99 10:50a $
23 * $Author: Samir $
24 *
25 * Console system
26 *
27 * $Log: /DescentIII/Main/ui/UIConsole.cpp $
28 *
29 * 8 8/11/99 10:50a Samir
30 * possible fix to console bug with really wacky formatted strings
31 * exceeding rowsize.
32 *
33 * 7 4/14/99 1:52a Jeff
34 * fixed case mismatched #includes
35 *
36 * 6 11/03/98 7:05p Samir
37 * added word wrapping console.
38 *
39 * 5 10/13/98 8:21p Samir
40 * disable input to console gadget.
41 *
42 * 4 6/23/98 5:05p Samir
43 * console supports color per line ONLY.
44 *
45 * 3 6/22/98 2:15p Samir
46 * added UIConsoleGadget.
47 *
48 * 2 2/02/98 7:35p Samir
49 * Full scrolling for console windows.
50 *
51 * 1 1/30/98 3:26p Samir
52 * UI Console initial revision.
53 *
54 * $NoKeywords: $
55 */
56
57 #include <cstdarg>
58 #include <cstring>
59
60 #include "UIlib.h"cxx . . $ `<file>/ui/UIlib.h`/
61 #include "grtext.h"cxx . . $ `<file>/grtext/grtext.h`/
62 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
63 #include "textaux.h"cxx . . $ `<file>/lib/textaux.h`/
64
65 #define CONSOLE_LINE_FILLERcxx . . $ `ui/UIConsole.cpp:65:9`! 128
66
67 // UIConsoleGadget should display a simple console gadget inside a window
68
69 // int m_ConsoleFont; // standard font for text in console
70 // char *m_ConsoleBuffer; // text buffer of console.
71
72 UIConsoleGadgetcxx . . $ UIConsoleGadget#::UIConsoleGadgetcxx . . $ UIConsoleGadget#UIConsoleGadget(49f6e7a06ebc5aa8).() {
73 m_ConsoleFontcxx . . $ UIConsoleGadget#m_ConsoleFont. = -1;
74 m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
75 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
76 m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
77 }
78
79 UIConsoleGadgetcxx . . $ UIConsoleGadget#::~cxx . . $ UIConsoleGadget#`~UIConsoleGadget`(ced63f7c635d850d).UIConsoleGadgetcxx . . $ UIConsoleGadget#() {}
80
81 // font = console font, NOT GADGET FONT. This font is used to proportion window
82 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::Createcxx . . $ UIConsoleGadget#Create(f586159e43e8c8b5).(UIWindowcxx . . $ UIWindow# *parent, int id, int x, int y, int font, int cols, int rows, int flags) {
83 int cw, ch, w, h;
84 int i;
85
86 grtext_SetFontcxx . . $ grtext_SetFont(d4f767463ce0a6b3).(font);
87 cw = grtext_GetLineWidthcxx . . $ `grtext/grtext.h:172:9`!("A");
88 ch = grfont_GetHeightcxx . . $ grfont_GetHeight(7864480464b09eea).(font) + 1;
89
90 w = cw * (cols + 3);
91 h = ch * (rows + 3);
92
93 UIGadgetcxx . . $ UIGadget#::Createcxx . . $ UIGadget#Create(43707b8add2f137d).(parent, id, x, y, w, h, flags);
94
95 // initialize console info.
96 m_ConsoleFontcxx . . $ UIConsoleGadget#m_ConsoleFont. = font;
97 m_Rowscxx . . $ UIConsoleGadget#m_Rows. = rows;
98 m_Colscxx . . $ UIConsoleGadget#m_Cols. = cols;
99 m_VisRowscxx . . $ UIConsoleGadget#m_VisRows. = m_Rowscxx . . $ UIConsoleGadget#m_Rows.;
100 m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. = 0;
101 m_CurRowcxx . . $ UIConsoleGadget#m_CurRow. = 0;
102 m_CurColcxx . . $ UIConsoleGadget#m_CurCol. = 0;
103 m_OffXcxx . . $ UIConsoleGadget#m_OffX. = cw + cw / 2;
104 m_OffYcxx . . $ UIConsoleGadget#m_OffY. = ch + ch / 2;
105 m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize. = m_Colscxx . . $ UIConsoleGadget#m_Cols. + CONSOLE_LINE_FILLERcxx . . $ `ui/UIConsole.cpp:65:9`! + 1;
106 m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. = 0;
107
108 grtext_Resetcxx . . $ grtext_Reset(49f6e7a06ebc5aa8).();
109
110 // extra 32 bytes per row for color coding.
111 m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(m_Rows * m_Rowsize);
112 m_PutsBufLencxx . . $ UIConsoleGadget#m_PutsBufLen. = 512;
113 m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(m_PutsBufLen);
114
115 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows. = new ddgr_colorcxx . . $ ddgr_color#[m_Rowscxx . . $ UIConsoleGadget#m_Rows.];
116 memset(m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer., 0, m_Rowscxx . . $ UIConsoleGadget#m_Rows. * m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.);
117 for (i = 0; i < m_Rowscxx . . $ UIConsoleGadget#m_Rows.; i++)
118 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[i] = GR_GREENcxx . . $ GR_GREEN.;
119
120 // lock out any input to this gadget.
121 UIGadgetcxx . . $ UIGadget#::Disablecxx . . $ UIGadget#Disable(49f6e7a06ebc5aa8).();
122 }
123
124 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::OnDestroycxx . . $ UIConsoleGadget#OnDestroy(49f6e7a06ebc5aa8).() {
125 if (m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.)
126 delete[] m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.;
127 if (m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer.)
128 mem_freecxx . . $ `mem/mem.h:80:9`!(m_ConsoleBuffer);
129 if (m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer.)
130 mem_freecxx . . $ `mem/mem.h:80:9`!(m_PutsBuffer);
131
132 UIGadgetcxx . . $ UIGadget#::OnDestroycxx . . $ UIGadget#OnDestroy(49f6e7a06ebc5aa8).();
133 }
134
135 // functions
136 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::putscxx . . $ UIConsoleGadget#puts(d7adf5cfafc0e15a).(ddgr_colorcxx . . $ ddgr_color# col, const char *str) {
137 int len = strlen(str);
138 uint8_tcxx . . $ uint8_t# r = GR_COLOR_REDcxx . . $ GR_COLOR_RED(4c679297bf94b9eb).(col), g = GR_COLOR_GREENcxx . . $ GR_COLOR_GREEN(4c679297bf94b9eb).(col), b = GR_COLOR_BLUEcxx . . $ GR_COLOR_BLUE(4c679297bf94b9eb).(col);
139 char *linebuf;
140
141 if (len >= m_PutsBufLencxx . . $ UIConsoleGadget#m_PutsBufLen.) {
142 mem_freecxx . . $ `mem/mem.h:80:9`!(m_PutsBuffer);
143 m_PutsBufLencxx . . $ UIConsoleGadget#m_PutsBufLen. = len + 1;
144 m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(m_PutsBufLen);
145 }
146
147 textaux_WordWrapcxx . . $ textaux_WordWrap(2480230cce71d9d7).(str, m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer., m_Wcxx . . $ UIObject#m_W. - 2 * m_OffXcxx . . $ UIConsoleGadget#m_OffX., m_ConsoleFontcxx . . $ UIConsoleGadget#m_ConsoleFont.);
148 len = strlen(m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer.);
149
150 m_LineColorcxx . . $ UIConsoleGadget#m_LineColor. = col;
151 if (m_CurColcxx . . $ UIConsoleGadget#m_CurCol. == 0)
152 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.] = m_LineColorcxx . . $ UIConsoleGadget#m_LineColor.;
153
154 if ((m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. + 5) >= m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.) {
155 UIConsoleGadgetcxx . . $ UIConsoleGadget#::PutCharcxx . . $ UIConsoleGadget#PutChar(d4f767463ce0a6b3).('\n'); // simulate newline to reset line index.
156
157 // force return character. note that this isn't a newline, but
158 // the above PutChar should've done this anyways. this is redundant code but since this bug didn't
159 // happen often and is hard to reproduce, i'm not sure the aboce PutChar would really work, but the below lines
160 // should definitely prevent a crash though may overwrite some text in the console window. but the
161 // newline should work and not mess up the display.
162 if (m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. != 0) { // Above PutChar should've done this anyways. useless redundant code.
163 m_CurColcxx . . $ UIConsoleGadget#m_CurCol. = 0; // doesn't really slow anything down.
164 m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. = 0;
165 }
166 }
167
168 // Error("Console line overflow in UIConsoleGadget line index (%d).\n", m_LineIndex);
169 linebuf = m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. + (m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize. * m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.);
170 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.++] = GR_COLOR_CHARcxx . . $ `lib/grdefs.h:82:9`!;
171 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.++] = (r == 0 ? 1 : r);
172 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.++] = (g == 0 ? 1 : g);
173 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.++] = (b == 0 ? 1 : b);
174
175 for (int i = 0; i < len; i++)
176 UIConsoleGadgetcxx . . $ UIConsoleGadget#::PutCharcxx . . $ UIConsoleGadget#PutChar(d4f767463ce0a6b3).(m_PutsBuffercxx . . $ UIConsoleGadget#m_PutsBuffer.[i]);
177 }
178
179 // Inheritable operations
180 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::OnDrawcxx . . $ UIConsoleGadget#OnDraw(49f6e7a06ebc5aa8).() {
181 int x, y, cy, index;
182 char *linebuf;
183
184 // draw frame
185 if (m_Flagscxx . . $ UIGadget#m_Flags. & UIF_BORDERcxx . . $ `ui/ui.h:344:9`!) {
186 ui_DrawBoxcxx . . $ ui_DrawBox(fe09a4b73d8d396f).(GR_GREENcxx . . $ GR_GREEN., 1, 1, m_Wcxx . . $ UIObject#m_W. - 1, m_Hcxx . . $ UIObject#m_H. - 1);
187 ui_DrawBoxcxx . . $ ui_DrawBox(fe09a4b73d8d396f).(GR_RGBcxx . . $ GR_RGB(530956271a9518a5).(64, 255, 64), 3, 3, m_Wcxx . . $ UIObject#m_W. - 3, m_Hcxx . . $ UIObject#m_H. - 3);
188 }
189
190 // draw text
191 ui_SetCharAlphacxx . . $ ui_SetCharAlpha(ac50b47256ae7414).(255);
192 ui_DrawSetFontcxx . . $ ui_DrawSetFont(d4f767463ce0a6b3).(m_ConsoleFontcxx . . $ UIConsoleGadget#m_ConsoleFont.);
193
194 x = m_OffXcxx . . $ UIConsoleGadget#m_OffX.;
195 y = m_OffYcxx . . $ UIConsoleGadget#m_OffY.;
196
197 index = 0;
198
199 for (cy = m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart.; cy < (m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. + m_VisRowscxx . . $ UIConsoleGadget#m_VisRows.); cy++) {
200 linebuf = m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. + (m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize. * cy);
201 ui_DrawStringcxx . . $ ui_DrawString(c261e420eb73c8b5).(m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[cy], x, y, linebuf);
202 y = y + ui_GetFontHeightcxx . . $ ui_GetFontHeight(b126dc7c1de90089).() + 1;
203 }
204 }
205
206 // OnUserProcess
207 // Takes care of scrolling of text and any keyboard input.
208
209 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::OnUserProcesscxx . . $ UIConsoleGadget#OnUserProcess(49f6e7a06ebc5aa8).() {}
210
211 // Internal functions
212
213 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::PutCharcxx . . $ UIConsoleGadget#PutChar(d4f767463ce0a6b3).(int ch) {
214 char *linebuf;
215
216 ASSERTcxx . . $ `misc/pserror.h:246:9`!(m_CurRow < m_Rows);
217 // ASSERT(m_CurCol < m_Cols);
218
219 if (ch == '\n') {
220 m_CurColcxx . . $ UIConsoleGadget#m_CurCol. = 0;
221 m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. = 0;
222 m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.++;
223 if (m_CurRowcxx . . $ UIConsoleGadget#m_CurRow. < (m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. + m_VisRowscxx . . $ UIConsoleGadget#m_VisRows.))
224 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.] = m_LineColorcxx . . $ UIConsoleGadget#m_LineColor.;
225 } else {
226 linebuf = m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. + (m_CurRowcxx . . $ UIConsoleGadget#m_CurRow. * m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.);
227 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.] = (char)ch;
228 linebuf[m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. + 1] = 0;
229
230 // adjust current character position.
231 m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex.++;
232 m_CurColcxx . . $ UIConsoleGadget#m_CurCol.++;
233 if (m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. >= m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.) {
234 m_LineIndexcxx . . $ UIConsoleGadget#m_LineIndex. = 0;
235 m_CurColcxx . . $ UIConsoleGadget#m_CurCol. = 0;
236 m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.++;
237 if (m_CurRowcxx . . $ UIConsoleGadget#m_CurRow. < (m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. + m_VisRowscxx . . $ UIConsoleGadget#m_VisRows.))
238 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.] = m_LineColorcxx . . $ UIConsoleGadget#m_LineColor.;
239 }
240 }
241 if (m_CurRowcxx . . $ UIConsoleGadget#m_CurRow. >= (m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. + m_VisRowscxx . . $ UIConsoleGadget#m_VisRows.)) {
242 // PUT SCROLL CODE HERE
243 UIConsoleGadgetcxx . . $ UIConsoleGadget#::Scrollcxx . . $ UIConsoleGadget#Scroll(49f6e7a06ebc5aa8).();
244 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.] = m_LineColorcxx . . $ UIConsoleGadget#m_LineColor.;
245 }
246 }
247
248 void UIConsoleGadgetcxx . . $ UIConsoleGadget#::Scrollcxx . . $ UIConsoleGadget#Scroll(49f6e7a06ebc5aa8).() {
249 char *linebuf, *linebuf2;
250 int row;
251
252 // adjust visible row start if visible region is in buffer
253 // other wise scroll all text.
254 if (m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart. < (m_Rowscxx . . $ UIConsoleGadget#m_Rows. - m_VisRowscxx . . $ UIConsoleGadget#m_VisRows.)) {
255 m_VisRowStartcxx . . $ UIConsoleGadget#m_VisRowStart.++;
256 } else {
257 for (row = 1; row < m_Rowscxx . . $ UIConsoleGadget#m_Rows.; row++) {
258 linebuf = (m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. + (m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.)*row);
259 linebuf2 = (m_ConsoleBuffercxx . . $ UIConsoleGadget#m_ConsoleBuffer. + (m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.) * (row - 1));
260 memcpy(linebuf2, linebuf, m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.);
261 memset(linebuf, 0, m_Rowsizecxx . . $ UIConsoleGadget#m_Rowsize.);
262 m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[row - 1] = m_ColorRowscxx . . $ UIConsoleGadget#m_ColorRows.[row];
263 }
264 m_CurRowcxx . . $ UIConsoleGadget#m_CurRow.--;
265 }
266 }
267
268 // UIConsole should display a simple console window where anyone can dump text into
269
270 // int m_ConsoleFont; // standard font for text in console
271 // char *m_ConsoleBuffer; // text buffer of console.
272
273 UIConsolecxx . . $ UIConsole#::UIConsolecxx . . $ UIConsole#UIConsole(49f6e7a06ebc5aa8).() {}
274
275 UIConsolecxx . . $ UIConsole#::~cxx . . $ UIConsole#`~UIConsole`(ced63f7c635d850d).UIConsolecxx . . $ UIConsole#() {}
276
277 // font = console font, NOT GADGET FONT. This font is used to proportion window
278 void UIConsolecxx . . $ UIConsole#::Createcxx . . $ UIConsole#Create(1559a299834bcdd5).(int x, int y, int font, int cols, int rows) {
279 UIWindowcxx . . $ UIWindow#::Createcxx . . $ UIWindow#Create(1559a299834bcdd5).(x, y, 10, 10);
280 UIPrimativeItemcxx . . $ UIPrimativeItem# itemcxx . . $ UIPrimativeItem#UIPrimativeItem(cc289a6b3dfc48da).{GR_BLACKcxx . . $ GR_BLACK.};
281 UIWindowcxx . . $ UIWindow#::SetBackItemcxx . . $ UIWindow#SetBackItem(52afca12b6cce14a).(&item);
282
283 m_Consolecxx . . $ UIConsole#m_Console..Createcxx . . $ UIConsoleGadget#Create(f586159e43e8c8b5).(this, -1, 0, 0, font, cols, rows);
284
285 m_Wcxx . . $ UIObject#m_W. = m_Consolecxx . . $ UIConsole#m_Console..Wcxx . . $ UIObject#W(455f465bc33b4cdf).();
286 m_Hcxx . . $ UIObject#m_H. = m_Consolecxx . . $ UIConsole#m_Console..Hcxx . . $ UIObject#H(455f465bc33b4cdf).();
287 }
288
289 void UIConsolecxx . . $ UIConsole#::Destroycxx . . $ UIConsole#Destroy(49f6e7a06ebc5aa8).() { UIWindowcxx . . $ UIWindow#::Destroycxx . . $ UIWindow#Destroy(49f6e7a06ebc5aa8).(); }
290
291 // functions
292 void UIConsolecxx . . $ UIConsole#::putscxx . . $ UIConsole#puts(85c52e162fed56f9).(const char *str) { m_Consolecxx . . $ UIConsole#m_Console..putscxx . . $ UIConsoleGadget#puts(d7adf5cfafc0e15a).(GR_GREENcxx . . $ GR_GREEN., str); }
293
294 void UIConsolecxx . . $ UIConsole#::putscxx . . $ UIConsole#puts(d7adf5cfafc0e15a).(ddgr_colorcxx . . $ ddgr_color# col, const char *str) { m_Consolecxx . . $ UIConsole#m_Console..putscxx . . $ UIConsoleGadget#puts(d7adf5cfafc0e15a).(col, str); }
295
296 // Inheritable operations
297 void UIConsolecxx . . $ UIConsole#::OnDrawcxx . . $ UIConsole#OnDraw(49f6e7a06ebc5aa8).() {
298
299 // draw frame
300 UIWindowcxx . . $ UIWindow#::OnDrawcxx . . $ UIWindow#OnDraw(49f6e7a06ebc5aa8).();
301 }