0


DX7 for VB6 游戏引擎加强版

DX7 for VB6 游戏引擎加强版

  1. Option Explicit
  2. '**************************************************************
  3. '
  4. ' 《梦想之翼》
  5. '
  6. 'VB+DirectX7编写,包括图像、键盘、鼠标、声音处理。
  7. '
  8. '经过多次改进和完善,是一个比较易用的引擎。
  9. '
  10. ' ----作者:袁进峰
  11. ' 2004913
  12. ' ----加强:liuhan
  13. ' 2010730
  14. '
  15. '**************************************************************
  16. '======================《加装JPG作准备》=======================
  17. Private 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
  18. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  19. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  20. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  21. '========================《鼠标指针位置》======================
  22. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long '屏幕绝对坐标
  23. Private Declare Function showCursor Lib "user32" Alias "ShowCursor" (ByVal bShow As Long) As Long '显示或隐藏鼠标
  24. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  25. Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  26. Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long '转换屏幕绝对座标为对象相对坐标
  27. Public Type POINTAPI
  28. x As Long
  29. y As Long
  30. End Type
  31. '=======================《显示或隐藏鼠标》=====================
  32. 'Public Declare Function showCursor Lib "user32" Alias "ShowCursor" (ByVal bShow As Long) As Long
  33. '==================《用于显示、控制速度的函数》================
  34. Private Declare Function GetTickCount Lib "kernel32" () As Long
  35. Dim FPS_Count As Long
  36. '显示速度所用变量
  37. Dim mTimer As Long
  38. Dim AddFPS As Integer
  39. Public FPS As Integer
  40. '==============================================================
  41. Public Type POS
  42. x As Integer
  43. y As Integer
  44. End Type
  45. '======================Sleep函数控制速度=======================
  46. 'Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long) '-----Sleep函数
  47. '======================《窗口标题的高度,边宽度》======================
  48. Public Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
  49. Const SM_CYCAPTION = 4 ' Height of caption or title
  50. Const SM_CXFRAME = 32 ' Width of window frame
  51. Const SM_CYFRAME = 33 ' Height of window frame
  52. '======================《窗口标题的高度,边宽度》======================
  53. '设置窗体结构信息函数
  54. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  55. '获取窗体结构信息函数
  56. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  57. Private Const GWL_STYLE = (-16) '窗口样式
  58. Private Const WS_BORDER = &H800000 '创建一个有边框的窗体。
  59. Private Const WS_MAXIMIZE = &H1000000 '窗口最大化
  60. Private Const WS_CAPTION = &HC00000 '带标题栏的窗口
  61. Private Const WS_SYSMENU = &H80000
  62. Private Const WS_SIZEBOX = &H40000
  63. Private Const WS_MAXIMIZEBOX = &H10000
  64. Private Const WS_MINIMIZEBOX = &H20000
  65. Private 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
  66. Const HWND_TOPMOST = -1
  67. Const HWND_NOTOPMOST = -2
  68. Const SWP_NOSIZE = &H1
  69. Const SWP_NOMOVE = &H2
  70. Const SWP_NOACTIVATE = &H10
  71. Const SWP_SHOWWINDOW = &H40
  72. Private Declare Function ShowWindow Lib "user32.dll " (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  73. Private Const SW_MAXIMIZE As Long = 3
  74. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  75. Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
  76. 'liuhan
  77. Public goFull As Boolean
  78. Private Obj_STYLE As Long
  79. Private Obj_RECT As RECT
  80. Private Src_RECT As RECT
  81. '==============================================================
  82. Dim ObjhWnd As Long
  83. Dim BlthWnd As Long
  84. Dim Dx As New DirectX7
  85. Dim DDraw As DirectDraw7
  86. Public MainSurf As DirectDrawSurface7
  87. Public BackSurf As DirectDrawSurface7
  88. Dim Clipper As DirectDrawClipper
  89. Dim Gamea As DirectDrawGammaControl
  90. Public destRect As RECT
  91. Public srcRect As RECT
  92. Dim DI As DirectInput
  93. Public KeyDevice As DirectInputDevice
  94. Public KeyState As DIKEYBOARDSTATE
  95. Public dimouse As DirectInputDevice
  96. Public MouseState As DIMOUSESTATE
  97. Dim DSound As DirectSound
  98. Dim objdmloader As DirectMusicLoader
  99. Dim objdmperf As DirectMusicPerformance
  100. Public objdmseg As DirectMusicSegment
  101. Public objdmsegst As DirectMusicSegmentState
  102. Dim g_MapW As Integer
  103. Dim g_MapH As Integer
  104. Dim StdFont As New StdFont
  105. Dim Font As IFont
  106. Public Type DSurface
  107. Image As DirectDrawSurface7
  108. W As Integer
  109. H As Integer
  110. End Type
  111. Public Sub Window_Full()
  112. Dim iHwnd As Long
  113. iHwnd = GetWindowLong(ObjhWnd, GWL_STYLE) '获取原风格
  114. iHwnd = iHwnd And Not (WS_BORDER) '去除不用的风格
  115. iHwnd = iHwnd And WS_MAXIMIZE
  116. iHwnd = SetWindowLong(ObjhWnd, GWL_STYLE, iHwnd) '设置新的风格
  117. End Sub
  118. Public Sub Window_Mode()
  119. Dim iHwnd As Long
  120. 'iHwnd = GetWindowLong(ObjhWnd, GWL_STYLE) '获取原风格
  121. 'iHwnd = iHwnd Or WS_BORDER '加上自定义风格
  122. iHwnd = SetWindowLong(ObjhWnd, GWL_STYLE, Obj_STYLE) '设置新的风格
  123. 'iHwnd = SetWindowPos(ObjhWnd, HWND_NOTOPMOST, Obj_RECT.Left, Obj_RECT.Top, Obj_RECT.Right - Obj_RECT.Left, Obj_RECT.Bottom - Obj_RECT.Top, SWP_SHOWWINDOW Or SWP_NOACTIVATE)
  124. iHwnd = MoveWindow(ObjhWnd, Obj_RECT.Left, Obj_RECT.Top, Obj_RECT.Right - Obj_RECT.Left, Obj_RECT.Bottom - Obj_RECT.Top, 1)
  125. End Sub
  126. '初始化DDraw
  127. Public Sub InitEngine(FormhWnd As Long, _
  128. Optional Width As Long, Optional Height As Long, _
  129. Optional FullScreen As Boolean = False, _
  130. Optional FWidth As Integer = 640, Optional FHeight As Integer = 480, _
  131. Optional Color As Byte = 16, Optional Switch As Boolean = False)
  132. g_MapW = Width
  133. g_MapH = Height
  134. ObjhWnd = FormhWnd
  135. 'If FullScreen = True Then
  136. ' Window_Full
  137. 'SetWindowLong ObjhWnd, GWL_STYLE, STYLE_NONE
  138. 'Dim iHwnd As Long
  139. 'iHwnd = SetWindowPos(ObjhWnd, HWND_TOPMOST, 0, 0, Screen.Width, Screen.Height, SWP_SHOWWINDOW)
  140. 'ShowWindow ObjhWnd, SW_MAXIMIZE
  141. 'Else
  142. ' Window_Mode
  143. 'End If
  144. goFull = FullScreen
  145. Set DDraw = Dx.DirectDrawCreate("")
  146. '========================《设置显示模式》==============================
  147. If FullScreen = True Then
  148. Call DDraw.SetCooperativeLevel(FormhWnd, DDSCL_FULLSCREEN Or DDSCL_EXCLUSIVE)
  149. Call DDraw.SetDisplayMode(FWidth, FHeight, Color, 0, DDSDM_DEFAULT)
  150. Else
  151. Call DDraw.SetCooperativeLevel(FormhWnd, DDSCL_NORMAL)
  152. GetWindowRect ObjhWnd, Obj_RECT '获取位置高宽
  153. Obj_STYLE = GetWindowLong(ObjhWnd, GWL_STYLE) '获取样式风格
  154. End If
  155. '======================================================================
  156. '定义变量
  157. Dim ddsd As DDSURFACEDESC2
  158. '========================《设置主表面》================================
  159. ddsd.lFlags = DDSD_CAPS 'Or DDSD_BACKBUFFERCOUNT
  160. ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  161. Set MainSurf = DDraw.CreateSurface(ddsd)
  162. '========================《设置缓冲表面》==============================
  163. ddsd.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
  164. ddsd.lWidth = Width
  165. ddsd.lHeight = Height
  166. ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
  167. Set BackSurf = DDraw.CreateSurface(ddsd)
  168. '==========================《初始化字体》==============================
  169. Set Font = StdFont
  170. Font.Name = "宋体"
  171. '************************************************************
  172. Call InitDI(FormhWnd)
  173. Call InitWav(FormhWnd)
  174. Call InitMid
  175. If FullScreen = True Then Call initGamma '初始化Gamma
  176. End Sub
  177. '=======================《剪切》=======================================
  178. '窗体调用成功后,调用,必写
  179. Public Sub ClipperhWnd(hwnd As Long)
  180. BlthWnd = hwnd
  181. Set Clipper = DDraw.CreateClipper(0)
  182. Clipper.SetHWnd hwnd
  183. MainSurf.SetClipper Clipper
  184. Call Dx.GetWindowRect(hwnd, destRect)
  185. End Sub
  186. 'LoadImge(DirectDrawSurface7变量,图像路径,透明色)
  187. Public Function LoadImage(FileName As String, Optional Color As Long = &HF81F) As DSurface
  188. On Error GoTo LoadImageErr
  189. Dim ddsd As DDSURFACEDESC2
  190. ddsd.lFlags = DDSD_CAPS
  191. ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY
  192. '装载图像
  193. Set LoadImage.Image = DDraw.CreateSurfaceFromFile(FileName, ddsd)
  194. 'Set image = DDraw.CreateSurfaceFromResource(, "PIC1", ddsd)
  195. LoadImage.W = ddsd.lWidth
  196. LoadImage.H = ddsd.lHeight
  197. '设置透明色(liuhan2010-05-20自动取左上角为透明色)
  198. Dim Tcolor As Long, key As DDCOLORKEY
  199. LoadImage.Image.Lock srcRect, ddsd, DDLOCK_WAIT, BlthWnd '锁住页面
  200. Tcolor = LoadImage.Image.GetLockedPixel(0, 0) '获得 0,0 点的颜色
  201. LoadImage.Image.Unlock srcRect '解锁页面
  202. key.low = Tcolor
  203. key.high = Tcolor
  204. Call LoadImage.Image.SetColorKey(DDCKEY_SRCBLT, key)
  205. '设置透明色
  206. 'Dim key As DDCOLORKEY
  207. 'key.low = Color
  208. 'key.high = Color
  209. 'Call LoadImage.Image.SetColorKey(DDCKEY_SRCBLT, key)
  210. Exit Function
  211. LoadImageErr:
  212. MsgBox "没有找到" + FileName + "图像文件。"
  213. End Function
  214. '2010-06-12加入袁进峰的直接读出gif、jpg图像过程
  215. 'LoadImgeTDC(DirectDrawSurface7变量,图像路径,透明色)
  216. Public Function LoadImageTDC(FileName As String, Optional Color As Long = &HF81F) As DSurface
  217. On Error GoTo LoadImageErr
  218. Dim SDesc As DDSURFACEDESC2
  219. Dim TPict As StdPicture
  220. Set TPict = LoadPicture(FileName)
  221. SDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  222. SDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_SYSTEMMEMORY '加上DDSCAPS_SYSTEMMEMORY加快速度?
  223. SDesc.lHeight = CLng((TPict.Height * 0.001) * 567 / Screen.TwipsPerPixelY)
  224. SDesc.lWidth = CLng((TPict.Width * 0.001) * 567 / Screen.TwipsPerPixelX)
  225. Set LoadImageTDC.Image = DDraw.CreateSurface(SDesc)
  226. LoadImageTDC.W = SDesc.lWidth
  227. LoadImageTDC.H = SDesc.lHeight
  228. Dim SDC As Long, TDC As Long
  229. SDC = LoadImageTDC.Image.GetDC
  230. TDC = CreateCompatibleDC(0)
  231. SelectObject TDC, TPict.Handle
  232. BitBlt SDC, 0, 0, SDesc.lWidth, SDesc.lHeight, TDC, 0, 0, vbSrcCopy
  233. LoadImageTDC.Image.ReleaseDC SDC
  234. DeleteDC TDC
  235. '设置透明色(liuhan2010-05-20自动取左上角为透明色)
  236. Dim Tcolor As Long, key As DDCOLORKEY
  237. LoadImageTDC.Image.Lock srcRect, SDesc, DDLOCK_WAIT, BlthWnd '锁住页面
  238. Tcolor = LoadImageTDC.Image.GetLockedPixel(0, 0) '获得 0,0 点的颜色
  239. LoadImageTDC.Image.Unlock srcRect '解锁页面
  240. key.low = Tcolor
  241. key.high = Tcolor
  242. Call LoadImageTDC.Image.SetColorKey(DDCKEY_SRCBLT, key)
  243. '设置透明色
  244. 'Dim key As DDCOLORKEY
  245. 'key.low = Color
  246. 'key.high = Color
  247. 'Call LoadImageTDC.Image.SetColorKey(DDCKEY_SRCBLT, key)
  248. Set TPict = Nothing
  249. Exit Function
  250. LoadImageErr:
  251. MsgBox "没有找到" + FileName + "图像文件。"
  252. End Function
  253. '*********************************************************************
  254. 'BltFxImage(DirectDrawSurface7变量,输出目标位置XY,WH,择取源图XY,WH,透明色作用开关)2010-06-28
  255. Public Sub BltFxRDImage(Image As DSurface, xSrc As Integer, ySrc As Integer, Width As Integer, Height As Integer, _
  256. xImage As Integer, yImage As Integer, wImage As Integer, hImage As Integer, _
  257. Optional UseColorkey As Boolean = False)
  258. Dim ImageRECT As RECT '输入输出时图像的大小
  259. Dim BX As Integer, BY As Integer '输出图像的位置
  260. BX = xSrc
  261. BY = ySrc
  262. Dim wZoom As Single, hZoom As Single '目标、输出图像比例
  263. wZoom = wImage / Width
  264. hZoom = hImage / Height
  265. '-----------------源图像的大小------------------
  266. ImageRECT.Left = xImage
  267. ImageRECT.Top = yImage
  268. ImageRECT.Right = wImage
  269. ImageRECT.Bottom = hImage
  270. '-----------------目标图像的大小------------------
  271. destRect.Left = xSrc
  272. destRect.Top = ySrc
  273. destRect.Right = xSrc + Width
  274. destRect.Bottom = ySrc + Height
  275. 'DDBLTFX图方向结构
  276. Dim FX As DDBLTFX
  277. 'FX.lDDFX = DDBLTFX_MIRRORLEFTRIGHT
  278. 'FX.lDDFX = DDBLTFX_MIRRORUPDOWN
  279. FX.lDDFX = DDBLTFX_MIRRORLEFTRIGHT Or DDBLTFX_MIRRORUPDOWN
  280. '自己做的自动剪切处理,比DirectX提供的快很多
  281. '----------------若碰边自动剪切--------------------
  282. If xSrc < 0 Then
  283. destRect.Left = 0
  284. ImageRECT.Right = (xSrc + Width) * wZoom
  285. End If
  286. If ySrc < 0 Then
  287. destRect.Top = 0
  288. ImageRECT.Bottom = destRect.Bottom * hZoom
  289. End If
  290. If Width + xSrc > g_MapW Then
  291. destRect.Right = g_MapW
  292. ImageRECT.Left = (Width + xSrc - g_MapW) * wZoom
  293. If ImageRECT.Left >= wImage Then Exit Sub
  294. End If
  295. If Height + ySrc > g_MapH Then
  296. destRect.Bottom = g_MapH
  297. ImageRECT.Top = (ySrc + Height - g_MapH) * hZoom
  298. If ImageRECT.Top >= hImage Then Exit Sub
  299. End If
  300. 'If ImageRECT.Left >= ImageRECT.Right Then Exit Sub
  301. 'If ImageRECT.Top >= ImageRECT.Bottom Then Exit Sub
  302. '一点也没出画出来
  303. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  304. '-------------------------------------------------
  305. 'liuhan (2010-05-21)
  306. If UseColorkey = True Then
  307. '透明绘图(yes)
  308. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_KEYSRC Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  309. Else
  310. '透明绘图(no)
  311. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_WAIT Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  312. End If
  313. End Sub
  314. '*********************************************************************
  315. 'BltFxImage(DirectDrawSurface7变量,输出目标位置X、Y,W、H,择取源图X、Y,W、H,透明色作用开关)2010-06-28
  316. Public Sub BltFxDImage(Image As DSurface, xSrc As Integer, ySrc As Integer, Width As Integer, Height As Integer, _
  317. xImage As Integer, yImage As Integer, wImage As Integer, hImage As Integer, _
  318. Optional UseColorkey As Boolean = False)
  319. Dim ImageRECT As RECT '输入输出时图像的大小
  320. Dim BX As Integer, BY As Integer '输出图像的位置
  321. BX = xSrc
  322. BY = ySrc
  323. Dim wZoom As Single, hZoom As Single '目标、输出图像比例
  324. wZoom = wImage / Width
  325. hZoom = hImage / Height
  326. '-----------------源图像的大小------------------
  327. ImageRECT.Left = xImage
  328. ImageRECT.Top = yImage
  329. ImageRECT.Right = wImage
  330. ImageRECT.Bottom = hImage
  331. '-----------------目标图像的大小------------------
  332. destRect.Left = xSrc
  333. destRect.Top = ySrc
  334. destRect.Right = xSrc + Width
  335. destRect.Bottom = ySrc + Height
  336. 'DDBLTFX图方向结构
  337. Dim FX As DDBLTFX
  338. 'FX.lDDFX = DDBLTFX_MIRRORLEFTRIGHT
  339. FX.lDDFX = DDBLTFX_MIRRORUPDOWN
  340. '自己做的自动剪切处理,比DirectX提供的快很多
  341. '----------------若碰边自动剪切--------------------
  342. If xSrc < 0 Then
  343. destRect.Left = 0
  344. ImageRECT.Left = (Abs(xSrc) + xImage) * wZoom
  345. If ImageRECT.Left >= wImage Then Exit Sub
  346. End If
  347. If ySrc < 0 Then
  348. destRect.Top = 0
  349. ImageRECT.Bottom = destRect.Bottom * hZoom
  350. End If
  351. If Width + xSrc > g_MapW Then
  352. destRect.Right = g_MapW
  353. ImageRECT.Right = (g_MapW - xSrc) * wZoom
  354. End If
  355. If Height + ySrc > g_MapH Then
  356. destRect.Bottom = g_MapH
  357. ImageRECT.Top = (ySrc + Height - g_MapH) * hZoom
  358. If ImageRECT.Top >= hImage Then Exit Sub
  359. End If
  360. 'If ImageRECT.Left >= ImageRECT.Right Then Exit Sub
  361. 'If ImageRECT.Top >= ImageRECT.Bottom Then Exit Sub
  362. '一点也没出画出来
  363. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  364. '-------------------------------------------------
  365. 'liuhan (2010-05-21)
  366. If UseColorkey = True Then
  367. '透明绘图(yes)
  368. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_KEYSRC Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  369. Else
  370. '透明绘图(no)
  371. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_WAIT Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  372. End If
  373. End Sub
  374. '*********************************************************************
  375. 'BltFxImage(DirectDrawSurface7变量,输出目标位置X、Y,W、H,择取源图X、Y,W、H,透明色作用开关)2010-06-28
  376. Public Sub BltFxRImage(Image As DSurface, xSrc As Integer, ySrc As Integer, Width As Integer, Height As Integer, _
  377. xImage As Integer, yImage As Integer, wImage As Integer, hImage As Integer, _
  378. Optional UseColorkey As Boolean = False)
  379. Dim ImageRECT As RECT '输入输出时图像的大小
  380. Dim BX As Integer, BY As Integer '输出图像的位置
  381. BX = xSrc
  382. BY = ySrc
  383. Dim wZoom As Single, hZoom As Single '目标、输出图像比例
  384. wZoom = wImage / Width
  385. hZoom = hImage / Height
  386. '-----------------源图像的大小------------------
  387. ImageRECT.Left = xImage
  388. ImageRECT.Top = yImage
  389. ImageRECT.Right = wImage
  390. ImageRECT.Bottom = hImage
  391. '-----------------目标图像的大小------------------
  392. destRect.Left = xSrc
  393. destRect.Top = ySrc
  394. destRect.Right = xSrc + Width
  395. destRect.Bottom = ySrc + Height
  396. 'DDBLTFX图方向结构
  397. Dim FX As DDBLTFX
  398. FX.lDDFX = DDBLTFX_MIRRORLEFTRIGHT
  399. 'FX.lDDFX = DDBLTFX_MIRRORUPDOWN
  400. '自己做的自动剪切处理,比DirectX提供的快很多
  401. '----------------若碰边自动剪切--------------------
  402. If xSrc < 0 Then
  403. destRect.Left = 0
  404. ImageRECT.Right = (xSrc + Width) * wZoom
  405. End If
  406. If ySrc < 0 Then
  407. destRect.Top = 0
  408. ImageRECT.Top = (Abs(ySrc) + yImage) * hZoom
  409. If ImageRECT.Top >= hImage Then Exit Sub
  410. End If
  411. If Width + xSrc > g_MapW Then
  412. destRect.Right = g_MapW
  413. ImageRECT.Left = (Width + xSrc - g_MapW) * wZoom
  414. If ImageRECT.Left >= wImage Then Exit Sub
  415. End If
  416. If Height + ySrc > g_MapH Then
  417. destRect.Bottom = g_MapH
  418. ImageRECT.Bottom = (g_MapH - ySrc) * hZoom
  419. End If
  420. '一点也没出画出来
  421. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  422. '-------------------------------------------------
  423. 'liuhan (2010-05-21)
  424. If UseColorkey = True Then
  425. '透明绘图(yes)
  426. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_KEYSRC Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  427. Else
  428. '透明绘图(no)
  429. Call BackSurf.BltFx(destRect, Image.Image, ImageRECT, DDBLT_WAIT Or DDBLT_DDFX, FX) 'DDBLT_KEYSRC Or DDBLT_WAIT
  430. End If
  431. End Sub
  432. '*********************************************************************
  433. 'BltImage(DirectDrawSurface7变量,输出目标位置X、Y,W、H,择取源图X、Y,W、H,透明色作用开关)2010-06-26
  434. Public Sub BltImage(Image As DSurface, xSrc As Integer, ySrc As Integer, Width As Integer, Height As Integer, _
  435. xImage As Integer, yImage As Integer, wImage As Integer, hImage As Integer, _
  436. Optional UseColorkey As Boolean = False)
  437. Dim ImageRECT As RECT '输入输出时图像的大小
  438. Dim BX As Integer, BY As Integer '输出图像的位置
  439. BX = xSrc
  440. BY = ySrc
  441. Dim wZoom As Single, hZoom As Single '目标、输出图像比例
  442. wZoom = wImage / Width
  443. hZoom = hImage / Height
  444. '-----------------源图像的大小------------------
  445. ImageRECT.Left = xImage
  446. ImageRECT.Top = yImage
  447. ImageRECT.Right = wImage
  448. ImageRECT.Bottom = hImage
  449. '-----------------目标图像的大小------------------
  450. destRect.Left = xSrc
  451. destRect.Top = ySrc
  452. destRect.Right = xSrc + Width
  453. destRect.Bottom = ySrc + Height
  454. '自己做的自动剪切处理,比DirectX提供的快很多
  455. '----------------若碰边自动剪切--------------------
  456. If xSrc < 0 Then
  457. destRect.Left = 0
  458. ImageRECT.Left = (Abs(xSrc) + xImage) * wZoom
  459. If ImageRECT.Left >= wImage Then Exit Sub
  460. End If
  461. If ySrc < 0 Then
  462. destRect.Top = 0
  463. ImageRECT.Top = (Abs(ySrc) + yImage) * hZoom
  464. If ImageRECT.Top >= hImage Then Exit Sub
  465. End If
  466. If Width + xSrc > g_MapW Then
  467. destRect.Right = g_MapW
  468. ImageRECT.Right = (g_MapW - xSrc) * wZoom
  469. End If
  470. If Height + ySrc > g_MapH Then
  471. destRect.Bottom = g_MapH
  472. ImageRECT.Bottom = (g_MapH - ySrc) * hZoom
  473. End If
  474. '一点也没出画出来
  475. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  476. '-------------------------------------------------
  477. 'liuhan (2010-05-21)
  478. If UseColorkey = True Then
  479. '透明绘图(yes)
  480. Call BackSurf.Blt(destRect, Image.Image, ImageRECT, DDBLT_KEYSRC) 'DDBLT_KEYSRC Or DDBLT_WAIT
  481. Else
  482. '透明绘图(no)
  483. Call BackSurf.Blt(destRect, Image.Image, ImageRECT, DDBLT_WAIT) 'DDBLT_KEYSRC Or DDBLT_WAIT
  484. End If
  485. End Sub
  486. '*********************************************************************
  487. 'BltImage(DirectDrawSurface7变量,输出目标位置X、Y,择取源图X、Y,W、H,透明色作用开关)2010-06-26
  488. Public Sub BltFastImage(Image As DSurface, xSrc As Integer, ySrc As Integer, _
  489. xImage As Integer, yImage As Integer, Width As Integer, Height As Integer, _
  490. Optional UseColorkey As Boolean = False)
  491. Dim ImageRECT As RECT '输入输出时图像的大小
  492. Dim BX As Integer, BY As Integer '输出图像的位置
  493. BX = xSrc
  494. BY = ySrc
  495. '-----------------输出图像的大小------------------
  496. ImageRECT.Left = xImage
  497. ImageRECT.Top = yImage
  498. ImageRECT.Right = xImage + Width
  499. ImageRECT.Bottom = yImage + Height
  500. '自己做的自动剪切处理,比DirectX提供的快很多
  501. '----------------若碰边自动剪切--------------------
  502. If xSrc < 0 Then
  503. BX = 0
  504. ImageRECT.Left = Abs(xSrc) + xImage
  505. If ImageRECT.Left >= ImageRECT.Right Then Exit Sub
  506. End If
  507. If ySrc < 0 Then
  508. BY = 0
  509. ImageRECT.Top = Abs(ySrc) + yImage
  510. If ImageRECT.Top >= ImageRECT.Bottom Then Exit Sub
  511. End If
  512. If Width + xSrc > g_MapW Then
  513. ImageRECT.Right = xImage - xSrc + g_MapW
  514. End If
  515. If Height + ySrc > g_MapH Then
  516. ImageRECT.Bottom = yImage - ySrc + g_MapH
  517. End If
  518. '一点也没出画出来
  519. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  520. '-------------------------------------------------
  521. 'liuhan(2010-5-21)
  522. If UseColorkey = True Then
  523. '透明绘图(yes)
  524. Call BackSurf.BltFast(BX, BY, Image.Image, ImageRECT, DDBLTFAST_SRCCOLORKEY) 'DDBLTFAST_WAIT
  525. Else
  526. '透明绘图(no)
  527. Call BackSurf.BltFast(BX, BY, Image.Image, ImageRECT, DDBLTFAST_WAIT) 'DDBLTFAST_SRCCOLORKEY
  528. End If
  529. End Sub
  530. '************************画出所有**************************************
  531. 'BltImageAll(图像,X,Y,透明色作用开关)
  532. Public Sub BltImageAll(Image As DSurface, xSrc As Integer, ySrc As Integer, Optional UseColorkey As Boolean = False)
  533. Dim ImageRECT As RECT '输入输出时图像的大小
  534. Dim BX As Integer, BY As Integer '输出图像的位置
  535. BX = xSrc
  536. BY = ySrc
  537. '-----------------输出图像的大小------------------
  538. ImageRECT.Left = 0
  539. ImageRECT.Top = 0
  540. ImageRECT.Right = Image.W
  541. ImageRECT.Bottom = Image.H
  542. '自己做的自动剪切处理,比DirectX提供的快很多
  543. '----------------若碰边自动剪切--------------------
  544. If xSrc < 0 Then
  545. BX = 0
  546. ImageRECT.Left = Abs(xSrc)
  547. If ImageRECT.Left >= ImageRECT.Right Then Exit Sub
  548. End If
  549. If ySrc < 0 Then
  550. BY = 0
  551. ImageRECT.Top = Abs(ySrc)
  552. If ImageRECT.Top >= ImageRECT.Bottom Then Exit Sub
  553. End If
  554. If Image.W + xSrc > g_MapW Then
  555. ImageRECT.Right = g_MapW - xSrc
  556. End If
  557. If Image.H + ySrc > g_MapH Then
  558. ImageRECT.Bottom = g_MapH - ySrc
  559. End If
  560. '一点也没出画出来
  561. If ImageRECT.Right <= 0 Or ImageRECT.Bottom <= 0 Then Exit Sub
  562. '-------------------------------------------------
  563. 'liuhan(2010-5-21)
  564. If UseColorkey = True Then
  565. '透明绘图(yes)
  566. Call BackSurf.BltFast(BX, BY, Image.Image, ImageRECT, DDBLTFAST_SRCCOLORKEY) 'DDBLTFAST_WAIT
  567. Else
  568. '透明绘图(no)
  569. Call BackSurf.BltFast(BX, BY, Image.Image, ImageRECT, DDBLTFAST_WAIT) 'DDBLTFAST_SRCCOLORKEY
  570. End If
  571. End Sub
  572. '1━━━━━━━━《字体输出》━━━━━━━
  573. Public Sub PrintText(Text As String, x As Integer, y As Integer, _
  574. Optional FontSize As Integer = 10, Optional Color As Long = 0)
  575. Font.Size = FontSize
  576. BackSurf.SetFont Font
  577. BackSurf.SetForeColor Color
  578. BackSurf.DrawText x, y, Text, False
  579. End Sub
  580. '1━━━━━━━━《初始化Gamea色彩控制(只适用于全屏独占模式)》━━━━━━━
  581. Private Sub initGamma()
  582. Dim mmap As DDGAMMARAMP
  583. Set Gamea = MainSurf.GetDirectDrawGammaControl
  584. Call Gamea.GetGammaRamp(DDSGR_DEFAULT, mmap)
  585. End Sub
  586. '2.1━━━━━━━━━━━━━━━━全屏下淡入━━━━━━━━━━━━━━━━
  587. Public Sub FadeIn()
  588. Dim NewGammamp As DDGAMMARAMP, i As Integer, j As Integer, K As Long
  589. For i = 256 To 0 Step -8
  590. For j = 0 To 255
  591. K = CLng(j) * CLng(i)
  592. If K > 32767 Then K = K - 65536
  593. NewGammamp.red(j) = K
  594. NewGammamp.green(j) = K
  595. NewGammamp.blue(j) = K
  596. Next j
  597. Call Gamea.SetGammaRamp(DDSGR_DEFAULT, NewGammamp)
  598. Next i
  599. End Sub
  600. '2.2━━━━━━━━━━━━━━━━全屏下淡出━━━━━━━━━━━━━━━━
  601. Public Sub FadeOut()
  602. Dim NewGammamp As DDGAMMARAMP, i As Integer, j As Integer, K As Long
  603. For i = 0 To 256 Step 8
  604. For j = 0 To 255
  605. K = CLng(j) * CLng(i)
  606. If K > 32767 Then K = K - 65536
  607. NewGammamp.red(j) = K
  608. NewGammamp.green(j) = K
  609. NewGammamp.blue(j) = K
  610. Next j
  611. Call Gamea.SetGammaRamp(DDSGR_DEFAULT, NewGammamp)
  612. Next i
  613. End Sub
  614. 'end━━━━━━━━━━━━━━绘制画面显示到屏幕━━━━━━━━━━━━━━
  615. Public Sub BltScreen()
  616. Call Dx.GetWindowRect(BlthWnd, destRect)
  617. If goFull = False And BlthWnd = ObjhWnd Then
  618. destRect.Top = destRect.Top + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)
  619. 'destRect.Bottom = destRect.Bottom + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)
  620. destRect.Left = destRect.Left + GetSystemMetrics(SM_CXFRAME)
  621. destRect.Right = destRect.Right + GetSystemMetrics(SM_CXFRAME)
  622. End If
  623. Call MainSurf.Blt(destRect, BackSurf, srcRect, DDBLT_WAIT)
  624. Call BackSurf.BltColorFill(srcRect, 0)
  625. End Sub
  626. '1━━━━━━━回恢丢失页面(用于全屏模式,必须写在循环中)━━━━━━━━
  627. Private Function ExclusiveMode() As Boolean
  628. Dim lngTestExMode As Long
  629. 'This function tests if we're still in exclusive mode
  630. lngTestExMode = DDraw.TestCooperativeLevel
  631. If (lngTestExMode = DD_OK) Then
  632. ExclusiveMode = True
  633. Else
  634. ExclusiveMode = False
  635. End If
  636. End Function
  637. '2━━━━━━━回恢丢失页面(用于全屏模式,必须写在循环中)━━━━━━━━
  638. Public Function LostSurfaces() As Boolean
  639. 'This function will tell if we should reload our bitMapAZ or not
  640. LostSurfaces = False
  641. Do Until ExclusiveMode
  642. DoEvents
  643. LostSurfaces = True
  644. Loop
  645. 'If we did lose our bitMapAZ, restore the surfaces and return 'true'
  646. DoEvents
  647. If LostSurfaces Then
  648. DDraw.RestoreAllSurfaces
  649. End If
  650. End Function
  651. '=========================初始化键盘和鼠标处理=======================
  652. Private Sub InitDI(hwnd As Long)
  653. Set DI = Dx.DirectInputCreate() ' Create the DirectInput Device
  654. Set KeyDevice = DI.CreateDevice("GUID_SysKeyboard") ' Set it to use the keyboard.
  655. KeyDevice.SetCommonDataFormat DIFORMAT_KEYBOARD ' Set the data format to the keyboard format
  656. KeyDevice.SetCooperativeLevel hwnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE ' Set Cooperative level
  657. KeyDevice.Acquire
  658. Set dimouse = DI.CreateDevice("guid_sysmouse")
  659. dimouse.SetCommonDataFormat DIFORMAT_MOUSE
  660. dimouse.SetCooperativeLevel hwnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
  661. dimouse.Acquire
  662. End Sub
  663. '功能:返回鼠标X坐标
  664. Public Function MouseX() As Long
  665. Dim t As POINTAPI
  666. Dim client As RECT
  667. GetCursorPos t
  668. GetClientRect ObjhWnd, client
  669. ScreenToClient ObjhWnd, t
  670. MouseX = t.x * g_MapW / client.Right
  671. If t.x < client.Left Then MouseX = 0
  672. If t.x > client.Right Then MouseX = client.Right
  673. End Function
  674. '功能:返回鼠标Y坐标
  675. Public Function MouseY() As Long
  676. Dim t As POINTAPI
  677. Dim client As RECT
  678. GetCursorPos t
  679. GetClientRect ObjhWnd, client
  680. ScreenToClient ObjhWnd, t
  681. MouseY = t.y * g_MapH / client.Bottom
  682. If t.y < client.Top Then MouseY = 0
  683. If t.y > client.Bottom Then MouseY = client.Bottom
  684. End Function
  685. '1━━━━━━━━━━━━━初始化WAV(音效处理)━━━━━━━━━━━━━
  686. Private Sub InitWav(hwnd As Long)
  687. Set DSound = Dx.DirectSoundCreate("")
  688. DSound.SetCooperativeLevel hwnd, DSSCL_PRIORITY
  689. End Sub
  690. '2━━━━━━━━━━━━━━装入Wav音效文件━━━━━━━━━━━━━━
  691. Public Function LoadWav(FileName As String) As DirectSoundBuffer
  692. Dim BufferDesc As DSBUFFERDESC
  693. Dim WaveFormat As WAVEFORMATEX
  694. BufferDesc.lFlags = DSBCAPS_CTRLVOLUME Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPOSITIONNOTIFY
  695. Set LoadWav = DSound.CreateSoundBufferFromFile(FileName, BufferDesc, WaveFormat)
  696. End Function
  697. '3━━━━━━━━━━━━━━播放Wav音效文件━━━━━━━━━━━━━━
  698. Public Sub PlayWav(Sound As DirectSoundBuffer, nClose As Boolean, LoopSound As Boolean)
  699. If nClose Then
  700. Sound.Stop
  701. Sound.SetCurrentPosition 0
  702. End If
  703. If LoopSound Then
  704. Sound.Play 1
  705. Else
  706. Sound.Play 0
  707. End If
  708. End Sub
  709. '4━━━━Wav音量(-10000-0)声道控制(左-10000至右10000调节)━━━━━
  710. Public Sub SetWav(Sound As DirectSoundBuffer, Optional VolumeValue As Integer, Optional PanValue As Integer)
  711. If PanValue > 10000 Then VolumeValue = 10000
  712. If PanValue < -10000 Then VolumeValue = -10000
  713. Sound.SetPan PanValue
  714. If VolumeValue > 0 Then VolumeValue = 0
  715. If VolumeValue < -10000 Then VolumeValue = -10000
  716. Sound.SetVolume VolumeValue
  717. End Sub
  718. '1━━━━━━━━━━━━━━初始化MID设置━━━━━━━━━━━━━━
  719. Private Sub InitMid()
  720. '建立directmusicloader对象
  721. Set objdmloader = Dx.DirectMusicLoaderCreate
  722. '建立directmusicperformance对象
  723. Set objdmperf = Dx.DirectMusicPerformanceCreate
  724. '初始化directmusicperformance对象
  725. objdmperf.Init Nothing, 0
  726. objdmperf.SetPort -1, 80
  727. objdmperf.SetMasterAutoDownload True
  728. objdmperf.SetMasterVolume 0
  729. End Sub
  730. '2━━━━━━━━━━━━━━装入midi文件━━━━━━━━━━━━━━
  731. Public Sub LoadMid(FileName As String)
  732. Set objdmseg = Nothing
  733. Set objdmseg = objdmloader.LoadSegment(FileName)
  734. End Sub
  735. '3━━━━━━━━━━━━━━播放midi文件━━━━━━━━━━━━━━
  736. Public Sub PlayMid(Optional Play As Boolean = True, Optional Start As Long)
  737. If Play = True Then
  738. If objdmperf.IsPlaying(objdmseg, objdmsegst) = True Then
  739. '停止播放
  740. Call objdmperf.Stop(objdmseg, objdmsegst, 0, 0)
  741. End If
  742. objdmseg.SetStartPoint (Start)
  743. Set objdmsegst = objdmperf.PlaySegment(objdmseg, 0, 0)
  744. Else
  745. '停止播放midi文件
  746. Call objdmperf.Stop(objdmseg, objdmsegst, 0, 0)
  747. End If
  748. End Sub
  749. '4━━━━━━━━━━━━━━播放midi音量,节奏频率━━━━━━━━━━━━━━
  750. Public Sub SetMid(Optional VolumeValue As Integer, Optional TempoValue As Integer)
  751. If VolumeValue > 0 Then VolumeValue = 0
  752. If VolumeValue < -10000 Then VolumeValue = -10000
  753. Call objdmperf.SetMasterVolume(VolumeValue)
  754. Call objdmperf.SetMasterTempo(TempoValue)
  755. End Sub
  756. '=========================================================
  757. '*****************《控帧》*******************
  758. Public Sub ControlFPS(Time As Integer)
  759. Do While GetTickCount - FPS_Count < Time
  760. DoEvents
  761. Loop
  762. FPS_Count = GetTickCount
  763. End Sub
  764. '***************《获得速度》*****************
  765. Public Function GetFPSx() As Integer
  766. If GetTickCount() - mTimer >= 1000 Then
  767. mTimer = GetTickCount
  768. GetFPSx = AddFPS + 1
  769. AddFPS = 0
  770. Else
  771. AddFPS = AddFPS + 1
  772. End If
  773. End Function
  774. '***************《获得速度》*****************
  775. Public Sub GetFPS() '(FPS As Integer)
  776. If GetTickCount() - mTimer >= 1000 Then
  777. mTimer = GetTickCount
  778. FPS = AddFPS + 1
  779. AddFPS = 0
  780. Else
  781. AddFPS = AddFPS + 1
  782. End If
  783. End Sub
  784. '======================退出Engine=========================
  785. Public Sub ExitEngine()
  786. 'ExitDDraw
  787. Call DDraw.RestoreDisplayMode
  788. Call DDraw.SetCooperativeLevel(ObjhWnd, DDSCL_NORMAL)
  789. Set BackSurf = Nothing
  790. Set MainSurf = Nothing
  791. Set Dx = Nothing
  792. Set Gamea = Nothing
  793. 'ExitMid
  794. Set objdmsegst = Nothing
  795. Set objdmseg = Nothing
  796. Set objdmperf = Nothing
  797. Set objdmloader = Nothing
  798. 'ExitDI
  799. Set DI = Nothing
  800. Set KeyDevice = Nothing
  801. Set dimouse = Nothing
  802. 'ExitWav
  803. Set DSound = Nothing
  804. Set StdFont = Nothing
  805. Set Font = Nothing
  806. End Sub

本文转载自: https://blog.csdn.net/xiaoyao961/article/details/140838992
版权归原作者 专注VB编程开发20年 所有, 如有侵权,请联系我们删除。

“DX7 for VB6 游戏引擎加强版”的评论:

还没有评论