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.
28 #define FG_COLOR grd_curcanv->cv_font_fg_color
29 #define get_msecs() approx_fsec_to_msec(timer_get_approx_seconds())
33 static ConsoleInformation Console;
34 #define console (&Console)
37 void CON_UpdateOffset(void);
38 /*! Frees all the memory loaded by the console */
41 /*! Sets the alpha channel of an SDL_Surface to the specified value (0 - transparend,
42 255 - opaque). Use this function also for OpenGL. */
43 void CON_Alpha(unsigned char alpha);
44 /*! Internal: Sets the alpha channel of an SDL_Surface to the specified value.
45 Preconditions: the surface in question is RGBA. 0 <= a <= 255, where 0 is transparent and 255 opaque */
46 void CON_AlphaGL(SDL_Surface *s, int alpha);
47 /*! Sets a background image for the console */
49 int CON_Background(grs_bitmap *image);
50 /*! Sets font info for the console */
51 void CON_Font(grs_font *font, int fg, int bg);
52 /*! Beams a console to another screen surface. Needed if you want to make a Video restart in your program. This
53 function first changes the OutputScreen Pointer then calls CON_Resize to adjust the new size. */
54 int CON_Transfer(grs_screen* new_outputscreen, int x, int y, int w, int h);
55 /*! Modify the prompt of the console */
56 void CON_SetPrompt(char* newprompt);
57 /*! Set the key, that invokes a CON_Hide() after press. default is ESCAPE and you can always hide using
58 ESCAPE and the HideKey. compared against event->key.keysym.sym !! */
59 void CON_SetHideKey(int key);
60 /*! Internal: executes the command typed in at the console (called if you press ENTER)*/
61 void CON_Execute(char* command);
62 /*! Internal: Gets called when TAB was pressed */
63 void CON_TabCompletion(void);
64 /*! Internal: makes newline (same as printf("\n") or CON_Out("\n") ) */
65 void CON_NewLineConsole(void);
66 /*! Internal: shift command history (the one you can switch with the up/down keys) */
67 void CON_NewLineCommand(void);
68 /*! Internal: updates console after resize etc. */
69 void CON_UpdateConsole(void);
72 /*! Internal: draws the commandline the user is typing in to the screen. called by update? */
73 void DrawCommandLine();
75 /*! Internal: Gets called if you press the LEFT key (move cursor left) */
76 void Cursor_Left(void);
77 /*! Internal: Gets called if you press the RIGHT key (move cursor right) */
78 void Cursor_Right(void);
79 /*! Internal: Gets called if you press the HOME key (move cursor to the beginning
81 void Cursor_Home(void);
82 /*! Internal: Gets called if you press the END key (move cursor to the end of the line*/
83 void Cursor_End(void);
84 /*! Internal: Called if you press DELETE (deletes character under the cursor) */
85 void Cursor_Del(void);
86 /*! Internal: Called if you press BACKSPACE (deletes character left of cursor) */
87 void Cursor_BSpace(void);
88 /*! Internal: Called if you type in a character (add the char to the command) */
89 void Cursor_Add(int event);
91 /*! Internal: Called if you press Ctrl-C (deletes the commandline) */
92 void Clear_Command(void);
93 /*! Internal: Called if you press Ctrl-L (deletes the History) */
94 void Clear_History(void);
96 /*! Internal: Called if you press UP key (switches through recent typed in commands */
97 void Command_Up(void);
98 /*! Internal: Called if you press DOWN key (switches through recent typed in commands */
99 void Command_Down(void);
102 /* Takes keys from the keyboard and inputs them to the console
103 If the event was not handled (i.e. WM events or unknown ctrl-shift
104 sequences) the function returns the event for further processing. */
105 int CON_Events(int event)
110 if(event & KEY_CTRLED)
113 switch(event & ~KEY_CTRLED)
132 else if(event & KEY_ALTED)
134 //the console does not handle ALT combinations!
139 //first of all, check if the console hide key was pressed
140 if(event == console->HideKey)
145 switch (event & 0xff)
151 if(event & KEY_SHIFTED)
153 console->ConsoleScrollBack = console->LineBuffer-1;
160 if(event & KEY_SHIFTED)
162 console->ConsoleScrollBack = 0;
169 console->ConsoleScrollBack += CON_LINE_SCROLL;
170 if(console->ConsoleScrollBack > console->LineBuffer-1)
171 console->ConsoleScrollBack = console->LineBuffer-1;
176 console->ConsoleScrollBack -= CON_LINE_SCROLL;
177 if(console->ConsoleScrollBack < 0)
178 console->ConsoleScrollBack = 0;
200 console->InsMode = 1-console->InsMode;
206 if(strlen(console->Command) > 0) {
207 CON_NewLineCommand();
209 // copy the input into the past commands strings
210 strcpy(console->CommandLines[0], console->Command);
212 // display the command including the prompt
213 CON_Out("%s%s", console->Prompt, console->Command);
216 CON_Execute(console->Command);
219 console->CommandScrollBack = -1;
227 if (key_to_ascii(event) == 255)
241 /* CON_AlphaGL() -- sets the alpha channel of an SDL_Surface to the
242 * specified value. Preconditions: the surface in question is RGBA.
243 * 0 <= a <= 255, where 0 is transparent and 255 is opaque. */
244 void CON_AlphaGL(SDL_Surface *s, int alpha) {
249 SDL_PixelFormat *format;
250 static char errorPrinted = 0;
253 /* debugging assertions -- these slow you down, but hey, crashing sucks */
255 PRINT_ERROR("NULL Surface passed to CON_AlphaGL\n");
259 /* clamp alpha value to 0...255 */
260 if(alpha < SDL_ALPHA_TRANSPARENT)
261 val = SDL_ALPHA_TRANSPARENT;
262 else if(alpha > SDL_ALPHA_OPAQUE)
263 val = SDL_ALPHA_OPAQUE;
267 /* loop over alpha channels of each pixel, setting them appropriately. */
271 switch (format->BytesPerPixel) {
273 /* 16-bit surfaces don't seem to support alpha channels. */
276 PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL.\n");
280 /* we can do this very quickly in 32-bit mode. 24-bit is more
281 * difficult. And since 24-bit mode is reall the same as 32-bit,
282 * so it usually ends up taking this route too. Win! Unroll loop
283 * and use pointer arithmetic for extra speed. */
284 int numpixels = h * (w << 2);
285 Uint8 *pix = (Uint8 *) (s->pixels);
286 Uint8 *last = pix + numpixels;
288 if((numpixels & 0x7) == 0)
289 for(pixel = pix + 3; pixel < last; pixel += 32)
290 *pixel = *(pixel + 4) = *(pixel + 8) = *(pixel + 12) = *(pixel + 16) = *(pixel + 20) = *(pixel + 24) = *(pixel + 28) = val;
292 for(pixel = pix + 3; pixel < last; pixel += 4)
297 /* we have no choice but to do this slowly. <sigh> */
298 for(y = 0; y < h; ++y)
299 for(x = 0; x < w; ++x) {
301 /* Lock the surface for direct access to the pixels */
302 if(SDL_MUSTLOCK(s) && SDL_LockSurface(s) < 0) {
303 PRINT_ERROR("Can't lock surface: ");
304 fprintf(stderr, "%s\n", SDL_GetError());
307 pixel = DT_GetPixel(s, x, y);
310 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
311 pixel = SDL_MapRGBA(format, r, g, b, val);
312 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
313 DT_PutPixel(s, x, y, pixel);
315 /* unlock surface again */
317 SDL_UnlockSurface(s);
325 /* Updates the console buffer */
326 void CON_UpdateConsole(void) {
330 grs_canvas *canv_save;
336 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
340 Screenlines = console->ConsoleSurface->cv_h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
342 canv_save = grd_curcanv;
343 gr_set_current_canvas(console->ConsoleSurface);
346 SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha));
352 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
353 SDL_SetAlpha(console->ConsoleSurface, 0, SDL_ALPHA_OPAQUE);
356 /* draw the background image if there is one */
357 if(console->BackgroundImage)
358 gr_bitmap(0, 0, console->BackgroundImage);
360 /* Draw the text from the back buffers, calculate in the scrollback from the user
361 * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey
362 * for the font, and then clear it when we're done.
365 if((console->OutputScreen->flags & SDL_OPENGLBLIT) && (console->OutputScreen->format->BytesPerPixel > 2)) {
366 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
367 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
371 //now draw text from last but second line to top
372 for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) {
373 if(console->ConsoleScrollBack != 0 && loop == 0)
374 for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++)
376 orig_color = FG_COLOR;
377 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);
378 FG_COLOR = orig_color;
382 orig_color = FG_COLOR;
383 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), console->ConsoleLines[console->ConsoleScrollBack + loop]);
384 FG_COLOR = orig_color;
388 gr_set_current_canvas(canv_save);
391 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
392 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
396 void CON_UpdateOffset(void) {
400 switch(console->Visible) {
402 console->RaiseOffset -= CON_OPENCLOSE_SPEED;
403 if(console->RaiseOffset <= 0) {
404 console->RaiseOffset = 0;
405 console->Visible = CON_CLOSED;
409 console->RaiseOffset += CON_OPENCLOSE_SPEED;
410 if(console->RaiseOffset >= console->ConsoleSurface->cv_h) {
411 console->RaiseOffset = console->ConsoleSurface->cv_h;
412 console->Visible = CON_OPEN;
421 /* Draws the console buffer to the screen if the console is "visible" */
422 void CON_DrawConsole(void) {
423 grs_canvas *canv_save;
429 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
430 if(console->Visible == CON_CLOSED)
433 /* Update the scrolling offset */
436 /* Update the command line since it has a blinking cursor */
440 /* before drawing, make sure the alpha channel of the console surface is set
441 * properly. (sigh) I wish we didn't have to do this every frame... */
442 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
443 CON_AlphaGL(console->ConsoleSurface, console->ConsoleAlpha);
446 canv_save = grd_curcanv;
447 gr_set_current_canvas(&console->OutputScreen->sc_canvas);
449 clip = gr_create_sub_bitmap(&console->ConsoleSurface->cv_bitmap, 0, console->ConsoleSurface->cv_h - console->RaiseOffset, console->ConsoleSurface->cv_w, console->RaiseOffset);
451 gr_bitmap(0, 0, clip);
452 gr_free_sub_bitmap(clip);
455 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
456 SDL_UpdateRects(console->OutputScreen, 1, &DestRect);
459 gr_set_current_canvas(canv_save);
463 /* Initializes the console */
464 void CON_Init(grs_font *Font, grs_screen *DisplayScreen, int lines, int x, int y, int w, int h)
468 console->Visible = CON_CLOSED;
469 console->RaiseOffset = 0;
470 console->ConsoleLines = NULL;
471 console->CommandLines = NULL;
472 console->TotalConsoleLines = 0;
473 console->ConsoleScrollBack = 0;
474 console->TotalCommands = 0;
475 console->BackgroundImage = NULL;
477 console->ConsoleAlpha = SDL_ALPHA_OPAQUE;
480 console->InsMode = 1;
481 console->CursorPos = 0;
482 console->CommandScrollBack = 0;
483 console->OutputScreen = DisplayScreen;
484 console->Prompt = CON_DEFAULT_PROMPT;
485 console->HideKey = CON_DEFAULT_HIDEKEY;
487 /* make sure that the size of the console is valid */
488 if(w > console->OutputScreen->sc_w || w < Font->ft_w * 32)
489 w = console->OutputScreen->sc_w;
490 if(h > console->OutputScreen->sc_h || h < Font->ft_h)
491 h = console->OutputScreen->sc_h;
493 /* load the console surface */
494 console->ConsoleSurface = gr_create_canvas(w, h);
496 /* Load the consoles font */
498 grs_canvas *canv_save;
500 canv_save = grd_curcanv;
501 gr_set_current_canvas(console->ConsoleSurface);
502 gr_set_curfont(Font);
503 gr_set_fontcolor(gr_getcolor(63,63,63), -1);
504 gr_set_current_canvas(canv_save);
508 /* Load the dirty rectangle for user input */
509 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
511 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
514 /* calculate the number of visible characters in the command line */
515 console->VChars = (w - CON_CHAR_BORDER) / console->ConsoleSurface->cv_font->ft_w;
516 if(console->VChars > CON_CHARS_PER_LINE)
517 console->VChars = CON_CHARS_PER_LINE;
519 /* We would like to have a minumum # of lines to guarentee we don't create a memory error */
520 if(h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h) > lines)
521 console->LineBuffer = h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
523 console->LineBuffer = lines;
526 console->ConsoleLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
527 console->CommandLines = (char **)d_malloc(sizeof(char *) * console->LineBuffer);
528 for(loop = 0; loop <= console->LineBuffer - 1; loop++) {
529 console->ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
530 console->CommandLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
532 memset(console->Command, 0, CON_CHARS_PER_LINE);
533 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
534 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
535 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
538 CON_Out("Console initialised.");
539 CON_NewLineConsole();
542 /* Makes the console visible */
543 void CON_Show(void) {
545 console->Visible = CON_OPENING;
550 /* Hides the console (make it invisible) */
551 void CON_Hide(void) {
553 console->Visible = CON_CLOSING;
556 /* tells wether the console is visible or not */
557 int CON_isVisible(void) {
560 return((console->Visible == CON_OPEN) || (console->Visible == CON_OPENING));
563 /* Frees all the memory loaded by the console */
564 void CON_Free(void) {
570 for(i = 0; i <= console->LineBuffer - 1; i++) {
571 d_free(console->ConsoleLines[i]);
572 d_free(console->CommandLines[i]);
574 d_free(console->ConsoleLines);
575 d_free(console->CommandLines);
577 console->ConsoleLines = NULL;
578 console->CommandLines = NULL;
580 gr_free_canvas(console->ConsoleSurface);
581 console->ConsoleSurface = NULL;
583 if (console->BackgroundImage)
584 gr_free_bitmap(console->BackgroundImage);
585 console->BackgroundImage = NULL;
587 gr_free_bitmap(console->InputBackground);
588 console->InputBackground = NULL;
592 /* Increments the console lines */
593 void CON_NewLineConsole(void) {
600 temp = console->ConsoleLines[console->LineBuffer - 1];
602 for(loop = console->LineBuffer - 1; loop > 0; loop--)
603 console->ConsoleLines[loop] = console->ConsoleLines[loop - 1];
605 console->ConsoleLines[0] = temp;
607 memset(console->ConsoleLines[0], 0, CON_CHARS_PER_LINE);
608 if(console->TotalConsoleLines < console->LineBuffer - 1)
609 console->TotalConsoleLines++;
611 //Now adjust the ConsoleScrollBack
612 //dont scroll if not at bottom
613 if(console->ConsoleScrollBack != 0)
614 console->ConsoleScrollBack++;
616 if(console->ConsoleScrollBack > console->LineBuffer-1)
617 console->ConsoleScrollBack = console->LineBuffer-1;
622 /* Increments the command lines */
623 void CON_NewLineCommand(void) {
630 temp = console->CommandLines[console->LineBuffer - 1];
633 for(loop = console->LineBuffer - 1; loop > 0; loop--)
634 console->CommandLines[loop] = console->CommandLines[loop - 1];
636 console->CommandLines[0] = temp;
638 memset(console->CommandLines[0], 0, CON_CHARS_PER_LINE);
639 if(console->TotalCommands < console->LineBuffer - 1)
640 console->TotalCommands++;
643 /* Draws the command line the user is typing in to the screen */
644 /* completely rewritten by C.Wacha */
645 void DrawCommandLine() {
649 grs_font* CurrentFont;
651 static unsigned int LastBlinkTime = 0; /* Last time the consoles cursor blinked */
652 static int LastCursorPos = 0; // Last Cursor Position
653 static int Blink = 0; /* Is the cursor currently blinking */
654 grs_canvas *canv_save;
657 commandbuffer = console->VChars - strlen(console->Prompt)-1; // -1 to make cursor visible
660 CurrentFont = console->ConsoleSurface->cv_font;
663 //Concatenate the left and right side to command
664 strcpy(console->Command, console->LCommand);
665 strncat(console->Command, console->RCommand, strlen(console->RCommand));
667 //calculate display offset from current cursor position
668 if(console->Offset < console->CursorPos - commandbuffer)
669 console->Offset = console->CursorPos - commandbuffer;
670 if(console->Offset > console->CursorPos)
671 console->Offset = console->CursorPos;
673 //first add prompt to visible part
674 strcpy(console->VCommand, console->Prompt);
676 //then add the visible part of the command
677 strncat(console->VCommand, &console->Command[console->Offset], strlen(&console->Command[console->Offset]));
679 //now display the result
682 //once again we're drawing text, so in OpenGL context we need to temporarily set up
683 //software-mode transparency.
684 if(console->OutputScreen->flags & SDL_OPENGLBLIT) {
685 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
686 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
690 canv_save = grd_curcanv;
691 gr_set_current_canvas(console->ConsoleSurface);
693 //first of all restore InputBackground
694 gr_bitmap(0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->InputBackground);
697 orig_color = FG_COLOR;
698 gr_string(CON_CHAR_BORDER, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->VCommand);
699 FG_COLOR = orig_color;
701 //at last add the cursor
702 //check if the blink period is over
703 if(get_msecs() > LastBlinkTime) {
704 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
711 //check if cursor has moved - if yes display cursor anyway
712 if(console->CursorPos != LastCursorPos) {
713 LastCursorPos = console->CursorPos;
714 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
719 int prompt_width, cmd_width, h, w;
721 gr_get_string_size(console->Prompt, &prompt_width, &h, &w);
722 gr_get_string_size(console->LCommand + console->Offset, &cmd_width, &h, &w);
723 x = CON_CHAR_BORDER + prompt_width + cmd_width;
724 orig_color = FG_COLOR;
726 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_INS_CURSOR);
728 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_OVR_CURSOR);
729 FG_COLOR = orig_color;
732 gr_set_current_canvas(canv_save);
736 if(console->OutputScreen->flags & SDL_OPENGLBLIT) {
737 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
743 # define vsnprintf _vsnprintf
746 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
747 void CON_Out(const char *str, ...) {
749 //keep some space free for stuff like CON_Out("blablabla %s", console->Command);
750 char temp[CON_CHARS_PER_LINE + 128];
756 va_start(marker, str);
757 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
762 //temp now contains the complete string we want to output
763 // the only problem is that temp is maybe longer than the console
764 // width so we have to cut it into several pieces
766 if(console->ConsoleLines) {
767 while(strlen(ptemp) > console->VChars) {
768 CON_NewLineConsole();
769 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
770 console->ConsoleLines[0][console->VChars] = '\0';
771 ptemp = &ptemp[console->VChars];
773 CON_NewLineConsole();
774 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
775 console->ConsoleLines[0][console->VChars] = '\0';
782 /* Sets the alpha level of the console, 0 turns off alpha blending */
783 void CON_Alpha(unsigned char alpha) {
787 /* store alpha as state! */
788 console->ConsoleAlpha = alpha;
790 if((console->OutputScreen->flags & SDL_OPENGLBLIT) == 0) {
792 SDL_SetAlpha(console->ConsoleSurface, 0, alpha);
794 SDL_SetAlpha(console->ConsoleSurface, SDL_SRCALPHA, alpha);
797 // CON_UpdateConsole();
802 /* Adds background image to the console, scaled to size of console*/
803 int CON_Background(grs_bitmap *image)
808 /* Free the background from the console */
810 if (console->BackgroundImage)
811 gr_free_bitmap(console->BackgroundImage);
812 console->BackgroundImage = NULL;
814 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
819 /* Load a new background */
820 if (console->BackgroundImage)
821 gr_free_bitmap(console->BackgroundImage);
822 console->BackgroundImage = gr_create_bitmap(console->ConsoleSurface->cv_w, console->ConsoleSurface->cv_h);
823 gr_bitmap_scale_to(image, console->BackgroundImage);
826 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
828 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);
833 /* Sets font info for the console */
834 void CON_Font(grs_font *font, int fg, int bg)
836 grs_canvas *canv_save;
838 canv_save = grd_curcanv;
839 gr_set_current_canvas(console->ConsoleSurface);
840 gr_set_curfont(font);
841 gr_set_fontcolor(fg, bg);
842 gr_set_current_canvas(canv_save);
845 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
846 /* resizes the console, has to reset alot of stuff
847 * returns 1 on error */
848 int CON_Resize(int x, int y, int w, int h)
853 /* make sure that the size of the console is valid */
854 if(w > console->OutputScreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
855 w = console->OutputScreen->sc_w;
856 if(h > console->OutputScreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
857 h = console->OutputScreen->sc_h;
859 /* resize console surface */
860 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
861 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
863 /* Load the dirty rectangle for user input */
864 gr_free_bitmap(console->InputBackground);
865 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
867 /* Now reset some stuff dependent on the previous size */
868 console->ConsoleScrollBack = 0;
870 /* Reload the background image (for the input text area) in the console */
871 if(console->BackgroundImage) {
873 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
875 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);
879 /* restore the alpha level */
880 CON_Alpha(console->ConsoleAlpha);
885 /* Transfers the console to another screen surface, and adjusts size */
886 int CON_Transfer(grs_screen *new_outputscreen, int x, int y, int w, int h)
891 console->OutputScreen = new_outputscreen;
893 return(CON_Resize(x, y, w, h));
896 /* Sets the Prompt for console */
897 void CON_SetPrompt(char* newprompt) {
901 //check length so we can still see at least 1 char :-)
902 if(strlen(newprompt) < console->VChars)
903 console->Prompt = d_strdup(newprompt);
905 CON_Out("prompt too long. (max. %i chars)", console->VChars - 1);
908 /* Sets the key that deactivates (hides) the console. */
909 void CON_SetHideKey(int key) {
911 console->HideKey = key;
914 /* Executes the command entered */
915 void CON_Execute(char* command) {
920 void CON_TabCompletion(void) {
927 command = d_strdup(console->LCommand);
928 command = cmd_complete(command);
931 return; //no tab completion took place so return silently
934 if(j > CON_CHARS_PER_LINE - 2)
935 j = CON_CHARS_PER_LINE-1;
937 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
938 console->CursorPos = 0;
940 for(i = 0; i < j; i++) {
941 console->CursorPos++;
942 console->LCommand[i] = command[i];
944 //add a trailing space
945 console->CursorPos++;
946 console->LCommand[j] = ' ';
947 console->LCommand[j+1] = '\0';
950 void Cursor_Left(void) {
951 char temp[CON_CHARS_PER_LINE];
953 if(console->CursorPos > 0) {
954 console->CursorPos--;
955 strcpy(temp, console->RCommand);
956 strcpy(console->RCommand, &console->LCommand[strlen(console->LCommand)-1]);
957 strcat(console->RCommand, temp);
958 console->LCommand[strlen(console->LCommand)-1] = '\0';
959 //CON_Out("L:%s, R:%s", console->LCommand, console->RCommand);
963 void Cursor_Right(void) {
964 char temp[CON_CHARS_PER_LINE];
966 if(console->CursorPos < strlen(console->Command)) {
967 console->CursorPos++;
968 strncat(console->LCommand, console->RCommand, 1);
969 strcpy(temp, console->RCommand);
970 strcpy(console->RCommand, &temp[1]);
971 //CON_Out("L:%s, R:%s", console->LCommand, console->RCommand);
975 void Cursor_Home(void) {
976 char temp[CON_CHARS_PER_LINE];
978 console->CursorPos = 0;
979 strcpy(temp, console->RCommand);
980 strcpy(console->RCommand, console->LCommand);
981 strncat(console->RCommand, temp, strlen(temp));
982 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
985 void Cursor_End(void) {
986 console->CursorPos = strlen(console->Command);
987 strncat(console->LCommand, console->RCommand, strlen(console->RCommand));
988 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
991 void Cursor_Del(void) {
992 char temp[CON_CHARS_PER_LINE];
994 if(strlen(console->RCommand) > 0) {
995 strcpy(temp, console->RCommand);
996 strcpy(console->RCommand, &temp[1]);
1000 void Cursor_BSpace(void) {
1001 if(console->CursorPos > 0) {
1002 console->CursorPos--;
1004 if(console->Offset < 0)
1005 console->Offset = 0;
1006 console->LCommand[strlen(console->LCommand)-1] = '\0';
1010 void Cursor_Add(int event)
1012 if(strlen(console->Command) < CON_CHARS_PER_LINE - 1)
1014 console->CursorPos++;
1015 console->LCommand[strlen(console->LCommand)] = key_to_ascii(event);
1016 console->LCommand[strlen(console->LCommand)] = '\0';
1020 void Clear_Command(void) {
1021 console->CursorPos = 0;
1022 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
1023 memset(console->Command, 0, CON_CHARS_PER_LINE);
1024 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1025 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1028 void Clear_History(void) {
1031 for(loop = 0; loop <= console->LineBuffer - 1; loop++)
1032 memset(console->ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
1035 void Command_Up(void) {
1036 if(console->CommandScrollBack < console->TotalCommands - 1) {
1037 /* move back a line in the command strings and copy the command to the current input string */
1038 console->CommandScrollBack++;
1039 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1040 console->Offset = 0;
1041 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1042 console->CursorPos = strlen(console->CommandLines[console->CommandScrollBack]);
1043 CON_UpdateConsole();
1047 void Command_Down(void) {
1048 if(console->CommandScrollBack > -1) {
1049 /* move forward a line in the command strings and copy the command to the current input string */
1050 console->CommandScrollBack--;
1051 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1052 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1053 console->Offset = 0;
1054 if(console->CommandScrollBack > -1)
1055 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1056 console->CursorPos = strlen(console->LCommand);
1057 CON_UpdateConsole();
1070 #include "pstypes.h"
1073 #include "console.h"
1077 #include "gamefont.h"
1082 int text_console_enabled = 1;
1085 #define text_console_enabled (!isvga())
1089 /* Console specific cvars */
1090 /* How discriminating we are about which messages are displayed */
1091 cvar_t con_threshold = {"con_threshold", "0",};
1093 /* Private console stuff */
1094 #define CON_NUM_LINES 40
1096 static int con_initialized;
1099 /* Free the console */
1102 if (con_initialized)
1104 con_initialized = 0;
1108 /* Initialise the console */
1111 grs_screen fake_screen;
1114 fake_screen.sc_w = 320;
1115 fake_screen.sc_h = 200;
1119 CON_Init(&fake_font, &fake_screen, CON_NUM_LINES, 0, 0, 320, 200);
1123 /* Initialise the cvars */
1124 cvar_registervariable (&con_threshold);
1126 con_initialized = 1;
1132 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
1133 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
1134 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
1136 void con_background(char *filename)
1142 gr_init_bitmap_data(&bmp);
1143 pcx_error = pcx_read_bitmap(filename, &bmp, BM_LINEAR, pal);
1144 Assert(pcx_error == PCX_ERROR_NONE);
1145 gr_remap_bitmap_good(&bmp, pal, -1, -1);
1146 CON_Background(&bmp);
1147 gr_free_bitmap_data(&bmp);
1151 void con_init_gfx(void)
1153 CON_Font(SMALL_FONT, gr_getcolor(63, 63, 63), -1);
1154 CON_Transfer(grd_curscreen, 0, 0, SWIDTH, SHEIGHT / 2);
1156 con_background(CON_BG);
1160 void con_resize(void)
1162 CON_Font(SMALL_FONT, gr_getcolor(63, 63, 63), -1);
1163 CON_Resize(0, 0, SWIDTH, SHEIGHT / 2);
1164 con_background(CON_BG);
1167 /* Print a message to the console */
1168 void con_printf(int priority, char *fmt, ...)
1173 if (priority <= ((int)con_threshold.value))
1175 va_start (arglist, fmt);
1176 vsprintf (buffer, fmt, arglist);
1179 if (con_initialized)
1182 if (text_console_enabled)
1184 /* Produce a sanitised version and send it to the standard output */
1203 printf("%s", buffer);