title: Pixel to rect in win32api C
body: <p>I'm creating an engine with primary operating system call. I can create a window and manipulate most of events. But I'm having trouble drawing images. I'm using stb_image to load the images, and it's doing well.
  When I try to draw the images with points I keep seems it drawing point by point 'cause it is doing in executing time. I would like to know if there's a way to put those pixels into a rect and then draw onto screen.</p>
  <p>Here is the functions I've already made:</p>
  <p>Image functions:</p>
  <pre class="lang-c prettyprint-override"><code>typedef struct ImageTag ImageSet;
  struct ImageTag {
      B32U id;
      B8 * path;
      B32 w, h, bpp;
      size_t size;
      BO * data;
      B32U image_id;
      IMAGE_PUSH push;
      IMAGE_FLIP flip;
      IMAGE_MODE mode;
      ImageSet * preview, *next;
      STATUS (*load)(ImageSet * p);
      STATUS (*next_first)(ImageSet ** p, ImageSet * n);
      STATUS (*next_last)(ImageSet ** p, ImageSet * n);
      ImageSet * (*search)(ImageSet * p, B32U id);
      STATUS (*clone)(ImageSet ** p1, ImageSet * p2);
      STATUS (*pixel_set)(ImageSet * i, B32 x, B32 y, PixelSet * p);
      PixelSet (*pixel_get)(ImageSet * i, B32 x, B32 y);
      BO (*zoom_fit)(ImageSet * p, B32 w, B32 h);
      BO (*draw)(ImageSet * p, B32 x, B32 y);
      BO (*draw_map)(B32 x, B32 y);
      STATUS (*pop)(ImageSet * p);
      STATUS (*destroy)(ImageSet * p);
  };
  
  static PixelSet image_of_pixel_get(ImageSet * i, B32 x, B32 y) {
      PixelSet c = pixel_start();
      if (!i)
          return c;
      c.r = ((B8U*) i-&gt;data)[i-&gt;bpp * (y * i-&gt;w + x) + 0];
      c.g = ((B8U*) i-&gt;data)[i-&gt;bpp * (y * i-&gt;w + x) + 1];
      c.b = ((B8U*) i-&gt;data)[i-&gt;bpp * (y * i-&gt;w + x) + 2];
      c.a = i-&gt;bpp &gt;= 4 ? ((B8U*) i-&gt;data)[i-&gt;bpp * (y * i-&gt;w + x) + 3] : 0xff;
      return c;
  }
  
  BO static image_of_map_draw(B32 i, B32 j) {
      if ((!gear || !gear-&gt;map || !gear-&gt;map-&gt;image)
              || ((i &lt; 0 &amp;&amp; i &gt; gear-&gt;map-&gt;image-&gt;w - gear-&gt;w)
                      || (j &lt; 0 &amp;&amp; j &gt; gear-&gt;map-&gt;image-&gt;h - gear-&gt;h)))
          return;
      switch (gear-&gt;up-&gt;system) {
      case SYSTEM_PRIMARY: {
  #if defined WIN32 || WIN64 || WINDOWS_XP
          B32 x, y;
          for (y = 0; y &lt; gear-&gt;h; y++) {
              for (x = 0; x &lt; gear-&gt;w; x++) {
                  PixelSet c =
                          pixel_rgba_start(
                                  ((B8U *) (gear-&gt;map-&gt;image-&gt;data))[gear-&gt;map-&gt;image-&gt;bpp
                                          * ((j + y) * gear-&gt;map-&gt;image-&gt;w
                                                  + (i + x)) + 0],
                                  ((B8U *) (gear-&gt;map-&gt;image-&gt;data))[gear-&gt;map-&gt;image-&gt;bpp
                                          * ((j + y) * gear-&gt;map-&gt;image-&gt;w
                                                  + (i + x)) + 1],
                                  ((B8U *) (gear-&gt;map-&gt;image-&gt;data))[gear-&gt;map-&gt;image-&gt;bpp
                                          * ((j + y) * gear-&gt;map-&gt;image-&gt;w
                                                  + (i + x)) + 2],
                                  gear-&gt;map-&gt;image-&gt;bpp &gt;= 4 ?
                                          ((B8U *) (gear-&gt;map-&gt;image-&gt;data))[gear-&gt;map-&gt;image-&gt;bpp
                                                  * ((j + y)
                                                          * gear-&gt;map-&gt;image-&gt;w
                                                          + (i + x)) + 3] :
                                          255);
                  windows_put_pixel(x, y, &amp;c);
              }
          }
          //printf(&quot;%i %i\n&quot;, i, j);
  #endif
      }
          break;
      case SYSTEM_ALLEGRO:
      case SYSTEM_SDL:
          break;
      case SYSTEM_CONSOLE:
      default:
          break;
      }
  }
  </code></pre>
  <p>operating system functions:</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;place/plugin/graphic/primary-of.h&gt;
  
  #if defined WIN32 || WIN64 || WINDOWS_XP
  
  #include &lt;place/machine.h&gt;
  #include &lt;place/driver/opengl-of.h&gt;
  #include &lt;place/plugin/graphic/windows-of.h&gt;
  #include &lt;stdio.h&gt;
  
  extern MachineSet *gear;
  LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  
  HBITMAP create_bitmap(HDC screen, void * data, B32 w, B32 h, B32 bpp) {
      BITMAPINFOHEADER bmih;
      bmih.biSize = sizeof(BITMAPINFOHEADER);
      bmih.biWidth = w;
      bmih.biHeight = h;
      bmih.biPlanes = 1;
      bmih.biBitCount = 32;
      bmih.biCompression = BI_RGB;
      bmih.biSizeImage = 0;
      bmih.biXPelsPerMeter = 10;
      bmih.biYPelsPerMeter = 10;
      bmih.biClrUsed = 0;
      bmih.biClrImportant = 0;
  
      BITMAPINFO dbmi;
      ZeroMemory(&amp;dbmi, sizeof(dbmi));
      dbmi.bmiHeader = bmih;
      dbmi.bmiColors-&gt;rgbBlue = 255;
      dbmi.bmiColors-&gt;rgbGreen = 255;
      dbmi.bmiColors-&gt;rgbRed = 255;
      dbmi.bmiColors-&gt;rgbReserved = 0;
  
      HDC dc = CreateCompatibleDC(NULL);
  
      HBITMAP image = CreateDIBSection(dc, &amp;dbmi, DIB_RGB_COLORS, (void**) &amp;data, NULL, 0);
      return image;
  }
  
  LRESULT CALLBACK windows_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
      switch (uMsg) {
      case WM_PAINT: {
          if (gear-&gt;up-&gt;driver == GRAPHIC_PRIMARY) {
              PAINTSTRUCT ps;
              WINDOWS_PLUGIN(gear)-&gt;screen = BeginPaint(WINDOWS_PLUGIN(gear)-&gt;window, &amp;ps);
              int choice = 1;
              switch (choice) {
              case 0: {
                  // this is what I've tried unsuccessfully
                  HBITMAP bmp = create_bitmap(WINDOWS_PLUGIN(gear)-&gt;screen,
                          gear-&gt;map-&gt;image-&gt;data, gear-&gt;map-&gt;size-&gt;x,
                          gear-&gt;map-&gt;size-&gt;y, gear-&gt;map-&gt;image-&gt;bpp);
                  windows_draw(bmp, gear-&gt;map-&gt;size-&gt;x * 0.1,
                          gear-&gt;map-&gt;size-&gt;y * 0.1);
  
              }
                  break;
              case 1:{
                  // this is what I'm using
                  gear-&gt;map-&gt;image-&gt;draw_map(gear-&gt;map-&gt;pos-&gt;x,gear-&gt;map-&gt;pos-&gt;y);
              }break;
              }
              EndPaint(WINDOWS_PLUGIN(gear)-&gt;window, &amp;ps);
          }
      }
          return 0;
      // ...
      return DefWindowProc(hwnd, uMsg, wParam, lParam);
  }
  
  WINDOWS_OF_CALL BO WINDOWS_OF_TYPE windows_put_pixel(B32 x, B32 y, PixelSet * c) {
      SetPixel(WINDOWS_PLUGIN(gear)-&gt;screen, x, y, RGB(c-&gt;r, c-&gt;g, c-&gt;b));
  }
  
  WINDOWS_OF_CALL BO WINDOWS_OF_TYPE windows_draw(HBITMAP bmp, B32 w, B32 h) {
      HDC hdcMem = CreateCompatibleDC(NULL);
  
      BitBlt(WINDOWS_PLUGIN(gear)-&gt;screen, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY);
  
      DeleteDC(hdcMem);
  }
  
  #elif defined __linux__
  
  // ...
  
  #endif
  </code></pre>
  <p>I wanted to put those pixels onto a windows buffer before to draw it.</p>
  <p>This is the video showing what's happening</p>
  <p><a href="https://youtu.be/Hhpm5459hoA" rel="nofollow noreferrer">https://youtu.be/Hhpm5459hoA</a></p>