Background flickers when swapping buffers in game loop

Hi I am a beginner in c++, opengl and sdl2. I’m trying to hack together some code featuring a game loop in sdl2 and some open gl shape drawring with a view to moving the shape around in real time and then doing some collision detection. The only problem is when I use SDL_SetRenderDrawColor to clear the screen the background flashes black and white while the loop is running. Can somebody help me set the background colour and then run the loop without the flickering? Thanks.

// -lSDL2 -lGL

#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <SDL2/SDL.h>
#include <GL/glut.h>  // GLUT, include glu.h and gl.h

typedef int32_t i32;
typedef uint32_t u32;
typedef int32_t b32;

#define WinWidth 500
#define WinHeight 500

void display() {

   glBegin(GL_TRIANGLES);           // Begin drawing the pyramid with 4 triangles
      // Front
      glColor3f(1.0f, 0.0f, 0.0f);     // Red
      glVertex3f( 0.0f, 1.0f, 0.0f);
      glColor3f(0.0f, 1.0f, 0.0f);     // Green
      glVertex3f(-1.0f, -1.0f, 1.0f);
      glColor3f(0.0f, 0.0f, 1.0f);     // Blue
      glVertex3f(1.0f, -1.0f, 1.0f);
   glEnd();   // Done drawing the pyramid

void display2() {
               glColor3f(0.5f, 0.0f, 1.0f); // make this vertex purple
               glVertex2f(-0.75, 0.75);
               glColor3f(1.0f, 0.0f, 0.0f); // make this vertex red
               glVertex2f(-0.75, -0.75);
               glColor3f(0.0f, 1.0f, 0.0f); // make this vertex green
               glVertex2f(0.75, -0.75);
               glColor3f(1.0f, 1.0f, 0.0f); // make this vertex yellow
               glVertex2f(0.75, 0.75);

int main (int ArgCount, char **Args)
  u32 WindowFlags = SDL_WINDOW_OPENGL;
  SDL_Window *Window = SDL_CreateWindow("OpenGL Test", 0, 0, WinWidth,        WinHeight, WindowFlags);
  SDL_GLContext Context = SDL_GL_CreateContext(Window);
  SDL_Renderer* renderer;
  renderer = SDL_CreateRenderer(Window, -1, 0);

  b32 Running = 1;
  b32 FullScreen = 0;
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

        // Clear the entire screen to our selected color.

    while (Running)
    SDL_Event Event;
    while (SDL_PollEvent(&Event))
      if (Event.type == SDL_KEYDOWN)
        switch (Event.key.keysym.sym)
          case SDLK_ESCAPE:
            Running = 0;
          case 'f':
            FullScreen = !FullScreen;
            if (FullScreen)
              SDL_SetWindowFullscreen(Window, WindowFlags | SDL_WINDOW_FULLSCREEN_DESKTOP);
              SDL_SetWindowFullscreen(Window, WindowFlags);
      else if (Event.type == SDL_QUIT)
        Running = 0;
  return 0;

If your goal is to use OpenGL directly, it’d be recommended to not mix calls to OpenGL with calls to SDL_Renderer, as SDL_Renderer is an abstraction that uses OpenGL or any other supported API behind the scenes. Although mixing the two sometimes works as you’d expect, it is not guaranteed to do so.

In your example, you are calling SDL_RenderClear once outside of the loop, meaning you only clear one buffer with the desired color, leaving the other unchanged, with a default black background, that’d explain the flicker.

My advice would be avoiding use of the SDL_SetRenderDrawColor and SDL_RenderClear, and instead calling glClearColor(1.0f, 1.0f, 1.0f, 1.0f) and glClear(GL_COLOR_BUFFER_BIT) just before display2()