~ DAGOVAR :: A VISUAL BASIC GAME ~
API FUNCTIONS TUTORIAL FOR GAMESFor making games with the API technology (Application Programming Interface) of Windows we must know some useful functions. The API of Windows contains hundreds of functions, but only some of them will have application on this field. I will show a bunch of them, explaining them as far as my knowledge would allow me.
BitBltWe will use this function to draw all the graphics for the games. It is similar to PaintPicture method found on forms and PictureBoxes, and it allows us to draw the pictures with transparent background if we want. This function is supposed to be faster than the PaintPicture method. For using this function in a project, we must paste the following code into a .bas module. Every API function and its constants must be declared in a .bas module; we cannot declare them in .frm modules.
Public Declare Function BitBlt Lib "gdi32" (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
Public Const SRCCOPY = &HCC0020
Public Const SRCERASE = &H440328
Public Const SRCINVERT = &H660046
Public Const SRCPAINT = &HEE0086
Public Const SRCAND = &H8800C6
Public Const WHITENESS = &HFF0062
Public Const BLACKNESS = &H42
These are the parameters:
HdestDC - In this parameter we will specify the destination graphical device - for example a PictureBox - where we want to draw the graphic.
X - In this parameter we will specify the X coordinate in the destination graphical device where we want the graphic to be drawn.
Y - In this parameter we will specify the Y coordinate in the destination graphical device where we want the graphic to be drawn.
NWidth - In this parameter we will specify the width of the graphic to be drawn.
NHeight - In this parameter we will specify the height of the graphic to be drawn.
HSrcDC - In this parameter we will specify the source graphical device - for example a PictureBox - that contains the graphic that we want to draw.
XSrc - In this parameter we will specify the X coordinate in the source graphical device where it is placed the graphic to be drawn.
YSrc - In this parameter we will specify the Y coordinate in the source graphical device where it is placed the graphic to be drawn.
DwRop - In this parameter we will specify one of the constants of the function, that define how the pixels of the graphic to be drawn will combinate with the pixels of the destination graphical device. For instance, if we use SRCCOPY, the graphic will be drawn as opaque, if we use SRCAND, the pixels of the graphic to be drawn will mix with the pixels of the destination graphic device, if we use SRCINVERT the graphic will be drawn with inverted colors... For the rest of them, I don't know what they do, but knowing this will be enough to draw graphics with an invisible background.
SndPlaySoundThis function plays .wav sound files, but it can only play one sound at the same time.
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public Const SND_PURGE = &H40
Public Const SND_NOWAIT = &H2000
lpszSoundName - In this parameter we will specify the name of the sound file that we want to play.
uFlags - In this parameter we will specify one of the constants that define the way the sound is played. I don't know what all of these functions do, but the most used is SND_ASYNC.
ShowCursorIt is used to show or hide the Windows mouse cursor. It can cause misfunctioning in the programs, so if this happens, instead of using this function, you can create an icon file with no picture and assign it to the MouseIcon property, and this will render as invisible the mouse cursor.
Public Declare Function ShowCursor Lib "user32" (ByVal bShow As Integer) As Integer
GetTickCountThis function returns the number of miliseconds elapsed since Windows was started. We will use this function to make all the time-based calculations in our games, because it is very precise.
Public Declare Function GetTickCount Lib "kernel32" () As Long
GetKeyStateThis function is used to detect which keys are being pressed in the keyboard. We need to use this function when we want our game to detect more than one key pressed at the same time, because the KeyDown event of Visual Basic can only detect one single key at the same time.
Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Public Const KEY_TOGGLED As Integer = &H1
Public Const KEY_DOWN As Integer = &H1000
This function has only a parameter, which specifies the constant of the key that we want to control.
GetCursorPosThis function allows to know the mouse cursor position on the screen, no matter which Visual Basic control is below it.
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
This function only has a parameter, which uses a POINTAPI type variable to store the X and Y coordinates of the position where the mouse cursor is.
IntersectRectThis function compares the positions of two rectangles and determines if they intersect or not. It is used to determine if two 2D objects collide or not, because these are always defined by a rectangular area. This function is quite simple, and it is only useful for flat 2D games and not for isometric 2D games.
Public Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
lpDestRect - In this parameter we will specify a rectangle that the function will use to save the values of the intersection coordinates from the two rectangles to be compared. If there is not intersection, it will put 0 in all the values. Using these values, we can know not only if the rectangles are intersecting, but also the size of the overlay area.
lpSrc1Rect - In this parameter we will specify the first rectangle to be compared.
lpSrc2Rect - In this parameter we will specify the second rectangle to be compared.
~ About Dagovar ~
~ Defining characters ~
~ Desert Vixens 2 maps ~
~ Desert Vixens 3 maps ~
~ Tile-based terrains ~
~ Random maps ~
~ Screenshots ~
~ Algorithms ~
~ Este tutorial en español ~
~ Source code examples ~
Return To Index