我正在制作这样的 DIB 部分:
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFO);
bmi.bmiHeader.biWidth = MY_WIDTH;
bmi.bmiHeader.biHeight = -MY_HEIGHT; // Order pixels from top to bottom
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32; // last byte not used, 32 bit for alignment
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;
bmi.bmiColors[0].rgbBlue = 0;
bmi.bmiColors[0].rgbGreen = 0;
bmi.bmiColors[0].rgbRed = 0;
bmi.bmiColors[0].rgbReserved = 0;
hbm = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, 0);
绘画时:
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hbm);
onFrame(pixels);
BitBlt(hdc, 0, 0, MY_WIDTH, MY_HEIGHT, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
onFrame():
void onFrame(pixel *pixels) {
// This is where all the drawing takes place
pixel *p;
// +0.005 each frame
static float frameOffset = 0;
float px; // % of the way across the bitmap
float py; // % of the way down the bitmap
for (int x = 0; x < MY_WIDTH; ++x) {
for (int y = 0; y < MY_HEIGHT; ++y) {
p = &pixels[y * MY_WIDTH + x];
px = float(x) / float(MY_WIDTH);
py = float(y) / float(MY_HEIGHT);
p->r = 0;//(unsigned char)(((cos(px + frameOffset * 10) / sin(py + frameOffset)) * cos(frameOffset * 3) * 10) * 127 + 127);
p->g = 255;//~p->r;
p->b = 255;
}
}
frameOffset += 0.005f;
}
有时,Visual Studio 调试器只是退出paint 方法而不执行所有语句(!),有时会出现访问冲突,有时一切都已执行但没有任何反应(窗口只是保持白色)。它永远不会起作用。我基于来自http://www.daniweb.com/software-development/cpp/code/241875/fast-animation-with-the-windows-gdi的代码; 那里的代码有效,但我的代码显然略有不同。对我来说一个大问题是我不知道如何调试问题:我不知道如何检查对象,访问冲突在 ntdll.dll 中(所以没有堆栈跟踪),并且当它失败时不那么引人注目它默默地失败了。BitBlt 返回 true(在实际调用的 1/4 次运行中)。我尝试在 onFrame() 调用之前和之后调用 GdiFlush,但这并没有真正帮助。究竟是什么导致了这个问题?