Search code examples
csdl-ttf

How to print out text (SDL_TTF) from an array?


I posted parts of my code that I think are relevent. I'm trying to draw out some text via SDL_TTF for my menu. I'm getting a string of char from the server everytime i clicked on a button. Something like "I1P1I2P1I3P1I4P1I5P1I6P1I7P1I8P1I9P1" "I1" means lobby 1 and P1 means 1 player is connected. However i only want to print out "I1" and then have bunch of space say 200 pixels and then print out "P1" then jump to next line to print "I2" and "P1". I tried the text warpping but it's ignoreing it and printing out the whole line of text. Secondly how can i go about to print empty spaces between "I1" and "P1". Is there an easier/efficient way to go about printing text via SDL_TTF from an array?

typedef struct
{
    Menu menu;

    SDL_Texture     *label;
    SDL_Renderer    *rendererMenu;
    TTF_Font        *font;
    char            *lobbyResponse;
}   MenuState;

void dispayText(char *text, MenuState *menu)
{
    SDL_Color color         = {255, 255, 255, 255};
    SDL_Surface *surface    = TTF_RenderText_Blended_Wrapped(menu->font, text, color, 4);
    menu->label             = SDL_CreateTextureFromSurface(menu->rendererMenu, surface);
    menu->menu.labelW       = surface->w;
    menu->menu.labelH       = surface->h;
    //pos.x = x;
    //pos.y = y;
    SDL_FreeSurface(surface);
}



int processEventsMenu(SDL_Window *window, MenuState *menu, TCPsocket *tcpsock)
{
    SDL_Event ev;
    menu->lobbyResponse = malloc(sizeof(char[1024])); // <- moved it here for clarity

    while (SDL_PollEvent(&ev))
    {
        switch(ev.type)
        {
            case SDL_MOUSEBUTTONDOWN:
                if (ev.button.button == SDL_BUTTON_LEFT)
                {
                    SendLobbyMessage(tcpsock, refreshCommand);
                    ReceiveLobbyMessage(tcpsock, menu->lobbyResponse);
                    printf("Created lobby with id: %c\n",menu->lobbyResponse[0]);
                    dispayText(menu->lobbyResponse, menu);
                    printf("Lobbys: %c\n", menu->lobbyResponse[0]);
                }
        }
    }
}

Solution

  • puts("Something \t\t Something\n"); // \t allows to put "empty spaces" 
    

    I advise writing a function which would format the string as you'd like before printing it. Consider using strtok_r(). Another solution is to iterate char by char into the string and print \n to "jump to the next line" everytime you encounter |.

    One possible solution which seems to work for me is :

    const char*         s = "I1P1I2P1I3P1I4P1I5P1I6P1I7P1I8P1I9P1";
    const unsigned   size = strlen(s);
    char*          result = malloc(sizeof(char) * (strlen(s) + 2 * strlen(s) / 4) + 1);
    /* We allocated the memory necessary to put the whole of the string `s`
    inside of the string `result`, and a tab and a newline, considering
    that for every 4 characters inside of `s`, we will put 1 tab and 1
    newline*/
    
    int                 i = 0;
    int                 j = 0;
    
    while (i < size + 1)
    {
        result[j] = s[i];
        if ((i + 1) % 2 == 0)       // Every 2 char that were passed from _s_ to _result_
        {
            if ((i + 1) % 4 == 0)
                result[++j] = '\n'; // Either we add a newline...
            else
                result[++j] = '\t'; // ... or we add a tab
        }
        j++;
        i++;
    }
    printf("Original String : %s\n", s);
    printf("Formatted String : \n%s\n", result);
    

    Output is :

    Original String : I1P1I2P1I3P1I4P1I5P1I6P1I7P1I8P1I9P1
    Formatted String : 
    I1  P1
    I2  P1
    I3  P1
    I4  P1
    I5  P1
    I6  P1
    I7  P1
    I8  P1
    I9  P1