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"
61 #include "grtext.h"
62 #include "mem.h"
63 #include "textaux.h"
64
65 #define CONSOLE_LINE_FILLER 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 UIConsoleGadget::UIConsoleGadget() {
73 m_ConsoleFont = -1;
74 m_ConsoleBuffer = NULL;
75 m_ColorRows = NULL;
76 m_PutsBuffer = NULL;
77 }
78
79 UIConsoleGadget::~UIConsoleGadget() {}
80
81 // font = console font, NOT GADGET FONT. This font is used to proportion window
82 void UIConsoleGadget::Create(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_SetFont(font);
87 cw = grtext_GetLineWidth("A");
88 ch = grfont_GetHeight(font) + 1;
89
90 w = cw * (cols + 3);
91 h = ch * (rows + 3);
92
93 UIGadget::Create(parent, id, x, y, w, h, flags);
94
95 // initialize console info.
96 m_ConsoleFont = font;
97 m_Rows = rows;
98 m_Cols = cols;
99 m_VisRows = m_Rows;
100 m_VisRowStart = 0;
101 m_CurRow = 0;
102 m_CurCol = 0;
103 m_OffX = cw + cw / 2;
104 m_OffY = ch + ch / 2;
105 m_Rowsize = m_Cols + CONSOLE_LINE_FILLER + 1;
106 m_LineIndex = 0;
107
108 grtext_Reset();
109
110 // extra 32 bytes per row for color coding.
111 m_ConsoleBuffer = (char *)mem_malloc(m_Rows * m_Rowsize);
112 m_PutsBufLen = 512;
113 m_PutsBuffer = (char *)mem_malloc(m_PutsBufLen);
114
115 m_ColorRows = new ddgr_color[m_Rows];
116 memset(m_ConsoleBuffer, 0, m_Rows * m_Rowsize);
117 for (i = 0; i < m_Rows; i++)
118 m_ColorRows[i] = GR_GREEN;
119
120 // lock out any input to this gadget.
121 UIGadget::Disable();
122 }
123
124 void UIConsoleGadget::OnDestroy() {
125 if (m_ColorRows)
126 delete[] m_ColorRows;
127 if (m_ConsoleBuffer)
128 mem_free(m_ConsoleBuffer);
129 if (m_PutsBuffer)
130 mem_free(m_PutsBuffer);
131
132 UIGadget::OnDestroy();
133 }
134
135 // functions
136 void UIConsoleGadget::puts(ddgr_color col, const char *str) {
137 int len = strlen(str);
138 uint8_t r = GR_COLOR_RED(col), g = GR_COLOR_GREEN(col), b = GR_COLOR_BLUE(col);
139 char *linebuf;
140
141 if (len >= m_PutsBufLen) {
142 mem_free(m_PutsBuffer);
143 m_PutsBufLen = len + 1;
144 m_PutsBuffer = (char *)mem_malloc(m_PutsBufLen);
145 }
146
147 textaux_WordWrap(str, m_PutsBuffer, m_W - 2 * m_OffX, m_ConsoleFont);
148 len = strlen(m_PutsBuffer);
149
150 m_LineColor = col;
151 if (m_CurCol == 0)
152 m_ColorRows[m_CurRow] = m_LineColor;
153
154 if ((m_LineIndex + 5) >= m_Rowsize) {
155 UIConsoleGadget::PutChar('\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_LineIndex != 0) { // Above PutChar should've done this anyways. useless redundant code.
163 m_CurCol = 0; // doesn't really slow anything down.
164 m_LineIndex = 0;
165 }
166 }
167
168 // Error("Console line overflow in UIConsoleGadget line index (%d).\n", m_LineIndex);
169 linebuf = m_ConsoleBuffer + (m_Rowsize * m_CurRow);
170 linebuf[m_LineIndex++] = GR_COLOR_CHAR;
171 linebuf[m_LineIndex++] = (r == 0 ? 1 : r);
172 linebuf[m_LineIndex++] = (g == 0 ? 1 : g);
173 linebuf[m_LineIndex++] = (b == 0 ? 1 : b);
174
175 for (int i = 0; i < len; i++)
176 UIConsoleGadget::PutChar(m_PutsBuffer[i]);
177 }
178
179 // Inheritable operations
180 void UIConsoleGadget::OnDraw() {
181 int x, y, cy, index;
182 char *linebuf;
183
184 // draw frame
185 if (m_Flags & UIF_BORDER) {
186 ui_DrawBox(GR_GREEN, 1, 1, m_W - 1, m_H - 1);
187 ui_DrawBox(GR_RGB(64, 255, 64), 3, 3, m_W - 3, m_H - 3);
188 }
189
190 // draw text
191 ui_SetCharAlpha(255);
192 ui_DrawSetFont(m_ConsoleFont);
193
194 x = m_OffX;
195 y = m_OffY;
196
197 index = 0;
198
199 for (cy = m_VisRowStart; cy < (m_VisRowStart + m_VisRows); cy++) {
200 linebuf = m_ConsoleBuffer + (m_Rowsize * cy);
201 ui_DrawString(m_ColorRows[cy], x, y, linebuf);
202 y = y + ui_GetFontHeight() + 1;
203 }
204 }
205
206 // OnUserProcess
207 // Takes care of scrolling of text and any keyboard input.
208
209 void UIConsoleGadget::OnUserProcess() {}
210
211 // Internal functions
212
213 void UIConsoleGadget::PutChar(int ch) {
214 char *linebuf;
215
216 ASSERT(m_CurRow < m_Rows);
217 // ASSERT(m_CurCol < m_Cols);
218
219 if (ch == '\n') {
220 m_CurCol = 0;
221 m_LineIndex = 0;
222 m_CurRow++;
223 if (m_CurRow < (m_VisRowStart + m_VisRows))
224 m_ColorRows[m_CurRow] = m_LineColor;
225 } else {
226 linebuf = m_ConsoleBuffer + (m_CurRow * m_Rowsize);
227 linebuf[m_LineIndex] = (char)ch;
228 linebuf[m_LineIndex + 1] = 0;
229
230 // adjust current character position.
231 m_LineIndex++;
232 m_CurCol++;
233 if (m_LineIndex >= m_Rowsize) {
234 m_LineIndex = 0;
235 m_CurCol = 0;
236 m_CurRow++;
237 if (m_CurRow < (m_VisRowStart + m_VisRows))
238 m_ColorRows[m_CurRow] = m_LineColor;
239 }
240 }
241 if (m_CurRow >= (m_VisRowStart + m_VisRows)) {
242 // PUT SCROLL CODE HERE
243 UIConsoleGadget::Scroll();
244 m_ColorRows[m_CurRow] = m_LineColor;
245 }
246 }
247
248 void UIConsoleGadget::Scroll() {
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_VisRowStart < (m_Rows - m_VisRows)) {
255 m_VisRowStart++;
256 } else {
257 for (row = 1; row < m_Rows; row++) {
258 linebuf = (m_ConsoleBuffer + (m_Rowsize)*row);
259 linebuf2 = (m_ConsoleBuffer + (m_Rowsize) * (row - 1));
260 memcpy(linebuf2, linebuf, m_Rowsize);
261 memset(linebuf, 0, m_Rowsize);
262 m_ColorRows[row - 1] = m_ColorRows[row];
263 }
264 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 UIConsole::UIConsole() {}
274
275 UIConsole::~UIConsole() {}
276
277 // font = console font, NOT GADGET FONT. This font is used to proportion window
278 void UIConsole::Create(int x, int y, int font, int cols, int rows) {
279 UIWindow::Create(x, y, 10, 10);
280 UIPrimativeItem item{GR_BLACK};
281 UIWindow::SetBackItem(&item);
282
283 m_Console.Create(this, -1, 0, 0, font, cols, rows);
284
285 m_W = m_Console.W();
286 m_H = m_Console.H();
287 }
288
289 void UIConsole::Destroy() { UIWindow::Destroy(); }
290
291 // functions
292 void UIConsole::puts(const char *str) { m_Console.puts(GR_GREEN, str); }
293
294 void UIConsole::puts(ddgr_color col, const char *str) { m_Console.puts(col, str); }
295
296 // Inheritable operations
297 void UIConsole::OnDraw() {
298
299 // draw frame
300 UIWindow::OnDraw();
301 }