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

Drawing & Animation I

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2053 更新时间:2009/4/23 16:39:27

Drawing & Animation

Using the Win32 GDI #1

This three part tutorial first appeared some years ago on the old VBExplorer.com . Since then several errors and bugs have been discovered by various users on the VBExplorer.com Forums. This version reflects the changes made to overcome the bugs and erros. The text will note where a bug correction and / or update has been done, and of course also why. 
A big ''''Thank you'''' goes out to all the people reading and reporting the bugs in the previous version of this tutorial. Please provide comments, questions, bug reports etc. at the VBExplorer.com forums in the Graphics & Game Programming section.

Graphics

One of the most important aspects of any game are the graphics. Graphics provide a visual interface for game play and more importantly, the means to explore exciting and fantastical virtual worlds. This means that cool graphics and smooth animation are key issues you will need to consider if you hope to create the right atmosphere and environment for enjoyable game play. While certainly not the only issues, they are two of the main factors that will affect the popularity and possible commercial success of your game.

Download Sample Programs Here.

Graphics programming in the world of Windows and GDI is largely based on Bitmaps. A bitmap is a structure, generally composed of a multitude of small dots, known as pixels, each being of a specific color. A bitmap file, with the extension BMP, is the file format that will be used throughout this book to store graphical elements. We will go a bit more into what a bitmap actually is and discuss various methods to manipulate them from both a programming and a design perspective.

The GDI system is part of the Win32 API. The GDI system contains the functions that draw and manipulate the visual interface of Windows. The GDI system is the system we, as game programmers, are most interested in.

There are many ways to display a bitmap in Visual Basic. Let''''s look a one of the simplest which is the Picture property.

A standard Form has a Picture property, which can be used to display a bitmap. The other two controls most often used for presenting graphics in Visual Basic, are the Image Control and the Picture Box. Of these two, the Picture Box is the control you are likely to use most often as a game programmer.

The main reason for using the PictureBox control, instead of the image control, is that it has a hDC property. This hDC property is used when we draw bitmaps to and from our drawing areas using an API function, which we will discuss next.

An hDC is also known as a Device Context, or more correctly it is the handle to a device context. A Device Context is a Win32 object, which we create in memory, which will be used to draw graphics to and from. A DC also many other associated attributes, besides the graphics attribute. Among these attributes are the Font, Brush, Pen, Drawing Mode which we''''ll discuss a little later. 

 

Drawing a Bitmap

Most games with graphics must be able to rapidly draw several graphics repeatedly from a source context onto the destination, which would be the actual gaming area. The need for this is quite obvious, since few games are compromised of a single graphic file with no animation. Certainly none that would interest a famous future game developer like you. So in order to make our games more interesting we need a way to draw a bitmap (or parts of it) from one place (the source), to another place (the destination).

The Win32 API is a collection of functions, contained in various DLL''''s, which form the backbone of windows. They include all the functions the operating system uses to handle memory, disk operations and anything else it may be called to accomplish. Even though Visual Basic controls encapsulate a lot of this functionality, thus hiding a lot of the complexity, there is usually a performance penalty to be paid for this lack of complexity. There are also some things that you just can''''t do without accessing the API directly.
The good news is that these functions are available to you. Though some API calls are more difficult to use and may contain some scary syntax, judicious use of API''''s can enhance the functionality and features you can add to your programs and are sometimes the only way to do what needs to be done.

The Win32 GDI system has a very special function for just the sort of thing that we need to do, and that is the BitBlt function. We will use this function throughout this book, so pay special attention to it. The function is declared as follows:

Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _

(ByVal hDestDC 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 dwRop As Long) As Long

 

 

This function draws the area defined from one hDC (Device Context) to another Device Context. Anything graphical in the source device context is drawn on the destination device context. Already here you can observe the use of the hDC property of a picturebox, since this means that we can draw the contents of one picturebox to another picturebox, simply by specifying the device context (hDC).

Let''''s take a look at the parameters in the BitBlt function:

hDestDC:The destination Device Context. XThe upper X-coordinate on the destination area. YThe upper Y-coordinate on the destination area.nWidhtThe Width of the drawing area in pixels. nHeightThe Height of the drawing area in pixels. hSrcDCThe Source Device Context. xSrcThe upper X-coordinate of the source area. ySrc The upper Y-coordinate of the source area.dwRop The Raster operation constant.

 

At this point you can really impress your family and friends by using words like hDC, dwRop and BitBlt in casual conversation and are likely to be the life of the party but if your goal is to actually write a game we''''ll need to go on. Let''''s try a simple project to see how this all works.

BitBlt Test

This sample project demonstrates the use of the BitBlt function to draw a bitmap from a picturebox to a form. This sample project is found in BITBLTTEST.ZIP .

In the project we have a Form (frmBitBlt), a Picturebox with a small picture (picBitBlt) and a command button (cmdBitBlt).

NOTE: Since the BitBlt function expects the parameters to be in pixels, the ScaleMode property of the picturebox and the form has been changed from 1-Twips to 3-Pixels.

The code for this small program is very simple:

Option Explicit



Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _

(ByVal hDestDC 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 dwRop As Long) As Long

 


Private Sub cmdBitBlt_Click()

Me.Cls
BitBlt Me.hDC, 0, 0, picBitBlt.ScaleWidth, _
       picBitBlt.ScaleHeight, picBitBlt.hDC, 0, 0, vbSrcCopy

End Sub

 

When the button is clicked, the BitBlt function is called to draw the Picture of the picBitBlt picture box onto the form. We specify the hDestDC parameter of the BitBlt function to be that of the current form (Me.hDC). Then we specify the upper left X and Y coordinates of the destination to be 0. The Width and Height of the picture on the Destination is defined to be the ScaleWidth and ScaleHeight of the picture box. This ensures that we will not be copying the border of the picture box, but only the elements inside the picture box. The hSrcDC is set to the source hDC, which is of course the picBitBlt picturebox. We specify that the drawing should start at 0,0. The width and height of the source will then be the same as the width and height of the destination area.

Press the command button and observe how the picture is drawn onto the form.

 

The observant reader might want to protest a little bit now about the use of an API function, instead of the native VB PaintPicture method. After all, PaintPicture uses BitBlt to do its work so if they both do the same thing, why use the API? The reason for this is simple; the PaintPicture method is extremely slow, compared to the speed that can be achieved by using the BitBlt function directly. A quick test using each to copy the same picture shows that PaintPicture takes almost 10 times as long to copy a picture! In a game, where speed in serving up your graphics is a big issue, I''''m sure you will agree this would yield unacceptable performance.

 

Sprites, Raster and Masks

The Sprite

Let us take a step away from the actual coding and have a good look at a very central aspect of almost any graphical game, The Sprite. The Sprite or rather the sprites are the small things that move around the gaming field, either controlled by a

[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……
    咸宁网络警察报警平台