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

Drawing & Animation III

作者:闵涛 文章来源:闵涛的学习笔记 点击数:4667 更新时间:2009/4/23 16:39:28
d and drawn in the game. But as you also might have noticed, the Timer does not have a very good resolution. Put the somewhat irregular firing of the timer event on top of that, and you have a very bad scenario for games.

A much better scenario is to let the computer run as fast as it possible can, and then slow the game loop down with a defined time interval. This way we get maximum speed, and are still able to control the timing of the loop. You may sometimes hear game programmers refer to this as a throttle.

This can easily be accomplished in much the same way as we defined the frame ratio of the animated sprites in the previous project. The sample project in GAMELOOP.ZIP, demonstrates a possible implementation of such a scheme. The actual game loop is a procedure called RunGameLoop. The frame of the game loop is as such:


Private Sub RunGameLoop()

Const TickDifference As Long = 20
Dim LastTick As Long
Dim CurrentTick As Long

''''Show the form
Me.Show

Do
    CurrentTick = GetTickCount()
      
    If CurrentTick - LastTick > TickDifference Then
                
        ''''Do the game drawing and calculation here
        
        ''''Update the frame variable
        LastTick = GetTickCount()                
    Else        
        ''''It is not time yet, do something else here
    End If
   
   
    DoEvents
   
Loop

''''If we are here we are finished
Unload Me
Set frmGameLoop = Nothing

End Sub


We have two very important variables here, the LastTick, which keeps the time of the last time the game functions were executed and the CurrentTick, which keeps track the current time. The constant, TickDifference, is the actual time interval, which must pass before a frame update will occur.

So the trick is to simply subtract the CurrentTick amount from the LastTick, and test whether the difference between the variables is greater than the required time interval. If the difference is greater, then the game drawing and calculation must proceed, if not, then other things can be done. This comparison is tightly situated inside a Do匧oop which apparently never ends. So you somehow need an outside breaking of the loop. In the chapter about user input you will see several methods of testing whether a given key has been pressed, and based on that, exit the program.

In the sample project you can observe how much more effective this scheme is compared to the use of timers. The project simply draws a sprite and moves it over the screen. Run the sample project and push the Start normal Timer button. Then press the Start Loop button and observe the difference.

There are many other ways to implement a game loop. The one shown here is the one which, we will use throughout this book. The reason for this should become apparent in later chapters. You will notice that some programmers control the loop with the Sleep API function. This is not always a good method, since it freezes the whole process, and thus makes any other events in the application impossible until the specified time has elapsed.

More on Bitmaps

Bitmaps are the essential graphical element in VB game programming. That is the reason that a solid understanding of bitmapped graphics is so important for the game-programmer. Unfortunately bitmaps in Win32 is a major subject, which could probably fill a whole book by itself, so only the most common and elemental subjects will be covered here. If you decide to continue your game-programming career you will want to continue to explore advanced graphics programming theories and techniques.

Types of Bitmaps

There are three basic types of bitmaps that we will concern ourselves with, the three most common bitmaps, 1, 8 and 24 bit bitmaps.

The number of bits in a bitmap represents the possible number of colors the given bitmap can contain. This means that a 1-bit bitmap can have two possible colors, which are always black and white. The 1 bit bitmap is also known as a monochrome bitmap.

The 8-bit bitmap can contain up to 256 colors (28 = 256) and the 24-bit bitmap can have up to 16.7 millions colors (224 = 16.7 million). The value X-bit defines the size of each pixel in a bitmap. So an 8-bit bitmap with a width of 100 and a height of 200 takes up 100*200*8 bit = 160 KBits = 20 KB.

This sounds pretty easy, but unfortunately there is a bit more to it. A color in our bitmaps (8 & 24 bits. 1-bits bitmap are either black or white), is represented by a 24-bit value of the type RGB. The RGB type color is divided into 3 x 8 bit sets. Each of these 8-bit sets (which we might conveniently call a byte) represent the actual amount of color for Red, Green or Blue, hence the name RGB. Take a look at the following illustration:

 

So in a 24-bit bitmap, each single pixel is actually a 24-bit structure representing one of the many possible colors.

But what about 8-bit bitmaps how can each pixel be a 24-bit color, when they only take up 8-bit of space? Well, to put it simply, by using color tables. A color table is an array of 24-bit colors. In 8-bit bitmaps the array has 256 posts (from 0 to 255). So each pixel in an 8-bit bitmap does not represent an actual physical color, but instead an index to the associated color table. In a color table you only have two colors you can depend on (and this is actually not always the truth either), namely black and white. Black is index 0 and white is index 255 in the color table.

The use of color tables is a very unfortunate thing, since it makes the process of doing things to 8-bit bitmaps a bit more complicated than the 憂ormal?24-bit bitmaps. But since computers are still comparatively slow, you do not have many choices if you want super fast graphics.

Getting to the Bits

This next section is for those with a strong heart. This information is not that important in the simple samples which we have shown so far, but it is very important if you want to access the actual bits and bytes and do some interesting manipulation of the bitmaps. The function which will return the actual bytes of the bitmap pixels to us, is the GetBitmapBits API function, and the function which will return the bytes back to the bitmap is the SetBitmapBits API function. Let us start by examining the GetBitmapBits function. This function is declared as such:

Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, _
                    ByVal dwCount As Long, lpBits As Any) As Long

 

The first parameter (hBitmap) is a handle to a bitmap in memory. If you recall we got a handle to a bitmap before, in the GenerateDC function. We''''ll use this handle when we load the bitmap now. To do this we have to change the GenerateDC function a bit. We have to pass a new parameter, which will receive the handle (it is by default passed by reference so this is no problem), and then of course we cannot delete the handle in the function. After this modification the GenerateDC will look like this (This corresponds to the updated way of doing this. Since this in actuality didn''''t contain any bugs - nothing has been changed).


''''IN: FileName: The file name of the graphics
''''    BitmapHandle: The receiver of the loaded bitmap handle
''''OUT: The Generated DC
Public Function GenerateDC(FileName As String, ByRef BitmapHandle As Long) As Long

Dim DC As Long
Dim hBitmap As Long

''''Create a Device Context, compatible with the screen
DC = CreateCompatibleDC(0)

If DC = 0 Then
    GenerateDC = 0
    ''''Raise error
    Err.Raise vbObjectError + 1
    Exit Function
End If

''''Load the image.

上一页  [1] [2] [3] [4] [5] [6]  下一页


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