#SDL help , I am getting the rectangle but no text .

2 messages · Page 1 of 1 (latest)

wanton mantle
#
// Render the button rectangle
    SDL_Rect rect = { x, y, width, height };
    SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
    SDL_RenderFillRect(renderer, &rect);

    // Render text
    SDL_Color color = { 255, 255 ,255 };
    TTF_Font* font = TTF_OpenFont("arial.ttf", 24);
    SDL_Surface* surface = TTF_RenderText_Solid(font, text, color);
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);

    int textWidth, textHeight;
    SDL_QueryTexture(texture, NULL, NULL, &textWidth, &textHeight);
    int posX = x + (width - textWidth) / 2; // Center horizontally
    int posY = y + (height - textHeight) / 2; // Center vertically

    // Render the text
    SDL_Rect textRect = { posX, posY, textWidth, textHeight };
    SDL_RenderCopy(renderer, texture, NULL, &textRect);

    // Clean up
    SDL_DestroyTexture(texture);
    TTF_CloseFont(font);
spark ospreyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.