Is SDL_Texture needed for rendering vector graphics?


The code example from the SDL wiki creates randomly positioned red rectangles. However, SDL_Texture is for pixel data. I copied the program and commented the calls to SDL_CreateTexture, SDL_SetRenderTarget and SDL_RenderCopy and it appears to work the same as the original. So why is SDL_Texture needed at all in the code example?


That example is for SDL_CreateTexture() and the author decided to demonstrate creation of a texture and subsequent usage as a render target. To render rectangles, this is technically unnecessary, but the example is still meaningful. Maybe a comment about what it is doing would be good?

Thanks for your answer JonnyD. I am trying to get familiar with concepts behind SDL2, for example, When do I need a texture? What can it do? etc.

As a concrete example, I am trying to animate (move, rotate, etc) an object composed of vectors (lets say a square), I initialised the video subsystem, created a window, and a renderer for it, created a RGB surface, filled a rectangle in the surface, saved the surface to BMP (that worked), then I created texture from the surface and updated the screen but the resulting window is garbled. Any ideas?

string_t test_sdl2(string_t string){
string.rc = ERR_NOT_SET;
if (SDL_Init(SDL_INIT_VIDEO) < 0) goto error1;
SDL_Window win = NULL;
if ((win = SDL_CreateWindow(“SDL2 test”, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, 0)) == NULL) goto error1;
SDL_Renderer ren = NULL;
if ((ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED))
== NULL) goto error2;
surf = NULL;
if ((surf = SDL_CreateRGBSurfaceWithFormat(0, 100, 100, 32,
SDL_PIXELFORMAT_RGBA32)) == NULL) goto error3;
if (SDL_FillRect(surf, NULL, SDL_MapRGB(surf->format, 255, 0, 0)) < 0)
goto error4;
tex = NULL;
if (SDL_SaveBMP(surf, “surf.bmp”) < 0)
goto error4;
if ((tex = SDL_CreateTextureFromSurface(ren, surf)) == NULL) goto error4;

string.rc = SUCCESS;
return string;
string.rc = ERR_GENERAL;
string = append_message(string, SDL_GetError());
return string;

If you don’t render to a target texture you must redraw your graphics ‘from scratch’ every frame, i.e. the sequence should be:

draw your graphics

The reason is that after the SDL_RenderPresent() there is no guarantee that anything you’ve previously drawn will still be ‘in the buffer’ - and on some platforms it definitely won’t be.

So if you want to emulate an ‘old school’ graphics screen, on which you build up your output by adding one item at a time (as, for example, many BASIC languages do), you must create, and draw to, an intermediate texture. This is the only way you can be confident that the graphics will persist after the SDL_RenderPresent().

To insert into what rtrussell said, you’ll want to use SDL_RenderCopy() or similar to draw the texture onto the screen after SDL_RenderClear() and before SDL_RenderPresent() if you are intending to redraw from scratch.

If you need to move your objects, then your code needs to draw multiple frames (typically in a loop). Check out some SDL tutorials for example game loops.