How can I create a texture containing a geometric figure using SDL2?

I saw there is a way to convert Surface to Texture, and I also saw that there is a way to draw directly to the screen using DrawRect() / FillRect() functions... I'm not sure if the geometry has something similar. Ultimately I want to have a texture to render.

The way I'm making my engine draw is to read game objects' texture and copy it to the render using render.Copy(texture, rect1, rect2).

This makes me wonder if there's a way for my game object to hold a texture containing a rect, circle, or triangle texture. I couldn't find in the SDL wiki or tutorials online.

  • 2
    \$\begingroup\$ could you clarify what you are trying to do? According to your title, you are trying to generate a texture from geometry. but you are asking about a surface to texture. A surface in SDL is not meant as a geometry surface, but as a surface from a texture. etc, render to texture surfaces. \$\endgroup\$
    – Tordin
    Commented Feb 18, 2018 at 9:54
  • \$\begingroup\$ Sorry, I want to be able to have a geometry (euclidean or not) converted to texture. If it's reasonable... \$\endgroup\$
    – renno
    Commented Feb 18, 2018 at 13:51
  • \$\begingroup\$ So are you essentially asking how to draw a rectangle to a texture? \$\endgroup\$
    – user35344
    Commented Feb 18, 2018 at 13:57
  • \$\begingroup\$ yes, is it bad to do that? \$\endgroup\$
    – renno
    Commented Feb 18, 2018 at 14:16

1 Answer 1


I wouldn't suggest rendering shapes to textures, you'll probably get better performance & cleaner code if you just support rendering shapes in your renderer wrapper class.

However, here's how you would render shapes to textures:

You're probably looking for render textures. Use SDL_CreateTexture to create render texture (pass in SDL_TEXTUREACCESS_TARGET to get a target texture). Pass in the size of the shape you want to render.

Then use SDL_SetRenderTarget to bind the render target. After that you can just call SDL_RenderDrawRect to draw a rectangle to the render texture you have bound.

If you decide not to go with this approach, you could for an example have your objects return an enum, that tells the rendering function what kind of render call should be used to render the object.

  • \$\begingroup\$ Thanks! I thought the drawrect, for ex, was not using the GPU like textures do, but apparently it does... thanks for the enum suggestion as well \$\endgroup\$
    – renno
    Commented Feb 18, 2018 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .