转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Drawing & Animation I         ★★★★

Drawing & Animation I

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2589 更新时间:2009/4/23 16:39:27
human or by the computer. As you have probably already guessed, you use the BitBlt function to draw sprites onto the gaming field from a specified source. Since we are blitting a rectangular area this would all be very nice and neat, if all sprites were rectangular but they are not. In fact they are usually anything but rectangular, running the gamut from simple blobs to elaborate spaceships. So how can we make the rectangular area we just blitted any other shape we want? The simple answer is, we can not, but we can make it appear as though they are.

Raster Operations

The technique we will use to make the sprites appear to have the shape we want involves the use of Raster operations. In the previous sample, we simply drew what was in the picturebox onto the screen. This means that we set the Destination Pixel = Source Pixel. In other words, when we blit the picture we are specifying that each of the destination pixels be exactly like its corresponding source pixel. We specify the Raster Operation we want to apply using the last parameter of the BitBlt function: dwRop.

Before showing you how this can be used to make sprites of any shape, or more accurately, to make unwanted areas of the bitmap disappear (also known as transparency), lets take a closer look at Raster Operations.

When we are drawing an image from one Device Context to another, BitBlt is actually copying the value of each pixel in the source picture, and performs a specified Raster Operation on the destination with this copied value. To simplify, if we could hear BitBlt thinking, we might hear something like this "Ok, so now I have this black pixel from the source and I am going to copy it to the destination which is gray. Do you want me to mix the two colors, or place one on top of the other, or do something else to them?" The way we tell BitBlt how to handle the operation is by using the dwRop parameter we mentioned earlier.

So the Raster operation involves both the current value of the destination and the copied value from the source. The values used in this context, are the color values of the pixels. Each pixel in an image or device context is made of a number of bits, and the raster operations combine these bits to form a new pixel on the destination. The basis of the raster operations are the logical Boolean operators, NOT, AND, OR & XOR.

 

Visual Basic provides several constants for raster operations, here are a few of the most frequently used ones;


vbBlackness Sets destination = 0 (Black) vbDstInvert Inverts the destination rectangle (NOT Destination) vbMergeCopy Combines the source with the destination using AND vbMergePaint Combines the source with the destination using OR vbNotSrcCopy Inverts the source and then copies it to destination vbNotSrcErase Inverts the result of combining the source and destination using OR vbSrcAnd Combines the source and the destination with the AND operator vbSrcCopy Copies the source directly to the destination vbSrcErase Combines the inverted destination with the source using AND vbSrcInvert Combines source and destination with XOR vbSrcPaint Combines the source and the destination with OR vbWhiteness

Destination is set to white


The raster operation is done on the entire destination rectangle.

 

Masks

Now, just using these raster operation constants will not make a sprite transparent, but if we combined the original sprite bitmap with a mask bitmap, then we could reach transparency through these raster operations.

A Mask is a special bitmap consisting of only two colors, black and white. The areas on a mask, which are white, will be the transparent areas, and the black areas will be the areas we want to see.

Have a look at the following two bitmaps of a sprite and a mask of the sprite:

 

 

The left one is the mask of the first sprite, and the right one is the sprite. Basically we first Blit the mask onto the surface, and then blit the sprite at the same position. The result should be just a blue circle. Let''''s see how this is accomplished.

What follows is a bit involved, and some will find it difficult. You can still use BitBlt even if you don''''t understand all the specifics of Boolean math -just like you can drive a car without understanding how everything in a car''''s engine works. So if you don''''t follow all of this don''''t panic. At some point you will find that knowing how and why a thing works will allow you to supercharge your programming so determine to come back to this as your skills increase.

When the Mask is first blitted onto the game field, we specify the vbSrcAnd operator for BitBlt''''s dwRop parameter. This operator tells the BitBlt function to make the destination equal to the source color (pixel) combined with the destination pixel color using the AND operator.

Imagine that the destination has a background color defined as such: 1100 1001 (201 in decimal). By using the BitBlt function with vbSrcAnd operator the result on the destination would be.

For the white (white is 1111 1111) parts of the mask the resulting destination color would be:

 

Source: 1111 1111 AND Destination: 1100 1001 Result:

1100 1001

Which is the same as the original destination color (the background).

For the black (Black is 0000 0000) areas it would be:

Source: 0000 0000 AND Destination: 1100 1001 Result:

0000 0000

This would make all the black areas of the mask still be black and the white areas would be the same color as the destination background. So if we blitted the mask shown above, the destination would now have a perfect round black circle on it. It''''s almost as if we cut out a section of the bitmap to place our sprite in.

The next thing to do is to Blit the actual colored sprite onto the destination at the same coordinates as the mask. For this Blit we use the vbSrcPaint raster operation, which will make the destination equal to the source combined with the destination in an OR operation.

For the black areas of the sprite this would give a background color of:

Source: 0000 0000 OR Destination: 1100 1001 Result:

1100 1001

The same color as the original destination. So now the black areas of the sprite become the same color as the destination pixels they are combined with.

For the Blue areas (we define blue as 0011 1111, which might not be the truth on your system) the resulting colors would be:

Source: 0011 1111 OR Destination: 0000 0000 Result:

0011 1111

Which is the color blue.

So to sum up, in order to draw a transparent sprite:

  • Make all the areas of the sprite that should be transparent black
  • Create the mask, making all the areas that should be transparent on the original sprite white. Make all the areas that should be blitted unchanged to the destination black.
  • BitBlt the Mask onto the destination with the vbSrcAnd raster constant.
  • BitBlt the original sprite with the vbSrcPaint raster operator.
     

Let''''s take a look at the TRANSBLT sample project for this section which demonstrates the drawing of a transparent sprite. This sample is contained in the TRANSBLT.ZIP file.

The sample project has two picture boxes, picSprite and picMask. They contain the sprite and the mask. The images in these picture boxes will be blitted onto the form, and create the transparent sprite. The only enabled button at the start of the project is the cmdDrawMask button. This button will obviously draw the mask onto the form, with the vbSrcAnd raster operator. The code in the button looks like this:


Private Sub cmdDrawMask_Click()
''''Draws the mask with vbSrcAnd raster operation
BitBlt Me.hDC, 0, 0, picMask.ScaleWidth, picMask.ScaleHeight, picMask.hDC, 0, 0, vbSrcAnd
End Sub


The round black circle of the mask should now appear on your Form in top left corner.

The next thing to do is to draw the actual sprite (the blue circle) onto the form, at the exact same location as the mask. This is done is the cmdDrawSprite command button:


Private Sub cmdDrawSprite_Click()
''''Draws the sprite witht the vbSrcPaint raster operation
BitBlt Me.hDC, 0, 0, picSprite.ScaleWidth, picSprite.ScaleHeight, picSprite.hDC, 0, _
  0, vbSrcPaint
End Sub


Now all you should see is the blue circle, which is our transparent sprite.

Simple movement

This is all very nice for non-moving sprites, but what about moving sprites? Well, moving a sprite is not very complex, as you have probably already guessed it is a simple matter of varying the X and Y coordinates of the destination rectangle.

 

 

To demonstrate this, take a look at the MOVING1 sample project found in MOVING1.ZIP. This simple program uses a timer to vary the X and Y coordinates of the sprite, and thus makes it move.

The actual drawing process is the same as in the foregoing section:

  •  First the mask
  •  Then the actual sprite

To start the movement, click the cm

上一页  [1] [2] [3]  下一页


[Sql Server]Sql精妙语句--各种求值函数  [网页制作]网页表格之---多个表格纵向排列
[网页制作]JavaScript另类用法--读取和写入cookie  [网页制作]号称非常安全的上网工具---360安全浏览器介绍
[办公软件]信息技术教学篇---Word工具栏的显示、隐藏及四种菜…  [操作系统]开始菜单---运行命令大总结
[操作系统]网络转载---64位操作系统与32位的区别  [操作系统]ldap:///(没有响应)Windows无法访问指定设备、路径…
[网络技术]安全篇---交换机设置方法介绍  [聊天工具]Real10 & Xpdf installation on Linux Box
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台