2 * Code for controlling the console
3 * Based on an early version of SDL_Console
5 * Written By: Garrett Banuk <mongoose@mongeese.org>
6 * Code Cleanup and heavily extended by: Clemens Wacha <reflex-2000@gmx.net>
7 * Ported to use native Descent interfaces by: Bradley Bell <btb@icculus.org>
9 * This is free, just be sure to give us credit when using it
10 * in any of your programs.
38 int text_console_enabled = 1;
41 #define text_console_enabled (!isvga())
45 /* How discriminating we are about which messages are displayed */
46 cvar_t con_threshold = { "con_threshold", "0", CVAR_NONE };
48 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
49 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
50 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
51 #define CON_FONT SMALL_FONT
52 #define CON_DEFAULT_COLOR gr_find_closest_color(29, 29, 47)
54 #define CON_NUM_LINES 128
55 // Cut the buffer line if it becomes longer than this
56 #define CON_CHARS_PER_LINE 128
57 // Border in pixels from the most left to the first letter
58 #define CON_CHAR_BORDER 4
59 // Spacing in pixels between lines
60 #define CON_LINE_SPACE 1
61 // Scroll this many lines at a time (when pressing PGUP or PGDOWN)
62 #define CON_LINE_SCROLL 2
63 // Indicator showing that you scrolled up the history
64 #define CON_SCROLL_INDICATOR "^"
65 // Defines the opening/closing speed
66 #define CON_OPENCLOSE_SPEED 50
69 /* The console's data */
70 static int Visible; // Enum that tells which visible state we are in CON_HIDE, CON_SHOW, CON_RAISE, CON_LOWER
71 static int RaiseOffset; // Offset used when scrolling in the console
72 static char **ConsoleLines; // List of all the past lines
73 static int TotalConsoleLines; // Total number of lines in the console
74 static int ConsoleScrollBack; // How much the user scrolled back in the console
75 static int LineBuffer; // The number of visible lines in the console (autocalculated)
76 static int VChars; // The number of visible characters in one console line (autocalculated)
77 static grs_canvas *ConsoleSurface; // Canvas of the console
78 static grs_bitmap *BackgroundImage; // Background image for the console
79 static grs_bitmap *InputBackground; // Dirty rectangle to draw over behind the users background
81 /* console is ready to be written to */
82 static int con_initialized;
84 /* text foreground color */
89 static void con_update_offset(void);
90 /* Frees all the memory loaded by the console */
91 static void con_free(void);
92 static int con_background(grs_bitmap *image);
93 /* Sets font info for the console */
94 static void con_font(grs_font *font);
95 /* makes newline (same as printf("\n") or CON_Out("\n") ) */
96 static void con_newline(void);
97 /* updates console after resize etc. */
98 static void con_update(void);
99 /* Called if you press Ctrl-L (deletes the History) */
100 static void con_clear(void);
103 /* Takes keys from the keyboard and inputs them to the console
104 * If the event was not handled (i.e. WM events or unknown ctrl-shift
105 * sequences) the function returns the event for further processing. */
106 int con_key_handler(int key)
108 unsigned char character = key_to_ascii(key);
110 if (!con_is_visible())
116 case KEY_SHIFTED + KEY_ESC:
119 case KEY_CTRLED + KEY_L:
123 case KEY_SHIFTED + KEY_HOME:
124 ConsoleScrollBack = LineBuffer-1;
127 case KEY_SHIFTED + KEY_END:
128 ConsoleScrollBack = 0;
132 ConsoleScrollBack += CON_LINE_SCROLL;
133 if(ConsoleScrollBack > LineBuffer-1)
134 ConsoleScrollBack = LineBuffer-1;
138 ConsoleScrollBack -= CON_LINE_SCROLL;
139 if(ConsoleScrollBack < 0)
140 ConsoleScrollBack = 0;
143 case KEY_CTRLED + KEY_A:
144 case KEY_HOME: cli_cursor_home(); break;
146 case KEY_CTRLED + KEY_E: cli_cursor_end(); break;
147 case KEY_CTRLED + KEY_C: cli_clear(); break;
148 case KEY_LEFT: cli_cursor_left(); break;
149 case KEY_RIGHT: cli_cursor_right(); break;
150 case KEY_BACKSP: cli_cursor_backspace(); break;
151 case KEY_CTRLED + KEY_D:
152 case KEY_DELETE: cli_cursor_del(); break;
153 case KEY_UP: cli_history_prev(); break;
154 case KEY_DOWN: cli_history_next(); break;
155 case KEY_TAB: cli_autocomplete(); break;
156 case KEY_ENTER: cli_execute(); break;
158 CLI_insert_mode = !CLI_insert_mode;
161 if (character == 255)
163 cli_add_character(character);
170 /* Updates the console buffer */
171 static void con_update(void)
176 grs_canvas *canv_save;
178 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
179 if (!con_is_visible())
182 Screenlines = ConsoleSurface->cv_h / (CON_LINE_SPACE + ConsoleSurface->cv_font->ft_h);
184 canv_save = grd_curcanv;
185 gr_set_current_canvas(ConsoleSurface);
187 gr_set_fontcolor(CON_color, -1);
189 /* draw the background image if there is one */
191 gr_bitmap(0, 0, BackgroundImage);
193 // now draw text from last but second line to top
194 for (loop = 0; loop < Screenlines-1 && loop < LineBuffer - ConsoleScrollBack; loop++) {
195 if (ConsoleScrollBack != 0 && loop == 0)
196 for (loop2 = 0; loop2 < (VChars / 5) + 1; loop2++)
198 gr_string(CON_CHAR_BORDER + (loop2*5*ConsoleSurface->cv_font->ft_w), (Screenlines - loop - 2) * (CON_LINE_SPACE + ConsoleSurface->cv_font->ft_h), CON_SCROLL_INDICATOR);
202 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + ConsoleSurface->cv_font->ft_h), ConsoleLines[ConsoleScrollBack + loop]);
206 gr_set_current_canvas(canv_save);
210 static void con_update_offset(void)
214 RaiseOffset -= CON_OPENCLOSE_SPEED;
215 if(RaiseOffset <= 0) {
217 Visible = CON_CLOSED;
221 RaiseOffset += CON_OPENCLOSE_SPEED;
222 if(RaiseOffset >= ConsoleSurface->cv_h) {
223 RaiseOffset = ConsoleSurface->cv_h;
234 /* Draws the console buffer to the screen if the console is "visible" */
237 grs_canvas *canv_save;
240 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
241 if (Visible == CON_CLOSED)
244 /* Update the scrolling offset */
247 canv_save = grd_curcanv;
249 /* Update the command line since it has a blinking cursor */
250 gr_set_current_canvas(ConsoleSurface);
252 // restore InputBackground
253 gr_bitmap(0, ConsoleSurface->cv_h - ConsoleSurface->cv_font->ft_h, InputBackground);
255 cli_draw(ConsoleSurface->cv_h);
257 gr_set_current_canvas(&grd_curscreen->sc_canvas);
259 clip = gr_create_sub_bitmap(&ConsoleSurface->cv_bitmap, 0, ConsoleSurface->cv_h - RaiseOffset, ConsoleSurface->cv_w, RaiseOffset);
261 gr_bitmap(0, 0, clip);
262 gr_free_sub_bitmap(clip);
264 gr_set_current_canvas(canv_save);
268 void con_cmd_toggleconsole(int argc, char **argv)
271 cmd_appendf("help %s", argv[0]);
275 if (con_is_visible())
282 /* Initializes the console */
287 Visible = CON_CLOSED;
290 TotalConsoleLines = 0;
291 ConsoleScrollBack = 0;
292 BackgroundImage = NULL;
294 /* load the console surface */
295 ConsoleSurface = NULL;
297 /* Load the dirty rectangle for user input */
298 InputBackground = NULL;
300 VChars = CON_CHARS_PER_LINE - 1;
301 LineBuffer = CON_NUM_LINES;
303 ConsoleLines = (char **)d_malloc(sizeof(char *) * LineBuffer);
304 for (loop = 0; loop <= LineBuffer - 1; loop++) {
305 ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
312 cmd_addcommand("toggleconsole", con_cmd_toggleconsole, "toggleconsole\n" " show or hide the console");
313 cvar_registervariable (&con_threshold);
321 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
322 void con_init_gfx(int w, int h)
328 if (ConsoleSurface) {
329 /* resize console surface */
330 gr_free_bitmap_data(&ConsoleSurface->cv_bitmap);
331 gr_init_bitmap_alloc(&ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
333 /* load the console surface */
334 ConsoleSurface = gr_create_canvas(w, h);
337 CON_color = CON_DEFAULT_COLOR;
339 /* Load the consoles font */
342 /* make sure that the size of the console is valid */
343 if (w > grd_curscreen->sc_w || w < ConsoleSurface->cv_font->ft_w * 32)
344 w = grd_curscreen->sc_w;
345 if (h > grd_curscreen->sc_h || h < ConsoleSurface->cv_font->ft_h)
346 h = grd_curscreen->sc_h;
348 /* Load the dirty rectangle for user input */
350 gr_free_bitmap(InputBackground);
351 InputBackground = gr_create_bitmap(w, ConsoleSurface->cv_font->ft_h);
353 /* calculate the number of visible characters in the command line */
354 #if 0 // doesn't work because proportional font
355 VChars = (w - CON_CHAR_BORDER) / ConsoleSurface->cv_font->ft_w;
356 if (VChars >= CON_CHARS_PER_LINE)
357 VChars = CON_CHARS_PER_LINE - 1;
360 gr_init_bitmap_data(&bmp);
361 pcx_error = pcx_read_bitmap(CON_BG, &bmp, BM_LINEAR, pal);
362 Assert(pcx_error == PCX_ERROR_NONE);
363 gr_remap_bitmap_good(&bmp, pal, -1, -1);
364 con_background(&bmp);
365 gr_free_bitmap_data(&bmp);
369 /* Makes the console visible */
372 Visible = CON_OPENING;
377 /* Hides the console (make it invisible) */
380 Visible = CON_CLOSING;
385 /* tells wether the console is visible or not */
386 int con_is_visible(void)
388 return((Visible == CON_OPEN) || (Visible == CON_OPENING));
392 /* Frees all the memory loaded by the console */
393 static void con_free(void)
397 for (i = 0; i <= LineBuffer - 1; i++) {
398 d_free(ConsoleLines[i]);
400 d_free(ConsoleLines);
405 gr_free_canvas(ConsoleSurface);
406 ConsoleSurface = NULL;
409 gr_free_bitmap(BackgroundImage);
410 BackgroundImage = NULL;
413 gr_free_bitmap(InputBackground);
414 InputBackground = NULL;
420 /* Increments the console lines */
421 static void con_newline(void)
426 temp = ConsoleLines[LineBuffer - 1];
428 for (loop = LineBuffer - 1; loop > 0; loop--)
429 ConsoleLines[loop] = ConsoleLines[loop - 1];
431 ConsoleLines[0] = temp;
433 memset(ConsoleLines[0], 0, CON_CHARS_PER_LINE);
434 if (TotalConsoleLines < LineBuffer - 1)
437 //Now adjust the ConsoleScrollBack
438 //dont scroll if not at bottom
439 if(ConsoleScrollBack != 0)
442 if(ConsoleScrollBack > LineBuffer-1)
443 ConsoleScrollBack = LineBuffer-1;
447 static inline int con_get_width(void)
452 return ConsoleSurface->cv_bitmap.bm_w - CON_CHAR_BORDER;
456 static inline int con_get_string_width(char *string)
458 grs_canvas *canv_save;
464 canv_save = grd_curcanv;
465 gr_set_current_canvas(ConsoleSurface);
466 gr_get_string_size(string, &w, &h, &aw);
467 gr_set_current_canvas(canv_save);
474 # define vsnprintf _vsnprintf
477 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
478 static void con_out(const char *str, ...)
481 //keep some space free for stuff like CON_Out("blablabla %s", Command);
482 char temp[CON_CHARS_PER_LINE + 128];
485 va_start(marker, str);
486 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
491 // temp now contains the complete string we want to output
492 // the only problem is that temp is maybe longer than the console
493 // width so we have to cut it into several pieces
502 strcat(ConsoleLines[0], ptemp);
504 } else if (p - ptemp > VChars - strlen(ConsoleLines[0]) ||
505 con_get_string_width(ptemp) > con_get_width()) {
507 strncat(ConsoleLines[0], ptemp, VChars - strlen(ConsoleLines[0]));
508 ConsoleLines[0][VChars] = '\0';
514 strncat(ConsoleLines[0], ptemp, VChars - strlen(ConsoleLines[0]));
515 ConsoleLines[0][VChars] = '\0';
522 /* Adds background image to the console, scaled to size of console*/
523 static int con_background(grs_bitmap *image)
525 /* Free the background from the console */
528 gr_free_bitmap(BackgroundImage);
529 BackgroundImage = NULL;
533 /* Load a new background */
535 gr_free_bitmap(BackgroundImage);
536 BackgroundImage = gr_create_bitmap(ConsoleSurface->cv_w, ConsoleSurface->cv_h);
537 gr_bitmap_scale_to(image, BackgroundImage);
539 gr_bm_bitblt(BackgroundImage->bm_w, InputBackground->bm_h, 0, 0, 0, ConsoleSurface->cv_h - ConsoleSurface->cv_font->ft_h, BackgroundImage, InputBackground);
545 /* Sets font info for the console */
546 static void con_font(grs_font *font)
548 grs_canvas *canv_save;
550 canv_save = grd_curcanv;
551 gr_set_current_canvas(ConsoleSurface);
552 gr_set_curfont(font);
553 gr_set_current_canvas(canv_save);
557 static void con_clear(void)
561 for (loop = 0; loop <= LineBuffer - 1; loop++)
562 memset(ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
566 /* convert to ansi rgb colors 17-231 */
567 #define PAL2ANSI(x) ((36*gr_palette[(x)*3]/11) + (6*gr_palette[(x)*3+1]/11) + (gr_palette[(x)*3+2]/11) + 16)
569 /* Print a message to the console */
570 void con_printf(int priority, char *fmt, ...)
575 if (priority <= (con_threshold.intval))
577 va_start (arglist, fmt);
578 vsprintf (buffer, fmt, arglist);
584 if (!text_console_enabled)
587 if (isatty(fileno(stdout))) {
589 unsigned char color, spacing, underline;
599 printf("\x1B[38;5;%dm", PAL2ANSI(color));
606 //printf("<SPACING %d>", color);
613 //printf("<UNDERLINE>");
624 /* Produce a sanitised version and send it to the standard output */
643 printf("%s", buffer);