#include
int iCarX=-200,iCarY=200;
HINSTANCE hInstance;
HDC hdcSrc,hdcBack,hdcMem;
HBITMAP hBitmapSrc,hBitmapBack,hBitmapMem;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int Render(HDC hdc);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Spirit") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, TEXT ("Spirit Demo 精灵动画演示"), WS_OVERLAPPEDWINDOW ^ WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch (message){
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
hdc=GetDC(hwnd);
hdcSrc=CreateCompatibleDC(hdc); //源图像DC
hdcBack=CreateCompatibleDC(hdc); //目标图像DC
hdcMem=CreateCompatibleDC(hdc); //内存DC
hBitmapSrc=LoadBitmap(hInstance,TEXT("CARS")); //读图像
hBitmapBack=LoadBitmap(hInstance,TEXT("SCENE"));
hBitmapMem=CreateCompatibleBitmap(hdc,500,500); //为内存DC建立一个位图
SelectObject(hdcSrc,hBitmapSrc); //将位图分别选入DC
SelectObject(hdcBack,hBitmapBack);
SelectObject(hdcMem,hBitmapMem);
SetTimer(hwnd,1,10,NULL); //定时器,间隔10ms
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
Render(hdc); //我们的渲染函数
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_TIMER:
iCarX+=1; //移动
if (iCarX>389) {
KillTimer(hwnd,1);
return 0;
}
InvalidateRect(hwnd,NULL,NULL); //这两句使窗口内容更新,即调用我们的渲染函数
UpdateWindow(hwnd);
return 0;
case WM_DESTROY:
DeleteDC(hdcSrc);
DeleteDC(hdcBack);
DeleteObject(hBitmapSrc);
DeleteObject(hBitmapBack);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int Render(HDC hdc){
BitBlt(hdcMem,0,0,500,500,hdcBack,0,0,SRCCOPY); //将背景拷贝到内存DC上
BitBlt(hdcMem,iCarX,iCarY,220,133,hdcSrc,220,0,SRCAND); //处理掩码图
BitBlt(hdcMem,iCarX,iCarY,220,133,hdcSrc,0,0,SRCPAINT); //处理目标图像
BitBlt(hdc,0,0,500,500,hdcMem,0,0,SRCCOPY); //画到屏幕上的窗口的DC
return 0;
}
这是我的个人游戏开发论坛,你可以来这里看资料!
http://tieba.baidu.com/f?kw=%D0%C7%BC%CA%B9%A4%D7%F7%CA%D2