# C & SDL: Unexpected Behaviour with Renderer

I'm creating a clone of the memory testing game SIMON. My program is simply four differently colored squares that light up one at a time in a random order. I wrote some code that lights up one square, dims it, waits 250ms and then lights up the next in the sequence. Rinse and repeat until the sequence is complete.

My problem is that everytime it lights up a square, it's lighting up all of the previous lit ones as well, instead one at a time. I hope I'm explaining that clearly.

Below is my code. I know it's silly code, but I plan to come back and find a better way to do it.

SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
SDL_RenderFillRect(renderer, &squareTwo);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 50, 50, 0, 255);
SDL_RenderFillRect(renderer, &squareTwo);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &squareThree);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 50, 0, 0, 255);
SDL_RenderFillRect(renderer, &squareThree);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &squareThree);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 50, 0, 0, 255);
SDL_RenderFillRect(renderer, &squareThree);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &squareFour);
SDL_RenderPresent(renderer);

SDL_Delay(250);

SDL_SetRenderDrawColor(renderer, 0, 0, 50, 255);
SDL_RenderFillRect(renderer, &squareFour);
SDL_RenderPresent(renderer);


With the above code, I was hoping only one square will be lit up at once. Instead the first one lights up, and dims. And then the first and second square light up at the same time.

And to make this even stranger, I ran it on my work computer and it works like expected (both systems it was tested on are Linux, if that matters)! Maybe I changed something by accident after last testing it, but I really just don't recall that. Very strange.

Would anyone have any ideas or clues for me on this one?

• As a side note, you shouldn't use SDL_Delay. It halts the thread and stops your application from responding. – Tyyppi_77 Sep 29 '16 at 4:49
• Ahh, I see that now. Just reading through some documentation now, perhaps an SDL timer would be my best bet? – azurepancake Sep 29 '16 at 20:27
• Doesn't look like it as it launches the function calls on another thread. But I haven't ever actually used it. I personally would just implement a small utility timer class that uses SDL_GetTicks() to calculate the time passed each frame. – Tyyppi_77 Sep 30 '16 at 4:46

From the documentation of SDL_RenderPresent:
The backbuffer should be considered invalidated after each present; do not assume that previous contents will exist between frames. You are strongly encouraged to call SDL_RenderClear() to initialize the backbuffer before starting each new frame's drawing, even if you plan to overwrite every pixel.
You should try to call SDL_RenderClear() after each call to SDL_RenderPresent().
• Thanks for this! It was exactly what I needed. It took some playing around to get it right, but I essentially make a resetSquares() function that clears the renderer and redraws the dim squares after each one lights up. Here is the full code for anyone who is at all curious. – azurepancake Sep 29 '16 at 22:02