我创建了一个基本程序,使用该SetPixel()方法在 Windows 控制台中渲染精灵,它工作正常,但开销很大。我对此做了一些优化,这有所帮助,但仍然太慢。
目前,我的程序使用两个缓冲区COLORREF将较新的缓冲区绘制到屏幕上,交换它们并重新开始。然而,如果该像素发生变化,它只会重新绘制该像素。这极大地提高了性能,但仍然很慢。到目前为止,缓冲区交换还没有用指针完成,但真正的开销是SetPixel()这样的,我正在寻找一种使用 GDI 创建像素级图形的替代方法,它比SetPixel()
(忽略 anim_frame 和 img_data 向量的第一个维度)更快,如果我决定添加动画对象,它们就在那里)
void graphics_context::update_screen()
{
update_buffer();
for (int x = 0; x < this->width; x++)
{
for (int y = 0; y < this->height; y++)
{
if (this->buffer.at(x).at(y) != this->buffer_past.at(x).at(y))
{
for (int i = 0; i < this->scale_factor; i++)
{
for (int j = 0; j < this->scale_factor; j++)
{
int posX = i + (this->scale_factor * x) + this->width_offset;
int posY = j + (this->scale_factor * y) + this->height_offset;
SetPixel(this->target_dc, posX, posY, this->buffer.at(x).at(y));
}
}
}
}
}
buffer_past = buffer;
}
Run Code Online (Sandbox Code Playgroud)
这是update_buffer()方法:
void graphics_context::update_buffer()
{
for (int x = 0; x < this->width; x++)
{
for (int y = 0; y < this->height; y++)
{
buffer.at(x).at(y) = RGB(0, 0, 0);
}
}
//this->layers.at(1)->sprite; <- pointer to member gfx_obj pointer
for (int i = 0; i < this->layers.size(); i++)
{
gfx_object tmp_gfx = *this->layers.at(i)->sprite;
for (int x = 0; x < tmp_gfx.img_data.at(0).size(); x++)
{
for (int y = 0; y < tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(0).size(); y++)
{
if(tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(x).at(y) != RGB(0,255,0))
buffer.at(x + this->layers.at(i)->locX).at(y + this->layers.at(i)->locY) = tmp_gfx.img_data.at(tmp_gfx.anim_frame).at(x).at(y);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
BitBlt理想情况下,您希望每一帧在屏幕上使用并绘制一次。
否则,您会对每个帧执行多次绘制调用,并且绘制速度会因闪烁而变慢。例如:
case WM_PAINT:
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
for (...)
SetPixelV(hdc, ...) //<- slow with possible flicker
EndPaint(hwnd, &ps);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
主要问题不是SetPixel,而是我们对每一帧向显卡发出数千个绘图请求。
我们可以通过使用“内存设备上下文”形式的缓冲区来解决这个问题:
HDC hdesktop = GetDC(0);
memdc = CreateCompatibleDC(hdesktop);
hbitmap = CreateCompatibleBitmap(hdesktop, w, h);
SelectObject(memdc, hbitmap);
Run Code Online (Sandbox Code Playgroud)
现在您可以在 上完成所有绘图memdc。这些绘图速度会很快,因为它们不会发送到显卡。完成绘制后memdc,您就可以在目标窗口的实际设备上下文上BitBlt进行操作:memdchdc
//draw on memdc instead of drawing on hdc:
...
//draw memdc on to hdc:
BitBlt(hdc, 0, 0, w, h, memdc, 0, 0, SRCCOPY);
Run Code Online (Sandbox Code Playgroud)
在实践中你很少需要SetPixel。通常,您将位图加载到背景和精灵中,然后在memdc和BitBlt上绘制所有内容hdc。
在 Windows Vista 及更高版本中,您可以使用BeginBufferedPaint例程,这可能会更方便一些。例子:
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
class memory_dc
{
HDC hdc;
HBITMAP hbitmap;
HBITMAP holdbitmap;
public:
int w, h;
memory_dc()
{
hdc = NULL;
hbitmap = NULL;
}
~memory_dc()
{
cleanup();
}
void cleanup()
{
if(hdc)
{
SelectObject(hdc, holdbitmap);
DeleteObject(hbitmap);
DeleteDC(hdc);
}
}
void resize(int width, int height)
{
cleanup();
w = width;
h = height;
HDC hdesktop = GetDC(0);
hdc = CreateCompatibleDC(hdesktop);
hbitmap = CreateCompatibleBitmap(hdesktop, w, h);
holdbitmap = (HBITMAP)SelectObject(hdc, hbitmap);
ReleaseDC(0, hdc);
}
//handy operator to return HDC
operator HDC() { return hdc; }
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static memory_dc buffer;
static memory_dc sprite;
static memory_dc background;
switch(msg)
{
case WM_CREATE:
{
RECT rc;
GetClientRect(hwnd, &rc);
buffer.resize(rc.right, rc.bottom);
background.resize(rc.right, rc.bottom);
sprite.resize(20, 20);
//draw the background
rc = RECT{ 0, 0, sprite.w, sprite.h };
FillRect(sprite, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));
//draw the sprite
rc = RECT{ 0, 0, background.w, background.h };
FillRect(background, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
//draw the background on to buffer
BitBlt(buffer, 0, 0, background.w, background.w, background, 0, 0, SRCCOPY);
//draw the sprite on top, at some location
//or use TransparentBlt...
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
BitBlt(buffer, pt.x, pt.y, sprite.w, sprite.h, sprite, 0, 0, SRCCOPY);
//draw the buffer on to HDC
BitBlt(hdc, 0, 0, buffer.w, buffer.w, buffer, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
}
case WM_MOUSEMOVE:
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"classname";
RegisterClassEx(&wcex);
CreateWindow(wcex.lpszClassName, L"Test", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
0, 0, 600, 400, 0, 0, hInstance, 0);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
Run Code Online (Sandbox Code Playgroud)
请注意,这对于简单的绘图来说已经足够了。但 GDI 函数无法处理矩阵等。它们的透明度支持有限,因此您可能需要使用不同的技术,例如 Direct2D,它与 GPU 具有更好的集成