2013-07-20 43 views
2

我正在为简单的Direct3D游戏启动屏幕,虽然屏幕本身已正确创建和销毁,但BITMAP意图投影到启动屏幕上并未显示出来。我有这个至今:BitBlt启动屏幕上的位图

//variable declarations 
HWND splashWindow = NULL; 
BITMAP bm; 

//window procedure 
LRESULT WINAPI SplashProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(msg) 
    { 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 
     case WM_PAINT: 
     { 
      PAINTSTRUCT ps; 
      HDC hdc = BeginPaint(splashWindow, &ps); 
      HDC hdcMem = CreateCompatibleDC(hdc); 
      BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); 
      DeleteDC(hdcMem); 
      EndPaint(splashWindow, &ps); 
     } 
    } 
    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    //initialize splash window settings 
    WNDCLASSEX wc; 
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style   = CS_VREDRAW | CS_HREDRAW; 
    wc.lpfnWndProc = SplashProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = NULL; 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = "Splash Window"; 
    wc.hIconSm  = NULL; 
    RegisterClassEx(&wc); 

    //create and draw the splash window 
    HBITMAP splashBMP = (HBITMAP)LoadImage(hInstance, "assets\\textures\\splash.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 
    GetObject(splashBMP, sizeof(bm), &bm); 
    //^-splashBMP to bm - used here so the window has proper dimensions 
    splashWindow = CreateWindow("Splash Window", NULL, 
     WS_POPUPWINDOW | WS_EX_TOPMOST, CW_USEDEFAULT, CW_USEDEFAULT, 
     bm.bmWidth, bm.bmHeight, NULL, NULL, hInstance, NULL); 
    if (splashWindow == 0) return 0; 

    ShowWindow(splashWindow, nCmdShow); 
    UpdateWindow(splashWindow); 
    //after the game loads the splash screen is destroyed through DestroyWindow(splashWindow); 
    //and splashBMP is released by calling DeleteObject(splashBMP); 

真的,唯一重要的代码是SplashProc处理WM_PAINT消息。位图bm通过窗口的900x600尺寸(与splash.bmp相同)正在加载显示。但是,该窗口只是一个黑色屏幕,而不是splash.bmp中包含的herobrine face xD

+0

单挑:我搞砸了隔离代码。 wc.lpfnWndProc不再是DefWindowProc ... – Pere5

回答

1

这是那些几乎可以肯定地盯着代码,只要你错过了明显的代码。

你正在创建hdcMem,然后立即BitBlt ing到屏幕上。在那些你无疑想要一个SelectObject选择你的位图hdcMem

PAINTSTRUCT ps; 
HDC hdc = BeginPaint(splashWindow, &ps); 
HDC hdcMem = CreateCompatibleDC(hdc); 

// You need to add this 
HBITMAP oldBmp = SelectObject(hdcMem, splashBMP);  

BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY); 

// ...and this 
SelectObject(hdcMem, OldBmp); 

DeleteDC(hdcMem); 
EndPaint(splashWindow, &ps); 
+0

对不起,我有点避免GDI直到今天......创建'oldBmp'的目的是什么?我不能在SelectObject中使用splashBMP吗? – Pere5

+0

在删除DC之前,您应该将DC恢复为“原始”状态(即,没有选中任何“东西”)。为此,您保存原始位图并在销毁DC之前将其选回到DC中。我不确定它有多大的实际差异,但这就是微软说你应该做的。 –