SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

~ DAGOVAR :: A VISUAL BASIC GAME ~

API FUNCTIONS TUTORIAL FOR GAMES

For 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.

BitBlt

We 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.

BitBlt PicScreen.hDC, 260, 160, 100, 100, PicSurface.hDC, 10, 156, SRCAND 'Draws the transparent area of the graphic
BitBlt PicScreen.hDC, 260, 160, 100, 100, PicSurface.hDC, 210, 10, SRCPAINT 'Draws the visible area of the graphic


Bitblt function example

SndPlaySound

This 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.

SndPlaySound App.path & “disparo.wav”, SND_ASYNC

ShowCursor

It 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

Showcursor False 'Hides the mouse cursor
Showcursor True 'Shows the mouse cursor


GetTickCount

This 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

Dim Variable as Long
Variable = GetTickCount


GetKeyState

This 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.

Dim KeyLeft
Dim KeyRight
Dim KeyUp
Dim KeyDown

KeyLeft = GetKeyState(vbKeyLeft)
KeyRight = GetKeyState(vbKeyRight)
KeyUp = GetKeyState(vbKeyUp)
KeyDown = GetKeyState(vbKeyDown)

If KeyRight And KEY_DOWN Then
... 'If Right key is pressed execute the corresponding code
If KeyUp And KEY_DOWN Then
... 'If Right key and Up key are pressed at the same time, execute the corresponding code
End if
End if


GetCursorPos

This 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.

Public btnMousePosition As POINTAPI
Call GetCursorPos(btnMousePosition)
If btnMousePosition.X <= 800 And btnMousePosition.Y <= 600 Then
... 'If the coordinates of the mouse cursor are in a certain area, execute the corresponding code
End if


IntersectRect

This 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.

Dim TempRectángulo as RECT
Dim Rectángulo1 as RECT
Dim Rectángulo2 as RECT

... 'Here we would fill the values of the rectangles
If IntersectRect(TempRectángulo,Rectángulo1,Rectángulo2) = True Then
... 'If the rectangles are intersecting execute the corresponding code
End If


About Dagovar

~ About Dagovar ~

Defining characters

~ Defining characters ~

Desert Vixens 2 maps

~ Desert Vixens 2 maps ~

Desert Vixens 3 maps

~ Desert Vixens 3 maps ~

Tile-based terrains

~ Tile-based terrains ~

Random maps

~ Random maps ~

Screenshots

~ Screenshots ~

Algorithms

~ Algorithms ~

Funciones API para juegos

~ Este tutorial en español ~

Source code examples

~ Source code examples ~



Return To Index

Privacy Policy