0
\$\begingroup\$

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.

\$\endgroup\$
  • 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 Feb 18 '18 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\$ – tupan Feb 18 '18 at 13:51
  • \$\begingroup\$ So are you essentially asking how to draw a rectangle to a texture? \$\endgroup\$ – Tyyppi_77 Feb 18 '18 at 13:57
  • \$\begingroup\$ yes, is it bad to do that? \$\endgroup\$ – tupan Feb 18 '18 at 14:16
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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\$ – tupan Feb 18 '18 at 14:31

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.