📄 tiletutor.htm
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="Microsoft Word 97">
<TITLE>Visual Basic Explorer - VB Tile Based Game Basics</TITLE>
</HEAD>
<BODY LINK="#008080" VLINK="#008080" ALINK="#008080">
<FONT COLOR="teal"><H1>Doing Tiles in Visual Basic</H1></FONT>
<HR>
<IMG ALIGN="right" SRC="library/librarystuff/book.gif">
<STRONG>Name:</STRONG> <EM>Doing Tiles in Visual Basic
</EM>
<BR>
<STRONG>Author:</STRONG> <EM>Søren Christensen</EM>
<BR>
<STRONG>Date:</STRONG> <EM>September 21, 1998</EM>
<BR>
<STRONG>Level:</STRONG> <EM>All</EM>
<BR>
<STRONG>Credits:</STRONG> <EM>This tutorial was graciously provided by Søren Christensen who is part owner of Rankan Software. Rankan Software has developed the EasyX ActiveX control, which enables VB programmers to tap into the power of DirectX. Visit them at the <a href="http://www.rankan.com/" TARGET="_top">Rankan Software</A> web site.</EM>
<HR>
<FONT FACE="Verdana" SIZE=2>
<P>Much can be said about Visual Basic, but it has never been the preferred environment for game developers, but with the increased support for DirectX in Visual Basic (rumor has it that Microsoft is developing support for DirectX in Visual Basic even as I speak), this will probably change. But even as DirectX is a big thing, it is not everything when it comes to Windows gaming, especially when we are dealing with Visual Basic. So since the DirectX technologies are not officially supported, I will concentrate solely on using the normal Windows functions in the quest of showing you how tile based gaming can be implemented in Visual Basic.</P>
<P>Before beginning on the actual coding, a brief explanation of some of the technical terms would be appropriate. </P>
<P>API:</U> The <I>Application Programming Interface</I>. In this tutorial the word API refers to the Window Application Programming Interface. This interface consists of all the functions, which can be used to make a Win32 program. You should have a basic knowledge on what the API is, and how to use it in VB. All the specific API functions used in this tutorial will be briefly explained. </P>
<U>
<P>Device Context:</U> A device context is a Win32 structure, which defines several graphic attributes for a device (screen, printer or file). All graphics based API calls are made through a device context object, which ensures device-independence and encapsulates the actual hardware drawing.</P>
<P> </P>
<B><P>The <I>Gamefunctions.bas </I>module</P>
</B><P>Before doing any tiling, I believe a review of some of the functions in the <I>GameFunctions </I>module is appropriate. Some of the functions are not used in this project, and will therefore not be reviewed here. </P>
<P> The GameFunctions module contains some generic and quite useful functions for the VB game developer. Most important are the graphics functions, which are used to load graphics into a usable device context, from where it can be blitted. Also quite useful are the keyboard functions, which can be used to test for a pressed key. But enough with the rambles and let抯 get down with the technical specifications. </P>
<P> </P>
<P>The <I><U>GenerateDC</U> </I>function:</P>
<P>This function is the function I find most useful when working with graphics in VB. It will load a bitmap from file and generate a Device Context for it, so the various graphics functions are able to access it. Not only that, but it also makes is possible to create a game in VB without using a picturebox for loading the bitmap. Not that the picturebox is a bad thing, but it still provides additional overhead when used as a storage for pictures, especially in view of the fact that a normal game would require a lot of picture boxes for even the simplest animations. </P>
<P> The function takes one parameter, a string variable, which is the file name of the bitmap to load. </P>
<P> The first thing to do in creating a DC (device context) is logically to call the CreateCompatibleDC API function, which returns a freshly created handle to a compatible DC. It is important to check the return value from this function, since the handle is essential for the rest of the operations in this function. Simply checking that the value is not less than 1 does this. </P>
</FONT>
<PRE><FONT FACE="Courier" SIZE=2>
DC = CreateCompatibleDC(0)
If DC < 1 Then
GenerateDC = 0
Exit Function
End If
</FONT></PRE>
<FONT FACE="Verdana" SIZE=2>
<P> </P>
<P>Though we know have a handle to a device context, the context is still unusable, since it has not dimensions or colors, so we need a handle to the bitmap in order to get this information. To do this we use the LoadImage API function, which will load an image from a file and return a handle to it. The LoadImage function takes a few parameters, which are specified as constants (IMAGE_BITMAP, LR_LOADFROMFILE and LR_CREATEDIBSECTION). These are used to specify that we want to load the bitmap from the file specified in <I>lpsz</I> parameter, and that we want to create a DIB (device independent bitmap). This function does not work under WinNT, at least not with the LR_LOADFROMFILE flag, so beware of this. After calling the function a rudimentary error check is done to see if the bitmap was loaded. If it was not, the previous created DC will be destroyed and the function will return with 0. </P>
<P><i>Note: The next two lines should be all on one line.</i></p>
</FONT><PRE><FONT FACE="Courier" SIZE=2>
hBitmap = LoadImage(0, FileName, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
If hBitmap = 0 Then
DeleteDC DC
GenerateDC = 0
Exit Function
End If
</FONT></PRE>
<FONT FACE="Verdana" SIZE=2><P> </P>
<P>Now we have a DC and a handle to a bitmap, so know we need to combine these into a usable DC. This is done with the SelectObject API function. This function will select the bitmap into the DC and thereby specifying what we need, the dimensions and the colors for the DC. After this is done, we can return the DC as the workable device context, but we still need to do some cleaning, since we still have the bitmap loaded into memory. It can be deleted with the DeleteObject API function, passing the handle of the bitmap as the sole argument.</P>
</FONT>
<PRE><FONT FACE="Courier" SIZE=2>
SelectObject DC, hBitmap
GenerateDC = DC
DeleteObject hBitmap
</FONT></PRE>
<FONT FACE="Verdana" SIZE=2>
<P>So now a usable DC with a bitmap has been created, but this does not mean that it is all over, far from indeed. Since we have created a DC, with the CreateCompatibleDC function, we also need to delete it again, in order to save resources. This is done with the DestroyDC function in the GameFunctions module. </P>
<P>The <I>DestroyDC </I>function:</P>
<P> As mentioned, this function will destroy a device context created with the GenerateDC function. It is a simple function, which first check to see if the passed DC is valid and then tries to destroy it with the DeleteDC function. It returns the value from DeleteDC function. </P>
</FONT><PRE><FONT FACE="Courier" SIZE=2>
If DC > 0 Then
DestroyDC = DeleteDC(DC)
End If
</FONT></PRE><FONT FACE="Verdana" SIZE=2>
<P>The <I>Check<B>[Key]</B>Key </I>functions</P>
<P>These functions (5 in all) checks if a specific key is pressed. They all return true if the key is pressed. So what抯 the use of these functions, when there is already the <I>Form_Key___</I> events? Well I抣l explain later in the section, where I discuss the <I>Main Game Loop.</I> So to get back to these Key functions. They are all based on the GetKeyState API function. The GetKeyState function checks if the specific key is pressed and returns an appropriate value that is either -127 or
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -