/* Returns the width of the font numbers charcter */ int DT_FontWidth(int FontNumber) { BitFont *CurrentFont; CurrentFont = DT_FontPointer(FontNumber); if(CurrentFont) return CurrentFont->CharWidth; else return 0; }
/* Returns the height of the font numbers character * returns 0 if the fontnumber was invalid */ int DT_FontHeight(int FontNumber) { BitFont *CurrentFont; CurrentFont = DT_FontPointer(FontNumber); if(CurrentFont) return CurrentFont->CharHeight; else return 0; }
/* Updates the console, draws the background and the history lines. Does not draw the Commandline */ void CON_UpdateConsole(ConsoleInformation *console) { int loop; int loop2; int Screenlines; SDL_Rect DestRect; BitFont *CurrentFont = DT_FontPointer(console->FontNumber); if(!console) return; /* Due to the Blits, the update is not very fast: So only update if it's worth it */ if(!CON_isVisible(console)) return; Screenlines = console->ConsoleSurface->h / console->FontHeight; SDL_FillRect(console->ConsoleSurface, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, console->ConsoleAlpha)); // TR2 // if(console->OutputScreen->flags & SDL_OPENGLBLIT) // SDL_SetSurfaceAlphaMod(console->ConsoleSurface, SDL_ALPHA_OPAQUE); /* draw the background image if there is one */ if(console->BackgroundImage) { DestRect.x = console->BackX; DestRect.y = console->BackY; DestRect.w = console->BackgroundImage->w; DestRect.h = console->BackgroundImage->h; SDL_BlitSurface(console->BackgroundImage, NULL, console->ConsoleSurface, &DestRect); } /* Draw the text from the back buffers, calculate in the scrollback from the user * this is a normal SDL software-mode blit, so we need to temporarily set the ColorKey * for the font, and then clear it when we're done. */ /*TR2 if((console->OutputScreen->flags & SDL_OPENGLBLIT) && (console->OutputScreen->format->BytesPerPixel > 2)) { Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels); SDL_SetColorKey(CurrentFont->FontSurface, SDL_TRUE, *pix); }*/ /* now draw text from last but second line to top loop: for every line in the history loop2: draws the scroll indicators to the line above the Commandline */ for(loop = 0; loop < Screenlines-1 && loop < console->LineBuffer - console->ConsoleScrollBack; loop++) { if(console->ConsoleScrollBack != 0 && loop == 0) for(loop2 = 0; loop2 < (console->VChars / 5) + 1; loop2++) DT_DrawText(CON_SCROLL_INDICATOR, console->ConsoleSurface, console->FontNumber, CON_CHAR_BORDER + (loop2*5*console->FontWidth), (Screenlines - loop - 2) * console->FontHeight); else DT_DrawText(console->ConsoleLines[console->ConsoleScrollBack + loop], console->ConsoleSurface, console->FontNumber, CON_CHAR_BORDER, (Screenlines - loop - 2) * console->FontHeight); } // TR2 //if(console->OutputScreen->flags & SDL_OPENGLBLIT) // SDL_SetColorKey(CurrentFont->FontSurface, 0, 0); }
/* Takes the font type, coords, and text to draw to the surface*/ void DT_DrawText(const char *string, SDL_Surface *surface, int FontType, int x, int y) { int loop; size_t characters; int current; SDL_Rect SourceRect, DestRect; BitFont *CurrentFont; CurrentFont = DT_FontPointer(FontType); /* see how many characters can fit on the screen */ if(x > surface->w || y > surface->h) return; if(strlen(string) < (surface->w - x) / CurrentFont->CharWidth) characters = strlen(string); else characters = (surface->w - x) / CurrentFont->CharWidth; DestRect.x = x; DestRect.y = y; DestRect.w = CurrentFont->CharWidth; DestRect.h = CurrentFont->CharHeight; SourceRect.y = 0; SourceRect.w = CurrentFont->CharWidth; SourceRect.h = CurrentFont->CharHeight; /* Now draw it */ for(loop = 0; loop < characters; loop++) { current = string[loop]; if (current<0 || current > 255) current = 0; /* SourceRect.x = string[loop] * CurrentFont->CharWidth; */ SourceRect.x = current * CurrentFont->CharWidth; SDL_BlitSurface(CurrentFont->FontSurface, &SourceRect, surface, &DestRect); DestRect.x += CurrentFont->CharWidth; } /* if we're in OpenGL-mode, we need to manually update after blitting. */ // TR2 skipped /*if(surface->flags & SDL_OPENGLBLIT) { DestRect.x = x; DestRect.w = (Uint16)characters * CurrentFont->CharWidth; SDL_UpdateRects(surface, 1, &DestRect); }*/ }
/* sets the transparency value for the font in question. assumes that * we're in an OpenGL context. */ void DT_SetFontAlphaGL(int FontNumber, int a) { unsigned char val; BitFont *CurrentFont; unsigned char r_targ, g_targ, b_targ; int i, imax; unsigned char *pix; /* get pointer to font */ CurrentFont = DT_FontPointer(FontNumber); if(CurrentFont == NULL) { PRINT_ERROR("Setting font alpha for non-existent font!\n"); return; } if(CurrentFont->FontSurface->format->BytesPerPixel == 2) { PRINT_ERROR("16-bit SDL surfaces do not support alpha-blending under OpenGL\n"); return; } if(a < SDL_ALPHA_TRANSPARENT) val = SDL_ALPHA_TRANSPARENT; else if(a > SDL_ALPHA_OPAQUE) val = SDL_ALPHA_OPAQUE; else val = a; /* iterate over all pixels in the font surface. For each * pixel that is (255,0,255), set its alpha channel * appropriately. */ imax = CurrentFont->FontSurface->h * (CurrentFont->FontSurface->w << 2); pix = (unsigned char *)(CurrentFont->FontSurface->pixels); r_targ = 255; /*pix[0]; */ g_targ = 0; /*pix[1]; */ b_targ = 255; /*pix[2]; */ for(i = 3; i < imax; i += 4) if(pix[i - 3] == r_targ && pix[i - 2] == g_targ && pix[i - 1] == b_targ) pix[i] = val; /* also make sure that alpha blending is disabled for the font surface. */ SDL_SetSurfaceAlphaMod(CurrentFont->FontSurface, SDL_ALPHA_OPAQUE); }
/* completely rewritten by C.Wacha */ void DrawCommandLine() { SDL_Rect rect; int x; int commandbuffer; BitFont* CurrentFont; static Uint32 NextBlinkTime = 0; /* time the consoles cursor blinks again */ static int LastCursorPos = 0; /* Last Cursor Position */ static int Blink = 0; /* Is the cursor currently blinking */ if(!Topmost) return; commandbuffer = Topmost->VChars - strlen(Topmost->Prompt) - 1; /* -1 to make cursor visible */ CurrentFont = DT_FontPointer(Topmost->FontNumber); /* calculate display offset from current cursor position */ if(Topmost->Offset < Topmost->CursorPos - commandbuffer) Topmost->Offset = Topmost->CursorPos - commandbuffer; if(Topmost->Offset > Topmost->CursorPos) Topmost->Offset = Topmost->CursorPos; /* first add prompt to visible part */ strcpy(Topmost->VCommand, Topmost->Prompt); /* then add the visible part of the command */ strncat(Topmost->VCommand, &Topmost->Command[Topmost->Offset], strlen(&Topmost->Command[Topmost->Offset])); /* now display the result */ /* once again we're drawing text, so in OpenGL context we need to temporarily set up software-mode transparency. */ /*TR2 if(Topmost->OutputScreen->flags & SDL_OPENGLBLIT) { Uint32 *pix = (Uint32 *) (CurrentFont->FontSurface->pixels); SDL_SetColorKey(CurrentFont->FontSurface, SDL_TRUE, *pix); }*/ /* first of all restore InputBackground */ rect.x = 0; rect.y = Topmost->ConsoleSurface->h - Topmost->FontHeight; rect.w = Topmost->InputBackground->w; rect.h = Topmost->InputBackground->h; SDL_BlitSurface(Topmost->InputBackground, NULL, Topmost->ConsoleSurface, &rect); /* now add the text */ DT_DrawText(Topmost->VCommand, Topmost->ConsoleSurface, Topmost->FontNumber, CON_CHAR_BORDER, Topmost->ConsoleSurface->h - Topmost->FontHeight); /* at last add the cursor check if the blink period is over */ if(SDL_GetTicks() > NextBlinkTime) { NextBlinkTime = SDL_GetTicks() + CON_BLINK_RATE; Blink = 1 - Blink; } /* check if cursor has moved - if yes display cursor anyway */ if(Topmost->CursorPos != LastCursorPos) { LastCursorPos = Topmost->CursorPos; NextBlinkTime = SDL_GetTicks() + CON_BLINK_RATE; Blink = 1; } if(Blink) { x = CON_CHAR_BORDER + Topmost->FontWidth * (Topmost->CursorPos - Topmost->Offset + strlen(Topmost->Prompt)); if(Topmost->InsMode) DT_DrawText(CON_INS_CURSOR, Topmost->ConsoleSurface, Topmost->FontNumber, x, Topmost->ConsoleSurface->h - Topmost->FontHeight); else DT_DrawText(CON_OVR_CURSOR, Topmost->ConsoleSurface, Topmost->FontNumber, x, Topmost->ConsoleSurface->h - Topmost->FontHeight); } /* TR2 if(Topmost->OutputScreen->flags & SDL_OPENGLBLIT) { SDL_SetColorKey(CurrentFont->FontSurface, 0, 0); }*/ }
/* completely rewritten by C.Wacha */ void DrawCommandLine() { SDL_Rect rect; int x; int commandbuffer; BitFont *CurrentFont; static Uint32 NextBlinkTime = 0; /* time the consoles cursor blinks again */ static int LastCursorPos = 0; /* Last Cursor Position */ static int Blink = 0; /* Is the cursor currently blinking */ if (!Topmost) return; commandbuffer = Topmost->VChars - strlen(Topmost->Prompt) - 1; /* -1 to make cursor visible */ CurrentFont = DT_FontPointer(Topmost->FontNumber); /* calculate display offset from current cursor position */ if (Topmost->Offset < Topmost->CursorPos - commandbuffer) Topmost->Offset = Topmost->CursorPos - commandbuffer; if (Topmost->Offset > Topmost->CursorPos) Topmost->Offset = Topmost->CursorPos; /* first add prompt to visible part */ strcpy(Topmost->VCommand, Topmost->Prompt); /* then add the visible part of the command */ strncat(Topmost->VCommand, &Topmost->Command[Topmost->Offset], strlen(&Topmost->Command[Topmost->Offset])); /* first of all restore InputBackground */ rect.x = 0; rect.y = Topmost->ConsoleSurface->h - Topmost->FontHeight; rect.w = Topmost->InputBackground->w; rect.h = Topmost->InputBackground->h; SDL_BlitSurface(Topmost->InputBackground, NULL, Topmost->ConsoleSurface, &rect); /* now add the text */ DT_DrawText(Topmost->VCommand, Topmost->ConsoleSurface, Topmost->FontNumber, CON_CHAR_BORDER, Topmost->ConsoleSurface->h - Topmost->FontHeight); /* at last add the cursor check if the blink period is over */ if (SDL_GetTicks() > NextBlinkTime) { NextBlinkTime = SDL_GetTicks() + CON_BLINK_RATE; Blink = 1 - Blink; } /* check if cursor has moved - if yes display cursor anyway */ if (Topmost->CursorPos != LastCursorPos) { LastCursorPos = Topmost->CursorPos; NextBlinkTime = SDL_GetTicks() + CON_BLINK_RATE; Blink = 1; } if (Blink) { x = CON_CHAR_BORDER + Topmost->FontWidth * (Topmost->CursorPos - Topmost->Offset + strlen(Topmost-> Prompt)); if (Topmost->InsMode) DT_DrawText(CON_INS_CURSOR, Topmost->ConsoleSurface, Topmost->FontNumber, x, Topmost->ConsoleSurface->h - Topmost->FontHeight); else DT_DrawText(CON_OVR_CURSOR, Topmost->ConsoleSurface, Topmost->FontNumber, x, Topmost->ConsoleSurface->h - Topmost->FontHeight); } }