title: OpenGL Flashing?
body: <p>I'm trying to draw some text in OpenGL while the program draw a cube or any Opengl native so, when I try to put the text on the screen it flashes very fast and I don't know why, I tried to change the Sleep value and nothing... </p>
  
  <p>The code is below; <a href="http://gyazo.com/2cb75802048bd54b3c8a7fd2aa1be749" rel="nofollow">here is a GIF showing the problem</a>.</p>
  
  <p>The green background is the cube, the camera is very close to the background, you can move back with the <code>NUM_2</code>.</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include "default.h"
  
  using namespace std;
  
  LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  DWORD WINAPI WorkLoop(LPVOID PARAMS);
  void keyScan (MSG msg, Camera cam);
  
  HDC hDC;
  HGLRC hRC;
  HWND hwnd;
  
  RECT WBounds;
  
  
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  {
      WNDCLASSEX wcex;
      MSG msg;
      BOOL bQuit = FALSE;
  
      wcex.cbSize = sizeof(WNDCLASSEX);
      wcex.style = CS_OWNDC;
      wcex.lpfnWndProc = WindowProc;
      wcex.cbClsExtra = 0;
      wcex.cbWndExtra = 0;
      wcex.hInstance = hInstance;
      wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
      wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
      wcex.lpszMenuName = NULL;
      wcex.lpszClassName = "GLSample";
      wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
      Screen ();
  
      if (!RegisterClassEx(&amp;wcex))
          return 0;
  
  
      hwnd = CreateWindowEx(0,
                            "GLSample",
                            "OpenGL Testing",
                            WS_OVERLAPPEDWINDOW,
                            Scr.sx/2-630,
                            Scr.sy/2-450,
                            1260,
                            900,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
  
      GetClientRect(hwnd, &amp;WBounds);
  
      ShowWindow(hwnd, nCmdShow);
      EnableOpenGL(hwnd, &amp;hDC, &amp;hRC);  ///ENABLE OPENGL
  
      Camera cam = Camera (0, 0, -1);
  
      CreateThread(0, 0x1000, &amp;WorkLoop, 0, 0, 0);
  
      while (!bQuit)
      {
          if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
          {
              if (msg.message == WM_QUIT) bQuit = TRUE;
              else
              {
                  keyScan (msg, cam);
                  TranslateMessage(&amp;msg);
                  DispatchMessage(&amp;msg);
              }
          }
          else
          {
              renderSimulation (cam);
              SwapBuffers (hDC);
          }
          Sleep(1);
      }
  
      DisableOpenGL(hwnd, hDC, hRC);
      DestroyWindow(hwnd);
      return msg.wParam;
  }
  
  DWORD WINAPI WorkLoop(LPVOID PARAMS)
  {
      while (true)
      {
          InvalidateRect(hwnd, &amp;WBounds, true);
          Sleep(33);
      }
      ExitThread(0);
  }
  
  
  float x = 0.0f, y = 0.0f, z = 0.0f;
  float rx = 0.0f, ry = 0.0f, rz = 0.0f;
  char* textas = "test";
  
  void keyScan (MSG p, Camera cam)
  {
      if (p.message == WM_KEYDOWN)
      {
          if (p.wParam == ARROW_RIGHT)      {x += 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == ARROW_LEFT)  {x -= 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == ARROW_UP)    {y += 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == ARROW_DOWN)  {y -= 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == NUM_8)       {z += 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == NUM_2)       {z -= 0.1; cam.SetCameraPosition (x, y, z);}
          else if (p.wParam == L)           SetFullScreen (p.hwnd, hDC, hRC);
          else if (p.wParam == K)           textas = "cambiado";
      }
  }
  
  HFONT Font = CreateFont(40, 0, 0, 0,FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY, FF_MODERN, TEXT("Arial"));
  HPEN BoxPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
  HPEN OutlinePen = CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
  HPEN CHPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  HBRUSH CHBrush = CreateSolidBrush(RGB(0, 255, 0));
  
  void drawtext(HDC hdc, int x, int y, const char * text)
  {
      SetBkMode (hdc, TRANSPARENT);
      SetTextColor(hdc, RGB(0, 255, 0));
      SetBkColor(hdc, RGB(255, 255, 255));
      TextOutA(hdc, x, y, text, strlen(text));
  }
  
  /*void Draw(HDC hdc, int x, int y, float dist)
  {
      int width = 20000 / dist;
      int height = 45000 / dist;
      SelectObject(hdc, OutlinePen);
      SelectObject(hdc, WHITE_BRUSH);
      Rectangle(hdc, x - (width / 2), y - height, x + (width / 2), y);
      SelectObject(hdc, BoxPen);
      Rectangle(hdc, x - (width / 2), y - height, x + (width / 2), y);
      SetTextAlign(hdc, TA_CENTER | TA_NOUPDATECP);
      std::stringstream ss2;
      ss2 &lt;&lt; "Dist: " &lt;&lt; dist &lt;&lt; " m";
      drawtext(hdc, x, y + 90, ss2.str().c_str());
  }*/
  
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  {
      switch (uMsg)
      {
          case WM_PAINT:
          {
              int win_width = WBounds.right - WBounds.left;
              int win_height = WBounds.bottom + WBounds.left;
              PAINTSTRUCT ps;
              HDC Memhdc;
              HDC hdc;
              HBITMAP Membitmap;
              hdc = BeginPaint(hwnd, &amp;ps);
              Memhdc = CreateCompatibleDC (hdc);
              Membitmap = CreateCompatibleBitmap (hdc, win_width, win_height);
              SelectObject (Memhdc, Membitmap);
              //FillRect (Memhdc, &amp;WBounds, WHITE_BRUSH);
              SelectObject (Memhdc, Font);
              SetTextAlign (Memhdc, TA_LEFT | TA_NOUPDATECP);
              drawtext(Memhdc, 100, 100, textas);
  
              //Draw (Memhdc, 20, 50, 90.0);
  
              /*SelectObject(Memhdc, CHPen);
              SelectObject(Memhdc, CHBrush);*/
  
              BitBlt (hdc, 0, 0, win_width, win_height, Memhdc, 0, 0, SRCCOPY);
              DeleteObject(Membitmap);
              DeleteDC(Memhdc);
              DeleteDC(hdc);
              EndPaint(hwnd, &amp;ps);
              ValidateRect(hwnd, &amp;WBounds);
          }
          case WM_KEYDOWN:
          {
              switch (wParam)
              {
                  case VK_ESCAPE:
                      PostQuitMessage(0);
              }
          }
          case WM_ERASEBKGND:
              return 1;
          default:
              return DefWindowProc(hwnd, uMsg, wParam, lParam);
      }
      return 0;
  }
  </code></pre>