2 * Written By: Garrett Banuk <mongoose@mongeese.org>
3 * Code Cleanup and heavily extended by: Clemens Wacha <reflex-2000@gmx.net>
4 * Ported to use native Descent interfaces by: Bradley Bell <btb@icculus.org>
6 * This is free, just be sure to give us credit when using it
7 * in any of your programs.
19 #include "CON_console.h"
26 #define FG_COLOR grd_curcanv->cv_font_fg_color
27 #define get_msecs() approx_fsec_to_msec(timer_get_approx_seconds())
30 /* This contains a pointer to the "topmost" console. The console that
31 * is currently taking keyboard input. */
32 static ConsoleInformation *Topmost;
34 /* Takes keys from the keyboard and inputs them to the console
35 If the event was not handled (i.e. WM events or unknown ctrl-shift
36 sequences) the function returns the event for further processing. */
37 int CON_Events(int event)
41 if(!CON_isVisible(Topmost))
44 if(event & KEY_CTRLED)
47 switch(event & ~KEY_CTRLED)
56 Clear_Command(Topmost);
59 Clear_History(Topmost);
60 CON_UpdateConsole(Topmost);
66 else if(event & KEY_ALTED)
68 //the console does not handle ALT combinations!
73 //first of all, check if the console hide key was pressed
74 if(event == Topmost->HideKey)
85 if(event & KEY_SHIFTED)
87 Topmost->ConsoleScrollBack = Topmost->LineBuffer-1;
88 CON_UpdateConsole(Topmost);
94 if(event & KEY_SHIFTED)
96 Topmost->ConsoleScrollBack = 0;
97 CON_UpdateConsole(Topmost);
103 Topmost->ConsoleScrollBack += CON_LINE_SCROLL;
104 if(Topmost->ConsoleScrollBack > Topmost->LineBuffer-1)
105 Topmost->ConsoleScrollBack = Topmost->LineBuffer-1;
107 CON_UpdateConsole(Topmost);
110 Topmost->ConsoleScrollBack -= CON_LINE_SCROLL;
111 if(Topmost->ConsoleScrollBack < 0)
112 Topmost->ConsoleScrollBack = 0;
113 CON_UpdateConsole(Topmost);
119 Command_Down(Topmost);
122 Cursor_Left(Topmost);
125 Cursor_Right(Topmost);
128 Cursor_BSpace(Topmost);
134 Topmost->InsMode = 1-Topmost->InsMode;
137 CON_TabCompletion(Topmost);
140 if(strlen(Topmost->Command) > 0) {
141 CON_NewLineCommand(Topmost);
143 // copy the input into the past commands strings
144 strcpy(Topmost->CommandLines[0], Topmost->Command);
146 // display the command including the prompt
147 CON_Out(Topmost, "%s%s", Topmost->Prompt, Topmost->Command);
148 CON_UpdateConsole(Topmost);
150 CON_Execute(Topmost, Topmost->Command);
151 //printf("Command: %s\n", Topmost->Command);
153 Clear_Command(Topmost);
154 Topmost->CommandScrollBack = -1;
163 Cursor_Add(Topmost, event);
165 Cursor_Add(Topmost, event);
174 /* CON_AlphaGL() -- sets the alpha channel of an SDL_Surface to the
175 * specified value. Preconditions: the surface in question is RGBA.
176 * 0 <= a <= 255, where 0 is transparent and 255 is opaque. */
177 void CON_AlphaGL(SDL_Surface *s, int alpha) {
182 SDL_PixelFormat *format;
183 static char errorPrinted = 0;
186 /* debugging assertions -- these slow you down, but hey, crashing sucks */
188 PRINT_ERROR("NULL Surface passed to CON_AlphaGL\n");
192 /* clamp alpha value to 0...255 */
193 if(alpha < SDL_ALPHA_TRANSPARENT)
194 val = SDL_ALPHA_TRANSPARENT;
195 else if(alpha > SDL_ALPHA_OPAQUE)
196 val = SDL_ALPHA_OPAQUE;
200 /* loop over alpha channels of each pixel, setting them appropriately. */
204 switch (format->BytesPerPixel) {
206 /* 16-bit surfaces don't seem to support alpha channels. */
209 PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL.\n");
213 /* we can do this very quickly in 32-bit mode. 24-bit is more
214 * difficult. And since 24-bit mode is reall the same as 32-bit,
215 * so it usually ends up taking this route too. Win! Unroll loop
216 * and use pointer arithmetic for extra speed. */
217 int numpixels = h * (w << 2);
218 Uint8 *pix = (Uint8 *) (s->pixels);
219 Uint8 *last = pix + numpixels;
221 if((numpixels & 0x7) == 0)
222 for(pixel = pix + 3; pixel < last; pixel += 32)
223 *pixel = *(pixel + 4) = *(pixel + 8) = *(pixel + 12) = *(pixel + 16) = *(pixel + 20) = *(pixel + 24) = *(pixel + 28) = val;
225 for(pixel = pix + 3; pixel < last; pixel += 4)
230 /* we have no choice but to do this slowly. <sigh> */
231 for(y = 0; y < h; ++y)
232 for(x = 0; x < w; ++x) {
234 /* Lock the surface for direct access to the pixels */
235 if(SDL_MUSTLOCK(s) && SDL_LockSurface(s) < 0) {
236 PRINT_ERROR("Can't lock surface: ");
237 fprintf(stderr, "%s\n", SDL_GetError());
240 pixel = DT_GetPixel(s, x, y);
243 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
244 pixel = SDL_MapRGBA(format, r, g, b, val);
245 SDL_GetRGBA(pixel, format, &r, &g, &b, &a);
246 DT_PutPixel(s, x, y, pixel);
248 /* unlock surface again */
250 SDL_UnlockSurface(s);
258 /* Updates the console buffer */
259 void CON_UpdateConsole(ConsoleInformation *console) {
263 grs_canvas *canv_save;
269 /* Due to the Blits, the update is not very fast: So only update if it's worth it */
270 if(!CON_isVisible(console))
273 Screenlines = console->ConsoleSurface->cv_h / (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h);
275 canv_save = grd_curcanv;
276 gr_set_current_canvas(console->ConsoleSurface);
279 SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha));
285 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
286 SDL_SetAlpha(console->ConsoleSurface, 0, SDL_ALPHA_OPAQUE);
289 /* draw the background image if there is one */
290 if(console->BackgroundImage)
291 gr_bitmap(0, 0, console->BackgroundImage);
293 /* Draw the text from the back buffers, calculate in the scrollback from the user
294 * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey
295 * for the font, and then clear it when we're done.
298 if((console->OutputScreen->flags & SDL_OPENGLBLIT) && (console->OutputScreen->format->BytesPerPixel > 2)) {
299 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
300 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
304 //now draw text from last but second line to top
305 for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) {
306 if(console->ConsoleScrollBack != 0 && loop == 0)
307 for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++)
309 orig_color = FG_COLOR;
310 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);
311 FG_COLOR = orig_color;
315 orig_color = FG_COLOR;
316 gr_string(CON_CHAR_BORDER, (Screenlines - loop - 2) * (CON_LINE_SPACE + console->ConsoleSurface->cv_font->ft_h), console->ConsoleLines[console->ConsoleScrollBack + loop]);
317 FG_COLOR = orig_color;
321 gr_set_current_canvas(canv_save);
324 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
325 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
329 void CON_UpdateOffset(ConsoleInformation* console) {
333 switch(console->Visible) {
335 console->RaiseOffset -= CON_OPENCLOSE_SPEED;
336 if(console->RaiseOffset <= 0) {
337 console->RaiseOffset = 0;
338 console->Visible = CON_CLOSED;
342 console->RaiseOffset += CON_OPENCLOSE_SPEED;
343 if(console->RaiseOffset >= console->ConsoleSurface->cv_h) {
344 console->RaiseOffset = console->ConsoleSurface->cv_h;
345 console->Visible = CON_OPEN;
354 /* Draws the console buffer to the screen if the console is "visible" */
355 void CON_DrawConsole(ConsoleInformation *console) {
356 grs_canvas *canv_save;
362 /* only draw if console is visible: here this means, that the console is not CON_CLOSED */
363 if(console->Visible == CON_CLOSED)
366 /* Update the scrolling offset */
367 CON_UpdateOffset(console);
369 /* Update the command line since it has a blinking cursor */
373 /* before drawing, make sure the alpha channel of the console surface is set
374 * properly. (sigh) I wish we didn't have to do this every frame... */
375 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
376 CON_AlphaGL(console->ConsoleSurface, console->ConsoleAlpha);
379 canv_save = grd_curcanv;
380 gr_set_current_canvas(&console->OutputScreen->sc_canvas);
382 clip = gr_create_sub_bitmap(&console->ConsoleSurface->cv_bitmap, 0, console->ConsoleSurface->cv_h - console->RaiseOffset, console->ConsoleSurface->cv_w, console->RaiseOffset);
384 gr_bitmap(console->DispX, console->DispY, clip);
385 gr_free_sub_bitmap(clip);
388 if(console->OutputScreen->flags & SDL_OPENGLBLIT)
389 SDL_UpdateRects(console->OutputScreen, 1, &DestRect);
392 gr_set_current_canvas(canv_save);
396 /* Initializes the console */
397 ConsoleInformation *CON_Init(grs_font *Font, grs_screen *DisplayScreen, int lines, int x, int y, int w, int h)
400 ConsoleInformation *newinfo;
403 /* Create a new console struct and init it. */
404 if((newinfo = (ConsoleInformation *) d_malloc(sizeof(ConsoleInformation))) == NULL) {
405 //PRINT_ERROR("Could not allocate the space for a new console info struct.\n");
408 newinfo->Visible = CON_CLOSED;
409 newinfo->RaiseOffset = 0;
410 newinfo->ConsoleLines = NULL;
411 newinfo->CommandLines = NULL;
412 newinfo->TotalConsoleLines = 0;
413 newinfo->ConsoleScrollBack = 0;
414 newinfo->TotalCommands = 0;
415 newinfo->BackgroundImage = NULL;
417 newinfo->ConsoleAlpha = SDL_ALPHA_OPAQUE;
420 newinfo->InsMode = 1;
421 newinfo->CursorPos = 0;
422 newinfo->CommandScrollBack = 0;
423 newinfo->OutputScreen = DisplayScreen;
424 newinfo->Prompt = CON_DEFAULT_PROMPT;
425 newinfo->HideKey = CON_DEFAULT_HIDEKEY;
427 CON_SetExecuteFunction(newinfo, Default_CmdFunction);
428 CON_SetTabCompletion(newinfo, Default_TabFunction);
430 /* make sure that the size of the console is valid */
431 if(w > newinfo->OutputScreen->sc_w || w < Font->ft_w * 32)
432 w = newinfo->OutputScreen->sc_w;
433 if(h > newinfo->OutputScreen->sc_h || h < Font->ft_h)
434 h = newinfo->OutputScreen->sc_h;
435 if(x < 0 || x > newinfo->OutputScreen->sc_w - w)
439 if(y < 0 || y > newinfo->OutputScreen->sc_h - h)
444 /* load the console surface */
445 newinfo->ConsoleSurface = gr_create_canvas(w, h);
447 /* Load the consoles font */
449 grs_canvas *canv_save;
451 canv_save = grd_curcanv;
452 gr_set_current_canvas(newinfo->ConsoleSurface);
453 gr_set_curfont(Font);
454 gr_set_fontcolor(gr_getcolor(63,63,63), -1);
455 gr_set_current_canvas(canv_save);
459 /* Load the dirty rectangle for user input */
460 newinfo->InputBackground = gr_create_bitmap(w, newinfo->ConsoleSurface->cv_font->ft_h);
462 SDL_FillRect(newinfo->InputBackground, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
465 /* calculate the number of visible characters in the command line */
466 newinfo->VChars = (w - CON_CHAR_BORDER) / newinfo->ConsoleSurface->cv_font->ft_w;
467 if(newinfo->VChars > CON_CHARS_PER_LINE)
468 newinfo->VChars = CON_CHARS_PER_LINE;
470 /* We would like to have a minumum # of lines to guarentee we don't create a memory error */
471 if(h / (CON_LINE_SPACE + newinfo->ConsoleSurface->cv_font->ft_h) > lines)
472 newinfo->LineBuffer = h / (CON_LINE_SPACE + newinfo->ConsoleSurface->cv_font->ft_h);
474 newinfo->LineBuffer = lines;
477 newinfo->ConsoleLines = (char **)d_malloc(sizeof(char *) * newinfo->LineBuffer);
478 newinfo->CommandLines = (char **)d_malloc(sizeof(char *) * newinfo->LineBuffer);
479 for(loop = 0; loop <= newinfo->LineBuffer - 1; loop++) {
480 newinfo->ConsoleLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
481 newinfo->CommandLines[loop] = (char *)d_calloc(CON_CHARS_PER_LINE, sizeof(char));
483 memset(newinfo->Command, 0, CON_CHARS_PER_LINE);
484 memset(newinfo->LCommand, 0, CON_CHARS_PER_LINE);
485 memset(newinfo->RCommand, 0, CON_CHARS_PER_LINE);
486 memset(newinfo->VCommand, 0, CON_CHARS_PER_LINE);
489 CON_Out(newinfo, "Console initialised.");
490 CON_NewLineConsole(newinfo);
491 //CON_ListCommands(newinfo);
496 /* Makes the console visible */
497 void CON_Show(ConsoleInformation *console) {
499 console->Visible = CON_OPENING;
500 CON_UpdateConsole(console);
504 /* Hides the console (make it invisible) */
505 void CON_Hide(ConsoleInformation *console) {
507 console->Visible = CON_CLOSING;
510 /* tells wether the console is visible or not */
511 int CON_isVisible(ConsoleInformation *console) {
514 return((console->Visible == CON_OPEN) || (console->Visible == CON_OPENING));
517 /* Frees all the memory loaded by the console */
518 void CON_Destroy(ConsoleInformation *console) {
522 /* Frees all the memory loaded by the console */
523 void CON_Free(ConsoleInformation *console) {
529 //CON_DestroyCommands();
530 for(i = 0; i <= console->LineBuffer - 1; i++) {
531 d_free(console->ConsoleLines[i]);
532 d_free(console->CommandLines[i]);
534 d_free(console->ConsoleLines);
535 d_free(console->CommandLines);
537 console->ConsoleLines = NULL;
538 console->CommandLines = NULL;
540 gr_free_canvas(console->ConsoleSurface);
541 console->ConsoleSurface = NULL;
543 if (console->BackgroundImage)
544 gr_free_bitmap(console->BackgroundImage);
545 console->BackgroundImage = NULL;
547 gr_free_bitmap(console->InputBackground);
548 console->InputBackground = NULL;
554 /* Increments the console lines */
555 void CON_NewLineConsole(ConsoleInformation *console) {
562 temp = console->ConsoleLines[console->LineBuffer - 1];
564 for(loop = console->LineBuffer - 1; loop > 0; loop--)
565 console->ConsoleLines[loop] = console->ConsoleLines[loop - 1];
567 console->ConsoleLines[0] = temp;
569 memset(console->ConsoleLines[0], 0, CON_CHARS_PER_LINE);
570 if(console->TotalConsoleLines < console->LineBuffer - 1)
571 console->TotalConsoleLines++;
573 //Now adjust the ConsoleScrollBack
574 //dont scroll if not at bottom
575 if(console->ConsoleScrollBack != 0)
576 console->ConsoleScrollBack++;
578 if(console->ConsoleScrollBack > console->LineBuffer-1)
579 console->ConsoleScrollBack = console->LineBuffer-1;
584 /* Increments the command lines */
585 void CON_NewLineCommand(ConsoleInformation *console) {
592 temp = console->CommandLines[console->LineBuffer - 1];
595 for(loop = console->LineBuffer - 1; loop > 0; loop--)
596 console->CommandLines[loop] = console->CommandLines[loop - 1];
598 console->CommandLines[0] = temp;
600 memset(console->CommandLines[0], 0, CON_CHARS_PER_LINE);
601 if(console->TotalCommands < console->LineBuffer - 1)
602 console->TotalCommands++;
605 /* Draws the command line the user is typing in to the screen */
606 /* completely rewritten by C.Wacha */
607 void DrawCommandLine() {
611 grs_font* CurrentFont;
613 static unsigned int LastBlinkTime = 0; /* Last time the consoles cursor blinked */
614 static int LastCursorPos = 0; // Last Cursor Position
615 static int Blink = 0; /* Is the cursor currently blinking */
616 grs_canvas *canv_save;
622 commandbuffer = Topmost->VChars - strlen(Topmost->Prompt)-1; // -1 to make cursor visible
625 CurrentFont = Topmost->ConsoleSurface->cv_font;
628 //Concatenate the left and right side to command
629 strcpy(Topmost->Command, Topmost->LCommand);
630 strncat(Topmost->Command, Topmost->RCommand, strlen(Topmost->RCommand));
632 //calculate display offset from current cursor position
633 if(Topmost->Offset < Topmost->CursorPos - commandbuffer)
634 Topmost->Offset = Topmost->CursorPos - commandbuffer;
635 if(Topmost->Offset > Topmost->CursorPos)
636 Topmost->Offset = Topmost->CursorPos;
638 //first add prompt to visible part
639 strcpy(Topmost->VCommand, Topmost->Prompt);
641 //then add the visible part of the command
642 strncat(Topmost->VCommand, &Topmost->Command[Topmost->Offset], strlen(&Topmost->Command[Topmost->Offset]));
644 //now display the result
647 //once again we're drawing text, so in OpenGL context we need to temporarily set up
648 //software-mode transparency.
649 if(Topmost->OutputScreen->flags & SDL_OPENGLBLIT) {
650 Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels);
651 SDL_SetColorKey(CurrentFont->FontSurface, SDL_SRCCOLORKEY, *pix);
655 canv_save = grd_curcanv;
656 gr_set_current_canvas(Topmost->ConsoleSurface);
658 //first of all restore InputBackground
659 gr_bitmap(0, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, Topmost->InputBackground);
662 orig_color = FG_COLOR;
663 gr_string(CON_CHAR_BORDER, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, Topmost->VCommand);
664 FG_COLOR = orig_color;
666 //at last add the cursor
667 //check if the blink period is over
668 if(get_msecs() > LastBlinkTime) {
669 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
676 //check if cursor has moved - if yes display cursor anyway
677 if(Topmost->CursorPos != LastCursorPos) {
678 LastCursorPos = Topmost->CursorPos;
679 LastBlinkTime = get_msecs() + CON_BLINK_RATE;
684 int prompt_width, cmd_width, h, w;
686 gr_get_string_size(Topmost->Prompt, &prompt_width, &h, &w);
687 gr_get_string_size(Topmost->LCommand + Topmost->Offset, &cmd_width, &h, &w);
688 x = CON_CHAR_BORDER + prompt_width + cmd_width;
689 orig_color = FG_COLOR;
691 gr_string(x, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, CON_INS_CURSOR);
693 gr_string(x, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, CON_OVR_CURSOR);
694 FG_COLOR = orig_color;
697 gr_set_current_canvas(canv_save);
701 if(Topmost->OutputScreen->flags & SDL_OPENGLBLIT) {
702 SDL_SetColorKey(CurrentFont->FontSurface, 0, 0);
708 # define vsnprintf _vsnprintf
711 /* Outputs text to the console (in game), up to CON_CHARS_PER_LINE chars can be entered */
712 void CON_Out(ConsoleInformation *console, const char *str, ...) {
714 //keep some space free for stuff like CON_Out(console, "blablabla %s", console->Command);
715 char temp[CON_CHARS_PER_LINE + 128];
721 va_start(marker, str);
722 vsnprintf(temp, CON_CHARS_PER_LINE + 127, str, marker);
727 //temp now contains the complete string we want to output
728 // the only problem is that temp is maybe longer than the console
729 // width so we have to cut it into several pieces
731 if(console->ConsoleLines) {
732 while(strlen(ptemp) > console->VChars) {
733 CON_NewLineConsole(console);
734 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
735 console->ConsoleLines[0][console->VChars] = '\0';
736 ptemp = &ptemp[console->VChars];
738 CON_NewLineConsole(console);
739 strncpy(console->ConsoleLines[0], ptemp, console->VChars);
740 console->ConsoleLines[0][console->VChars] = '\0';
741 CON_UpdateConsole(console);
744 /* And print to stdout */
745 //printf("%s\n", temp);
750 /* Sets the alpha level of the console, 0 turns off alpha blending */
751 void CON_Alpha(ConsoleInformation *console, unsigned char alpha) {
755 /* store alpha as state! */
756 console->ConsoleAlpha = alpha;
758 if((console->OutputScreen->flags & SDL_OPENGLBLIT) == 0) {
760 SDL_SetAlpha(console->ConsoleSurface, 0, alpha);
762 SDL_SetAlpha(console->ConsoleSurface, SDL_SRCALPHA, alpha);
765 // CON_UpdateConsole(console);
770 /* Adds background image to the console, scaled to size of console*/
771 int CON_Background(ConsoleInformation *console, grs_bitmap *image)
776 /* Free the background from the console */
778 if (console->BackgroundImage)
779 gr_free_bitmap(console->BackgroundImage);
780 console->BackgroundImage = NULL;
782 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
787 /* Load a new background */
788 if (console->BackgroundImage)
789 gr_free_bitmap(console->BackgroundImage);
790 console->BackgroundImage = gr_create_bitmap(console->ConsoleSurface->cv_w, console->ConsoleSurface->cv_h);
791 gr_bitmap_scale_to(image, console->BackgroundImage);
794 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
796 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);
801 /* Sets font info for the console */
802 void CON_Font(ConsoleInformation *console, grs_font *font, int fg, int bg)
804 grs_canvas *canv_save;
806 canv_save = grd_curcanv;
807 gr_set_current_canvas(console->ConsoleSurface);
808 gr_set_curfont(font);
809 gr_set_fontcolor(fg, bg);
810 gr_set_current_canvas(canv_save);
813 /* takes a new x and y of the top left of the console window */
814 void CON_Position(ConsoleInformation *console, int x, int y) {
818 if(x < 0 || x > console->OutputScreen->sc_w - console->ConsoleSurface->cv_w)
823 if(y < 0 || y > console->OutputScreen->sc_h - console->ConsoleSurface->cv_h)
829 void gr_init_bitmap_alloc( grs_bitmap *bm, int mode, int x, int y, int w, int h, int bytesperline);
830 /* resizes the console, has to reset alot of stuff
831 * returns 1 on error */
832 int CON_Resize(ConsoleInformation *console, int x, int y, int w, int h)
837 /* make sure that the size of the console is valid */
838 if(w > console->OutputScreen->sc_w || w < console->ConsoleSurface->cv_font->ft_w * 32)
839 w = console->OutputScreen->sc_w;
840 if(h > console->OutputScreen->sc_h || h < console->ConsoleSurface->cv_font->ft_h)
841 h = console->OutputScreen->sc_h;
842 if(x < 0 || x > console->OutputScreen->sc_w - w)
846 if(y < 0 || y > console->OutputScreen->sc_h - h)
851 /* resize console surface */
852 gr_free_bitmap_data(&console->ConsoleSurface->cv_bitmap);
853 gr_init_bitmap_alloc(&console->ConsoleSurface->cv_bitmap, BM_LINEAR, 0, 0, w, h, w);
855 /* Load the dirty rectangle for user input */
856 gr_free_bitmap(console->InputBackground);
857 console->InputBackground = gr_create_bitmap(w, console->ConsoleSurface->cv_font->ft_h);
859 /* Now reset some stuff dependent on the previous size */
860 console->ConsoleScrollBack = 0;
862 /* Reload the background image (for the input text area) in the console */
863 if(console->BackgroundImage) {
865 SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));
867 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);
871 /* restore the alpha level */
872 CON_Alpha(console, console->ConsoleAlpha);
877 /* Transfers the console to another screen surface, and adjusts size */
878 int CON_Transfer(ConsoleInformation *console, grs_screen *new_outputscreen, int x, int y, int w, int h)
883 console->OutputScreen = new_outputscreen;
885 return(CON_Resize(console, x, y, w, h));
888 /* Sets the topmost console for input */
889 void CON_Topmost(ConsoleInformation *console) {
890 grs_canvas *canv_save;
896 // Make sure the blinking cursor is gone
898 canv_save = grd_curcanv;
899 gr_set_current_canvas(Topmost->ConsoleSurface);
901 gr_bitmap(0, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, Topmost->InputBackground);
902 orig_color = FG_COLOR;
903 gr_string(CON_CHAR_BORDER, Topmost->ConsoleSurface->cv_h - Topmost->ConsoleSurface->cv_font->ft_h, Topmost->VCommand);
904 FG_COLOR = orig_color;
906 gr_set_current_canvas(canv_save);
911 /* Sets the Prompt for console */
912 void CON_SetPrompt(ConsoleInformation *console, char* newprompt) {
916 //check length so we can still see at least 1 char :-)
917 if(strlen(newprompt) < console->VChars)
918 console->Prompt = d_strdup(newprompt);
920 CON_Out(console, "prompt too long. (max. %i chars)", console->VChars - 1);
923 /* Sets the key that deactivates (hides) the console. */
924 void CON_SetHideKey(ConsoleInformation *console, int key) {
926 console->HideKey = key;
929 /* Executes the command entered */
930 void CON_Execute(ConsoleInformation *console, char* command) {
932 console->CmdFunction(console, command);
935 void CON_SetExecuteFunction(ConsoleInformation *console, void(*CmdFunction)(ConsoleInformation *console2, char* command)) {
937 console->CmdFunction = CmdFunction;
940 void Default_CmdFunction(ConsoleInformation *console, char* command) {
941 CON_Out(console, " No CommandFunction registered");
942 CON_Out(console, " use 'CON_SetExecuteFunction' to register one");
943 CON_Out(console, " ");
944 CON_Out(console, "Unknown Command \"%s\"", command);
947 void CON_SetTabCompletion(ConsoleInformation *console, char*(*TabFunction)(char* command)) {
949 console->TabFunction = TabFunction;
952 void CON_TabCompletion(ConsoleInformation *console) {
959 command = d_strdup(console->LCommand);
960 command = console->TabFunction(command);
963 return; //no tab completion took place so return silently
966 if(j > CON_CHARS_PER_LINE - 2)
967 j = CON_CHARS_PER_LINE-1;
969 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
970 console->CursorPos = 0;
972 for(i = 0; i < j; i++) {
973 console->CursorPos++;
974 console->LCommand[i] = command[i];
976 //add a trailing space
977 console->CursorPos++;
978 console->LCommand[j] = ' ';
979 console->LCommand[j+1] = '\0';
982 char* Default_TabFunction(char* command) {
983 CON_Out(Topmost, " No TabFunction registered");
984 CON_Out(Topmost, " use 'CON_SetTabCompletion' to register one");
985 CON_Out(Topmost, " ");
989 void Cursor_Left(ConsoleInformation *console) {
990 char temp[CON_CHARS_PER_LINE];
992 if(Topmost->CursorPos > 0) {
993 Topmost->CursorPos--;
994 strcpy(temp, Topmost->RCommand);
995 strcpy(Topmost->RCommand, &Topmost->LCommand[strlen(Topmost->LCommand)-1]);
996 strcat(Topmost->RCommand, temp);
997 Topmost->LCommand[strlen(Topmost->LCommand)-1] = '\0';
998 //CON_Out(Topmost, "L:%s, R:%s", Topmost->LCommand, Topmost->RCommand);
1002 void Cursor_Right(ConsoleInformation *console) {
1003 char temp[CON_CHARS_PER_LINE];
1005 if(Topmost->CursorPos < strlen(Topmost->Command)) {
1006 Topmost->CursorPos++;
1007 strncat(Topmost->LCommand, Topmost->RCommand, 1);
1008 strcpy(temp, Topmost->RCommand);
1009 strcpy(Topmost->RCommand, &temp[1]);
1010 //CON_Out(Topmost, "L:%s, R:%s", Topmost->LCommand, Topmost->RCommand);
1014 void Cursor_Home(ConsoleInformation *console) {
1015 char temp[CON_CHARS_PER_LINE];
1017 Topmost->CursorPos = 0;
1018 strcpy(temp, Topmost->RCommand);
1019 strcpy(Topmost->RCommand, Topmost->LCommand);
1020 strncat(Topmost->RCommand, temp, strlen(temp));
1021 memset(Topmost->LCommand, 0, CON_CHARS_PER_LINE);
1024 void Cursor_End(ConsoleInformation *console) {
1025 Topmost->CursorPos = strlen(Topmost->Command);
1026 strncat(Topmost->LCommand, Topmost->RCommand, strlen(Topmost->RCommand));
1027 memset(Topmost->RCommand, 0, CON_CHARS_PER_LINE);
1030 void Cursor_Del(ConsoleInformation *console) {
1031 char temp[CON_CHARS_PER_LINE];
1033 if(strlen(Topmost->RCommand) > 0) {
1034 strcpy(temp, Topmost->RCommand);
1035 strcpy(Topmost->RCommand, &temp[1]);
1039 void Cursor_BSpace(ConsoleInformation *console) {
1040 if(Topmost->CursorPos > 0) {
1041 Topmost->CursorPos--;
1043 if(Topmost->Offset < 0)
1044 Topmost->Offset = 0;
1045 Topmost->LCommand[strlen(Topmost->LCommand)-1] = '\0';
1049 void Cursor_Add(ConsoleInformation *console, int event)
1051 if(strlen(Topmost->Command) < CON_CHARS_PER_LINE - 1)
1053 Topmost->CursorPos++;
1054 Topmost->LCommand[strlen(Topmost->LCommand)] = key_to_ascii(event);
1055 Topmost->LCommand[strlen(Topmost->LCommand)] = '\0';
1059 void Clear_Command(ConsoleInformation *console) {
1060 Topmost->CursorPos = 0;
1061 memset(Topmost->VCommand, 0, CON_CHARS_PER_LINE);
1062 memset(Topmost->Command, 0, CON_CHARS_PER_LINE);
1063 memset(Topmost->LCommand, 0, CON_CHARS_PER_LINE);
1064 memset(Topmost->RCommand, 0, CON_CHARS_PER_LINE);
1067 void Clear_History(ConsoleInformation *console) {
1070 for(loop = 0; loop <= console->LineBuffer - 1; loop++)
1071 memset(console->ConsoleLines[loop], 0, CON_CHARS_PER_LINE);
1074 void Command_Up(ConsoleInformation *console) {
1075 if(console->CommandScrollBack < console->TotalCommands - 1) {
1076 /* move back a line in the command strings and copy the command to the current input string */
1077 console->CommandScrollBack++;
1078 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1079 console->Offset = 0;
1080 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1081 console->CursorPos = strlen(console->CommandLines[console->CommandScrollBack]);
1082 CON_UpdateConsole(console);
1086 void Command_Down(ConsoleInformation *console) {
1087 if(console->CommandScrollBack > -1) {
1088 /* move forward a line in the command strings and copy the command to the current input string */
1089 console->CommandScrollBack--;
1090 memset(console->RCommand, 0, CON_CHARS_PER_LINE);
1091 memset(console->LCommand, 0, CON_CHARS_PER_LINE);
1092 console->Offset = 0;
1093 if(console->CommandScrollBack > -1)
1094 strcpy(console->LCommand, console->CommandLines[console->CommandScrollBack]);
1095 console->CursorPos = strlen(console->LCommand);
1096 CON_UpdateConsole(console);