2 * Code for controlling the console
3 * Based on an old 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 /* Console specific cvars */
46 /* How discriminating we are about which messages are displayed */
47 cvar_t con_threshold = {"con_threshold", "0",};
49 /* Private console stuff */
50 #define CON_NUM_LINES 128
52 #define FG_COLOR grd_curcanv->cv_font_fg_color
53 #define get_msecs() approx_fsec_to_msec(timer_get_approx_seconds())
55 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
56 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
57 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
59 //! Cut the buffer line if it becomes longer than this
60 #define CON_CHARS_PER_LINE 128
61 //! Cursor blink frequency in ms
62 #define CON_BLINK_RATE 500
63 //! Border in pixels from the most left to the first letter
64 #define CON_CHAR_BORDER 4
65 //! Spacing in pixels between lines
66 #define CON_LINE_SPACE 1
67 //! Default prompt used at the commandline
68 #define CON_DEFAULT_PROMPT "]"
69 //! Scroll this many lines at a time (when pressing PGUP or PGDOWN)
70 #define CON_LINE_SCROLL 2
71 //! Indicator showing that you scrolled up the history
72 #define CON_SCROLL_INDICATOR "^"
73 //! Cursor shown if we are in insert mode
74 #define CON_INS_CURSOR "_"
75 //! Cursor shown if we are in overwrite mode
76 #define CON_OVR_CURSOR "|"
77 //! Defines the default hide key (Hide() the console if pressed)
78 #define CON_DEFAULT_HIDEKEY KEY_ESC
79 //! Defines the opening/closing speed
80 #define CON_OPENCLOSE_SPEED 50
83 /*! This is a struct for the console's data */
84 typedef struct console_information_td {
85 int Visible; //! enum that tells which visible state we are in CON_HIDE, CON_SHOW, CON_RAISE, CON_LOWER
86 int RaiseOffset; //! Offset used when scrolling in the console
87 int HideKey; //! the key that can hide the console
88 char **ConsoleLines; //! List of all the past lines
89 char **CommandLines; //! List of all the past commands
90 int TotalConsoleLines; //! Total number of lines in the console
91 int ConsoleScrollBack; //! How much the user scrolled back in the console
92 int TotalCommands; //! Number of commands in the Back Commands
93 int LineBuffer; //! The number of visible lines in the console (autocalculated)
94 int VChars; //! The number of visible characters in one console line (autocalculated)
95 char* Prompt; //! Prompt displayed in command line
96 char Command[CON_CHARS_PER_LINE]; //! current command in command line = lcommand + rcommand
97 char RCommand[CON_CHARS_PER_LINE]; //! left hand side of cursor
98 char LCommand[CON_CHARS_PER_LINE]; //! right hand side of cursor
99 char VCommand[CON_CHARS_PER_LINE]; //! current visible command line
100 int CursorPos; //! Current cursor position in CurrentCommand
101 int Offset; //! CommandOffset (first visible char of command) - if command is too long to fit into console
102 int InsMode; //! Insert or Overwrite characters?
103 grs_canvas *ConsoleSurface; //! Canvas of the console
104 grs_bitmap *BackgroundImage; //! Background image for the console
105 grs_bitmap *InputBackground; //! Dirty rectangle to draw over behind the users background
107 unsigned char ConsoleAlpha; //! The consoles alpha level
109 int CommandScrollBack; //! How much the users scrolled back in the command lines
112 static ConsoleInformation Console;
113 #define console (&Console)
115 /* console is ready to be written to */
116 static int con_initialized;
120 void CON_UpdateOffset(void);
121 /*! Frees all the memory loaded by the console */
124 /*! Sets the alpha channel of an SDL_Surface to the specified value (0 - transparend,
125 255 - opaque). Use this function also for OpenGL. */
126 void CON_Alpha(unsigned char alpha);
127 /*! Internal: Sets the alpha channel of an SDL_Surface to the specified value.
128 Preconditions: the surface in question is RGBA. 0 <= a <= 255, where 0 is transparent and 255 opaque */
129 void CON_AlphaGL(SDL_Surface *s, int alpha);
130 /*! Sets a background image for the console */
132 int CON_Background(grs_bitmap *image);
133 /*! Sets font info for the console */
134 void CON_Font(grs_font *font, int fg, int bg);
135 /*! Modify the prompt of the console */
136 void CON_SetPrompt(char* newprompt);
137 /*! Set the key, that invokes a CON_Hide() after press. default is ESCAPE and you can always hide using
138 ESCAPE and the HideKey. compared against event->key.keysym.sym !! */
139 void CON_SetHideKey(int key);
140 /*! Internal: executes the command typed in at the console (called if you press ENTER)*/
141 void CON_Execute(char* command);
142 /*! Internal: Gets called when TAB was pressed */
143 void CON_TabCompletion(void);
144 /*! Internal: makes newline (same as printf("\n") or CON_Out("\n") ) */
145 void CON_NewLineConsole(void);
146 /*! Internal: shift command history (the one you can switch with the up/down keys) */
147 void CON_NewLineCommand(void);
148 /*! Internal: updates console after resize etc. */
149 void CON_UpdateConsole(void);
152 /*! Internal: draws the commandline the user is typing in to the screen. called by update? */
153 void DrawCommandLine();
155 /*! Internal: Gets called if you press the LEFT key (move cursor left) */
156 void Cursor_Left(void);
157 /*! Internal: Gets called if you press the RIGHT key (move cursor right) */
158 void Cursor_Right(void);
159 /*! Internal: Gets called if you press the HOME key (move cursor to the beginning
161 void Cursor_Home(void);
162 /*! Internal: Gets called if you press the END key (move cursor to the end of the line*/
163 void Cursor_End(void);
164 /*! Internal: Called if you press DELETE (deletes character under the cursor) */
165 void Cursor_Del(void);
166 /*! Internal: Called if you press BACKSPACE (deletes character left of cursor) */
167 void Cursor_BSpace(void);
168 /*! Internal: Called if you type in a character (add the char to the command) */
169 void Cursor_Add(int event);
171 /*! Internal: Called if you press Ctrl-C (deletes the commandline) */
172 void Clear_Command(void);
173 /*! Internal: Called if you press Ctrl-L (deletes the History) */
174 void Clear_History(void);
176 /*! Internal: Called if you press UP key (switches through recent typed in commands */
177 void Command_Up(void);
178 /*! Internal: Called if you press DOWN key (switches through recent typed in commands */
179 void Command_Down(void);
182 /* Takes keys from the keyboard and inputs them to the console
183 If the event was not handled (i.e. WM events or unknown ctrl-shift
184 sequences) the function returns the event for further processing. */
185 int CON_Events(int event)
190 if(event & KEY_CTRLED)
193 switch(event & ~KEY_CTRLED)
212 else if(event & KEY_ALTED)
214 //the console does not handle ALT combinations!
219 //first of all, check if the console hide key was pressed
220 if(event == console->HideKey)
225 switch (event & 0xff)
231 if(event & KEY_SHIFTED)
233 console->ConsoleScrollBack = console->LineBuffer-1;
240 if(event & KEY_SHIFTED)
242 console->ConsoleScrollBack = 0;
249 console->ConsoleScrollBack += CON_LINE_SCROLL;
250 if(console->ConsoleScrollBack > console->LineBuffer-1)
251 console->ConsoleScrollBack = console->LineBuffer-1;
256 console->ConsoleScrollBack -= CON_LINE_SCROLL;
257 if(console->ConsoleScrollBack < 0)
258 console->ConsoleScrollBack = 0;
280 console->InsMode = 1-console->InsMode;
286 if(strlen(console->Command) > 0) {
287 CON_NewLineCommand();
289 // copy the input into the past commands strings
290 strcpy(console->CommandLines[0], console->Command);
292 // display the command including the prompt
293 CON_Out("%s%s\n", console->Prompt, console->Command);
296 CON_Execute(console->Command);
299 console->CommandScrollBack = -1;
307 if (key_to_ascii(event) == 255)
321 /* CON_AlphaGL() -- sets the alpha channel of an SDL_Surface to the
322 * specified value. Preconditions: the surface in question is RGBA.
323 * 0 <= a <= 255, where 0 is transparent and 255 is opaque. */
324 void CON_AlphaGL(SDL_Surface *s, int alpha) {
329 SDL_PixelFormat *format;
330 static char errorPrinted = 0;
333 /* debugging assertions -- these slow you down, but hey, crashing sucks */
335 PRINT_ERROR("NULL Surface passed to CON_AlphaGL\n");
339 /* clamp alpha value to 0...255 */
340 if(alpha < SDL_ALPHA_TRANSPARENT)
341 val = SDL_ALPHA_TRANSPARENT;
342 else if(alpha > SDL_ALPHA_OPAQUE)
343 val = SDL_ALPHA_OPAQUE;
347 /* loop over alpha channels of each pixel, setting them appropriately. */
351 switch (format->BytesPerPixel) {
353 /* 16-bit surfaces don't seem to support alpha channels. */
356 PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL.\n");
360 /* we can do this very quickly in 32-bit mode. 24-bit is more
361 * difficult. And since 24-bit mode is reall the same as 32-bit,
362 * so it usually ends up taking this route too. Win! Unroll loop
363 * and use pointer arithmetic for extra speed. */
364 int numpixels = h * (w << 2);
365 Uint8 *pix = (Uint8 *) (s->pixels);
366 Uint8 *last = pix + numpixels;
368 if((numpixels & 0x7) == 0)
369 for(pixel = pix + 3; pixel < last; pixel += 32)
370 *pixel = *(pixel + 4) = *(pixel + 8) = *(pixel + 12) = *(pixel + 16) = *(pixel + 20) = *(pixel + 24) = *(pixel + 28) = val;
372 for(pixel = pix + 3; pixel < last; pixel += 4)
377 /* we have no choice but to do this slowly. <sigh> */
378 for(y = 0; y < h; ++y)
379 for(x = 0; x < w; ++x) {
381 /* Lock the surface for direct access to the pixels */
382 if(SDL_MUSTLOCK(s) && SDL_LockSurface(s) < 0) {
383 PRINT_ERROR("Can't lock surface: ");
384 fprintf(stderr, "%s\n", SDL_GetError());
387 pixel = DT_GetPixel(s, x, y);
390 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
391 pixel = SDL_MapRGBA(format, r, g, b, val);
392 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
393 DT_PutPixel(s, x, y, pixel);
395 /* unlock surface again */
397 SDL_UnlockSurface(s);
405 /* Updates the console buffer */
406 void CON_UpdateConsole(void) {
410 grs_canvas *canv_save;
413 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
417 Screenlines = console->ConsoleSurface->cv_h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
419 canv_save = grd_curcanv;
420 gr_set_current_canvas(console->ConsoleSurface);
423 SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha));
429 if(grd_curscreen->flags & SDL_OPENGLBLIT)
430 SDL_SetAlpha(console->ConsoleSurface, 0, SDL_ALPHA_OPAQUE);
433 /* draw the background image if there is one */
434 if(console->BackgroundImage)
435 gr_bitmap(0, 0, console->BackgroundImage);
437 /* Draw the text from the back buffers, calculate in the scrollback from the user
438 * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey
439 * for the font, and then clear it when we're done.
442 if((grd_curscreen->flags & SDL_OPENGLBLIT) && (grd_curscreen->format->BytesPerPixel > 2)) {
443 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
444 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
448 //now draw text from last but second line to top
449 for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) {
450 if(console->ConsoleScrollBack != 0 && loop == 0)
451 for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++)
453 orig_color = FG_COLOR;
454 gr_string(CON_CHAR_BORDER + (loop2*5*console->ConsoleSurface->cv_font->ft_w), (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), CON_SCROLL_INDICATOR);
455 FG_COLOR = orig_color;
459 orig_color = FG_COLOR;
460 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), console->ConsoleLines[console->ConsoleScrollBack + loop]);
461 FG_COLOR = orig_color;
465 gr_set_current_canvas(canv_save);
468 if(grd_curscreen->flags & SDL_OPENGLBLIT)
469 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
473 void CON_UpdateOffset(void) {
474 switch(console->Visible) {
476 console->RaiseOffset -= CON_OPENCLOSE_SPEED;
477 if(console->RaiseOffset <= 0) {
478 console->RaiseOffset = 0;
479 console->Visible = CON_CLOSED;
483 console->RaiseOffset += CON_OPENCLOSE_SPEED;
484 if(console->RaiseOffset >= console->ConsoleSurface->cv_h) {
485 console->RaiseOffset = console->ConsoleSurface->cv_h;
486 console->Visible = CON_OPEN;
495 /* Draws the console buffer to the screen if the console is "visible" */
496 void CON_DrawConsole(void) {
497 grs_canvas *canv_save;
500 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
501 if(console->Visible == CON_CLOSED)
504 /* Update the scrolling offset */
507 /* Update the command line since it has a blinking cursor */
511 /* before drawing, make sure the alpha channel of the console surface is set
512 * properly. (sigh) I wish we didn't have to do this every frame... */
513 if(grd_curscreen->flags & SDL_OPENGLBLIT)
514 CON_AlphaGL(console->ConsoleSurface, console->ConsoleAlpha);
517 canv_save = grd_curcanv;
518 gr_set_current_canvas(&grd_curscreen->sc_canvas);
520 clip = gr_create_sub_bitmap(&console->ConsoleSurface->cv_bitmap, 0, console->ConsoleSurface->cv_h - console->RaiseOffset, console->ConsoleSurface->cv_w, console->RaiseOffset);
522 gr_bitmap(0, 0, clip);
523 gr_free_sub_bitmap(clip);
526 if(grd_curscreen->flags & SDL_OPENGLBLIT)
527 SDL_UpdateRects(grd_curscreen, 1, &DestRect);
530 gr_set_current_canvas(canv_save);
534 /* Initializes the console */
539 console->Visible = CON_CLOSED;
540 console->RaiseOffset = 0;
541 console->ConsoleLines = NULL;
542 console->CommandLines = NULL;
543 console->TotalConsoleLines = 0;
544 console->ConsoleScrollBack = 0;
545 console->TotalCommands = 0;
546 console->BackgroundImage = NULL;
548 console->ConsoleAlpha = SDL_ALPHA_OPAQUE;
551 console->InsMode = 1;
552 console->CursorPos = 0;
553 console->CommandScrollBack = 0;
554 console->Prompt = d_strdup(CON_DEFAULT_PROMPT);
555 console->HideKey = CON_DEFAULT_HIDEKEY;
557 /* load the console surface */
558 console->ConsoleSurface = NULL;
560 /* Load the dirty rectangle for user input */
561 console->InputBackground = NULL;
563 console->VChars = CON_CHARS_PER_LINE - 1;
564 console->LineBuffer = CON_NUM_LINES;
566 console->ConsoleLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
567 console->CommandLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
568 for(loop = 0; loop <= console->LineBuffer - 1; loop++) {
569 console->ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
570 console->CommandLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
572 memset(console->Command, 0, CON_CHARS_PER_LINE);
573 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
574 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
575 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
579 /* Initialise the cvars */
580 cvar_registervariable (&con_threshold);
588 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
589 void CON_InitGFX(int w, int h)
595 if (console->ConsoleSurface) {
596 /* resize console surface */
597 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
598 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
600 /* load the console surface */
601 console->ConsoleSurface = gr_create_canvas(w, h);
604 /* Load the consoles font */
605 CON_Font(SMALL_FONT, gr_find_closest_color(29,29,47), -1);
607 /* make sure that the size of the console is valid */
608 if(w > grd_curscreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
609 w = grd_curscreen->sc_w;
610 if(h > grd_curscreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
611 h = grd_curscreen->sc_h;
613 /* Load the dirty rectangle for user input */
614 if (console->InputBackground)
615 gr_free_bitmap(console->InputBackground);
616 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
618 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
621 /* calculate the number of visible characters in the command line */
622 #if 0 // doesn't work because proportional font
623 console->VChars = (w - CON_CHAR_BORDER) / console->ConsoleSurface->cv_font->ft_w;
624 if(console->VChars >= CON_CHARS_PER_LINE)
625 console->VChars = CON_CHARS_PER_LINE - 1;
628 gr_init_bitmap_data(&bmp);
629 pcx_error = pcx_read_bitmap(CON_BG, &bmp, BM_LINEAR, pal);
630 Assert(pcx_error == PCX_ERROR_NONE);
631 gr_remap_bitmap_good(&bmp, pal, -1, -1);
632 CON_Background(&bmp);
633 gr_free_bitmap_data(&bmp);
637 /* Makes the console visible */
638 void CON_Show(void) {
639 console->Visible = CON_OPENING;
643 /* Hides the console (make it invisible) */
644 void CON_Hide(void) {
645 console->Visible = CON_CLOSING;
649 /* tells wether the console is visible or not */
650 int CON_isVisible(void) {
651 return((console->Visible == CON_OPEN) || (console->Visible == CON_OPENING));
654 /* Frees all the memory loaded by the console */
655 void CON_Free(void) {
658 for(i = 0; i <= console->LineBuffer - 1; i++) {
659 d_free(console->ConsoleLines[i]);
660 d_free(console->CommandLines[i]);
662 d_free(console->ConsoleLines);
663 d_free(console->CommandLines);
665 console->ConsoleLines = NULL;
666 console->CommandLines = NULL;
668 if (console->ConsoleSurface)
669 gr_free_canvas(console->ConsoleSurface);
670 console->ConsoleSurface = NULL;
672 if (console->BackgroundImage)
673 gr_free_bitmap(console->BackgroundImage);
674 console->BackgroundImage = NULL;
676 if (console->InputBackground)
677 gr_free_bitmap(console->InputBackground);
678 console->InputBackground = NULL;
680 d_free(console->Prompt);
686 /* Increments the console lines */
687 void CON_NewLineConsole(void) {
691 temp = console->ConsoleLines[console->LineBuffer - 1];
693 for(loop = console->LineBuffer - 1; loop > 0; loop--)
694 console->ConsoleLines[loop] = console->ConsoleLines[loop - 1];
696 console->ConsoleLines[0] = temp;
698 memset(console->ConsoleLines[0], 0, CON_CHARS_PER_LINE);
699 if(console->TotalConsoleLines < console->LineBuffer - 1)
700 console->TotalConsoleLines++;
702 //Now adjust the ConsoleScrollBack
703 //dont scroll if not at bottom
704 if(console->ConsoleScrollBack != 0)
705 console->ConsoleScrollBack++;
707 if(console->ConsoleScrollBack > console->LineBuffer-1)
708 console->ConsoleScrollBack = console->LineBuffer-1;
713 /* Increments the command lines */
714 void CON_NewLineCommand(void) {
718 temp = console->CommandLines[console->LineBuffer - 1];
721 for(loop = console->LineBuffer - 1; loop > 0; loop--)
722 console->CommandLines[loop] = console->CommandLines[loop - 1];
724 console->CommandLines[0] = temp;
726 memset(console->CommandLines[0], 0, CON_CHARS_PER_LINE);
727 if(console->TotalCommands < console->LineBuffer - 1)
728 console->TotalCommands++;
731 /* Draws the command line the user is typing in to the screen */
732 /* completely rewritten by C.Wacha */
733 void DrawCommandLine() {
737 grs_font* CurrentFont;
739 static unsigned int LastBlinkTime = 0; /* Last time the consoles cursor blinked */
740 static int LastCursorPos = 0; // Last Cursor Position
741 static int Blink = 0; /* Is the cursor currently blinking */
742 grs_canvas *canv_save;
745 commandbuffer = console->VChars - (int)strlen(console->Prompt) - 1; // -1 to make cursor visible
748 CurrentFont = console->ConsoleSurface->cv_font;
751 //Concatenate the left and right side to command
752 strcpy(console->Command, console->LCommand);
753 strncat(console->Command, console->RCommand, strlen(console->RCommand));
755 //calculate display offset from current cursor position
756 if(console->Offset < console->CursorPos - commandbuffer)
757 console->Offset = console->CursorPos - commandbuffer;
758 if(console->Offset > console->CursorPos)
759 console->Offset = console->CursorPos;
761 //first add prompt to visible part
762 strcpy(console->VCommand, console->Prompt);
764 //then add the visible part of the command
765 strncat(console->VCommand, &console->Command[console->Offset], strlen(&console->Command[console->Offset]));
767 //now display the result
770 //once again we're drawing text, so in OpenGL context we need to temporarily set up
771 //software-mode transparency.
772 if(grd_curscreen->flags & SDL_OPENGLBLIT) {
773 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
774 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
778 canv_save = grd_curcanv;
779 gr_set_current_canvas(console->ConsoleSurface);
781 //first of all restore InputBackground
782 gr_bitmap(0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->InputBackground);
785 orig_color = FG_COLOR;
786 gr_string(CON_CHAR_BORDER, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->VCommand);
787 FG_COLOR = orig_color;
789 //at last add the cursor
790 //check if the blink period is over
791 if(get_msecs() > LastBlinkTime) {
792 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
799 //check if cursor has moved - if yes display cursor anyway
800 if(console->CursorPos != LastCursorPos) {
801 LastCursorPos = console->CursorPos;
802 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
807 int prompt_width, cmd_width, h, w;
809 gr_get_string_size(console->Prompt, &prompt_width, &h, &w);
810 gr_get_string_size(console->LCommand + console->Offset, &cmd_width, &h, &w);
811 x = CON_CHAR_BORDER + prompt_width + cmd_width;
812 orig_color = FG_COLOR;
814 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_INS_CURSOR);
816 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_OVR_CURSOR);
817 FG_COLOR = orig_color;
820 gr_set_current_canvas(canv_save);
824 if(grd_curscreen->flags & SDL_OPENGLBLIT) {
825 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
831 static inline int con_get_width(void)
833 if (!console->ConsoleSurface)
836 return console->ConsoleSurface->cv_bitmap.bm_w - CON_CHAR_BORDER;
840 static inline int con_get_string_width(char *string)
842 grs_canvas *canv_save;
845 if (!console->ConsoleSurface)
848 canv_save = grd_curcanv;
849 gr_set_current_canvas(console->ConsoleSurface);
850 gr_get_string_size(string, &w, &h, &aw);
851 gr_set_current_canvas(canv_save);
858 # define vsnprintf _vsnprintf
861 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
862 void CON_Out(const char *str, ...) {
864 //keep some space free for stuff like CON_Out("blablabla %s", console->Command);
865 char temp[CON_CHARS_PER_LINE + 128];
868 va_start(marker, str);
869 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
874 //temp now contains the complete string we want to output
875 // the only problem is that temp is maybe longer than the console
876 // width so we have to cut it into several pieces
878 if(console->ConsoleLines) {
884 CON_NewLineConsole();
885 strcat(console->ConsoleLines[0], ptemp);
887 } else if (p - ptemp > console->VChars - strlen(console->ConsoleLines[0]) ||
888 con_get_string_width(ptemp) > con_get_width()) {
889 CON_NewLineConsole();
890 strncat(console->ConsoleLines[0], ptemp, console->VChars - strlen(console->ConsoleLines[0]));
891 console->ConsoleLines[0][console->VChars] = '\0';
897 strncat(console->ConsoleLines[0], ptemp, console->VChars - strlen(console->ConsoleLines[0]));
898 console->ConsoleLines[0][console->VChars] = '\0';
906 /* Sets the alpha level of the console, 0 turns off alpha blending */
907 void CON_Alpha(unsigned char alpha) {
908 /* store alpha as state! */
909 console->ConsoleAlpha = alpha;
911 if((grd_curscreen->flags & SDL_OPENGLBLIT) == 0) {
913 SDL_SetAlpha(console->ConsoleSurface, 0, alpha);
915 SDL_SetAlpha(console->ConsoleSurface, SDL_SRCALPHA, alpha);
918 // CON_UpdateConsole();
923 /* Adds background image to the console, scaled to size of console*/
924 int CON_Background(grs_bitmap *image)
926 /* Free the background from the console */
928 if (console->BackgroundImage)
929 gr_free_bitmap(console->BackgroundImage);
930 console->BackgroundImage = NULL;
932 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
937 /* Load a new background */
938 if (console->BackgroundImage)
939 gr_free_bitmap(console->BackgroundImage);
940 console->BackgroundImage = gr_create_bitmap(console->ConsoleSurface->cv_w, console->ConsoleSurface->cv_h);
941 gr_bitmap_scale_to(image, console->BackgroundImage);
944 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
946 gr_bm_bitblt(console->BackgroundImage->bm_w, console->InputBackground->bm_h, 0, 0, 0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->BackgroundImage, console->InputBackground);
951 /* Sets font info for the console */
952 void CON_Font(grs_font *font, int fg, int bg)
954 grs_canvas *canv_save;
956 canv_save = grd_curcanv;
957 gr_set_current_canvas(console->ConsoleSurface);
958 gr_set_curfont(font);
959 gr_set_fontcolor(fg, bg);
960 gr_set_current_canvas(canv_save);
964 /* resizes the console, has to reset alot of stuff
965 * returns 1 on error */
966 void CON_Resize(int w, int h)
968 /* make sure that the size of the console is valid */
969 if(w > grd_curscreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
970 w = grd_curscreen->sc_w;
971 if(h > grd_curscreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
972 h = grd_curscreen->sc_h;
974 /* resize console surface */
975 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
976 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
978 /* Load the dirty rectangle for user input */
979 gr_free_bitmap(console->InputBackground);
980 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
982 /* Now reset some stuff dependent on the previous size */
983 console->ConsoleScrollBack = 0;
985 /* Reload the background image (for the input text area) in the console */
986 if(console->BackgroundImage) {
988 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
990 gr_bm_bitblt(console->BackgroundImage->bm_w, console->InputBackground->bm_h, 0, 0, 0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->BackgroundImage, console->InputBackground);
994 /* restore the alpha level */
995 CON_Alpha(console->ConsoleAlpha);
999 /* Sets the Prompt for console */
1000 void CON_SetPrompt(char* newprompt) {
1001 //check length so we can still see at least 1 char :-)
1002 if(strlen(newprompt) < console->VChars) {
1003 d_free(console->Prompt);
1004 console->Prompt = d_strdup(newprompt);
1006 CON_Out("prompt too long. (max. %i chars)\n", console->VChars - 1);
1009 /* Sets the key that deactivates (hides) the console. */
1010 void CON_SetHideKey(int key) {
1011 console->HideKey = key;
1014 /* Executes the command entered */
1015 void CON_Execute(char* command) {
1016 cmd_append(command);
1019 void CON_TabCompletion(void) {
1023 command = cmd_complete(console->LCommand);
1026 return; //no tab completion took place so return silently
1028 j = (int)strlen(command);
1029 if(j > CON_CHARS_PER_LINE - 2)
1030 j = CON_CHARS_PER_LINE-1;
1032 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1033 console->CursorPos = 0;
1035 for(i = 0; i < j; i++) {
1036 console->CursorPos++;
1037 console->LCommand[i] = command[i];
1039 //add a trailing space
1040 console->CursorPos++;
1041 console->LCommand[j] = ' ';
1042 console->LCommand[j+1] = '\0';
1045 void Cursor_Left(void) {
1046 char temp[CON_CHARS_PER_LINE];
1048 if(console->CursorPos > 0) {
1049 console->CursorPos--;
1050 strcpy(temp, console->RCommand);
1051 strcpy(console->RCommand, &console->LCommand[strlen(console->LCommand)-1]);
1052 strcat(console->RCommand, temp);
1053 console->LCommand[strlen(console->LCommand)-1] = '\0';
1054 //CON_Out("L:%s, R:%s\n", console->LCommand, console->RCommand);
1058 void Cursor_Right(void) {
1059 char temp[CON_CHARS_PER_LINE];
1061 if(console->CursorPos < strlen(console->Command)) {
1062 console->CursorPos++;
1063 strncat(console->LCommand, console->RCommand, 1);
1064 strcpy(temp, console->RCommand);
1065 strcpy(console->RCommand, &temp[1]);
1066 //CON_Out("L:%s, R:%s\n", console->LCommand, console->RCommand);
1070 void Cursor_Home(void) {
1071 char temp[CON_CHARS_PER_LINE];
1073 console->CursorPos = 0;
1074 strcpy(temp, console->RCommand);
1075 strcpy(console->RCommand, console->LCommand);
1076 strncat(console->RCommand, temp, strlen(temp));
1077 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1080 void Cursor_End(void) {
1081 console->CursorPos = (int)strlen(console->Command);
1082 strncat(console->LCommand, console->RCommand, strlen(console->RCommand));
1083 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1086 void Cursor_Del(void) {
1087 char temp[CON_CHARS_PER_LINE];
1089 if(strlen(console->RCommand) > 0) {
1090 strcpy(temp, console->RCommand);
1091 strcpy(console->RCommand, &temp[1]);
1095 void Cursor_BSpace(void) {
1096 if(console->CursorPos > 0) {
1097 console->CursorPos--;
1099 if(console->Offset < 0)
1100 console->Offset = 0;
1101 console->LCommand[strlen(console->LCommand)-1] = '\0';
1105 void Cursor_Add(int event)
1107 if(strlen(console->Command) < CON_CHARS_PER_LINE - 1)
1109 console->CursorPos++;
1110 console->LCommand[strlen(console->LCommand)] = key_to_ascii(event);
1111 console->LCommand[strlen(console->LCommand)] = '\0';
1115 void Clear_Command(void) {
1116 console->CursorPos = 0;
1117 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
1118 memset(console->Command, 0, CON_CHARS_PER_LINE);
1119 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1120 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1123 void Clear_History(void) {
1126 for(loop = 0; loop <= console->LineBuffer - 1; loop++)
1127 memset(console->ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
1130 void Command_Up(void) {
1131 if(console->CommandScrollBack < console->TotalCommands - 1) {
1132 /* move back a line in the command strings and copy the command to the current input string */
1133 console->CommandScrollBack++;
1134 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1135 console->Offset = 0;
1136 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1137 console->CursorPos = (int)strlen(console->CommandLines[console->CommandScrollBack]);
1138 CON_UpdateConsole();
1142 void Command_Down(void) {
1143 if(console->CommandScrollBack > -1) {
1144 /* move forward a line in the command strings and copy the command to the current input string */
1145 console->CommandScrollBack--;
1146 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1147 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1148 console->Offset = 0;
1149 if(console->CommandScrollBack > -1)
1150 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1151 console->CursorPos = (int)strlen(console->LCommand);
1152 CON_UpdateConsole();
1156 /* convert to ansi rgb colors 17-231 */
1157 #define PAL2ANSI(x) ((36*gr_palette[(x)*3]/11) + (6*gr_palette[(x)*3+1]/11) + (gr_palette[(x)*3+2]/11) + 16)
1159 /* Print a message to the console */
1160 void con_printf(int priority, char *fmt, ...)
1165 if (priority <= (con_threshold.intval))
1167 va_start (arglist, fmt);
1168 vsprintf (buffer, fmt, arglist);
1171 if (con_initialized)
1174 if (!text_console_enabled)
1177 if (isatty(fileno(stdout))) {
1179 unsigned char color, spacing, underline;
1189 printf("\x1B[38;5;%dm", PAL2ANSI(color));
1196 //printf("<SPACING %d>", color);
1203 //printf("<UNDERLINE>");
1214 /* Produce a sanitised version and send it to the standard output */
1233 printf("%s", buffer);