如何以比SetPixel()更快的速度直接从RGB值的原始数组在屏幕上显示像素? 我喜欢在C++中制作动画,比如曼德尔布罗特集ZoMeR,生活模拟器游戏等,通过逐帧设置像素直接到屏幕上。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; } #包括 使用名称空间std; int main() { int xres=1366; int yres=768; char*R=新字符[xres*yres*3]; /* R是按顺序包含每个像素的RGB值的字符数组 这里对R的每个元素进行算术运算 */ HWND window;HDC dc;window=GetActiveWindow();dc=GetDC(window); 对于(int j=0;j
按照Remy的建议,我最终以这种方式显示像素阵列(对于需要一些代码作为示例的家伙):如何以比SetPixel()更快的速度直接从RGB值的原始数组在屏幕上显示像素? 我喜欢在C++中制作动画,比如曼德尔布罗特集ZoMeR,生活模拟器游戏等,通过逐帧设置像素直接到屏幕上。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; } #包括 使用名称空间std; int main() { int xres=1366; int yres=768; char*R=新字符[xres*yres*3]; /* R是按顺序包含每个像素的RGB值的字符数组 这里对R的每个元素进行算术运算 */ HWND window;HDC dc;window=GetActiveWindow();dc=GetDC(window); 对于(int j=0;j,c++,windows,animation,bitmap,pixel,C++,Windows,Animation,Bitmap,Pixel,按照Remy的建议,我最终以这种方式显示像素阵列(对于需要一些代码作为示例的家伙): 你的直觉是正确的:窗口之间的交互和设置单个像素是瓶颈,首选的方法是创建内存中的位图。应该不难找到这方面的教程。一旦你实现了它,你可能会对速度的提高感到惊讶。(另一个选项可能是切换到SDL,SDL也可以以相同的方式加快速度,但可以最小化Windows特定部分。)使用CreateDIBitmap()从您提供的像素数据创建位图。生成的位图将与您要绘制的窗口HDC兼容,因此您可以使用CreateCompatibleDC
你的直觉是正确的:窗口之间的交互和设置单个像素是瓶颈,首选的方法是创建内存中的位图。应该不难找到这方面的教程。一旦你实现了它,你可能会对速度的提高感到惊讶。(另一个选项可能是切换到SDL,SDL也可以以相同的方式加快速度,但可以最小化Windows特定部分。)使用
CreateDIBitmap()
从您提供的像素数据创建位图。生成的位图将与您要绘制的窗口HDC兼容,因此您可以使用CreateCompatibleDC()
和选择object()
以准备绘制位图,然后BitBlt()
将位图实际绘制到窗口上。另请参见CreateDibSection。另外,不要忘了在末尾的DeleteDC(src)
之前调用DeleteObject(map)
。请参见。在MinGW上,我必须在“-o”的名称后添加“-lgdi32”论点
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