Original Post
This is a repost of a problem first posted last week. When I try to load a bitmap I just get a blank screen. I''m using 8.1 on XP and heres the code. The two library files are DXGUID and DDRAW.
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <ddraw.h>
#include <memory.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam);
LPDIRECTDRAW7 g_pdd;
LPDIRECTDRAWSURFACE7 g_pddsprimary;
LPDIRECTDRAWSURFACE7 g_pddsback;
LPDIRECTDRAWSURFACE7 g_pddstemp;
LPDIRECTDRAWSURFACE7 lpSurface;
DDSURFACEDESC2 ddsd;
DDSCAPS2 ddsc;
DDBLTFX ddbltfx;
HRESULT hRet;
HDC hdc;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = NULL;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Got it!";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
hWnd = CreateWindowEx(
NULL,
"Got it!",
"Got it!",
WS_POPUP,
0,
0,
1024,
768,
NULL,
NULL,
hInst,
NULL
);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
void CreateSurface(LPDIRECTDRAWSURFACE7 *lpSource, int xs, int ys)
{
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = xs;
ddsd.dwHeight = ys;
g_pdd->CreateSurface(&ddsd, lpSource, NULL);
}
void DrawHBitmap(IDirectDrawSurface7 *lpSurface, HBITMAP hBitmap, int x, int y, int width, int height)
{
HDC hdcImage;
HDC hdc;
BITMAP bm;
if (lpSurface == NULL || hBitmap == NULL)
return;
lpSurface->Restore();
hdcImage = CreateCompatibleDC(NULL);
SelectObject(hdcImage, hBitmap);
GetObject(hBitmap, sizeof(bm), &bm);
width = width == 0 ? bm.bmWidth : width;
height = height == 0 ? bm.bmHeight : height;
lpSurface->GetDC(&hdc);
BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY);
lpSurface->ReleaseDC(hdc);
DeleteDC(hdcImage);
}
void CreateBitmapSurface(LPDIRECTDRAWSURFACE7 lpSurface, char *fname, int xs, int ys)
{
HBITMAP hBitmap;
CreateSurface(&lpSurface, xs, ys);
hBitmap = (HBITMAP)LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
DrawHBitmap(lpSurface, hBitmap, 0, 0, xs, ys);
DeleteObject(hBitmap);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam)
{
//find out which message is being sent
switch(nMsg)
{
case WM_CREATE:
SetTimer(hWnd, 1, 3000, NULL);
hRet = DirectDrawCreateEx(NULL, (void**)&g_pdd,IID_IDirectDraw7, NULL);
if(hRet != DD_OK)
MessageBox(hWnd, "DirectDrawCreateEx Failed", "Error", NULL);
hRet = g_pdd->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
if(hRet != DD_OK)
MessageBox(hWnd, "SetCooperativeLevel Failed", "Error", NULL);;
hRet = g_pdd->SetDisplayMode(1024, 768, 16,0,0);
if(hRet != DD_OK)
MessageBox(hWnd, "SetDisplayMode Failed", "Error", NULL);;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
hRet = g_pdd->CreateSurface(&ddsd, &g_pddsprimary, NULL);
if (hRet != DD_OK)
MessageBox(hWnd, "CreateSurface Failed", "Error", NULL);
ZeroMemory(&ddsc,sizeof(ddsc));
ddsc.dwCaps = DDSCAPS_BACKBUFFER;
hRet = g_pddsprimary->GetAttachedSurface(&ddsc,&g_pddsback);
break;
case WM_KEYDOWN:
KillTimer(hWnd, 1);
PostQuitMessage(0);
break;
case WM_TIMER:
if (g_pddsback->GetDC(&hdc) == DD_OK)
{
ZeroMemory(&ddbltfx,sizeof(ddbltfx));
ddbltfx.dwSize = sizeof(ddbltfx);
CreateBitmapSurface(lpSurface, "Bitmap.bmp", 128, 128);
g_pddsback->Blt(NULL, lpSurface, NULL, NULL, &ddbltfx);
}
g_pddsprimary->Flip(NULL,0);
break;
case WM_DESTROY:
KillTimer(hWnd, 1);
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, nMsg, wParam, lParam));
}
return 0;
}