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)
52 #define CON_NUM_LINES 128
53 // Cut the buffer line if it becomes longer than this
54 #define CON_CHARS_PER_LINE 128
55 // Border in pixels from the most left to the first letter
56 #define CON_CHAR_BORDER 4
57 // Spacing in pixels between lines
58 #define CON_LINE_SPACE 1
59 // Scroll this many lines at a time (when pressing PGUP or PGDOWN)
60 #define CON_LINE_SCROLL 2
61 // Indicator showing that you scrolled up the history
62 #define CON_SCROLL_INDICATOR "^"
63 // Defines the opening/closing speed
64 #define CON_OPENCLOSE_SPEED 50
67 /* The console's data */
68 static int Visible; // Enum that tells which visible state we are in CON_HIDE, CON_SHOW, CON_RAISE, CON_LOWER
69 static int RaiseOffset; // Offset used when scrolling in the console
70 static char **ConsoleLines; // List of all the past lines
71 static int TotalConsoleLines; // Total number of lines in the console
72 static int ConsoleScrollBack; // How much the user scrolled back in the console
73 static int LineBuffer; // The number of visible lines in the console (autocalculated)
74 static int VChars; // The number of visible characters in one console line (autocalculated)
75 static grs_canvas *ConsoleSurface; // Canvas of the console
76 static grs_bitmap *BackgroundImage; // Background image for the console
77 static grs_bitmap *InputBackground; // Dirty rectangle to draw over behind the users background
79 /* console is ready to be written to */
80 static int con_initialized;
84 static void con_update_offset(void);
85 /* Frees all the memory loaded by the console */
86 static void con_free(void);
87 static int con_background(grs_bitmap *image);
88 /* Sets font info for the console */
89 static void con_font(grs_font *font, int fg, int bg);
90 /* makes newline (same as printf("\n") or CON_Out("\n") ) */
91 static void con_newline(void);
92 /* updates console after resize etc. */
93 static void con_update(void);
94 /* Called if you press Ctrl-L (deletes the History) */
95 static void con_clear(void);
98 /* Takes keys from the keyboard and inputs them to the console
99 * If the event was not handled (i.e. WM events or unknown ctrl-shift
100 * sequences) the function returns the event for further processing. */
101 int con_key_handler(int key)
103 unsigned char character = key_to_ascii(key);
105 if (!con_is_visible())
111 case KEY_SHIFTED + KEY_ESC:
114 case KEY_CTRLED + KEY_L:
118 case KEY_SHIFTED + KEY_HOME:
119 ConsoleScrollBack = LineBuffer-1;
122 case KEY_SHIFTED + KEY_END:
123 ConsoleScrollBack = 0;
127 ConsoleScrollBack += CON_LINE_SCROLL;
128 if(ConsoleScrollBack > LineBuffer-1)
129 ConsoleScrollBack = LineBuffer-1;
133 ConsoleScrollBack -= CON_LINE_SCROLL;
134 if(ConsoleScrollBack < 0)
135 ConsoleScrollBack = 0;
138 case KEY_CTRLED + KEY_A:
139 case KEY_HOME: cli_cursor_home(); break;
141 case KEY_CTRLED + KEY_E: cli_cursor_end(); break;
142 case KEY_CTRLED + KEY_C: cli_clear(); break;
143 case KEY_LEFT: cli_cursor_left(); break;
144 case KEY_RIGHT: cli_cursor_right(); break;
145 case KEY_BACKSP: cli_cursor_backspace(); break;
146 case KEY_CTRLED + KEY_D:
147 case KEY_DELETE: cli_cursor_del(); break;
148 case KEY_UP: cli_history_prev(); break;
149 case KEY_DOWN: cli_history_next(); break;
150 case KEY_TAB: cli_autocomplete(); break;
151 case KEY_ENTER: cli_execute(); break;
153 CLI_insert_mode = !CLI_insert_mode;
156 if (character == 255)
158 cli_add_character(character);
165 /* Updates the console buffer */
166 static void con_update(void)
171 grs_canvas *canv_save;
173 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
174 if (!con_is_visible())
177 Screenlines = ConsoleSurface->cv_h / (CON_LINE_SPACE + ConsoleSurface->cv_font->ft_h);
179 canv_save = grd_curcanv;
180 gr_set_current_canvas(ConsoleSurface);
182 /* draw the background image if there is one */
184 gr_bitmap(0, 0, BackgroundImage);
186 // now draw text from last but second line to top
187 for (loop = 0; loop < Screenlines-1 && loop < LineBuffer - ConsoleScrollBack; loop++) {
188 if (ConsoleScrollBack != 0 && loop == 0)
189 for (loop2 = 0; loop2 < (VChars / 5) + 1; loop2++)
191 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);
195 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + ConsoleSurface->cv_font->ft_h), ConsoleLines[ConsoleScrollBack + loop]);
199 gr_set_current_canvas(canv_save);
203 static void con_update_offset(void)
207 RaiseOffset -= CON_OPENCLOSE_SPEED;
208 if(RaiseOffset <= 0) {
210 Visible = CON_CLOSED;
214 RaiseOffset += CON_OPENCLOSE_SPEED;
215 if(RaiseOffset >= ConsoleSurface->cv_h) {
216 RaiseOffset = ConsoleSurface->cv_h;
227 /* Draws the console buffer to the screen if the console is "visible" */
230 grs_canvas *canv_save;
233 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
234 if (Visible == CON_CLOSED)
237 /* Update the scrolling offset */
240 canv_save = grd_curcanv;
242 /* Update the command line since it has a blinking cursor */
243 gr_set_current_canvas(ConsoleSurface);
245 // restore InputBackground
246 gr_bitmap(0, ConsoleSurface->cv_h - ConsoleSurface->cv_font->ft_h, InputBackground);
248 cli_draw(ConsoleSurface->cv_h);
250 gr_set_current_canvas(&grd_curscreen->sc_canvas);
252 clip = gr_create_sub_bitmap(&ConsoleSurface->cv_bitmap, 0, ConsoleSurface->cv_h - RaiseOffset, ConsoleSurface->cv_w, RaiseOffset);
254 gr_bitmap(0, 0, clip);
255 gr_free_sub_bitmap(clip);
257 gr_set_current_canvas(canv_save);
261 void con_cmd_toggleconsole(int argc, char **argv)
264 cmd_appendf("help %s", argv[0]);
268 if (con_is_visible())
275 /* Initializes the console */
280 Visible = CON_CLOSED;
283 TotalConsoleLines = 0;
284 ConsoleScrollBack = 0;
285 BackgroundImage = NULL;
287 /* load the console surface */
288 ConsoleSurface = NULL;
290 /* Load the dirty rectangle for user input */
291 InputBackground = NULL;
293 VChars = CON_CHARS_PER_LINE - 1;
294 LineBuffer = CON_NUM_LINES;
296 ConsoleLines = (char **)d_malloc(sizeof(char *) * LineBuffer);
297 for (loop = 0; loop <= LineBuffer - 1; loop++) {
298 ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
305 cmd_addcommand("toggleconsole", con_cmd_toggleconsole, "toggleconsole\n" " show or hide the console");
306 cvar_registervariable (&con_threshold);
314 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
315 void con_init_gfx(int w, int h)
321 if (ConsoleSurface) {
322 /* resize console surface */
323 gr_free_bitmap_data(&ConsoleSurface->cv_bitmap);
324 gr_init_bitmap_alloc(&ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
326 /* load the console surface */
327 ConsoleSurface = gr_create_canvas(w, h);
330 /* Load the consoles font */
331 con_font(SMALL_FONT, gr_find_closest_color(29,29,47), -1);
333 /* make sure that the size of the console is valid */
334 if (w > grd_curscreen->sc_w || w < ConsoleSurface->cv_font->ft_w * 32)
335 w = grd_curscreen->sc_w;
336 if (h > grd_curscreen->sc_h || h < ConsoleSurface->cv_font->ft_h)
337 h = grd_curscreen->sc_h;
339 /* Load the dirty rectangle for user input */
341 gr_free_bitmap(InputBackground);
342 InputBackground = gr_create_bitmap(w, ConsoleSurface->cv_font->ft_h);
344 /* calculate the number of visible characters in the command line */
345 #if 0 // doesn't work because proportional font
346 VChars = (w - CON_CHAR_BORDER) / ConsoleSurface->cv_font->ft_w;
347 if (VChars >= CON_CHARS_PER_LINE)
348 VChars = CON_CHARS_PER_LINE - 1;
351 gr_init_bitmap_data(&bmp);
352 pcx_error = pcx_read_bitmap(CON_BG, &bmp, BM_LINEAR, pal);
353 Assert(pcx_error == PCX_ERROR_NONE);
354 gr_remap_bitmap_good(&bmp, pal, -1, -1);
355 con_background(&bmp);
356 gr_free_bitmap_data(&bmp);
360 /* Makes the console visible */
363 Visible = CON_OPENING;
368 /* Hides the console (make it invisible) */
371 Visible = CON_CLOSING;
376 /* tells wether the console is visible or not */
377 int con_is_visible(void)
379 return((Visible == CON_OPEN) || (Visible == CON_OPENING));
383 /* Frees all the memory loaded by the console */
384 static void con_free(void)
388 for (i = 0; i <= LineBuffer - 1; i++) {
389 d_free(ConsoleLines[i]);
391 d_free(ConsoleLines);
396 gr_free_canvas(ConsoleSurface);
397 ConsoleSurface = NULL;
400 gr_free_bitmap(BackgroundImage);
401 BackgroundImage = NULL;
404 gr_free_bitmap(InputBackground);
405 InputBackground = NULL;
411 /* Increments the console lines */
412 static void con_newline(void)
417 temp = ConsoleLines[LineBuffer - 1];
419 for (loop = LineBuffer - 1; loop > 0; loop--)
420 ConsoleLines[loop] = ConsoleLines[loop - 1];
422 ConsoleLines[0] = temp;
424 memset(ConsoleLines[0], 0, CON_CHARS_PER_LINE);
425 if (TotalConsoleLines < LineBuffer - 1)
428 //Now adjust the ConsoleScrollBack
429 //dont scroll if not at bottom
430 if(ConsoleScrollBack != 0)
433 if(ConsoleScrollBack > LineBuffer-1)
434 ConsoleScrollBack = LineBuffer-1;
438 static inline int con_get_width(void)
443 return ConsoleSurface->cv_bitmap.bm_w - CON_CHAR_BORDER;
447 static inline int con_get_string_width(char *string)
449 grs_canvas *canv_save;
455 canv_save = grd_curcanv;
456 gr_set_current_canvas(ConsoleSurface);
457 gr_get_string_size(string, &w, &h, &aw);
458 gr_set_current_canvas(canv_save);
465 # define vsnprintf _vsnprintf
468 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
469 static void con_out(const char *str, ...)
472 //keep some space free for stuff like CON_Out("blablabla %s", Command);
473 char temp[CON_CHARS_PER_LINE + 128];
476 va_start(marker, str);
477 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
482 // temp now contains the complete string we want to output
483 // the only problem is that temp is maybe longer than the console
484 // width so we have to cut it into several pieces
493 strcat(ConsoleLines[0], ptemp);
495 } else if (p - ptemp > VChars - strlen(ConsoleLines[0]) ||
496 con_get_string_width(ptemp) > con_get_width()) {
498 strncat(ConsoleLines[0], ptemp, VChars - strlen(ConsoleLines[0]));
499 ConsoleLines[0][VChars] = '\0';
505 strncat(ConsoleLines[0], ptemp, VChars - strlen(ConsoleLines[0]));
506 ConsoleLines[0][VChars] = '\0';
513 /* Adds background image to the console, scaled to size of console*/
514 static int con_background(grs_bitmap *image)
516 /* Free the background from the console */
519 gr_free_bitmap(BackgroundImage);
520 BackgroundImage = NULL;
524 /* Load a new background */
526 gr_free_bitmap(BackgroundImage);
527 BackgroundImage = gr_create_bitmap(ConsoleSurface->cv_w, ConsoleSurface->cv_h);
528 gr_bitmap_scale_to(image, BackgroundImage);
530 gr_bm_bitblt(BackgroundImage->bm_w, InputBackground->bm_h, 0, 0, 0, ConsoleSurface->cv_h - ConsoleSurface->cv_font->ft_h, BackgroundImage, InputBackground);
536 /* Sets font info for the console */
537 static void con_font(grs_font *font, int fg, int bg)
539 grs_canvas *canv_save;
541 canv_save = grd_curcanv;
542 gr_set_current_canvas(ConsoleSurface);
543 gr_set_curfont(font);
544 gr_set_fontcolor(fg, bg);
545 gr_set_current_canvas(canv_save);
549 static void con_clear(void)
553 for (loop = 0; loop <= LineBuffer - 1; loop++)
554 memset(ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
558 /* convert to ansi rgb colors 17-231 */
559 #define PAL2ANSI(x) ((36*gr_palette[(x)*3]/11) + (6*gr_palette[(x)*3+1]/11) + (gr_palette[(x)*3+2]/11) + 16)
561 /* Print a message to the console */
562 void con_printf(int priority, char *fmt, ...)
567 if (priority <= (con_threshold.intval))
569 va_start (arglist, fmt);
570 vsprintf (buffer, fmt, arglist);
576 if (!text_console_enabled)
579 if (isatty(fileno(stdout))) {
581 unsigned char color, spacing, underline;
591 printf("\x1B[38;5;%dm", PAL2ANSI(color));
598 //printf("<SPACING %d>", color);
605 //printf("<UNDERLINE>");
616 /* Produce a sanitised version and send it to the standard output */
635 printf("%s", buffer);