I'm trying to render a texture(picture), but SDL_RenderCopy keeps the window's background black.

Here's the relevant code:

SDL_Rect rect; rect.x = 0, rect.y = 0;
    SDL_RenderCopy(g_renderer, g_texture, NULL, &rect);
    // Window stays dark although i've rendered via RenderCopy

    while(SDL_PollEvent(&event) != 0)
        if(event.type == SDL_QUIT)
            quit = true;

        else if (event.type == SDL_KEYDOWN)
            move_picture(event.key.keysym.sym, rect);

If I change the SDL_RenderCopy function call to:

SDL_RenderCopy(g_renderer, g_texture, NULL, NULL);

The picture is visible in the center of the window, but its not the result I'm looking for, as I would want the image to render to the position I set to my rectangle.

After a couple of searches I've found that this is a known behavior. I've tried changing the way I'm loading a texture from CreateTextureFromSurface to IMG_LoadSurface, but the rendering still doesn't work.


You need to set the size of your destination rectangle. Currently you're rendering a 0x0 portion of the texture. So change your code to also include the size of the preferred result. See https://wiki.libsdl.org/SDL_RenderCopy for more info.

Assuming your image you're trying to render is 32 wide and 32 tall, you would need to set the size of the rect like this: rect.w = 32; rect.h = 32;. Then as you're using NULL as the source rectangle, SDL will stretch the texture to the size you want.

  • \$\begingroup\$ Well, it works, thank you! is there a way to find the .w and .h of a picture without guessing? \$\endgroup\$ – RedWhistle Nov 22 '15 at 17:13
  • 1
    \$\begingroup\$ Yes, use SDL_QueryTexture. (Also as the most friendly reminder, don't forget to upvote and accept my answer if you found it useful.) \$\endgroup\$ – Tyyppi_77 Nov 22 '15 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.