Strange "bug" with SDL2 on Mac M1

I started testing SDL stuff today in C and when I put a font this effect happened around, also something similar happened with the image borders when I made it smaller.

Here is my code and a video proof:

#include <SDL.h>
#include <SDL_ttf.h>
#include <stdbool.h>
#include <stdio.h>

SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
TTF_Font *font = NULL;
SDL_Surface *surface = NULL;
SDL_Texture *texture = NULL;

int main(void)
{
    bool isRunning = true;
    SDL_Event event;
    int x = 1280/2, y = 800/2;

    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();

    window = SDL_CreateWindow(
        "Test SDL",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        1280, 800,
        0);

    renderer = SDL_CreateRenderer(window,-1,0);

    font = TTF_OpenFont("../resources/OpenSans-Regular.ttf", 42);
    SDL_Color white = {255, 255, 255};
    surface = TTF_RenderText_Solid(font, "Testing Font", white);
    texture = SDL_CreateTextureFromSurface(renderer, surface);

    int textW = 0;
    int textH = 0;
    SDL_QueryTexture(texture, NULL, NULL, &textW, &textH);
    SDL_Rect dsrect = {x-150, y-150, textW, textH};

    while (isRunning)
    {
        SDL_PollEvent(&event);
        switch (event.type)
        {
            case SDL_QUIT:
                isRunning = false;
                break;
        }

        SDL_RenderCopy(renderer, texture, NULL, &dsrect);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    TTF_Quit();
    SDL_Quit();

    return 0;
}

For the issue:
Try adding SDL_RenderClear(renderer); right at the start of your drawing commands.

        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, &dsrect);
        SDL_RenderPresent(renderer);

By default Mac tries to preserve the contents of the previous frame, and since the screen has never been cleared in your code, the contents are junk from memory. Kind of a cool effect in the video, but not something you actually want to do in live code.

Side note (not the current issue):
You might want to define white this way: SDL_Color white = {255, 255, 255, 255}; to set the alpha value. (On some platforms that could otherwise be initialized with a similar garbage value, so you’ll get random alpha on your text output, possibly only on some computers).

Perfect!!! Thank you very much, I understand that this issue is Mac specific??

1 Like

It’s at least a common issue on most Apple systems. To be fair, SDL runs on too many different systems that I don’t own for me to say that it only happens on Mac/IOS.

(I don’t own a Mac either, I just happened to remember the issue from reading through this post, which was a recommended by Peter87 here to explain why it’s important to call SDL_RenderClear() even if you have a background image that fills the whole screen)

Okeyyyy thanks for all