Example #1
0
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;
}
Example #2
0
/* Initializes the console */
ConsoleInformation *CON_Init(SDL_Surface *Surface, SDL_Surface *DisplayScreen, int lines, SDL_Rect rect) {
    int loop;
    SDL_Surface *Temp;
    ConsoleInformation *newinfo;


    /* Create a new console struct and init it. */
    if((newinfo = (ConsoleInformation *) malloc(sizeof(ConsoleInformation))) == NULL) {
        PRINT_ERROR("Could not allocate the space for a new console info struct.\n");
        return NULL;
    }
    newinfo->Visible = CON_CLOSED;
    newinfo->WasUnicode = 0;
    newinfo->RaiseOffset = 0;
    newinfo->ConsoleLines = NULL;
    newinfo->CommandLines = NULL;
    newinfo->TotalConsoleLines = 0;
    newinfo->ConsoleScrollBack = 0;
    newinfo->TotalCommands = 0;
    newinfo->BackgroundImage = NULL;
    newinfo->ConsoleAlpha = SDL_ALPHA_OPAQUE;
    newinfo->Offset = 0;
    newinfo->InsMode = 1;
    newinfo->CursorPos = 0;
    newinfo->CommandScrollBack = 0;
    newinfo->OutputScreen = DisplayScreen;
    newinfo->Prompt = CON_DEFAULT_PROMPT;
    newinfo->HideKey = CON_DEFAULT_HIDEKEY;

    CON_SetExecuteFunction(newinfo, Default_CmdFunction);
    CON_SetTabCompletion(newinfo, Default_TabFunction);

    /* Load the consoles font */
    if (-1 == (newinfo->FontNumber = DT_LoadFontFromSurface(Surface, TRANS_FONT, DisplayScreen))) {
        PRINT_ERROR("Could not load the font ");
        PRINT_ERROR("for the console!");
        return NULL;
    }

    newinfo->FontHeight = DT_FontHeight(newinfo->FontNumber);
    newinfo->FontWidth = DT_FontWidth(newinfo->FontNumber);

    /* make sure that the size of the console is valid */
    if(rect.w > newinfo->OutputScreen->w || rect.w < newinfo->FontWidth * 32)
        rect.w = newinfo->OutputScreen->w;
    if(rect.h > newinfo->OutputScreen->h || rect.h < newinfo->FontHeight)
        rect.h = newinfo->OutputScreen->h;
    if(rect.x < 0 || rect.x > newinfo->OutputScreen->w - rect.w)
        newinfo->DispX = 0;
    else
        newinfo->DispX = rect.x;
    if(rect.y < 0 || rect.y > newinfo->OutputScreen->h - rect.h)
        newinfo->DispY = 0;
    else
        newinfo->DispY = rect.y;

    /* load the console surface */
    Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, newinfo->OutputScreen->format->BitsPerPixel, 0, 0, 0, 0);
    if(Temp == NULL) {
        PRINT_ERROR("Couldn't create the ConsoleSurface\n");
        return NULL;
    }
    newinfo->ConsoleSurface = SDL_ConvertSurfaceFormat(Temp, DisplayScreen->format->format, DisplayScreen->flags);
    SDL_FreeSurface(Temp);
    SDL_FillRect(newinfo->ConsoleSurface, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, newinfo->ConsoleAlpha));

    /* Load the dirty rectangle for user input */
    Temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, newinfo->FontHeight, newinfo->OutputScreen->format->BitsPerPixel, 0, 0, 0, SDL_ALPHA_OPAQUE);
    if(Temp == NULL) {
        PRINT_ERROR("Couldn't create the InputBackground\n");
        return NULL;
    }
    newinfo->InputBackground = SDL_ConvertSurfaceFormat(Temp, DisplayScreen->format->format, DisplayScreen->flags);
    SDL_FreeSurface(Temp);
    SDL_FillRect(newinfo->InputBackground, NULL, SDL_MapRGBA(newinfo->ConsoleSurface->format, 0, 0, 0, SDL_ALPHA_OPAQUE));

    /* calculate the number of visible characters in the command line */
    newinfo->VChars = (rect.w - CON_CHAR_BORDER) / newinfo->FontWidth;
    if(newinfo->VChars > CON_CHARS_PER_LINE)
        newinfo->VChars = CON_CHARS_PER_LINE;

    /* deprecated! Memory errors disabled by C.Wacha :-)
       We would like to have a minumum # of lines to guarentee we don't create a memory error */
    /*
    if(rect.h / newinfo->FontHeight > lines)
    	newinfo->LineBuffer = rect.h / newinfo->FontHeight;
    else
    	newinfo->LineBuffer = lines;
    */
    newinfo->LineBuffer = lines;

    newinfo->ConsoleLines = (char **)malloc(sizeof(char *) * newinfo->LineBuffer);
    newinfo->CommandLines = (char **)malloc(sizeof(char *) * newinfo->LineBuffer);
    for(loop = 0; loop <= newinfo->LineBuffer - 1; loop++) {
        newinfo->ConsoleLines[loop] = (char *)calloc(CON_CHARS_PER_LINE+1, sizeof(char));
        newinfo->CommandLines[loop] = (char *)calloc(CON_CHARS_PER_LINE+1, sizeof(char));
    }
    memset(newinfo->Command, 0, CON_CHARS_PER_LINE+1);
    memset(newinfo->LCommand, 0, CON_CHARS_PER_LINE+1);
    memset(newinfo->RCommand, 0, CON_CHARS_PER_LINE+1);
    memset(newinfo->VCommand, 0, CON_CHARS_PER_LINE+1);


    CON_Out(newinfo, "Console initialised.");
    CON_NewLineConsole(newinfo);

    return newinfo;
}