//Create the DirectDraw object the primary and auxillary surfaces
if (InitDirectDraw(g_hWnd))
{
if (!CreateDirectDrawSurfaces(g_hWnd))
{
printf("CreateDirectDrawSurfaces failed!\r\n");
FreeDirectDrawResources();
return FALSE;
}
}
else
{
printf("InitDirectDraw failed!\r\n");
FreeDirectDrawResources();
return FALSE;
}
printf("Checkpoint 1: InitDirectDraw and CreateDirectDrawSurfaces succeeded\r\n");
//Initialize the surfaces with the image
if (!InitSurfaces())
{
printf("InitSurfaces failed!\r\n");
return FALSE;
}
BOOL InitDirectDraw(HWND hWnd)
{
HRESULT result;
result = DirectDrawCreate(NULL, &g_ddraw, NULL);
if(SUCCEEDED(result))
{
g_ddraw->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
return TRUE;
}
printf("DirectDrawCreate failed!\r\n");
return FALSE;
}
BOOL CreateDirectDrawSurfaces(HWND hWnd)
{
DDSURFACEDESC ddsd;
HRESULT hr;
RECT rect = {0,};
ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
//create the primary surface and set the clipper
if ( (g_ddraw->CreateSurface(&ddsd, &g_primarySurface, NULL)) == DD_OK)
{
if ( (g_ddraw->CreateClipper(0, &g_primaryClipper, NULL)) == DD_OK)
{
if ( (g_primaryClipper->SetHWnd(0, hWnd)) != DD_OK)
{
printf("SetHWnd on primary clip failed hr=0x%x\r\n", hr);
}
if ( (g_primarySurface->SetClipper(g_primaryClipper)) != DD_OK)
{
printf("SetClipper failed hr=0x%x\r\n", hr);
}
}
else
{
printf("CreateClipper failed hr=0x%x\r\n", hr);
return FALSE;
}
}
else
{
printf("CreateSurface failed hr=0x%x\r\n", hr);
return FALSE;
}
//get the size of the client area (this will be used for backbuffer)
GetClientRect(hWnd, &rect);
ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
//create the back buffer
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = rect.right – rect.left;
ddsd.dwHeight = rect.bottom – rect.top;
printf("screen width:%d, height:%d\r\n", ddsd.dwWidth, ddsd.dwHeight);
if ( (g_ddraw->CreateSurface(&ddsd, &g_backBuffer, NULL)) != DD_OK)
{
printf("CreateSurface on back buffer failed hr=0x%x\r\n", hr);
return FALSE;
}
ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
//create a surface to hold the marbel’s image
ddsd.dwSize = sizeof(DDSURFACEDESC);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = 48; //BMP’s width
ddsd.dwHeight = 48; //BMP’s height
if ( (hr = g_ddraw->CreateSurface(&ddsd, &g_marbleSurface, NULL)) != DD_OK)
{
printf("CreateSurface marble surface failed hr=0x%x\r\n", hr);
return FALSE;
}
return TRUE;
}
BOOL InitSurfaces()
{
HBITMAP hbm;
//load the bitmap resource
hbm = DDGetBitmapHandle(g_hInst, szMarbleBitmap);
if (hbm == NULL)
{
printf("DDGetBitmapHandle failed\r\n");
return FALSE;
}
DDCopyBitmap(g_marbleSurface, hbm, 0, 0, 48, 48);
DeleteObject(hbm);
return TRUE;
}
void DisplayFrame()
{
HRESULT hr;
POINT p;
RECT destRect;
p.x = p.y = 0;
ClientToScreen(g_hWnd, &p);
GetClientRect(g_hWnd, &destRect);
OffsetRect(&destRect, p.x, p.y);
//blt the back buffer on the primary surface
while (TRUE)
{
hr = g_primarySurface->Blt(&destRect, g_backBuffer, NULL, 0, NULL);
if (hr == DD_OK)
break;
if (hr != DDERR_WASSTILLDRAWING)
break;
}
}
case WM_ACTIVATE:
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
switch(LOWORD(wParam))
{
case WA_ACTIVE:
case WA_CLICKACTIVE:
g_bHasFocus = TRUE;
break;
case WA_INACTIVE:
g_bHasFocus = FALSE;
break;
default:
break;
}
break;
case WM_CANCELMODE:
g_bHasFocus = FALSE;
DisplayTextOnScreen(TEXT("Tap here to continue.."));
//DisplayFrame();
break;
case WM_ENTERMENULOOP:
g_bHasFocus = !(BOOL)wParam;
break;
case WM_EXITMENULOOP:
g_bHasFocus = TRUE;
break;
void DisplayTextOnScreen(TCHAR *tszStr)
{
HDC hdc;
RECT rc = {0,}, destRect = {0,}, srcRect = {0,};
int nMsg;
SIZE size = {0,};
HRESULT hr;
if (g_primarySurface->GetDC(&hdc) == DD_OK)
{
SetBkColor(hdc, RGB(115, 115, 115));
SetTextColor(hdc, RGB(255, 255, 255));
GetClientRect(g_hWnd, &rc);
nMsg = lstrlen(tszStr);
GetTextExtentPoint(hdc, tszStr, nMsg, &size);
ExtTextOut(hdc,
(rc.right – size.cx)/2 – 25,
rc.bottom – size.cy*2,
//(rc.bottom – size.cy)/2,// – (rc.bottom – size.cy)/4,
ETO_OPAQUE,
NULL,
tszStr,
nMsg,
NULL);
g_primarySurface->ReleaseDC(hdc);
}
}
Update: The problem mentioned above (Screen 4) is solved. Look here.