#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPSTR lpszClass="Heart";
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,800,600,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
#ifdef STRESS
#else
#endif
#define MANSPEED 5
int cx;
const int cy=480;
HBITMAP hBit;
enum {STOP,RUN} Status;
int nStage;
int nLife;
int nScore;
void DrawBitmap(HDC hdc, int x, int y, HBITMAP hBit);
void OnTimer(HWND hWnd);
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TCHAR *Mes="PRESS BUTTON ENTER";
switch(iMessage) {
case WM_CREATE:
hWndMain=hWnd;
hBit=LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_HERO1));
return 0;
case WM_KEYDOWN:
switch (wParam){
case VK_RETURN:
if(Status == STOP){
Status=RUN;
#ifdef STRESS
nStage=10;
#else
nStage=1;
#endif
nLife=5;
nScore=0;
SetTimer(hWnd,1,20,NULL);
}
break;
}
return 0;
case WM_TIMER:
OnTimer(hWnd);
return 0;
case WM_PAINT:
hdc=BeginPaint(hWnd, &ps);
if(Status == RUN){
DrawBitmap(hdc,0,0,hBit);
}
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
KillTimer(hWnd,1);
DeleteObject(hBit);
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
void OnTimer(HWND hWnd)
{
HDC hdc;
RECT crt;
HDC hMemDC;
HBITMAP OldBit;
hdc=GetDC(hWnd);
GetClientRect(hWnd,&crt);
if(hBit==NULL){
hBit=CreateCompatibleBitmap(hdc,crt.right,crt.bottom);
}
hMemDC=CreateCompatibleDC(hdc);
OldBit=(HBITMAP)SelectObject(hMemDC,hBit);
if(GetKeyState(VK_LEFT) < 0){
if(cx > 10) cx-=MANSPEED;
}
if(GetKeyState(VK_RIGHT) < 0){
if(cx < 780) cx+=MANSPEED;
}
TransparentBlt(hdc, cx, cy, 30, 60, hMemDC, 0, 0, 30, 60, RGB(255,0,0));
SelectObject(hMemDC,OldBit);
DeleteDC(hMemDC);
ReleaseDC(hWnd,hdc);
InvalidateRect(hWnd,NULL,FALSE);
}
void DrawBitmap(HDC hdc, int x, int y, HBITMAP hBit)
{
HDC MemDC;
HBITMAP OldBitmap;
int bx,by;
BITMAP bit;
MemDC=CreateCompatibleDC(hdc);
OldBitmap=(HBITMAP)SelectObject(MemDC,hBit);
GetObject(hBit,sizeof(BITMAP),&bit);
bx=bit.bmWidth;
by=bit.bmHeight;
TransparentBlt(hdc, cx, cy, 30, 60, MemDC, 0, 0, 30, 60, RGB(255,0,0));
SelectObject(MemDC,OldBitmap);
DeleteDC(MemDC);
}
책 보고 여러 기능 넣어볼려고 이것저것 쳐놨는데 일단 캐릭터 좌우 이동만 해볼려고 합니다.
그런데 캐릭터가 지나가면 뒤에 잔상이 안 생겨야 되는데, 잔상이 안 사라지고 계속 남아있네요.
OnTimer 함수에서 메모리 DC에 그림을 그리고 Invalidate라는 함수로 인수 FALSE로 해주면 초기화 안된상태에서
DrawBitmap으로 다시 그려주면 더블버퍼링이 되서 화면이 깜빡이지 않고 빨리 그려지면서 잔상도 안남아야 되는데;;;
뭐가 문제인지 모르겠습니다. 지적 좀 부탁드립니다.