home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD15906342001.psc / yiGEngine.bas < prev    next >
Encoding:
BASIC Source File  |  2000-03-03  |  6.4 KB  |  178 lines

  1. Attribute VB_Name = "YIGE"
  2.  
  3. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. 'yar-interactive 2D game engine for Visual Basic     :
  5. '                                                    :
  6. '  http://www.yarinteractive.com                     :
  7. '                                                    :
  8. 'IMPORTANT:                                          :
  9. 'We'll be releasing a full version of this engine in :
  10. 'About two weeks from now (March 20th 2001) be sure  :
  11. 'to check our website then for your free copy        :
  12. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  13.  
  14. 'read the comments in each sub for more info on
  15. 'how to utilize it...
  16. Option Explicit
  17.  
  18. ''''''These Variables are for the example game'''''''
  19. Public CurX As Integer
  20. Public CurY As Integer
  21. Public UX As Integer
  22. Public UY As Integer
  23. Public TX As Integer
  24. Public TY As Integer
  25. Public AY As Integer
  26. Public AX As Integer
  27. Public kflag As Boolean
  28.  
  29. Public UDir As Byte
  30. Public TDir As Byte
  31.  
  32. Public iHeight As Integer
  33. Public iWidth As Integer
  34. Public FPS As Integer
  35. Public gpos As Integer
  36. 'Leave the rest of them though...
  37.  
  38. Private Const CCDEVICENAME = 32
  39. Private Const CCFORMNAME = 32
  40.  
  41. Private Const DISP_CHANGE_SUCCESSFUL = 0
  42. Private Const DISP_CHANGE_RESTART = 1
  43. Private Const DISP_CHANGE_FAILED = -1
  44. Private Const DISP_CHANGE_BADMODE = -2
  45. Private Const DISP_CHANGE_NOTUPDATED = -3
  46. Private Const DISP_CHANGE_BADFLAGS = -4
  47. Private Const DISP_CHANGE_BADPARAM = -5
  48.  
  49. Private Const CDS_UPDATEREGISTRY = &H1
  50. Private Const CDS_TEST = &H2
  51.  
  52. Private Const DM_BITSPERPEL = &H40000
  53. Private Const DM_PELSWIDTH = &H80000
  54. Private Const DM_PELSHEIGHT = &H100000
  55. Public Const Cmd1Color = &H808080
  56.  
  57.  
  58. Private Type DEVMODE
  59.   dmDeviceName As String * CCDEVICENAME
  60.   dmSpecVersion As Integer
  61.   dmDriverVersion As Integer
  62.   dmSize As Integer
  63.   dmDriverExtra As Integer
  64.   dmFields As Long
  65.   dmOrientation As Integer
  66.   dmPaperSize As Integer
  67.   dmPaperLength As Integer
  68.   dmPaperWidth As Integer
  69.   dmScale As Integer
  70.   dmCopies As Integer
  71.   dmDefaultSource As Integer
  72.   dmPrintQuality As Integer
  73.   dmColor As Integer
  74.   dmDuplex As Integer
  75.   dmYResolution As Integer
  76.   dmTTOption As Integer
  77.   dmCollate As Integer
  78.   dmFormName As String * CCFORMNAME
  79.   dmUnusedPadding As Integer
  80.   dmBitsPerPel As Integer
  81.   dmPelsWidth As Long
  82.   dmPelsHeight As Long
  83.   dmDisplayFlags As Long
  84.   dmDisplayFrequency As Long
  85. End Type
  86.  
  87. Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
  88. Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  89.  
  90. Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwflags As Long) As Long
  91. 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
  92. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  93. Public Declare Function GdiFlush Lib "gdi32" () As Long
  94. Public Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
  95. Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  96.  
  97.  
  98. '###### YIGE Functions #######
  99.  
  100. 'Change Screen settings allows you to specifie the display mode,
  101. 'the recommended display mode for this engine is 640 X 480 With 16 bit color
  102. 'That display mode can be accessed like this:
  103.  
  104. 'ChangeScreenSettings 640, 480, 16
  105. '(or 24 bit color - it makes it run alot faster at 24)
  106.  
  107. 'To restore their screen mode when the game is done, do as follows
  108. 'Call RestoreScrnMode
  109.  
  110. Public Function ChangeScreenSettings(lWidth As Integer, lHeight As Integer, lColors As Integer)
  111. Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
  112. lIndex = 0
  113. Do
  114.   lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
  115.   If lTemp = 0 Then Exit Do
  116.   lIndex = lIndex + 1
  117.  
  118.   With tDevMode
  119.     If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight And .dmBitsPerPel = lColors Then
  120.       lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
  121.       Exit Do
  122.     End If
  123.   End With
  124. Loop
  125. Select Case lTemp
  126.   Case DISP_CHANGE_SUCCESSFUL
  127. '    MsgBox "The display settings change was successful", vbInformation
  128.   Case DISP_CHANGE_RESTART
  129.     MsgBox "The computer must be restarted in order for the graphics mode to work", vbQuestion
  130.   Case DISP_CHANGE_FAILED
  131.     MsgBox "The display driver failed the specified graphics mode", vbCritical
  132.   Case DISP_CHANGE_BADMODE
  133.     MsgBox "The graphics mode is not supported", vbCritical
  134.   Case DISP_CHANGE_NOTUPDATED
  135.     MsgBox "Unable to write settings to the registry", vbCritical
  136.   Case DISP_CHANGE_BADFLAGS
  137.     MsgBox "An invalid set of flags was passed in", vbCritical
  138. End Select
  139. End Function
  140.  
  141. 'Call this procedure directly before calling the ChangeScreenSettings
  142. 'function so we cn restore their settings later,
  143. '(see RestoreRes function)
  144. Public Sub RememberScreenRes()
  145. iWidth = Screen.Width \ Screen.TwipsPerPixelX
  146. iHeight = Screen.Height \ Screen.TwipsPerPixelY
  147. End Sub
  148.  
  149. Public Sub PauseSystem(StopDurationInMilliseconds As Long) 'Stop the engine
  150.  Sleep (StopDurationInMilliseconds) 'bring CPU into a hault
  151. End Sub
  152.  
  153. Public Sub RestoreRes() 'restore the screen settings...
  154. ChangeScreenSettings iWidth, iHeight, 24
  155. GdiFlush
  156. ShowCursor 1
  157. End Sub
  158.  
  159.  
  160. 'This is the main part of this engine that does the blitting
  161. 'use the DrawSprite sub to draw your sprites to the game screen
  162. '(See our example)
  163.  
  164. 'Call the CLS and Refresh functions of the game
  165. 'surface (game surface is usually a picturebox
  166. 'or form, its the object that all the sprites are displayed in...
  167. Public Sub DrawSprite(GameSurf As Object, SpriteSource As Object, SpriteX As Integer, SpriteY As Integer, SpriteWidth As Integer, SpriteHeight As Integer, DrawMode As Long, SpriteID As Integer)
  168. BitBlt GameSurf.hDC, SpriteX, SpriteY, SpriteWidth, SpriteHeight, SpriteSource.hDC, SpriteSource.ScaleLeft, SpriteSource.ScaleTop, DrawMode
  169. End Sub
  170.  
  171. Public Function IsKeyDown(AsciiKeyCode) As Boolean
  172. 'use this function to tell if a key
  173. 'is down, it can detect multiple kepresses, unlike the
  174. 'keydown function... (See Example Game)...
  175. If GetKeyState(AsciiKeyCode) < -125 Then IsKeyDown = True
  176. End Function
  177.  
  178.