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())
32 static ConsoleInformation *Console;
34 /* Pointer to our one console */
35 static ConsoleInformation *console;
38 void CON_UpdateOffset(void);
40 void CON_Destroy(void);
41 /*! Frees all the memory loaded by the console */
44 /*! Sets the alpha channel of an SDL_Surface to the specified value (0 - transparend,
45 255 - opaque). Use this function also for OpenGL. */
46 void CON_Alpha(unsigned char alpha);
47 /*! Internal: Sets the alpha channel of an SDL_Surface to the specified value.
48 Preconditions: the surface in question is RGBA. 0 <= a <= 255, where 0 is transparent and 255 opaque */
49 void CON_AlphaGL(SDL_Surface *s, int alpha);
50 /*! Sets a background image for the console */
52 int CON_Background(grs_bitmap *image);
53 /*! Sets font info for the console */
54 void CON_Font(grs_font *font, int fg, int bg);
55 /*! Changes current position of the console */
56 void CON_Position(int x, int y);
57 /*! Beams a console to another screen surface. Needed if you want to make a Video restart in your program. This
58 function first changes the OutputScreen Pointer then calls CON_Resize to adjust the new size. */
59 int CON_Transfer(grs_screen* new_outputscreen, int x, int y, int w, int h);
60 /*! Modify the prompt of the console */
61 void CON_SetPrompt(char* newprompt);
62 /*! Set the key, that invokes a CON_Hide() after press. default is ESCAPE and you can always hide using
63 ESCAPE and the HideKey. compared against event->key.keysym.sym !! */
64 void CON_SetHideKey(int key);
65 /*! Internal: executes the command typed in at the console (called if you press ENTER)*/
66 void CON_Execute(char* command);
67 /*! Internal: Gets called when TAB was pressed */
68 void CON_TabCompletion(void);
69 /*! Internal: makes newline (same as printf("\n") or CON_Out("\n") ) */
70 void CON_NewLineConsole(void);
71 /*! Internal: shift command history (the one you can switch with the up/down keys) */
72 void CON_NewLineCommand(void);
73 /*! Internal: updates console after resize etc. */
74 void CON_UpdateConsole(void);
77 /*! Internal: draws the commandline the user is typing in to the screen. called by update? */
78 void DrawCommandLine();
80 /*! Internal: Gets called if you press the LEFT key (move cursor left) */
81 void Cursor_Left(void);
82 /*! Internal: Gets called if you press the RIGHT key (move cursor right) */
83 void Cursor_Right(void);
84 /*! Internal: Gets called if you press the HOME key (move cursor to the beginning
86 void Cursor_Home(void);
87 /*! Internal: Gets called if you press the END key (move cursor to the end of the line*/
88 void Cursor_End(void);
89 /*! Internal: Called if you press DELETE (deletes character under the cursor) */
90 void Cursor_Del(void);
91 /*! Internal: Called if you press BACKSPACE (deletes character left of cursor) */
92 void Cursor_BSpace(void);
93 /*! Internal: Called if you type in a character (add the char to the command) */
94 void Cursor_Add(int event);
96 /*! Internal: Called if you press Ctrl-C (deletes the commandline) */
97 void Clear_Command(void);
98 /*! Internal: Called if you press Ctrl-L (deletes the History) */
99 void Clear_History(void);
101 /*! Internal: Called if you press UP key (switches through recent typed in commands */
102 void Command_Up(void);
103 /*! Internal: Called if you press DOWN key (switches through recent typed in commands */
104 void Command_Down(void);
107 /* Takes keys from the keyboard and inputs them to the console
108 If the event was not handled (i.e. WM events or unknown ctrl-shift
109 sequences) the function returns the event for further processing. */
110 int CON_Events(int event)
115 if(event & KEY_CTRLED)
118 switch(event & ~KEY_CTRLED)
137 else if(event & KEY_ALTED)
139 //the console does not handle ALT combinations!
144 //first of all, check if the console hide key was pressed
145 if(event == console->HideKey)
150 switch (event & 0xff)
156 if(event & KEY_SHIFTED)
158 console->ConsoleScrollBack = console->LineBuffer-1;
165 if(event & KEY_SHIFTED)
167 console->ConsoleScrollBack = 0;
174 console->ConsoleScrollBack += CON_LINE_SCROLL;
175 if(console->ConsoleScrollBack > console->LineBuffer-1)
176 console->ConsoleScrollBack = console->LineBuffer-1;
181 console->ConsoleScrollBack -= CON_LINE_SCROLL;
182 if(console->ConsoleScrollBack < 0)
183 console->ConsoleScrollBack = 0;
205 console->InsMode = 1-console->InsMode;
211 if(strlen(console->Command) > 0) {
212 CON_NewLineCommand();
214 // copy the input into the past commands strings
215 strcpy(console->CommandLines[0], console->Command);
217 // display the command including the prompt
218 CON_Out("%s%s", console->Prompt, console->Command);
221 CON_Execute(console->Command);
222 //printf("Command: %s\n", console->Command);
225 console->CommandScrollBack = -1;
233 if (key_to_ascii(event) == 255)
247 /* CON_AlphaGL() -- sets the alpha channel of an SDL_Surface to the
248 * specified value. Preconditions: the surface in question is RGBA.
249 * 0 <= a <= 255, where 0 is transparent and 255 is opaque. */
250 void CON_AlphaGL(SDL_Surface *s, int alpha) {
255 SDL_PixelFormat *format;
256 static char errorPrinted = 0;
259 /* debugging assertions -- these slow you down, but hey, crashing sucks */
261 PRINT_ERROR("NULL Surface passed to CON_AlphaGL\n");
265 /* clamp alpha value to 0...255 */
266 if(alpha < SDL_ALPHA_TRANSPARENT)
267 val = SDL_ALPHA_TRANSPARENT;
268 else if(alpha > SDL_ALPHA_OPAQUE)
269 val = SDL_ALPHA_OPAQUE;
273 /* loop over alpha channels of each pixel, setting them appropriately. */
277 switch (format->BytesPerPixel) {
279 /* 16-bit surfaces don't seem to support alpha channels. */
282 PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL.\n");
286 /* we can do this very quickly in 32-bit mode. 24-bit is more
287 * difficult. And since 24-bit mode is reall the same as 32-bit,
288 * so it usually ends up taking this route too. Win! Unroll loop
289 * and use pointer arithmetic for extra speed. */
290 int numpixels = h * (w << 2);
291 Uint8 *pix = (Uint8 *) (s->pixels);
292 Uint8 *last = pix + numpixels;
294 if((numpixels & 0x7) == 0)
295 for(pixel = pix + 3; pixel < last; pixel += 32)
296 *pixel = *(pixel + 4) = *(pixel + 8) = *(pixel + 12) = *(pixel + 16) = *(pixel + 20) = *(pixel + 24) = *(pixel + 28) = val;
298 for(pixel = pix + 3; pixel < last; pixel += 4)
303 /* we have no choice but to do this slowly. <sigh> */
304 for(y = 0; y < h; ++y)
305 for(x = 0; x < w; ++x) {
307 /* Lock the surface for direct access to the pixels */
308 if(SDL_MUSTLOCK(s) && SDL_LockSurface(s) < 0) {
309 PRINT_ERROR("Can't lock surface: ");
310 fprintf(stderr, "%s\n", SDL_GetError());
313 pixel = DT_GetPixel(s, x, y);
316 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
317 pixel = SDL_MapRGBA(format, r, g, b, val);
318 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
319 DT_PutPixel(s, x, y, pixel);
321 /* unlock surface again */
323 SDL_UnlockSurface(s);
331 /* Updates the console buffer */
332 void CON_UpdateConsole(void) {
336 grs_canvas *canv_save;
342 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
346 Screenlines = console->ConsoleSurface->cv_h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
348 canv_save = grd_curcanv;
349 gr_set_current_canvas(console->ConsoleSurface);
352 SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha));
358 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
359 SDL_SetAlpha(console->ConsoleSurface, 0, SDL_ALPHA_OPAQUE);
362 /* draw the background image if there is one */
363 if(console->BackgroundImage)
364 gr_bitmap(0, 0, console->BackgroundImage);
366 /* Draw the text from the back buffers, calculate in the scrollback from the user
367 * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey
368 * for the font, and then clear it when we're done.
371 if((console->OutputScreen->flags & SDL_OPENGLBLIT) && (console->OutputScreen->format->BytesPerPixel > 2)) {
372 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
373 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
377 //now draw text from last but second line to top
378 for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) {
379 if(console->ConsoleScrollBack != 0 && loop == 0)
380 for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++)
382 orig_color = FG_COLOR;
383 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);
384 FG_COLOR = orig_color;
388 orig_color = FG_COLOR;
389 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), console->ConsoleLines[console->ConsoleScrollBack + loop]);
390 FG_COLOR = orig_color;
394 gr_set_current_canvas(canv_save);
397 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
398 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
402 void CON_UpdateOffset(void) {
406 switch(console->Visible) {
408 console->RaiseOffset -= CON_OPENCLOSE_SPEED;
409 if(console->RaiseOffset <= 0) {
410 console->RaiseOffset = 0;
411 console->Visible = CON_CLOSED;
415 console->RaiseOffset += CON_OPENCLOSE_SPEED;
416 if(console->RaiseOffset >= console->ConsoleSurface->cv_h) {
417 console->RaiseOffset = console->ConsoleSurface->cv_h;
418 console->Visible = CON_OPEN;
427 /* Draws the console buffer to the screen if the console is "visible" */
428 void CON_DrawConsole(void) {
429 grs_canvas *canv_save;
435 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
436 if(console->Visible == CON_CLOSED)
439 /* Update the scrolling offset */
442 /* Update the command line since it has a blinking cursor */
446 /* before drawing, make sure the alpha channel of the console surface is set
447 * properly. (sigh) I wish we didn't have to do this every frame... */
448 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
449 CON_AlphaGL(console->ConsoleSurface, console->ConsoleAlpha);
452 canv_save = grd_curcanv;
453 gr_set_current_canvas(&console->OutputScreen->sc_canvas);
455 clip = gr_create_sub_bitmap(&console->ConsoleSurface->cv_bitmap, 0, console->ConsoleSurface->cv_h - console->RaiseOffset, console->ConsoleSurface->cv_w, console->RaiseOffset);
457 gr_bitmap(console->DispX, console->DispY, clip);
458 gr_free_sub_bitmap(clip);
461 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
462 SDL_UpdateRects(console->OutputScreen, 1, &DestRect);
465 gr_set_current_canvas(canv_save);
469 /* Initializes the console */
470 ConsoleInformation *CON_Init(grs_font *Font, grs_screen *DisplayScreen, int lines, int x, int y, int w, int h)
473 ConsoleInformation *newinfo;
476 /* Create a new console struct and init it. */
477 if((newinfo = (ConsoleInformation *) d_malloc(sizeof(ConsoleInformation))) == NULL) {
478 //PRINT_ERROR("Could not allocate the space for a new console info struct.\n");
481 newinfo->Visible = CON_CLOSED;
482 newinfo->RaiseOffset = 0;
483 newinfo->ConsoleLines = NULL;
484 newinfo->CommandLines = NULL;
485 newinfo->TotalConsoleLines = 0;
486 newinfo->ConsoleScrollBack = 0;
487 newinfo->TotalCommands = 0;
488 newinfo->BackgroundImage = NULL;
490 newinfo->ConsoleAlpha = SDL_ALPHA_OPAQUE;
493 newinfo->InsMode = 1;
494 newinfo->CursorPos = 0;
495 newinfo->CommandScrollBack = 0;
496 newinfo->OutputScreen = DisplayScreen;
497 newinfo->Prompt = CON_DEFAULT_PROMPT;
498 newinfo->HideKey = CON_DEFAULT_HIDEKEY;
500 /* make sure that the size of the console is valid */
501 if(w > newinfo->OutputScreen->sc_w || w < Font->ft_w * 32)
502 w = newinfo->OutputScreen->sc_w;
503 if(h > newinfo->OutputScreen->sc_h || h < Font->ft_h)
504 h = newinfo->OutputScreen->sc_h;
505 if(x < 0 || x > newinfo->OutputScreen->sc_w - w)
509 if(y < 0 || y > newinfo->OutputScreen->sc_h - h)
514 /* load the console surface */
515 newinfo->ConsoleSurface = gr_create_canvas(w, h);
517 /* Load the consoles font */
519 grs_canvas *canv_save;
521 canv_save = grd_curcanv;
522 gr_set_current_canvas(newinfo->ConsoleSurface);
523 gr_set_curfont(Font);
524 gr_set_fontcolor(gr_getcolor(63,63,63), -1);
525 gr_set_current_canvas(canv_save);
529 /* Load the dirty rectangle for user input */
530 newinfo->InputBackground = gr_create_bitmap(w, newinfo->ConsoleSurface->cv_font->ft_h);
532 SDL_FillRect(newinfo->InputBackground, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
535 /* calculate the number of visible characters in the command line */
536 newinfo->VChars = (w - CON_CHAR_BORDER) / newinfo->ConsoleSurface->cv_font->ft_w;
537 if(newinfo->VChars > CON_CHARS_PER_LINE)
538 newinfo->VChars = CON_CHARS_PER_LINE;
540 /* We would like to have a minumum # of lines to guarentee we don't create a memory error */
541 if(h / (CON_LINE_SPACE + newinfo->ConsoleSurface->cv_font->ft_h) > lines)
542 newinfo->LineBuffer = h / (CON_LINE_SPACE + newinfo->ConsoleSurface->cv_font->ft_h);
544 newinfo->LineBuffer = lines;
547 newinfo->ConsoleLines = (char **)d_malloc(sizeof(char *) * newinfo->LineBuffer);
548 newinfo->CommandLines = (char **)d_malloc(sizeof(char *) * newinfo->LineBuffer);
549 for(loop = 0; loop <= newinfo->LineBuffer - 1; loop++) {
550 newinfo->ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
551 newinfo->CommandLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
553 memset(newinfo->Command, 0, CON_CHARS_PER_LINE);
554 memset(newinfo->LCommand, 0, CON_CHARS_PER_LINE);
555 memset(newinfo->RCommand, 0, CON_CHARS_PER_LINE);
556 memset(newinfo->VCommand, 0, CON_CHARS_PER_LINE);
559 CON_Out("Console initialised.");
560 CON_NewLineConsole();
561 //CON_ListCommands();
566 /* Makes the console visible */
567 void CON_Show(void) {
569 console->Visible = CON_OPENING;
574 /* Hides the console (make it invisible) */
575 void CON_Hide(void) {
577 console->Visible = CON_CLOSING;
580 /* tells wether the console is visible or not */
581 int CON_isVisible(void) {
584 return((console->Visible == CON_OPEN) || (console->Visible == CON_OPENING));
587 /* Frees all the memory loaded by the console */
588 void CON_Destroy(void) {
592 /* Frees all the memory loaded by the console */
593 void CON_Free(void) {
599 //CON_DestroyCommands();
600 for(i = 0; i <= console->LineBuffer - 1; i++) {
601 d_free(console->ConsoleLines[i]);
602 d_free(console->CommandLines[i]);
604 d_free(console->ConsoleLines);
605 d_free(console->CommandLines);
607 console->ConsoleLines = NULL;
608 console->CommandLines = NULL;
610 gr_free_canvas(console->ConsoleSurface);
611 console->ConsoleSurface = NULL;
613 if (console->BackgroundImage)
614 gr_free_bitmap(console->BackgroundImage);
615 console->BackgroundImage = NULL;
617 gr_free_bitmap(console->InputBackground);
618 console->InputBackground = NULL;
624 /* Increments the console lines */
625 void CON_NewLineConsole(void) {
632 temp = console->ConsoleLines[console->LineBuffer - 1];
634 for(loop = console->LineBuffer - 1; loop > 0; loop--)
635 console->ConsoleLines[loop] = console->ConsoleLines[loop - 1];
637 console->ConsoleLines[0] = temp;
639 memset(console->ConsoleLines[0], 0, CON_CHARS_PER_LINE);
640 if(console->TotalConsoleLines < console->LineBuffer - 1)
641 console->TotalConsoleLines++;
643 //Now adjust the ConsoleScrollBack
644 //dont scroll if not at bottom
645 if(console->ConsoleScrollBack != 0)
646 console->ConsoleScrollBack++;
648 if(console->ConsoleScrollBack > console->LineBuffer-1)
649 console->ConsoleScrollBack = console->LineBuffer-1;
654 /* Increments the command lines */
655 void CON_NewLineCommand(void) {
662 temp = console->CommandLines[console->LineBuffer - 1];
665 for(loop = console->LineBuffer - 1; loop > 0; loop--)
666 console->CommandLines[loop] = console->CommandLines[loop - 1];
668 console->CommandLines[0] = temp;
670 memset(console->CommandLines[0], 0, CON_CHARS_PER_LINE);
671 if(console->TotalCommands < console->LineBuffer - 1)
672 console->TotalCommands++;
675 /* Draws the command line the user is typing in to the screen */
676 /* completely rewritten by C.Wacha */
677 void DrawCommandLine() {
681 grs_font* CurrentFont;
683 static unsigned int LastBlinkTime = 0; /* Last time the consoles cursor blinked */
684 static int LastCursorPos = 0; // Last Cursor Position
685 static int Blink = 0; /* Is the cursor currently blinking */
686 grs_canvas *canv_save;
689 commandbuffer = console->VChars - strlen(console->Prompt)-1; // -1 to make cursor visible
692 CurrentFont = console->ConsoleSurface->cv_font;
695 //Concatenate the left and right side to command
696 strcpy(console->Command, console->LCommand);
697 strncat(console->Command, console->RCommand, strlen(console->RCommand));
699 //calculate display offset from current cursor position
700 if(console->Offset < console->CursorPos - commandbuffer)
701 console->Offset = console->CursorPos - commandbuffer;
702 if(console->Offset > console->CursorPos)
703 console->Offset = console->CursorPos;
705 //first add prompt to visible part
706 strcpy(console->VCommand, console->Prompt);
708 //then add the visible part of the command
709 strncat(console->VCommand, &console->Command[console->Offset], strlen(&console->Command[console->Offset]));
711 //now display the result
714 //once again we're drawing text, so in OpenGL context we need to temporarily set up
715 //software-mode transparency.
716 if(console->OutputScreen->flags & SDL_OPENGLBLIT) {
717 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
718 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
722 canv_save = grd_curcanv;
723 gr_set_current_canvas(console->ConsoleSurface);
725 //first of all restore InputBackground
726 gr_bitmap(0, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->InputBackground);
729 orig_color = FG_COLOR;
730 gr_string(CON_CHAR_BORDER, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, console->VCommand);
731 FG_COLOR = orig_color;
733 //at last add the cursor
734 //check if the blink period is over
735 if(get_msecs() > LastBlinkTime) {
736 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
743 //check if cursor has moved - if yes display cursor anyway
744 if(console->CursorPos != LastCursorPos) {
745 LastCursorPos = console->CursorPos;
746 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
751 int prompt_width, cmd_width, h, w;
753 gr_get_string_size(console->Prompt, &prompt_width, &h, &w);
754 gr_get_string_size(console->LCommand + console->Offset, &cmd_width, &h, &w);
755 x = CON_CHAR_BORDER + prompt_width + cmd_width;
756 orig_color = FG_COLOR;
758 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_INS_CURSOR);
760 gr_string(x, console->ConsoleSurface->cv_h - console->ConsoleSurface->cv_font->ft_h, CON_OVR_CURSOR);
761 FG_COLOR = orig_color;
764 gr_set_current_canvas(canv_save);
768 if(console->OutputScreen->flags & SDL_OPENGLBLIT) {
769 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
775 # define vsnprintf _vsnprintf
778 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
779 void CON_Out(const char *str, ...) {
781 //keep some space free for stuff like CON_Out("blablabla %s", console->Command);
782 char temp[CON_CHARS_PER_LINE + 128];
788 va_start(marker, str);
789 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
794 //temp now contains the complete string we want to output
795 // the only problem is that temp is maybe longer than the console
796 // width so we have to cut it into several pieces
798 if(console->ConsoleLines) {
799 while(strlen(ptemp) > console->VChars) {
800 CON_NewLineConsole();
801 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
802 console->ConsoleLines[0][console->VChars] = '\0';
803 ptemp = &ptemp[console->VChars];
805 CON_NewLineConsole();
806 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
807 console->ConsoleLines[0][console->VChars] = '\0';
811 /* And print to stdout */
812 //printf("%s\n", temp);
817 /* Sets the alpha level of the console, 0 turns off alpha blending */
818 void CON_Alpha(unsigned char alpha) {
822 /* store alpha as state! */
823 console->ConsoleAlpha = alpha;
825 if((console->OutputScreen->flags & SDL_OPENGLBLIT) == 0) {
827 SDL_SetAlpha(console->ConsoleSurface, 0, alpha);
829 SDL_SetAlpha(console->ConsoleSurface, SDL_SRCALPHA, alpha);
832 // CON_UpdateConsole();
837 /* Adds background image to the console, scaled to size of console*/
838 int CON_Background(grs_bitmap *image)
843 /* Free the background from the console */
845 if (console->BackgroundImage)
846 gr_free_bitmap(console->BackgroundImage);
847 console->BackgroundImage = NULL;
849 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
854 /* Load a new background */
855 if (console->BackgroundImage)
856 gr_free_bitmap(console->BackgroundImage);
857 console->BackgroundImage = gr_create_bitmap(console->ConsoleSurface->cv_w, console->ConsoleSurface->cv_h);
858 gr_bitmap_scale_to(image, console->BackgroundImage);
861 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
863 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);
868 /* Sets font info for the console */
869 void CON_Font(grs_font *font, int fg, int bg)
871 grs_canvas *canv_save;
873 canv_save = grd_curcanv;
874 gr_set_current_canvas(console->ConsoleSurface);
875 gr_set_curfont(font);
876 gr_set_fontcolor(fg, bg);
877 gr_set_current_canvas(canv_save);
880 /* takes a new x and y of the top left of the console window */
881 void CON_Position(int x, int y) {
885 if(x < 0 || x > console->OutputScreen->sc_w - console->ConsoleSurface->cv_w)
890 if(y < 0 || y > console->OutputScreen->sc_h - console->ConsoleSurface->cv_h)
896 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
897 /* resizes the console, has to reset alot of stuff
898 * returns 1 on error */
899 int CON_Resize(int x, int y, int w, int h)
904 /* make sure that the size of the console is valid */
905 if(w > console->OutputScreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
906 w = console->OutputScreen->sc_w;
907 if(h > console->OutputScreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
908 h = console->OutputScreen->sc_h;
909 if(x < 0 || x > console->OutputScreen->sc_w - w)
913 if(y < 0 || y > console->OutputScreen->sc_h - h)
918 /* resize console surface */
919 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
920 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
922 /* Load the dirty rectangle for user input */
923 gr_free_bitmap(console->InputBackground);
924 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
926 /* Now reset some stuff dependent on the previous size */
927 console->ConsoleScrollBack = 0;
929 /* Reload the background image (for the input text area) in the console */
930 if(console->BackgroundImage) {
932 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
934 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);
938 /* restore the alpha level */
939 CON_Alpha(console->ConsoleAlpha);
944 /* Transfers the console to another screen surface, and adjusts size */
945 int CON_Transfer(grs_screen *new_outputscreen, int x, int y, int w, int h)
950 console->OutputScreen = new_outputscreen;
952 return(CON_Resize(x, y, w, h));
955 /* Sets the Prompt for console */
956 void CON_SetPrompt(char* newprompt) {
960 //check length so we can still see at least 1 char :-)
961 if(strlen(newprompt) < console->VChars)
962 console->Prompt = d_strdup(newprompt);
964 CON_Out("prompt too long. (max. %i chars)", console->VChars - 1);
967 /* Sets the key that deactivates (hides) the console. */
968 void CON_SetHideKey(int key) {
970 console->HideKey = key;
973 /* Executes the command entered */
974 void CON_Execute(char* command) {
979 void CON_TabCompletion(void) {
986 command = d_strdup(console->LCommand);
987 command = cmd_complete(command);
990 return; //no tab completion took place so return silently
993 if(j > CON_CHARS_PER_LINE - 2)
994 j = CON_CHARS_PER_LINE-1;
996 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
997 console->CursorPos = 0;
999 for(i = 0; i < j; i++) {
1000 console->CursorPos++;
1001 console->LCommand[i] = command[i];
1003 //add a trailing space
1004 console->CursorPos++;
1005 console->LCommand[j] = ' ';
1006 console->LCommand[j+1] = '\0';
1009 void Cursor_Left(void) {
1010 char temp[CON_CHARS_PER_LINE];
1012 if(console->CursorPos > 0) {
1013 console->CursorPos--;
1014 strcpy(temp, console->RCommand);
1015 strcpy(console->RCommand, &console->LCommand[strlen(console->LCommand)-1]);
1016 strcat(console->RCommand, temp);
1017 console->LCommand[strlen(console->LCommand)-1] = '\0';
1018 //CON_Out("L:%s, R:%s", console->LCommand, console->RCommand);
1022 void Cursor_Right(void) {
1023 char temp[CON_CHARS_PER_LINE];
1025 if(console->CursorPos < strlen(console->Command)) {
1026 console->CursorPos++;
1027 strncat(console->LCommand, console->RCommand, 1);
1028 strcpy(temp, console->RCommand);
1029 strcpy(console->RCommand, &temp[1]);
1030 //CON_Out("L:%s, R:%s", console->LCommand, console->RCommand);
1034 void Cursor_Home(void) {
1035 char temp[CON_CHARS_PER_LINE];
1037 console->CursorPos = 0;
1038 strcpy(temp, console->RCommand);
1039 strcpy(console->RCommand, console->LCommand);
1040 strncat(console->RCommand, temp, strlen(temp));
1041 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1044 void Cursor_End(void) {
1045 console->CursorPos = strlen(console->Command);
1046 strncat(console->LCommand, console->RCommand, strlen(console->RCommand));
1047 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1050 void Cursor_Del(void) {
1051 char temp[CON_CHARS_PER_LINE];
1053 if(strlen(console->RCommand) > 0) {
1054 strcpy(temp, console->RCommand);
1055 strcpy(console->RCommand, &temp[1]);
1059 void Cursor_BSpace(void) {
1060 if(console->CursorPos > 0) {
1061 console->CursorPos--;
1063 if(console->Offset < 0)
1064 console->Offset = 0;
1065 console->LCommand[strlen(console->LCommand)-1] = '\0';
1069 void Cursor_Add(int event)
1071 if(strlen(console->Command) < CON_CHARS_PER_LINE - 1)
1073 console->CursorPos++;
1074 console->LCommand[strlen(console->LCommand)] = key_to_ascii(event);
1075 console->LCommand[strlen(console->LCommand)] = '\0';
1079 void Clear_Command(void) {
1080 console->CursorPos = 0;
1081 memset(console->VCommand, 0, CON_CHARS_PER_LINE);
1082 memset(console->Command, 0, CON_CHARS_PER_LINE);
1083 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1084 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1087 void Clear_History(void) {
1090 for(loop = 0; loop <= console->LineBuffer - 1; loop++)
1091 memset(console->ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
1094 void Command_Up(void) {
1095 if(console->CommandScrollBack < console->TotalCommands - 1) {
1096 /* move back a line in the command strings and copy the command to the current input string */
1097 console->CommandScrollBack++;
1098 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1099 console->Offset = 0;
1100 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1101 console->CursorPos = strlen(console->CommandLines[console->CommandScrollBack]);
1102 CON_UpdateConsole();
1106 void Command_Down(void) {
1107 if(console->CommandScrollBack > -1) {
1108 /* move forward 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 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1112 console->Offset = 0;
1113 if(console->CommandScrollBack > -1)
1114 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1115 console->CursorPos = strlen(console->LCommand);
1116 CON_UpdateConsole();
1129 #include "pstypes.h"
1132 #include "console.h"
1136 #include "gamefont.h"
1141 int text_console_enabled = 1;
1144 #define text_console_enabled (!isvga())
1148 /* Console specific cvars */
1149 /* How discriminating we are about which messages are displayed */
1150 cvar_t con_threshold = {"con_threshold", "0",};
1152 /* Private console stuff */
1153 #define CON_NUM_LINES 40
1155 static int con_initialized;
1158 /* Free the console */
1161 if (con_initialized)
1163 con_initialized = 0;
1167 /* Initialise the console */
1170 grs_screen fake_screen;
1173 fake_screen.sc_w = 320;
1174 fake_screen.sc_h = 200;
1178 Console = CON_Init(&fake_font, &fake_screen, CON_NUM_LINES, 0, 0, 320, 200);
1183 /* Initialise the cvars */
1184 cvar_registervariable (&con_threshold);
1186 con_initialized = 1;
1192 #define CON_BG_HIRES (cfexist("scoresb.pcx")?"scoresb.pcx":"scores.pcx")
1193 #define CON_BG_LORES (cfexist("scores.pcx")?"scores.pcx":"scoresb.pcx") // Mac datafiles only have scoresb.pcx
1194 #define CON_BG ((SWIDTH>=640)?CON_BG_HIRES:CON_BG_LORES)
1196 void con_background(char *filename)
1202 gr_init_bitmap_data(&bmp);
1203 pcx_error = pcx_read_bitmap(filename, &bmp, BM_LINEAR, pal);
1204 Assert(pcx_error == PCX_ERROR_NONE);
1205 gr_remap_bitmap_good(&bmp, pal, -1, -1);
1206 CON_Background(&bmp);
1207 gr_free_bitmap_data(&bmp);
1211 void con_init_gfx(void)
1213 CON_Font(SMALL_FONT, gr_getcolor(63, 63, 63), -1);
1214 CON_Transfer(grd_curscreen, 0, 0, SWIDTH, SHEIGHT / 2);
1216 con_background(CON_BG);
1220 void con_resize(void)
1222 CON_Font(SMALL_FONT, gr_getcolor(63, 63, 63), -1);
1223 CON_Resize(0, 0, SWIDTH, SHEIGHT / 2);
1224 con_background(CON_BG);
1227 /* Print a message to the console */
1228 void con_printf(int priority, char *fmt, ...)
1233 if (priority <= ((int)con_threshold.value))
1235 va_start (arglist, fmt);
1236 vsprintf (buffer, fmt, arglist);
1239 if (con_initialized)
1242 /* for (i=0; i<l; i+=CON_LINE_LEN,con_line++)
1244 memcpy(con_display, &buffer[i], min(80, l-i));
1247 if (text_console_enabled)
1249 /* Produce a sanitised version and send it to the standard output */
1268 printf("%s", buffer);