title: How to display pixels on screen directly from a raw array of RGB values faster than SetPixel()?
body: <p>I enjoy making "animations" in c++ such as a MandelBrot Set zoomer, Game of Life simulator etc. by setting pixels directly to the screen frame-by-frame. The SetPixel() command makes this incredibly easy, although unfortunately it's also painfully slow. Here is the sort of set-up I use for each frame, if I wanted to paint the entire screen with the contents of the array R:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  using namespace std;
  int main()
  {
      int xres = 1366;
      int yres = 768;
      char *R = new char [xres*yres*3];
  
      /*
      R is a char array containing the RGB value of each pixel sequentially
      Arithmetic operations done to each element of R here
      */
  
      HWND window; HDC dc; window = GetActiveWindow(); dc = GetDC(window);
  
      for (int j=0 ; j&lt;yres ; j++)
          for (int i=0 ; i&lt;xres ; i++)
              SetPixel(dc,i,j,RGB(R[j*xres+3*i],R[j*xres+3*i+1],R[j*xres+3*i+2]));
  
      delete [] R;
      return 0;
  }
  </code></pre>
  
  <p>On my machine this takes almost 5 seconds to execute for the obvious reason that SetPixel() is being called over a million times. Best case scenario I could get this to run 100x faster and get a smooth 20fps animation.</p>
  
  <p>I hear that converting R into a bitmap file in some way and then using BitBlt to display the frame in one clean command is the way to go, but I have no idea how to implement this for my setup and would greatly appreciate any help.</p>
  
  <p>If it is relevant, I am running on Windows 7 and using Code::Blocks as my IDE.</p>