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.
42 int text_console_enabled = 1;
45 #define text_console_enabled (!isvga())
49 /* Console specific cvars */
50 /* How discriminating we are about which messages are displayed */
51 cvar_t con_threshold = {"con_threshold", "0",};
53 /* Private console stuff */
54 #define CON_NUM_LINES 40
56 #define FG_COLOR grd_curcanv->cv_font_fg_color
57 #define get_msecs() approx_fsec_to_msec(timer_get_approx_seconds())
59 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
60 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
61 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
63 //! Cut the buffer line if it becomes longer than this
64 #define CON_CHARS_PER_LINE 128
65 //! Cursor blink frequency in ms
66 #define CON_BLINK_RATE 500
67 //! Border in pixels from the most left to the first letter
68 #define CON_CHAR_BORDER 4
69 //! Spacing in pixels between lines
70 #define CON_LINE_SPACE 1
71 //! Default prompt used at the commandline
72 #define CON_DEFAULT_PROMPT "]"
73 //! Scroll this many lines at a time (when pressing PGUP or PGDOWN)
74 #define CON_LINE_SCROLL 2
75 //! Indicator showing that you scrolled up the history
76 #define CON_SCROLL_INDICATOR "^"
77 //! Cursor shown if we are in insert mode
78 #define CON_INS_CURSOR "_"
79 //! Cursor shown if we are in overwrite mode
80 #define CON_OVR_CURSOR "|"
81 //! Defines the default hide key (Hide() the console if pressed)
82 #define CON_DEFAULT_HIDEKEY KEY_ESC
83 //! Defines the opening/closing speed
84 #define CON_OPENCLOSE_SPEED 50
87 /*! This is a struct for the console's data */
88 typedef struct console_information_td {
89 int Visible; //! enum that tells which visible state we are in CON_HIDE, CON_SHOW, CON_RAISE, CON_LOWER
90 int RaiseOffset; //! Offset used when scrolling in the console
91 int HideKey; //! the key that can hide the console
92 char **ConsoleLines; //! List of all the past lines
93 char **CommandLines; //! List of all the past commands
94 int TotalConsoleLines; //! Total number of lines in the console
95 int ConsoleScrollBack; //! How much the user scrolled back in the console
96 int TotalCommands; //! Number of commands in the Back Commands
97 int LineBuffer; //! The number of visible lines in the console (autocalculated)
98 int VChars; //! The number of visible characters in one console line (autocalculated)
99 char* Prompt; //! Prompt displayed in command line
100 char Command[CON_CHARS_PER_LINE]; //! current command in command line = lcommand + rcommand
101 char RCommand[CON_CHARS_PER_LINE]; //! left hand side of cursor
102 char LCommand[CON_CHARS_PER_LINE]; //! right hand side of cursor
103 char VCommand[CON_CHARS_PER_LINE]; //! current visible command line
104 int CursorPos; //! Current cursor position in CurrentCommand
105 int Offset; //! CommandOffset (first visible char of command) - if command is too long to fit into console
106 int InsMode; //! Insert or Overwrite characters?
107 grs_canvas *ConsoleSurface; //! Canvas of the console
108 grs_bitmap *BackgroundImage; //! Background image for the console
109 grs_bitmap *InputBackground; //! Dirty rectangle to draw over behind the users background
111 unsigned char ConsoleAlpha; //! The consoles alpha level
113 int CommandScrollBack; //! How much the users scrolled back in the command lines
116 static ConsoleInformation Console;
117 #define console (&Console)
119 /* console is ready to be written to */
120 static int con_initialized;
124 void CON_UpdateOffset(void);
125 /*! Frees all the memory loaded by the console */
128 /*! Sets the alpha channel of an SDL_Surface to the specified value (0 - transparend,
129 255 - opaque). Use this function also for OpenGL. */
130 void CON_Alpha(unsigned char alpha);
131 /*! Internal: Sets the alpha channel of an SDL_Surface to the specified value.
132 Preconditions: the surface in question is RGBA. 0 <= a <= 255, where 0 is transparent and 255 opaque */
133 void CON_AlphaGL(SDL_Surface *s, int alpha);
134 /*! Sets a background image for the console */
136 int CON_Background(grs_bitmap *image);
137 /*! Sets font info for the console */
138 void CON_Font(grs_font *font, int fg, int bg);
139 /*! Modify the prompt of the console */
140 void CON_SetPrompt(char* newprompt);
141 /*! Set the key, that invokes a CON_Hide() after press. default is ESCAPE and you can always hide using
142 ESCAPE and the HideKey. compared against event->key.keysym.sym !! */
143 void CON_SetHideKey(int key);
144 /*! Internal: executes the command typed in at the console (called if you press ENTER)*/
145 void CON_Execute(char* command);
146 /*! Internal: Gets called when TAB was pressed */
147 void CON_TabCompletion(void);
148 /*! Internal: makes newline (same as printf("\n") or CON_Out("\n") ) */
149 void CON_NewLineConsole(void);
150 /*! Internal: shift command history (the one you can switch with the up/down keys) */
151 void CON_NewLineCommand(void);
152 /*! Internal: updates console after resize etc. */
153 void CON_UpdateConsole(void);
156 /*! Internal: draws the commandline the user is typing in to the screen. called by update? */
157 void DrawCommandLine();
159 /*! Internal: Gets called if you press the LEFT key (move cursor left) */
160 void Cursor_Left(void);
161 /*! Internal: Gets called if you press the RIGHT key (move cursor right) */
162 void Cursor_Right(void);
163 /*! Internal: Gets called if you press the HOME key (move cursor to the beginning
165 void Cursor_Home(void);
166 /*! Internal: Gets called if you press the END key (move cursor to the end of the line*/
167 void Cursor_End(void);
168 /*! Internal: Called if you press DELETE (deletes character under the cursor) */
169 void Cursor_Del(void);
170 /*! Internal: Called if you press BACKSPACE (deletes character left of cursor) */
171 void Cursor_BSpace(void);
172 /*! Internal: Called if you type in a character (add the char to the command) */
173 void Cursor_Add(int event);
175 /*! Internal: Called if you press Ctrl-C (deletes the commandline) */
176 void Clear_Command(void);
177 /*! Internal: Called if you press Ctrl-L (deletes the History) */
178 void Clear_History(void);
180 /*! Internal: Called if you press UP key (switches through recent typed in commands */
181 void Command_Up(void);
182 /*! Internal: Called if you press DOWN key (switches through recent typed in commands */
183 void Command_Down(void);
186 /* Takes keys from the keyboard and inputs them to the console
187 If the event was not handled (i.e. WM events or unknown ctrl-shift
188 sequences) the function returns the event for further processing. */
189 int CON_Events(int event)
194 if(event & KEY_CTRLED)
197 switch(event & ~KEY_CTRLED)
216 else if(event & KEY_ALTED)
218 //the console does not handle ALT combinations!
223 //first of all, check if the console hide key was pressed
224 if(event == console->HideKey)
229 switch (event & 0xff)
235 if(event & KEY_SHIFTED)
237 console->ConsoleScrollBack = console->LineBuffer-1;
244 if(event & KEY_SHIFTED)
246 console->ConsoleScrollBack = 0;
253 console->ConsoleScrollBack += CON_LINE_SCROLL;
254 if(console->ConsoleScrollBack > console->LineBuffer-1)
255 console->ConsoleScrollBack = console->LineBuffer-1;
260 console->ConsoleScrollBack -= CON_LINE_SCROLL;
261 if(console->ConsoleScrollBack < 0)
262 console->ConsoleScrollBack = 0;
284 console->InsMode = 1-console->InsMode;
290 if(strlen(console->Command) > 0) {
291 CON_NewLineCommand();
293 // copy the input into the past commands strings
294 strcpy(console->CommandLines[0], console->Command);
296 // display the command including the prompt
297 CON_Out("%s%s\n", console->Prompt, console->Command);
300 CON_Execute(console->Command);
303 console->CommandScrollBack = -1;
311 if (key_to_ascii(event) == 255)
325 /* CON_AlphaGL() -- sets the alpha channel of an SDL_Surface to the
326 * specified value. Preconditions: the surface in question is RGBA.
327 * 0 <= a <= 255, where 0 is transparent and 255 is opaque. */
328 void CON_AlphaGL(SDL_Surface *s, int alpha) {
333 SDL_PixelFormat *format;
334 static char errorPrinted = 0;
337 /* debugging assertions -- these slow you down, but hey, crashing sucks */
339 PRINT_ERROR("NULL Surface passed to CON_AlphaGL\n");
343 /* clamp alpha value to 0...255 */
344 if(alpha < SDL_ALPHA_TRANSPARENT)
345 val = SDL_ALPHA_TRANSPARENT;
346 else if(alpha > SDL_ALPHA_OPAQUE)
347 val = SDL_ALPHA_OPAQUE;
351 /* loop over alpha channels of each pixel, setting them appropriately. */
355 switch (format->BytesPerPixel) {
357 /* 16-bit surfaces don't seem to support alpha channels. */
360 PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL.\n");
364 /* we can do this very quickly in 32-bit mode. 24-bit is more
365 * difficult. And since 24-bit mode is reall the same as 32-bit,
366 * so it usually ends up taking this route too. Win! Unroll loop
367 * and use pointer arithmetic for extra speed. */
368 int numpixels = h * (w << 2);
369 Uint8 *pix = (Uint8 *) (s->pixels);
370 Uint8 *last = pix + numpixels;
372 if((numpixels & 0x7) == 0)
373 for(pixel = pix + 3; pixel < last; pixel += 32)
374 *pixel = *(pixel + 4) = *(pixel + 8) = *(pixel + 12) = *(pixel + 16) = *(pixel + 20) = *(pixel + 24) = *(pixel + 28) = val;
376 for(pixel = pix + 3; pixel < last; pixel += 4)
381 /* we have no choice but to do this slowly. <sigh> */
382 for(y = 0; y < h; ++y)
383 for(x = 0; x < w; ++x) {
385 /* Lock the surface for direct access to the pixels */
386 if(SDL_MUSTLOCK(s) && SDL_LockSurface(s) < 0) {
387 PRINT_ERROR("Can't lock surface: ");
388 fprintf(stderr, "%s\n", SDL_GetError());
391 pixel = DT_GetPixel(s, x, y);
394 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
395 pixel = SDL_MapRGBA(format, r, g, b, val);
396 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
397 DT_PutPixel(s, x, y, pixel);
399 /* unlock surface again */
401 SDL_UnlockSurface(s);
409 /* Updates the console buffer */
410 void CON_UpdateConsole(void) {
414 grs_canvas *canv_save;
417 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
421 Screenlines = console->ConsoleSurface->cv_h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
423 canv_save = grd_curcanv;
424 gr_set_current_canvas(console->ConsoleSurface);
427 SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha));
433 if(grd_curscreen->flags & SDL_OPENGLBLIT)
434 SDL_SetAlpha(console->ConsoleSurface, 0, SDL_ALPHA_OPAQUE);
437 /* draw the background image if there is one */
438 if(console->BackgroundImage)
439 gr_bitmap(0, 0, console->BackgroundImage);
441 /* Draw the text from the back buffers, calculate in the scrollback from the user
442 * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey
443 * for the font, and then clear it when we're done.
446 if((grd_curscreen->flags & SDL_OPENGLBLIT) && (grd_curscreen->format->BytesPerPixel > 2)) {
447 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
448 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
452 //now draw text from last but second line to top
453 for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) {
454 if(console->ConsoleScrollBack != 0 && loop == 0)
455 for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++)
457 orig_color = FG_COLOR;
458 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);
459 FG_COLOR = orig_color;
463 orig_color = FG_COLOR;
464 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), console->ConsoleLines[console->ConsoleScrollBack + loop]);
465 FG_COLOR = orig_color;
469 gr_set_current_canvas(canv_save);
472 if(grd_curscreen->flags & SDL_OPENGLBLIT)
473 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
477 void CON_UpdateOffset(void) {
478 switch(console->Visible) {
480 console->RaiseOffset -= CON_OPENCLOSE_SPEED;
481 if(console->RaiseOffset <= 0) {
482 console->RaiseOffset = 0;
483 console->Visible = CON_CLOSED;
487 console->RaiseOffset += CON_OPENCLOSE_SPEED;
488 if(console->RaiseOffset >= console->ConsoleSurface->cv_h) {
489 console->RaiseOffset = console->ConsoleSurface->cv_h;
490 console->Visible = CON_OPEN;
499 /* Draws the console buffer to the screen if the console is "visible" */
500 void CON_DrawConsole(void) {
501 grs_canvas *canv_save;
504 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
505 if(console->Visible == CON_CLOSED)
508 /* Update the scrolling offset */
511 /* Update the command line since it has a blinking cursor */
515 /* before drawing, make sure the alpha channel of the console surface is set
516 * properly. (sigh) I wish we didn't have to do this every frame... */
517 if(grd_curscreen->flags & SDL_OPENGLBLIT)
518 CON_AlphaGL(console->ConsoleSurface, console->ConsoleAlpha);
521 canv_save = grd_curcanv;
522 gr_set_current_canvas(&grd_curscreen->sc_canvas);
524 clip = gr_create_sub_bitmap(&console->ConsoleSurface->cv_bitmap, 0, console->ConsoleSurface->cv_h - console->RaiseOffset, console->ConsoleSurface->cv_w, console->RaiseOffset);
526 gr_bitmap(0, 0, clip);
527 gr_free_sub_bitmap(clip);
530 if(grd_curscreen->flags & SDL_OPENGLBLIT)
531 SDL_UpdateRects(grd_curscreen, 1, &DestRect);
534 gr_set_current_canvas(canv_save);
538 /* Initializes the console */
543 console->Visible = CON_CLOSED;
544 console->RaiseOffset = 0;
545 console->ConsoleLines = NULL;
546 console->CommandLines = NULL;
547 console->TotalConsoleLines = 0;
548 console->ConsoleScrollBack = 0;
549 console->TotalCommands = 0;
550 console->BackgroundImage = NULL;
552 console->ConsoleAlpha = SDL_ALPHA_OPAQUE;
555 console->InsMode = 1;
556 console->CursorPos = 0;
557 console->CommandScrollBack = 0;
558 console->Prompt = d_strdup(CON_DEFAULT_PROMPT);
559 console->HideKey = CON_DEFAULT_HIDEKEY;
561 /* load the console surface */
562 console->ConsoleSurface = NULL;
564 /* Load the dirty rectangle for user input */
565 console->InputBackground = NULL;
567 console->VChars = CON_CHARS_PER_LINE - 1;
568 console->LineBuffer = CON_NUM_LINES;
570 console->ConsoleLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
571 console->CommandLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
572 for(loop = 0; loop <= console->LineBuffer - 1; loop++) {
573 console->ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
574 console->CommandLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
576 memset(console->Command, 0, CON_CHARS_PER_LINE);
577 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
578 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
579 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
582 console_control_init();
584 /* Initialise the cvars */
585 cvar_registervariable (&con_threshold);
593 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
594 void CON_InitGFX(int w, int h)
600 if (console->ConsoleSurface) {
601 /* resize console surface */
602 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
603 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
605 /* load the console surface */
606 console->ConsoleSurface = gr_create_canvas(w, h);
609 /* Load the consoles font */
610 CON_Font(SMALL_FONT, gr_find_closest_color(29,29,47), -1);
612 /* make sure that the size of the console is valid */
613 if(w > grd_curscreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
614 w = grd_curscreen->sc_w;
615 if(h > grd_curscreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
616 h = grd_curscreen->sc_h;
618 /* Load the dirty rectangle for user input */
619 if (console->InputBackground)
620 gr_free_bitmap(console->InputBackground);
621 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
623 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
626 /* calculate the number of visible characters in the command line */
627 console->VChars = (w - CON_CHAR_BORDER) / console->ConsoleSurface->cv_font->ft_w;
628 if(console->VChars >= CON_CHARS_PER_LINE)
629 console->VChars = CON_CHARS_PER_LINE - 1;
631 gr_init_bitmap_data(&bmp);
632 pcx_error = pcx_read_bitmap(CON_BG, &bmp, BM_LINEAR, pal);
633 Assert(pcx_error == PCX_ERROR_NONE);
634 gr_remap_bitmap_good(&bmp, pal, -1, -1);
635 CON_Background(&bmp);
636 gr_free_bitmap_data(&bmp);
640 /* Makes the console visible */
641 void CON_Show(void) {
642 console->Visible = CON_OPENING;
646 /* Hides the console (make it invisible) */
647 void CON_Hide(void) {
648 console->Visible = CON_CLOSING;
652 /* tells wether the console is visible or not */
653 int CON_isVisible(void) {
654 return((console->Visible == CON_OPEN) || (console->Visible == CON_OPENING));
657 /* Frees all the memory loaded by the console */
658 void CON_Free(void) {
661 for(i = 0; i <= console->LineBuffer - 1; i++) {
662 d_free(console->ConsoleLines[i]);
663 d_free(console->CommandLines[i]);
665 d_free(console->ConsoleLines);
666 d_free(console->CommandLines);
668 console->ConsoleLines = NULL;
669 console->CommandLines = NULL;
671 if (console->ConsoleSurface)
672 gr_free_canvas(console->ConsoleSurface);
673 console->ConsoleSurface = NULL;
675 if (console->BackgroundImage)
676 gr_free_bitmap(console->BackgroundImage);
677 console->BackgroundImage = NULL;
679 if (console->InputBackground)
680 gr_free_bitmap(console->InputBackground);
681 console->InputBackground = NULL;
683 d_free(console->Prompt);
689 /* Increments the console lines */
690 void CON_NewLineConsole(void) {
694 temp = console->ConsoleLines[console->LineBuffer - 1];
696 for(loop = console->LineBuffer - 1; loop > 0; loop--)
697 console->ConsoleLines[loop] = console->ConsoleLines[loop - 1];
699 console->ConsoleLines[0] = temp;
701 memset(console->ConsoleLines[0], 0, CON_CHARS_PER_LINE);
702 if(console->TotalConsoleLines < console->LineBuffer - 1)
703 console->TotalConsoleLines++;
705 //Now adjust the ConsoleScrollBack
706 //dont scroll if not at bottom
707 if(console->ConsoleScrollBack != 0)
708 console->ConsoleScrollBack++;
710 if(console->ConsoleScrollBack > console->LineBuffer-1)
711 console->ConsoleScrollBack = console->LineBuffer-1;
716 /* Increments the command lines */
717 void CON_NewLineCommand(void) {
721 temp = console->CommandLines[console->LineBuffer - 1];
724 for(loop = console->LineBuffer - 1; loop > 0; loop--)
725 console->CommandLines[loop] = console->CommandLines[loop - 1];
727 console->CommandLines[0] = temp;
729 memset(console->CommandLines[0], 0, CON_CHARS_PER_LINE);
730 if(console->TotalCommands < console->LineBuffer - 1)
731 console->TotalCommands++;
734 /* Draws the command line the user is typing in to the screen */
735 /* completely rewritten by C.Wacha */
736 void DrawCommandLine() {
740 grs_font* CurrentFont;
742 static unsigned int LastBlinkTime = 0; /* Last time the consoles cursor blinked */
743 static int LastCursorPos = 0; // Last Cursor Position
744 static int Blink = 0; /* Is the cursor currently blinking */
745 grs_canvas *canv_save;
748 commandbuffer = console->VChars - strlen(console->Prompt)-1; // -1 to make cursor visible
751 CurrentFont = console->ConsoleSurface->cv_font;
754 //Concatenate the left and right side to command
755 strcpy(console->Command, console->LCommand);
756 strncat(console->Command, console->RCommand, strlen(console->RCommand));
758 //calculate display offset from current cursor position
759 if(console->Offset < console->CursorPos - commandbuffer)
760 console->Offset = console->CursorPos - commandbuffer;
761 if(console->Offset > console->CursorPos)
762 console->Offset = console->CursorPos;
764 //first add prompt to visible part
765 strcpy(console->VCommand, console->Prompt);
767 //then add the visible part of the command
768 strncat(console->VCommand, &console->Command[console->Offset], strlen(&console->Command[console->Offset]));
770 //now display the result
773 //once again we're drawing text, so in OpenGL context we need to temporarily set up
774 //software-mode transparency.
775 if(grd_curscreen->flags & SDL_OPENGLBLIT) {
776 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
777 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
781 canv_save = grd_curcanv;
782 gr_set_current_canvas(console->ConsoleSurface);
784 //first of all restore InputBackground
785 gr_bitmap(0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->InputBackground);
788 orig_color = FG_COLOR;
789 gr_string(CON_CHAR_BORDER, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->VCommand);
790 FG_COLOR = orig_color;
792 //at last add the cursor
793 //check if the blink period is over
794 if(get_msecs() > LastBlinkTime) {
795 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
802 //check if cursor has moved - if yes display cursor anyway
803 if(console->CursorPos != LastCursorPos) {
804 LastCursorPos = console->CursorPos;
805 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
810 int prompt_width, cmd_width, h, w;
812 gr_get_string_size(console->Prompt, &prompt_width, &h, &w);
813 gr_get_string_size(console->LCommand + console->Offset, &cmd_width, &h, &w);
814 x = CON_CHAR_BORDER + prompt_width + cmd_width;
815 orig_color = FG_COLOR;
817 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_INS_CURSOR);
819 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_OVR_CURSOR);
820 FG_COLOR = orig_color;
823 gr_set_current_canvas(canv_save);
827 if(grd_curscreen->flags & SDL_OPENGLBLIT) {
828 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
834 # define vsnprintf _vsnprintf
837 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
838 void CON_Out(const char *str, ...) {
840 //keep some space free for stuff like CON_Out("blablabla %s", console->Command);
841 char temp[CON_CHARS_PER_LINE + 128];
844 va_start(marker, str);
845 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
850 //temp now contains the complete string we want to output
851 // the only problem is that temp is maybe longer than the console
852 // width so we have to cut it into several pieces
854 if(console->ConsoleLines) {
860 CON_NewLineConsole();
861 strcat(console->ConsoleLines[0], ptemp);
864 if (p - ptemp > console->VChars - strlen(console->ConsoleLines[0])) {
865 CON_NewLineConsole();
866 strncat(console->ConsoleLines[0], ptemp, console->VChars - strlen(console->ConsoleLines[0]));
867 console->ConsoleLines[0][console->VChars] = '\0';
873 strncat(console->ConsoleLines[0], ptemp, console->VChars - strlen(console->ConsoleLines[0]));
874 console->ConsoleLines[0][console->VChars] = '\0';
882 /* Sets the alpha level of the console, 0 turns off alpha blending */
883 void CON_Alpha(unsigned char alpha) {
884 /* store alpha as state! */
885 console->ConsoleAlpha = alpha;
887 if((grd_curscreen->flags & SDL_OPENGLBLIT) == 0) {
889 SDL_SetAlpha(console->ConsoleSurface, 0, alpha);
891 SDL_SetAlpha(console->ConsoleSurface, SDL_SRCALPHA, alpha);
894 // CON_UpdateConsole();
899 /* Adds background image to the console, scaled to size of console*/
900 int CON_Background(grs_bitmap *image)
902 /* Free the background from the console */
904 if (console->BackgroundImage)
905 gr_free_bitmap(console->BackgroundImage);
906 console->BackgroundImage = NULL;
908 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
913 /* Load a new background */
914 if (console->BackgroundImage)
915 gr_free_bitmap(console->BackgroundImage);
916 console->BackgroundImage = gr_create_bitmap(console->ConsoleSurface->cv_w, console->ConsoleSurface->cv_h);
917 gr_bitmap_scale_to(image, console->BackgroundImage);
920 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
922 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);
927 /* Sets font info for the console */
928 void CON_Font(grs_font *font, int fg, int bg)
930 grs_canvas *canv_save;
932 canv_save = grd_curcanv;
933 gr_set_current_canvas(console->ConsoleSurface);
934 gr_set_curfont(font);
935 gr_set_fontcolor(fg, bg);
936 gr_set_current_canvas(canv_save);
940 /* resizes the console, has to reset alot of stuff
941 * returns 1 on error */
942 void CON_Resize(int w, int h)
944 /* make sure that the size of the console is valid */
945 if(w > grd_curscreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
946 w = grd_curscreen->sc_w;
947 if(h > grd_curscreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
948 h = grd_curscreen->sc_h;
950 /* resize console surface */
951 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
952 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
954 /* Load the dirty rectangle for user input */
955 gr_free_bitmap(console->InputBackground);
956 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
958 /* Now reset some stuff dependent on the previous size */
959 console->ConsoleScrollBack = 0;
961 /* Reload the background image (for the input text area) in the console */
962 if(console->BackgroundImage) {
964 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
966 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);
970 /* restore the alpha level */
971 CON_Alpha(console->ConsoleAlpha);
975 /* Sets the Prompt for console */
976 void CON_SetPrompt(char* newprompt) {
977 //check length so we can still see at least 1 char :-)
978 if(strlen(newprompt) < console->VChars) {
979 d_free(console->Prompt);
980 console->Prompt = d_strdup(newprompt);
982 CON_Out("prompt too long. (max. %i chars)\n", console->VChars - 1);
985 /* Sets the key that deactivates (hides) the console. */
986 void CON_SetHideKey(int key) {
987 console->HideKey = key;
990 /* Executes the command entered */
991 void CON_Execute(char* command) {
995 void CON_TabCompletion(void) {
999 command = cmd_complete(console->LCommand);
1002 return; //no tab completion took place so return silently
1004 j = strlen(command);
1005 if(j > CON_CHARS_PER_LINE - 2)
1006 j = CON_CHARS_PER_LINE-1;
1008 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1009 console->CursorPos = 0;
1011 for(i = 0; i < j; i++) {
1012 console->CursorPos++;
1013 console->LCommand[i] = command[i];
1015 //add a trailing space
1016 console->CursorPos++;
1017 console->LCommand[j] = ' ';
1018 console->LCommand[j+1] = '\0';
1021 void Cursor_Left(void) {
1022 char temp[CON_CHARS_PER_LINE];
1024 if(console->CursorPos > 0) {
1025 console->CursorPos--;
1026 strcpy(temp, console->RCommand);
1027 strcpy(console->RCommand, &console->LCommand[strlen(console->LCommand)-1]);
1028 strcat(console->RCommand, temp);
1029 console->LCommand[strlen(console->LCommand)-1] = '\0';
1030 //CON_Out("L:%s, R:%s\n", console->LCommand, console->RCommand);
1034 void Cursor_Right(void) {
1035 char temp[CON_CHARS_PER_LINE];
1037 if(console->CursorPos < strlen(console->Command)) {
1038 console->CursorPos++;
1039 strncat(console->LCommand, console->RCommand, 1);
1040 strcpy(temp, console->RCommand);
1041 strcpy(console->RCommand, &temp[1]);
1042 //CON_Out("L:%s, R:%s\n", console->LCommand, console->RCommand);
1046 void Cursor_Home(void) {
1047 char temp[CON_CHARS_PER_LINE];
1049 console->CursorPos = 0;
1050 strcpy(temp, console->RCommand);
1051 strcpy(console->RCommand, console->LCommand);
1052 strncat(console->RCommand, temp, strlen(temp));
1053 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1056 void Cursor_End(void) {
1057 console->CursorPos = strlen(console->Command);
1058 strncat(console->LCommand, console->RCommand, strlen(console->RCommand));
1059 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1062 void Cursor_Del(void) {
1063 char temp[CON_CHARS_PER_LINE];
1065 if(strlen(console->RCommand) > 0) {
1066 strcpy(temp, console->RCommand);
1067 strcpy(console->RCommand, &temp[1]);
1071 void Cursor_BSpace(void) {
1072 if(console->CursorPos > 0) {
1073 console->CursorPos--;
1075 if(console->Offset < 0)
1076 console->Offset = 0;
1077 console->LCommand[strlen(console->LCommand)-1] = '\0';
1081 void Cursor_Add(int event)
1083 if(strlen(console->Command) < CON_CHARS_PER_LINE - 1)
1085 console->CursorPos++;
1086 console->LCommand[strlen(console->LCommand)] = key_to_ascii(event);
1087 console->LCommand[strlen(console->LCommand)] = '\0';
1091 void Clear_Command(void) {
1092 console->CursorPos = 0;
1093 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
1094 memset(console->Command, 0, CON_CHARS_PER_LINE);
1095 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1096 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1099 void Clear_History(void) {
1102 for(loop = 0; loop <= console->LineBuffer - 1; loop++)
1103 memset(console->ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
1106 void Command_Up(void) {
1107 if(console->CommandScrollBack < console->TotalCommands - 1) {
1108 /* move back a line in the command strings and copy the command to the current input string */
1109 console->CommandScrollBack++;
1110 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1111 console->Offset = 0;
1112 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1113 console->CursorPos = strlen(console->CommandLines[console->CommandScrollBack]);
1114 CON_UpdateConsole();
1118 void Command_Down(void) {
1119 if(console->CommandScrollBack > -1) {
1120 /* move forward a line in the command strings and copy the command to the current input string */
1121 console->CommandScrollBack--;
1122 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1123 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1124 console->Offset = 0;
1125 if(console->CommandScrollBack > -1)
1126 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1127 console->CursorPos = strlen(console->LCommand);
1128 CON_UpdateConsole();
1132 /* convert to ansi rgb colors 17-231 */
1133 #define PAL2ANSI(x) ((36*gr_palette[(x)*3]/11) + (6*gr_palette[(x)*3+1]/11) + (gr_palette[(x)*3+2]/11) + 16)
1135 /* Print a message to the console */
1136 void con_printf(int priority, char *fmt, ...)
1141 if (priority <= (con_threshold.intval))
1143 va_start (arglist, fmt);
1144 vsprintf (buffer, fmt, arglist);
1147 if (con_initialized)
1150 if (!text_console_enabled)
1153 if (isatty(fileno(stdout))) {
1155 unsigned char color, spacing, underline;
1165 printf("\x1B[38;5;%dm", PAL2ANSI(color));
1172 //printf("<SPACING %d>", color);
1179 //printf("<UNDERLINE>");
1190 /* Produce a sanitised version and send it to the standard output */
1209 printf("%s", buffer);