所以我试图在Win32中克隆Pong,并且工作正常,但后来我用物理做了很多东西,当我测试它时,精灵位图不是。甚至还显示:/
所以,这是我初始化渲染内容的方法:
-r--------. 1 root root 539 2015-08-18 14:19 metadata.json
这是CreateDoubleBuffer函数:
int InitRenderer(int showCMD)
{
context = GetDC(winHandle);
if(!context)
{
return EXIT_FAILURE;
}
ShowWindow(winHandle, showCMD);
UpdateWindow(winHandle);
CreateDoubleBuffer(&globalBuffer);
ClearWindow(globalBuffer.hdcBack, globalBuffer.scrnRect);
return EXIT_SUCCESS;
}
BUFFER结构,供参考,如下所示:
void CreateDoubleBuffer(BUFFER *buffer)
{
buffer->hwnd = winHandle;
GetClientRect(winHandle, &(buffer->scrnRect));
buffer->hdcFront = GetDC(buffer->hwnd); //get a handle to the DC and plop it into the front buffer.
buffer->hdcBack = CreateCompatibleDC(buffer->hdcFront); //get a compatible DC for the Back buffer.
buffer->hdcBitmap = CreateCompatibleDC(buffer->hdcFront); //get a compatible DC for the bitmap.
buffer->hCompBitmap = CreateCompatibleBitmap(buffer->hdcFront, buffer->scrnRect.right, buffer->scrnRect.bottom); //Create a compatible bitmap as a dummy, and store in the front buffer.
buffer->hOldBitmap = (HBITMAP)SelectObject(buffer->hdcBack, buffer->hCompBitmap);
}
所以我有一个Sprite类,它只包装HBITMAP和文件名的字符串,以及一些操作它们的函数。当我想绘制精灵时,会调用此函数:
struct BUFFER // This is our back buffering structure
{
HWND hwnd; // This holds the current window's handle
RECT scrnRect; // This holds the client rectangle of the window
HANDLE hCompBitmap; // This holds the compatible bitmap for the backbuffer
HANDLE hOldBitmap; // This is used for storage to free when the program quits
HANDLE hOldBitmap2; // This is used as storage to swap between selected bitmaps when using selectObject()
HDC hdcFront; // This is the front buffer (The part we see)
HDC hdcBack; // This is the back buffer (the part we draw to, then flip)
HDC hdcBitmap; // This is a temp buffer to swap the bitmap back and forth from
};
在此函数的开头,SelectObject失败,因此buffer-> hOldBitmap2为null。 GetLastError返回的错误是1400,这意味着窗口句柄无效,所以我猜winHandle(一个全局变量,只是你知道)搞砸了。但我不知道如何。以下是我如何初始化它:
void RenderSprite(BUFFER *buffer, HBITMAP bmp, Vec2I pos, Vec2F origin)
{
buffer->hOldBitmap2 = (HBITMAP)SelectObject(buffer->hdcBitmap, bmp); //we put the bitmap into the extra HDC to hold it there.
if(!buffer->hOldBitmap2)
{
std::cout << GetLastError() << "\n";
}
BitBlt(buffer->hdcBack, pos.GetX() + (int)origin.GetX(), pos.GetY() + (int)origin.GetY(), buffer->scrnRect.right, buffer->scrnRect.bottom, buffer->hdcBitmap, 0, 0, SRCCOPY); //blit the bitmap into the backbuffer.
SelectObject(buffer->hdcBitmap, buffer->hOldBitmap2); //put the old handle to the bitmap back where it belongs.
}
自从它发挥作用以来,我还没有改变它。
就屏幕上发生的事情而言,我只是得到一个空白的白色窗口,所以没有崩溃消息或其他什么,但它只是......空白。
我已经查看了其他问题,这些问题的解决方案似乎与注册窗口类等问题有关。我已经检查了我的问题,但我发现它没有任何问题。我已经调试过,而hOldBitmap2是缓冲区中唯一的空值部分。其余的都很好。
任何帮助你的圣经提供的帮助都会非常感激。
答案 0 :(得分:0)
我刚遇到这个问题。
应用程序不能在一个以上的DC中选择单个位图 一个时间。位图是否已被选择到另一个DC中? – theB 8月27日 '15在18:09
正在关闭!
确定:
SelectObject( hdc_bitmap, buf_bitmap )
失败:
SelectObject( hdc_bitmap, buf_bitmap )
SelectObject( hdc_bitmap, buf_bitmap )
这意味着,如果您的 RenderSprite 函数尝试连续绘制两次相同的精灵, 它会失败。