• <i id='QC0AX'><tr id='QC0AX'><dt id='QC0AX'><q id='QC0AX'><span id='QC0AX'><b id='QC0AX'><form id='QC0AX'><ins id='QC0AX'></ins><ul id='QC0AX'></ul><sub id='QC0AX'></sub></form><legend id='QC0AX'></legend><bdo id='QC0AX'><pre id='QC0AX'><center id='QC0AX'></center></pre></bdo></b><th id='QC0AX'></th></span></q></dt></tr></i><div id='QC0AX'><tfoot id='QC0AX'></tfoot><dl id='QC0AX'><fieldset id='QC0AX'></fieldset></dl></div>

    <tfoot id='QC0AX'></tfoot>
  • <legend id='QC0AX'><style id='QC0AX'><dir id='QC0AX'><q id='QC0AX'></q></dir></style></legend>

    <small id='QC0AX'></small><noframes id='QC0AX'>

    • <bdo id='QC0AX'></bdo><ul id='QC0AX'></ul>

        如何比 SetPixel() 更快地从原始 RGB 值数组直接在屏幕上显示像素?

        时间:2023-12-02

          <tbody id='zzOPC'></tbody>

        <small id='zzOPC'></small><noframes id='zzOPC'>

        • <bdo id='zzOPC'></bdo><ul id='zzOPC'></ul>
          <legend id='zzOPC'><style id='zzOPC'><dir id='zzOPC'><q id='zzOPC'></q></dir></style></legend>

            <i id='zzOPC'><tr id='zzOPC'><dt id='zzOPC'><q id='zzOPC'><span id='zzOPC'><b id='zzOPC'><form id='zzOPC'><ins id='zzOPC'></ins><ul id='zzOPC'></ul><sub id='zzOPC'></sub></form><legend id='zzOPC'></legend><bdo id='zzOPC'><pre id='zzOPC'><center id='zzOPC'></center></pre></bdo></b><th id='zzOPC'></th></span></q></dt></tr></i><div id='zzOPC'><tfoot id='zzOPC'></tfoot><dl id='zzOPC'><fieldset id='zzOPC'></fieldset></dl></div>

              • <tfoot id='zzOPC'></tfoot>
                  本文介绍了如何比 SetPixel() 更快地从原始 RGB 值数组直接在屏幕上显示像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我喜欢用 C++ 制作动画",例如 MandelBrot Set 缩放器、生命游戏模拟器等,方法是将像素直接逐帧设置到屏幕上.SetPixel() 命令使这变得非常简单,尽管不幸的是它也非常缓慢.如果我想用数组 R 的内容绘制整个屏幕,这是我为每一帧使用的那种设置:

                  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:

                  #include <windows.h>
                  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<yres ; j++)
                          for (int i=0 ; i<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;
                  }
                  

                  在我的机器上,由于 SetPixel() 被调用超过一百万次的明显原因,这需要将近 5 秒的时间来执行.最好的情况是我可以让它运行速度提高 100 倍并获得流畅的 20fps 动画.

                  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.

                  我听说以某种方式将 R 转换为位图文件,然后使用 BitBlt 在一个干净的命令中显示帧是要走的路,但我不知道如何为我的设置实现这一点,非常感谢任何帮助.

                  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.

                  如果相关,我在 Windows 7 上运行并使用 Code::Blocks 作为我的 IDE.

                  If it is relevant, I am running on Windows 7 and using Code::Blocks as my IDE.

                  推荐答案

                  按照 Remy 的建议,我最终采用了这种显示像素数组的方式(对于需要一些代码的人来说):

                  Following Remy's advices I ended up with this way of showing pixel array (for guys, who needs some code as an example):

                  COLORREF *arr = (COLORREF*) calloc(512*512, sizeof(COLORREF));
                  /* Filling array here */
                  /* ... */
                  
                  // Creating temp bitmap
                  HBITMAP map = CreateBitmap(512 // width. 512 in my case
                                             512, // height
                                             1, // Color Planes, unfortanutelly don't know what is it actually. Let it be 1
                                             8*4, // Size of memory for one pixel in bits (in win32 4 bytes = 4*8 bits)
                                             (void*) arr); // pointer to array
                  // Temp HDC to copy picture
                  HDC src = CreateCompatibleDC(hdc); // hdc - Device context for window, I've got earlier with GetDC(hWnd) or GetDC(NULL);
                  SelectObject(src, map); // Inserting picture into our temp HDC
                  // Copy image from temp HDC to window
                  BitBlt(hdc, // Destination
                         10,  // x and
                         10,  // y - upper-left corner of place, where we'd like to copy
                         512, // width of the region
                         512, // height
                         src, // source
                         0,   // x and
                         0,   // y of upper left corner  of part of the source, from where we'd like to copy
                         SRCCOPY); // Defined DWORD to juct copy pixels. Watch more on msdn;
                  
                  DeleteDC(src); // Deleting temp HDC
                  

                  这篇关于如何比 SetPixel() 更快地从原始 RGB 值数组直接在屏幕上显示像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:高效获取windows桌面截图 下一篇:使用抖动将 24 位位图转换为 16 位的好的、优化的 C/C++ 算法是什么?

                  相关文章

                  1. <legend id='Ntzyr'><style id='Ntzyr'><dir id='Ntzyr'><q id='Ntzyr'></q></dir></style></legend>
                    • <bdo id='Ntzyr'></bdo><ul id='Ntzyr'></ul>

                    1. <small id='Ntzyr'></small><noframes id='Ntzyr'>

                      <i id='Ntzyr'><tr id='Ntzyr'><dt id='Ntzyr'><q id='Ntzyr'><span id='Ntzyr'><b id='Ntzyr'><form id='Ntzyr'><ins id='Ntzyr'></ins><ul id='Ntzyr'></ul><sub id='Ntzyr'></sub></form><legend id='Ntzyr'></legend><bdo id='Ntzyr'><pre id='Ntzyr'><center id='Ntzyr'></center></pre></bdo></b><th id='Ntzyr'></th></span></q></dt></tr></i><div id='Ntzyr'><tfoot id='Ntzyr'></tfoot><dl id='Ntzyr'><fieldset id='Ntzyr'></fieldset></dl></div>
                      <tfoot id='Ntzyr'></tfoot>