|
changing of the lamp. The GetTickCount() function returns the number of milliseconds since Windows was started. By calling this function and using it to check the elapsed time since a frame was last updated, we can determine whether or not it is time to change the frame. For this task we need two variables, one to keep track of the current elapsed time, and one to keep track of the time since the last frame was updated. In the the ANIMATION project these are declared as LastTick (for the last time since a frame was updated) and CurrentTick (for the current time). A constant, representing the defined time we want between each frame update, is also needed. In the project the constant FrameTime is used for this. It is set to a value of 1 second.
The process of determining whether frame should be updated or not, is very simple. We first get the current time and store it in the CurrentTick variable. Then the time since the last frame update is subtracted. The result is then compared against the defined interval between the frames and if it is greater, the frame is updated. In the code if would look like this (The bold areas):
Private Sub TimerAnimation_Timer()
Static X As Long Static Y As Long
''''Clear the form, since we do not have a background Me.Cls
''''Draw the mask BitBlt Me.hDC, X, Y, SpriteWidth, SpriteHeight, picMask.hDC, _ (FrameNumber - 1) * SpriteWidth, 0, vbSrcAnd
''''Draw the sprite BitBlt Me.hDC, X, Y, SpriteWidth, SpriteHeight, picSprite.hDC, _ (FrameNumber - 1) * SpriteWidth, 0, vbSrcPaint
''''Check to see if we need to update th frame If CurrentTick - LastTick > FrameTime Then FrameNumber = (FrameNumber Mod MaxFrames) + 1 LastTick = GetTickCount()
End If
''''Update drawing positions X = (X Mod Me.ScaleWidth) + 1 Y = (Y Mod Me.ScaleHeight) + 1
''''Force an update of the form Me.Refresh
End Sub
If you run the sample project now, you can observe that the sprite is moved a small distance before the frame of the sprite is updated.
This is just one way to control the frame rate of a given sprite animation. You could also have used the traveled distance of the sprite to change the frame, or more commonly a user action could trigger a frame change.
We may want to use a blinking lamp as part of the demo project since we use the example and the graphical representation may be very helpful. We can also encourage them to change the value of FrameRate or add a slider control so that they can see how they can control the blinking-Burt.
StretchBlt
There is also another way of animating a sprite, which does not require extra bitmaps, but simply changes the drawn sprite directly. The function for this is the StretchBlt function, which, as the name implies, can stretch or shrink a sprite.
The StretchBlt function is very similar to the BltBit function. Both require a source and destination DC, and they both do raster operations. The difference is that StretchBlt will stretch or shrink the size of the source rectangle to fit the size of the destination rectangle. The declaration is as follows:
Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, _ ByVal x As Long, ByVal y As Long, _ ByVal nWidth As Long, ByVal nHeight As Long, _ ByVal hSrcDC As Long, ByVal xSrc As Long, _ ByVal ySrc As Long, ByVal nSrcWidth As Long, _ ByVal nSrcHeight As Long, ByVal dwRop As Long _ ) As Long
With StretchBlt you can perform some tricks, that might otherwise require a new bitmap.

The Sample project STRETCHBLT found in STRETCHBLT.ZIP moves a sprite around the screen, stretching and shrinking it as it goes.
We have two picture boxes in the sample project, serving as storage for the sprites. The idea of the program is quite simple, stretch the sprite to a size of 96 pixels and then shrink it to 32 pixels while the sprite moves across the form.
Private Sub TimerStretch_Timer()
Static X As Long Static Y As Long
''''Clear the form, since we have no background Me.Cls
If Shrinking Then Stretch = Stretch - 2 Else Stretch = Stretch + 2 End If
If Stretch < 32 Then Shrinking = False If Stretch > MaxStretch Then Shrinking = True
''''Stretch the sprite onto the form StretchBlt Me.hdc, X, Y, Stretch, Stretch, picMask.hdc, 0, 0, _ SpriteWidth, SpriteHeight, vbSrcAnd
StretchBlt Me.hdc, X, Y, Stretch, Stretch, picSprite.hdc, 0, 0, _ SpriteWidth, SpriteHeight, vbSrcPaint
X = (X Mod Me.ScaleWidth) + 2 Y = (Y Mod Me.ScaleHeight) + 2
. Force update of the form Me.Refresh
End Sub
The first thing that is done in code is to check the stretching variable. This variable can be in one of two states, Shrinking or Stretching. To identify the different states we''''ll create a Boolean variable named Shrinking and set it to either True of False, depending on the desired state. The Stretch variable is allowed to be either 32 pixels less or more than the original sprite size, if they get out of this range, the state is changed, and the opposite stretch action will be used.
The sprite is drawn in the usual way, first the mask and then the sprite. But instead of using the BitBlt function we call the StretchBlt function, and set the destination width and height to the value of the stretch variable.
As you can observe, the StretchBlt can be a useful function for doing simple tricks on a sprite. You should use it cautiously though, since it may be a little slower than the ordinary BitBlt function, since some cycles are used when it stretches or shrinks an image.
Using and creating memory DCs
The follwing section has been updated to reflect bug fixes. In the previous version the bitmap handle was deleted in the GenerateDC function. This apparently cause some leaks as the created DC was not deleted correctly. Furthermore there were some wrong constant declaration and a error check which was faulty.
So far we have been using picture boxes as storage areas for the sprites. This does not come without a price since the picture box adds additional time and resource overhead to the animating scheme. This problem can be overcome by simply creating our own Device Contexts to hold the bitmaps. These Device Contexts reside in memory and allow us to perform the required operations without resorting to PictureBoxes. Before doing anything, some more specific information is required on what a Device Context is, and what can be done with it.
A Device Context is a Windows structure, with several important and useful attributes. Each of these attributes has a default value, which is set when the device context is created. The most important attribute of device context to us, is the Bitmap attribute. This attribute is initially set to nothing (meaning there is no bitmap associated with the device 上一页 [1] [2] [3] 下一页 [Sql Server]Sql精妙语句--各种求值函数 [网页制作]网页表格之---多个表格纵向排列 [网页制作]JavaScript另类用法--读取和写入cookie [网页制作]号称非常安全的上网工具---360安全浏览器介绍 [办公软件]信息技术教学篇---Word工具栏的显示、隐藏及四种菜… [操作系统]开始菜单---运行命令大总结 [操作系统]网络转载---64位操作系统与32位的区别 [操作系统]ldap:///(没有响应)Windows无法访问指定设备、路径… [网络技术]安全篇---交换机设置方法介绍 [聊天工具]Real10 & Xpdf installation on Linux Box
|