Saturday, November 5, 2011

Animation at Work of .Graphics Under Windows in C programming

Speed is the essence of life. So having the ability to display a bitmap in a window is fine, but if we can add movement and sound to it then nothing like it. So let us now see how to achieve this animation and sound effect.
If we are to animate an object in the window we need to carry out the following steps:

(a) Create an image that is to be animated as a resource.
(b) Prepare the image for later display.
(c) Repeatedly display this prepared image at suitable places in the window taking care that when the next image is displayed the previous image is erased.
(d) Check for collisions while displaying the prepared image.
 Let us now write a program that on execution makes a red colored ball move in the window. As the ball strikes the walls of the window a noise occurs. Note that the width and height of the red-colored ball is 22 pixels. Given below is the WndProc( ) function and the various message handlers that help achieve animation and sound effect.
HBITMAP hbmp ;
int x, y ;
HDC hmemdc ;
HGDIOBJ holdbmp ;
LRESULT CALLBACK WndProc ( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_DESTROY :
OnDestroy ( hWnd ) ;
break ;
case WM_CREATE :
OnCreate ( hWnd ) ;
break ;
case WM_TIMER :
OnTimer ( hWnd ) ;
break ;
default :
return DefWindowProc ( hWnd, message, wParam, lParam ) ;
}
return 0 ;
}
void OnCreate ( HWND hWnd )
{
RECT r ;
HDC hdc ;
hbmp = LoadBitmap ( hInst, MAKEINTRESOURCE ( IDB_BITMAP1 ) ) ;
hdc = GetDC ( hWnd ) ;
hmemdc = CreateCompatibleDC ( hdc ) ;
holdbmp = SelectObject ( hmemdc, hbmp ) ;
ReleaseDC ( hWnd, hdc ) ;
srand ( time ( NULL ) ) ;
GetClientRect ( hWnd, &r ) ;
x = rand( ) % r.right - 22 ;
y = rand( ) % r.bottom - 22 ;
SetTimer ( hWnd, 1, 50, NULL ) ;
}
void OnDestroy ( HWND hWnd )
{
KillTimer ( hWnd, 1 ) ;
SelectObject ( hmemdc, holdbmp ) ;
DeleteDC ( hmemdc ) ;
DeleteObject ( hbmp ) ;
PostQuitMessage ( 0 ) ;
}
void OnTimer ( HWND hWnd )
{
HDC hdc ;
RECT r ;
const int wd = 22, ht = 22 ;
static int dx = 10, dy = 10 ;
hdc = GetDC ( hWnd ) ;
BitBlt ( hdc, x, y, wd, ht, hmemdc, 0, 0, WHITENESS ) ;
GetClientRect ( hWnd, &r ) ;
x += dx ;
if ( x < 0 )
{
x = 0 ;
dx = 10 ;
PlaySound ("chord.wav", NULL, SND_FILENAME | SND_ASYNC ) ;
}
else if ( x > ( r.right - wd ) )
{
x = r.right - wd ;
dx = -10 ;
PlaySound ("chord.wav", NULL, SND_FILENAME | SND_ASYNC ) ;
}
y += dy ;
if ( y < 0 )
{
y = 0 ;
dy = 10 ;
PlaySound ("chord.wav", NULL, SND_FILENAME | SND_ASYNC ) ;
}
else if ( y > ( r.bottom - ht ) )
{
y = r.bottom - ht ;
dy = -10 ;
PlaySound ( "chord.wav", NULL, SND_FILENAME | SND_ASYNC );
}
BitBlt ( hdc, x, y, wd, ht, hmemdc, 0, 0, SRCCOPY ) ;
ReleaseDC ( hWnd, hdc ) ;
}
From the WndProc( ) function you can observe that we have handled two new messages here—WM_CREATE and WM_TIMER. For these messages we have called the handlers OnCreate( ) and OnTimer( ) respectively. Let us now understand these handlers one by one

No comments:

Post a Comment