Saturday, November 5, 2011

A Few More Points of Graphics Under Windows in C programming language

A few more points worth noting before we close our discussion on animation…

(a) One application can set up multiple timers to do different jobs at different intervals. Hence we need to pass the id of the timer that we want to set up to the SetTimer( ) function. In our case we have specified the id as 1.
(b) For multiple timers Windows would post multiple WM_TIMER messages. Each time it would pass the timer id as additional information about the message.
(c) For drawing as well as erasing the ball we have used the same function—BitBlt( ). While erasing we have used the raster operation code WHITENESS. When we use this code the color values of the source pixels get ignored. Thus red colored pixels of ball would get ignored leading to erasure of the ball in the window.
(d) The size of client area of the window can be obtained using the GetClientRect( ) API function.
(e) We want that every time we run the application the initial position of the ball should be different. To ensure this we have generated its initial x, y coordinates using the standard library function rand( ). However, this function doesn’t
generate true random numbers. To ensure that we do get true random numbers, somehow we need to tie the random number generation with time, as time of each execution of our program would be different. This has been achieved by making the call
srand ( time ( NULL ) ) ;
Here time( ) is function that returns the time. We have further passed this time to the srand( ) function.

(f) To be able to use rand( ) and srand( ) functions include the file ‘stdlib.h’. Similarly for time( ) function to work include the file ‘time.h’.
(g) In the call to the PlaySound( ) function the first parameter is the name of the wave file that is to be played. If first parameter is filename then the second has to be NULL. The third parameter is a set of flags. SND_FILENAME indicates that the first parameter is the filename, whereas SND_ASYNC indicates that the sound should be played in the background.
(h) To be able to use the PlaySound( ) function we need to link the library ‘winmm.lib’. This is done by using ‘Project | Settings’ menu item. On selection of this item a dialog pops up. In the ‘Link’ tab of this dialog mention the name ‘winmm.lib’ in the ‘Object / Library modules’ edit box.
(i) When the application terminates we have to instruct Windows not to send WM_TIMER messages to our application any more. For this we have called the KillTimer( ) API function passing to it the ID of the timer.

No comments:

Post a Comment