Redrawing on Window Resizing
static LRESULT
OnSize(HWND hwnd, int w, int h)
{
HRESULT hRes;
DDSURFACEDESC ddsd;
// Nothing to do if we are suspended.
if (!fSuspended)
{
// Update the source and destination rectangles (used by the
// blit that shows the rendering in the client area).
rDstRect.right = rDstRect.left + w;
rDstRect.bottom = rDstRect.top + h;
rSrcRect.right = w;
rSrcRect.bottom = h;
if (NULL != lpd3dDevice)
{
// Although we already have a device, we need to be sure it
// is big enough for the new window client size.
// Because the window in this sample has a fixed size, it
// should never be necessary to handle this case. This code
// will be useful when we make the application resizable.
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
hRes = lpddDevice->lpVtbl->GetSurfaceDesc(lpddDevice,
&ddsd);
if (FAILED(hRes))
{
FatalError(hwnd, IDS_ERRMSG_DEVICESIZE, hRes);
return 0L;
}
if ((w > (int)ddsd.dwWidth) || (h > (int)ddsd.dwHeight))
{
// The device is too small. We need to shut it down
// and rebuild it.
// Execute buffers are bound to devices, so when
// we release the device we must release the execute
// buffer.
ReleaseScene();
ReleaseDevice();
}
}
if (NULL == lpd3dDevice)
{
// No Direct3D device yet. This is either because this is
// the first time through the loop or because we discarded
// the existing device because it was not big enough for the
// new window client size.
hRes = CreateDevice((DWORD)w, (DWORD)h);
if (FAILED(hRes))
{
FatalError(hwnd, IDS_ERRMSG_CREATEDEVICE, hRes);
return 0L;
}
hRes = CreateScene();
if (FAILED(hRes))
{
FatalError(hwnd, IDS_ERRMSG_BUILDSCENE, hRes);
return 0L;
}
}
hRes = UpdateViewport();
if (FAILED(hRes))
{
FatalError(hwnd, IDS_ERRMSG_UPDATEVIEWPORT, hRes);
return 0L;
}
// Render at the new size and show the results in the window's
// client area.
hRes = DoFrame();
if (FAILED(hRes))
{
FatalError(hwnd, IDS_ERRMSG_RENDERSCENE, hRes);
return 0L;
}
}
return 0L;
}