#include <windows.h>
#include <ddraw.h>
#include "ddutil.h"
HWND hWndMain;
IDirectDraw7 *pDD;
IDirectDrawSurface7 *lpPrimary, *lpBackBuffer;
IDirectDrawSurface7 *lpA;
IDirectDrawSurface7 *lpSprites;
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);
lpA = DDLoadBitmap(pDD, "a.bmp", 0, 0);
lpSprites = DDLoadBitmap(pDD, "sprites.bmp", 0, 0);
DDSetColorKey(lpSprites, RGB(255, 0, 255));
}
void ExitDirectDraw()
{
lpSprites->Release();
lpSprites = NULL;
lpA->Release();
lpA = NULL;
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_SETCURSOR:
SetCursor(NULL);
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)
{
if (lpPrimary == NULL)
return;
RECT r;
r.left = 0;
r.top = 0;
r.right = 640;
r.bottom = 480;
lpBackBuffer->BltFast(0, 0, lpA, &r,
DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
r.left = 128;
r.top = 0;
r.right = 192;
r.bottom = 64;
lpBackBuffer->BltFast(288, 208, lpSprites, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);
static int x;
x++;
if (x > 100)
x = -100;
r.left = 0;
r.right = 64;
lpBackBuffer->BltFast(288 + x, 100, lpSprites, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);
POINT p;
GetCursorPos(&p);
r.left = 64;
r.right = 128;
lpBackBuffer->BltFast(p.x, p.y, lpSprites, &r,
DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);
lpPrimary->Flip(NULL, DDFLIP_WAIT);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
MSG msg;
RegisterWindowClass();
hWndMain = CreateWindowEx(WS_EX_APPWINDOW,
"SampleWindowClass", "DirectDraw Sprites 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;
}