Day 86/100 100 Days of Code

Chris DourisChris Douris
2 min read

One more session working on the text box. I think it is ready.

I created a new TTF_Font variable named textboxFont and changed the position of the interiorTextBox .

TTF_Font *textboxFont;

textboxFont = TTF_OpenFont(fontResource.location, 50);
TTF_SetFontStyle(textboxFont, TTF_STYLE_BOLD);

// Inner text box location
const SDL_FRect interiorBoxRect = {    exteriorbox.x * 1.22, 
                                    exteriorbox.y * 1.125, 
                                    interiorTextSurface->w, 
                                    interiorTextSurface->h};

I modified the TextBoxHandler() function to make the cursor blink. I used the frameTime counter to achieve this effect. I might need to create another variable similar to frameTime.

void TextBoxHandler(TTF_Font *font, SDL_Renderer *renderer, 
                    struct ExteriorBox exteriorbox, struct InteriorBox interiorTextBox, 
                    size_t length, double *frameTime)
{
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
    const SDL_FRect exteriorBoxRect = {exteriorbox.x, exteriorbox.y, exteriorbox.width, exteriorbox.height};
    SDL_RenderRect(renderer, &exteriorBoxRect);

    // Textbox text
    if (interiorTextBox.isEnabled && *frameTime < 0.5)
    {
        char *temp = calloc(length + 1, sizeof(char));
        SDL_Color fontColor = {0xFF, 0xFF, 0xFF, 0xFF};
        strcpy(temp, interiorTextBox.content);
        strcat(temp, interiorTextBox.cursor);
        SDL_Surface *interiorTextSurface = TTF_RenderText_Solid(font, temp, fontColor);
        SDL_Texture *interiortextTexture = SDL_CreateTextureFromSurface(renderer, interiorTextSurface);
        const SDL_FRect interiorBoxRect = {    exteriorbox.x * 1.22, exteriorbox.y * 1.125, 
                                                                    interiorTextSurface->w, 
                                                                    interiorTextSurface->h};

        SDL_RenderTexture(renderer, interiortextTexture, NULL, &interiorBoxRect);
        SDL_DestroySurface(interiorTextSurface);
        interiorTextSurface = NULL;
        SDL_DestroyTexture(interiortextTexture);
        interiortextTexture = NULL;
        free(temp);
        temp = NULL;
    }

    if (interiorTextBox.isEnabled && *frameTime >= 0.5 && strlen(interiorTextBox.content) > 0)
    {
        SDL_Color fontColor = {0xFF, 0xFF, 0xFF, 0xFF};
        SDL_Surface *interiorTextSurface = TTF_RenderText_Solid(font, interiorTextBox.content, fontColor);
        SDL_Texture *interiortextTexture = SDL_CreateTextureFromSurface(renderer, interiorTextSurface);
        const SDL_FRect interiorBoxRect = {    exteriorbox.x * 1.22, exteriorbox.y * 1.125, 
                                                                    interiorTextSurface->w, 
                                                                    interiorTextSurface->h};

        SDL_RenderTexture(renderer, interiortextTexture, NULL, &interiorBoxRect);
        SDL_DestroySurface(interiorTextSurface);
        interiorTextSurface = NULL;
        SDL_DestroyTexture(interiortextTexture);
        interiortextTexture = NULL;
    }

    if (*frameTime >= 1)
    {
        *frameTime = 0;
    }

    if (!interiorTextBox.isEnabled && strlen(interiorTextBox.content) > 0)
    {
        SDL_Color fontColor = {0xFF, 0xFF, 0xFF, 0xFF};
        SDL_Surface *interiorTextSurface = TTF_RenderText_Solid(font, interiorTextBox.content, fontColor);
        SDL_Texture *interiortextTexture = SDL_CreateTextureFromSurface(renderer, interiorTextSurface);
        const SDL_FRect interiorBoxRect = {    exteriorbox.x * 1.22, exteriorbox.y * 1.125, 
                                                                    interiorTextSurface->w, 
                                                                    interiorTextSurface->h};

        SDL_RenderTexture(renderer, interiortextTexture, NULL, &interiorBoxRect);
        SDL_DestroySurface(interiorTextSurface);
        interiorTextSurface = NULL;
        SDL_DestroyTexture(interiortextTexture);
        interiortextTexture = NULL;
    }
}

Result

0
Subscribe to my newsletter

Read articles from Chris Douris directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chris Douris
Chris Douris

AKA Chris, is a software developer from Athens, Greece. He started programming with basic when he was very young. He lost interest in programming during school years but after an unsuccessful career in audio, he decided focus on what he really loves which is technology. He loves working with older languages like C and wants to start programming electronics and microcontrollers because he wants to get into embedded systems programming.