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

我喜欢在c ++中制作“animation”,比如MandelBrot Set zoomer,Game of Life simulator等,通过将像素直接设置到屏幕上。 SetPixel()命令使这非常容易,但不幸的是它也很慢。 如果我想用数组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; } 

在我的机器上,这花费了将近5秒的时间来执行,原因是SetPixel()被调用超过一百万次。 最好的情况下,我可以得到这个运行速度快100倍,并得到一个平稳的20fpsanimation。

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

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

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

 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