![]() |
| ||
| learning WIN32 GDI 1 Attachment(s) I am trying to animate a bitmap in a window. I am able to load the bitmap in random locations multiple times, but I would like to have the previous instance of the bitmap erased each redraw, as I would like it to be a smoother animation instead of thousands of bitmaps on the screen. I also do not quite understand how to change the random drawing of the bitmap below into a smooth linear animation.
|
| ||
| Re: learning WIN32 GDI part one Don't load "asteroid.bmp" from file every time you call DrawBitmap(). That's a huge slowdown. Load the bitmap first, (say in Game_Init()) and have DrawBitmap() take a handle to the bitmap to draw instead of a filename. Don't forget to use DeleteObject() before terminating to get rid of the bitmap. part two To animate the asteroid moving about smoothly, you need: 1. a direction to travel. This should be (x, y) pixel offsets. Use floats, and round to the nearest integer before mapping the image. (If you use only integers your asteroid is limited in how fast or slow it travels.) 2. (possibly) a time to travel that distance. In the original asteroids game, the rocks only changed direction when impacted (hit by another rock or by the ship's weapon fire), so the time the asteroid traveled its direction was infinite, hence unnecessary to bother tracking. However, if you want it randomly changing direction, when time is up calculate a new direction to travel. Next, you need to calculate the (x, y) direction --randomly if so desired-- then begin moving the asteroid's position by adding the (x, y) offset each frame. All you need to track is the previous asteroid location. You know the bitmap's width and height, so use FillRect() to blacken the previous image, update the asteroid's location, then BitBlt() the asteroid to its new location. Consider how you want it to react when it reaches the edge of the client area. Do you want it to bounce off? Do you want it to disappear? Wrap around? Keep in mind you only calculate a new (x, y) direction when certain events occur, not every frame. You do, however, move the bitmap by adding the direction to the bitmap's current location each frame. part three Use double-buffering. Look up SetPixelFormat() for more information. Once you have updated everything for the frame, then SwapBuffers(). Hope this helps. |
| All times are GMT -4. The time now is 10:21 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC