# How do I render to a texture loaded from a PNG file using SDL2

I'm trying to create a simple 2D game using C++ and SDL2.

I thought it would be a good idea to go for a "layered" rendering model.

I would render my background first, then the world, then the constructs (things like building etc.), then the objects (NPC's etc.) and lastly a toggle-able debug-overlay.

Right now I'm just trying to render a background and a debug overlay but I've hit a wall.

I can't seem to be able to render my debug-overlay on top of my background.

I've set blending mode to SDL_BLENDMODE_NONE for the background and SDL_BLENDMODE_BLEND for the debug overlay.

The problem is however, that my background is loaded from a PNG file, but when calling SDL_CreateTextureFromSurface() the texture access is set to SDL_TEXTURE_ACCESS_STATIC. (Which can't be used as a target if I understand correctly).

My question is: How can I circumvent this issue? How can I make my background "drawable"?

EDIT: Grammar

You don't actually want to render your debug UI to the background texture. You want to render the background texture to the screen, and then your debug UI to the screen.

So the code flow should look something like this:

SDL_RenderClear(renderer);

// Render background
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);

// Render debug info.
RenderText(GetCurrentFPS(), 0, 0); // Or whatever your text rendering might look like.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for (BoundingBox* box : GetAllBoundingBoxes()) {
SDL_RenderDrawRect(renderer, box->GetRect());
}

SDL_RenderPresent(renderer);

• Thank you very much for your answer, I think the problem is elsewhere in my code. I'm able to draw bounding boxes but not text using a TTF font. The width and height of my texture class is being set though, so I'll keep looking into it
– anon
Aug 6, 2017 at 13:57
• If you're having a literal issue with your TTF rendering, it basically changes the whole nature of the question and is something that you perhaps should've asked instead then.
– user35344
Aug 6, 2017 at 14:01
• Yeah, I thought the problem was in my way of rendering when I asked the question, but drawing bounding boxes has helped me eliminate that possibility! Thanks anyway, I'll probably make a new question when I have an idea of what's causing the issue.
– anon
Aug 6, 2017 at 14:34