#include <windows.h>
#include <ddraw.h>
HWND hWndMain;
IDirectDraw7 *pDD;
IDirectDrawSurface7 *lpPrimary, *lpBackBuffer;
void InitDirectDraw()
{
DirectDrawCreateEx(NULL, (void **)&pDD, IID_IDirectDraw7, NULL);
pDD->SetCooperativeLevel(hWndMain,
DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
pDD->SetDisplayMode(640, 480, 16, 0, 0);
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 2;
pDD->CreateSurface(&ddsd, &lpPrimary, NULL);
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
lpPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpBackBuffer);
}
void ExitDirectDraw()
{
lpPrimary->Release();
lpPrimary = NULL;
pDD->Release();
pDD = NULL;
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
ExitDirectDraw();
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void RegisterWindowClass()
{
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = MainWindowProc;
wcx.hInstance = GetModuleHandle(NULL);
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = NULL;
wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "SampleWindowClass";
RegisterClassEx(&wcx);
}
void OnIdle(void)
{
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
MSG msg;
RegisterWindowClass();
hWndMain = CreateWindowEx(WS_EX_APPWINDOW,
"SampleWindowClass", "DirectDraw Bare-Bones Sample", WS_POPUP,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL);
ShowWindow(hWndMain, SW_SHOW);
InitDirectDraw();
for (;;)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, 0, 0))
break;
DispatchMessage(&msg);
}
OnIdle();
}
return 0;
}