Wondering if my implementation is a good way to go about this pixel simulation

Hello! I am new to coding with graphics. I’ve made a sand simulation in PyGame and I decided to rewrite it in SDL.

Before I code any further… I had a couple of concerns with my implementation.

I am directly working with the pixels by updating their colors using SDL_SetRenderDrawColor() and SDL_RenderDrawPoint(). No textures.

However, I am looping through the screen multiple times. Once to move elements that can be moved, and again to draw them.

I suspect I am thinking about this implementation inefficiently. I have no frame cap right now, but currently the screen width and height are set to 250, but when I move it up to something like 500, the simulation is very slow and I suspect it is due to the implementation I currently have for drawing the pixels.

I was wondering if anyone could provide me with some insight on creating a pixel-based game such as this? I’d appreciate some higher level abstraction, I just want to get better at coding in general.

Here is my Github repo with my full source code if anyone wants to take a look! Thank you very much.

I think drawing lots of pixels with SDL_RenderDrawPoint would be very slow and you’d be better off with SDL_RenderDrawPoints. See:

https://wiki.libsdl.org/SDL_RenderDrawPoints

Thanks for your reply!

How would I go about setting the color of each pixel if I’m drawing them all at once?

I was previously setting the draw color then rendering the pixel. If I’m rendering them in a batch, I’m not sure how to go about setting individual colors.

I’d draw all the pixels that are gonna be the same colour in batches.

Board::Board is leaking memory, by using new and not delete. push_back doesn’t move, it just copies de content. Just use ‘Tile t;’ instead: it would be build on stack (much faster) and you won’t need to delete it. Avoid pointer until you will be forced to use it.
There’s no point in making a Tile class just to have a pointer to Element. If you just want another name, use ‘using Tile = Element;’.
Untie those if/elses inside main, they only make things less readable and prone to errors.

Directly working with the pixels is involved in software rendering. Therefore, the image to be rendered should be in a format accessible to the CPU, in this case a C++ array, where reading/writing a pixel involves an array access. In SDL2, it is possible to access the video image by the window surface:

SDL_Surface* screenSurface = SDL_GetWindowSurface(window);
uint32_t* bitmapdata = (uint32_t*)screenSurface->pixels;
int width=screenSurface->w; int height=screenSurface->h;

Example renderer for draw chessboard:

for(int j=0;j<height;j++)for(int i=0;i<width;i++)bitmapdata[j*width+i]=((i*8/width)^(j*8/height))&1?0xFFFFFF:0x000000;

Then use SDL_UpdateWindowSurface(window); on each frame to display the image.

And to handle resize events:

SDL_Event event_;
while (SDL_PollEvent(&event_)){
 if(event_.type==SDL_QUIT)exit(0);
 if((event_.type==SDL_WINDOWEVENT)&&(event_.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)){screenSurface=SDL_GetWindowSurface(window);width=screenSurface->w;height=screenSurface->h;bitmapdata=(uint32_t*)screenSurface->pixels;}
}