I am new with SDL, and am making a Conway Game of Life.
I want to make a grid appear on the screen, so of course I could make one manually with SDL_RenderDrawLine and two loops. But I don't want to create this grid every single time, once is enough, after I can just copy it to the screen.
So I want to create the grid once, and save it somewhere, like a texture.
The problem is, how do I draw to a texture ?
From what I understand, SDL_RenderDrawLine will need a renderer (which is associated with a window) and so it won't work for my purpose as it will be drawing to that renderer buffer or something, and then to a window.
How can I do that ? You can load an image instead, but I'll lose the whole point of drawing it algorithmically (what if the tile size change, etc).
EDIT : So I'm trying to use SDL_SetRenderTarget, as well as the correct flag to create the texture. However it simply doesn't show up.
Here is the code that create the whole texture, and should render to it :
// part of my init function for SDL SDL_SetRenderDrawColor(d_renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_RenderClear(d_renderer); // Create square grid once d_grid = SDL_CreateTexture(d_renderer, SDL_GetWindowPixelFormat(d_window), SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT); if (d_grid == NULL){ fprintf(stderr, "Error creating blank grid texture: %s\n", SDL_GetError()); return false; } SDL_SetRenderTarget(d_renderer, d_grid); for (x = 0; x < MAP_WIDTH; x++){ SDL_RenderDrawLine(d_renderer, x * 10, 0, x * 10, WINDOW_HEIGHT); } for (y = 0; y < MAP_HEIGHT; y++){ SDL_RenderDrawLine(d_renderer, 0, y * 10, WINDOW_WIDTH, y * 10); } // TODO: Draw the grid to the texture SDL_RenderPresent(d_renderer); // Renderer will now draw to screen again SDL_SetRenderTarget(d_renderer, NULL); I've checked it, the two loops does create what I want.
Now when I update the screen:
// Reset display SDL_RenderClear(d_renderer); SDL_RenderCopy(d_renderer, d_grid, NULL, NULL); SDL_SetRenderDrawColor(d_renderer, 0x00, 0x00, 0x00, 0xff); /* Code used to render the game grid, removed for clarity */ SDL_RenderPresent(d_renderer); I don't understand how I'm supposed to do. According to LazyFoo tutorial, I need to use the SDL_RenderDraw* function, which will render to the texture, but using SDL_RenderPresent or not doesn't change anything. What am I doing wrong ?
SDL_RenderCopy(d_renderer, d_grid)? How about if you removeSDL_RenderPresentfrom the grid creation code. Have you tried saving the render texture to a file to see if the grid is properly rendered? Are you sure you are not overdrawing the texture? \$\endgroup\$SDL_SetRender... andSDL_RencdrClear). RemovingSDL_RenderPresentdoesn't work, I tried it a few time before and retried it now. AfterSDL_RenderCopy(...)it draw the game tiles. Haven't tried to save it to a file, I don't know how to do that yet. I can still try too, but as I said I tried to render it by pasting it into the function that update the diplay, and it work there. \$\endgroup\$