Console::Console(const std::string& font, Renderer& r) { const auto screen = GetSdlSurface(r); mSdlConsole = CON_Init(font.c_str(), screen, LINE_COUNT, { 0, 0, static_cast<Uint16>(screen->w), static_cast<Uint16>(screen->h * .75f) }); if (!mSdlConsole) { throw "Unable to create console."; } CON_Alpha(mSdlConsole, ALPHA_LEVEL); CON_Topmost(mSdlConsole); CON_SetPrompt(mSdlConsole, "> "); CON_SetHideKey(mSdlConsole, SDLK_BACKQUOTE); CON_SetExecuteFunction(mSdlConsole, ConsoleCmdHandler); CON_SetTabCompletion(mSdlConsole, ConsoleTabHandler); r.RegisterPostRenderHook([&](void*){ CON_DrawConsole(mSdlConsole); }); gConsoleMap[mSdlConsole] = this; }
ConsoleInformation* RC_ConsoleInit(const char *font_name, SDL_Surface *display_screen, int lines, SDL_Rect rect) { ConsoleInformation *ci = CON_Init(font_name, display_screen, lines, rect); CON_Position(ci, 0, 0); CON_Alpha(ci, 160); CON_Topmost(ci); CON_SetPrompt(ci, "-->"); CON_SetHideKey(ci, 96 /*tilde*/); return ci; }
/* resizes the console, has to reset alot of stuff * returns 1 on error */ int CON_Resize(ConsoleInformation *console, SDL_Rect rect, SDL_Surface* displayScreen) { SDL_Surface *Temp; SDL_Rect backgroundsrc, backgrounddest; if(!console) return 1; /* make sure that the size of the console is valid */ if(rect.w > console->OutputScreen->w || rect.w < console->FontWidth * 32) rect.w = console->OutputScreen->w; if(rect.h > console->OutputScreen->h || rect.h < console->FontHeight) rect.h = console->OutputScreen->h; if(rect.x < 0 || rect.x > console->OutputScreen->w - rect.w) console->DispX = 0; else console->DispX = rect.x; if(rect.y < 0 || rect.y > console->OutputScreen->h - rect.h) console->DispY = 0; else console->DispY = rect.y; /* load the console surface */ SDL_FreeSurface(console->ConsoleSurface); Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, console->OutputScreen->format->BitsPerPixel, 0, 0, 0, 0); if(Temp == NULL) { PRINT_ERROR("Couldn't create the console->ConsoleSurface\n"); return 1; } //console->ConsoleSurface = SDL_DisplayFormat(Temp); console->ConsoleSurface = SDL_ConvertSurfaceFormat(Temp, displayScreen->format->format, displayScreen->flags); SDL_FreeSurface(Temp); /* Load the dirty rectangle for user input */ SDL_FreeSurface(console->InputBackground); Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, console->FontHeight, console->OutputScreen->format->BitsPerPixel, 0, 0, 0, 0); if(Temp == NULL) { PRINT_ERROR("Couldn't create the input background\n"); return 1; } //console->InputBackground = SDL_DisplayFormat(Temp); console->InputBackground = SDL_ConvertSurfaceFormat(Temp, displayScreen->format->format, displayScreen->flags); SDL_FreeSurface(Temp); /* Now reset some stuff dependent on the previous size */ console->ConsoleScrollBack = 0; /* Reload the background image (for the input text area) in the console */ if(console->BackgroundImage) { backgroundsrc.x = 0; backgroundsrc.y = console->ConsoleSurface->h - console->FontHeight - console->BackY; backgroundsrc.w = console->BackgroundImage->w; backgroundsrc.h = console->InputBackground->h; backgrounddest.x = console->BackX; backgrounddest.y = 0; backgrounddest.w = console->BackgroundImage->w; backgrounddest.h = console->FontHeight; SDL_FillRect(console->InputBackground, NULL, SDL_MapRGBA(console->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE)); SDL_BlitSurface(console->BackgroundImage, &backgroundsrc, console->InputBackground, &backgrounddest); } /* restore the alpha level */ CON_Alpha(console, console->ConsoleAlpha); /* re-calculate the number of visible characters in the command line */ console->VChars = (rect.w - CON_CHAR_BORDER) / console->FontWidth; if(console->VChars > CON_CHARS_PER_LINE) console->VChars = CON_CHARS_PER_LINE; CON_UpdateConsole(console); return 0; }