用VB编写DirectX7.0游戏(下)
2008-02-23 06:54:32来源:互联网 阅读 ()
设置显示模式是使用SetDispalyMode函数实现的,函数的定义如下:
object.SetDisplayMode(w As Long, h As Long, bpp As Long, ref As Long, mode As CONST_DDSDMFLAGS
其中参数w、h分别指定屏幕的宽度和高度,bpp指定屏幕显示的颜色位数,参数ref指定屏幕的刷新频率,设置为0使用显示驱动的缺省刷新频率,mode指定附加的参数。要获得系统支持的显示模式,可以使用DirectDraw对象的GetDisplayModesEnum函数来遍历所有支持的显示模式。
2.4 建立平面对象
一个平面或者说DirectDrawSurface对象是DirectDraw中图形显示和绘制对象。用户可以在DirectDrawSurface上贴位图、绘制图形,还可以直接操作DirectDrawSurface对象使用显存里的内容。利用DirectDraw对象的CreateSurface方法可以建立一个DirectDrawSurface7对象。例如:
Public DDSFrontDesc As DDSURFACEDESC2
With DDSFrontDesc
.lFlags = DDSD_CAPS
.ddsCaps.lCaps=DDSCAPS_PRIMARYSURFACE '
End With
Set DDSFront = DDraw.CreateSurface(DDSFrontDesc)
也可以利用DirectDraw对象的CreateSurfaceFromFile函数或者CreateSurfaceFromResource函数建立一个DirectDrawSurface7对象,同时将图像文件或者资源文件中的图像装入建立的DirectDrawSurface中。如果上面的函数调用成功,函数将返回一个DirectDrawSurface对象。如果在设定DirectDraw对象的协作层时将其设置为全屏幕模式的话,为了改善图像性能,可以设立一个主平面和若干个屏下缓冲平面,首先在屏下平面中生成图像,然后将图像翻转到主平面上,这样可以有效地避免图像闪烁。
下面通过一个具体的范例来对DirectDraw进行说明:这个范例建立一个全屏幕的DirectDraw对象,通过操作主显示平面的显示内存在屏幕上显示火焰字的特效,然后按Enter键可以将DirectDraw平面中的图形保存起来。程序的具体实现如下:
建立一个新的工程文件,点击菜单中的 Project | Reference 选项,打开Object Library 列表窗口,将DirectX 7.0 For Visual Basic Type Library 加入工程文件。将Form1的Name属性改变为MainForm,在MainForm中加入一个PictureBox控件,将其的Visible属性设置为False。然后在MainForm的代码窗口中加入以下代码:
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim sRect As RECT
Dim hdcSrc As Long
If KeyAscii = 27 Then
ExitLoop = True
'End
ElseIf KeyAscii = VBKeyReturn Then
DDSFront.BltToDC Picture1.hDC, sRect, sRect
With Picture1
'获得与主显示平面兼容的图形设备句柄
hdcSrc = DDSFront.GetDC
'保存图像
Set .Picture = SaveTohBmp(hdcSrc, 0, 0, 640, 480)
'释放图形句柄
DDSFront.ReleaseDC hdcSrc
SavePicture Picture1, “c:\a.bmp”
End With
End If
End Sub
Public Sub Form_Paint()
BlitRect.Right = DDSBackDesc.lWidth
BlitRect.Bottom = DDSBackDesc.lHeight
DDSFront.Blt BlitRect, DDSBack, BlitRect, DDBLT_WAIT
End Sub
在工程文件中加入一个Module文件,这个文件中DirectDraw操作做出了定义,在这个Module中加入以下代码:
Option Explicit
Public DX As New DirectX7
Public DDraw As DirectDraw7
Public DDSFront As DirectDrawSurface7
Public DDSFrontDesc As DDSURFACEDESC2
Public DDSBack As DirectDrawSurface7
Public DDSBackDesc As DDSURFACEDESC2
Public Clipper As DirectDrawClipper
Dim Pict() As Byte
Dim AlphaRect As RECT
Dim X As Long, Y As Long
Dim Temp As Long
Dim Index As Long
Dim Index2 As Long
Dim Pos As Long
Dim PosPlus1 As Long
Dim PosPlus2 As Long
Dim PosPlus3 As Long
Public Pal(255) As PALETTEENTRY
Public Palette As DirectDrawPalette
Public BlitRect As RECT
Public FullSize As Boolean
Public ExitLoop As Boolean
Dim Accum As Long
Dim Msg(9) As String
Dim Counter As Long
Dim MsgIndex As Long
Dim bDrawText As Boolean
Dim lastTime As Long
Dim XPos As Long, YPos As Long
Dim wait As Long
Dim Angle As Single
Dim Flag As Boolean
Dim Count As Long
Dim CurModeActiveStatus As Boolean
Dim bRestore As Boolean
Dim Mode As Boolean
Private Sub Main()
InitializeDX
'初始化Picture1以获得DirectDraw界面图像
With MainForm.Picture1
.Width = 640 * Screen.TwipsPERPixelX
.Height = 480 * Screen.TwipsPerPixelY
End With
DDSBack.SetForeColor RGB(255, 255, 255)
MainForm.Font.Name = “宋体”
DDSBack.SetFont MainForm.Font
Msg(0) =“一个显示火焰字的演示”
Msg(1) =“演示”
Msg(2) =“利用VB阵列”
Msg(3) =“对显示内存”
Msg(4) =“进行直接存取”
Msg(5) =“{Esc}键退出”
'设置8位的调色板
For Index = 0 To 84
Pal(Index + 1).red = Index * 3 + 3
Pal(Index + 1).green = 0
Pal(Index + 1).blue = 0
Pal(Index + 86).red = 255
Pal(Index + 86).green = Index * 3 + 3
Pal(Index + 86).blue = 0
Pal(Index + 171).red = 255
Pal(Index + 171).green = 255
Pal(Index + 171).blue = Index * 3 + 3
Next
Set Palette = DDraw.CreatePalette(DDPCAPS_8BIT _ Or DDPCAPS_ALLOW256, Pal())
DDSFront.SetPalette Palette
AlphaRect.Right = DDSBackDesc.lWidth - 1
AlphaRect.Bottom=DDSBackDesc.lHeight- 1
DDSBack.Lock AlphaRect, DDSBackDesc, DDLOCK_WAIT, 0
DDSBack.GetLockedArray Pict()
For X = 0 To 639
For Y = 0 To 479
Pict(X, Y) = 0
Next
Next
'Corresponding unlock
DDSBack.Unlock AlphaRect
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:SQL的基本操作(1.数据类型)
下一篇:在VB中获取和修改计算机名字
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash