VB6 programming: DirectX 2D graphics learning log 13 Proportion compensation: the drawn graphics adjust the drawing scale according to the resolution
tutorial download link: https://download.csdn.net/download/gosub60/13696651
VB6 source code is as follows:
'---------------------------------
'标题:DirectX教程
'
'描述:比例补偿
'
'作者:Jacob Roman 翻译:[email protected] QQ:127644712
'
'日期:
'
'联系人:[email protected]
'---------------------------------
Option Explicit
Private Type RECT_SINGLE
Left As Single
Top As Single
Right As Single
Bottom As Single
End Type
'2D(已转换和已点燃)顶点格式类型。
Private Type TLVERTEX
X As Single
Y As Single
Z As Single
RHW As Single
Color As Long
Specular As Long
TU As Single
TV As Single
End Type
'一些颜色深度常数有助于使DX常数更具可读性。
Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
'2D(转换和点亮)顶点格式。
Private Const FVF_TLVERTEX As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR
Private DirectX8 As DirectX8 '主DirectX对象。
Private Direct3D As Direct3D8 '控制3D一切。
Private Direct3D_Device As Direct3DDevice8 '表示硬件渲染。
Private Fullscreen_Width As Long
Private Fullscreen_Height As Long
Private Fullscreen_Enabled As Boolean '帮助确定它是否为全屏模式。
Private Running As Boolean '帮助确定主游戏循环是否正在运行。
Private Vertex_List(3) As TLVERTEX '4个顶点将构成一个正方形。
Private Scalar As D3DVECTOR2
Private Frame As RECT_SINGLE
''使用此功能可以更轻松地设置具有所需信息的顶点。
Private Function Create_TLVertex(X As Single, Y As Single, Z As Single, RHW As Single, Color As Long, Specular As Long, TU As Single, TV As Single) As TLVERTEX
Create_TLVertex.X = X
Create_TLVertex.Y = Y
Create_TLVertex.Z = Z
Create_TLVertex.RHW = RHW
Create_TLVertex.Color = Color
Create_TLVertex.Specular = Specular
Create_TLVertex.TU = TU
Create_TLVertex.TV = TV
End Function
Private Sub Form_Activate()
frmMain.Caption = "DirectX教程:比例补偿"
frmMain.ScaleMode = vbPixels
Fullscreen_Width = 1024
Fullscreen_Height = 768
Dim Display_Mode As D3DDISPLAYMODE '显示模式说明。
Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer和视口说明。
Set DirectX8 = New DirectX8 '创建DirectX对象。
Set Direct3D = DirectX8.Direct3DCreate() '使用DirectX对象创建Direct3D对象。
If Fullscreen_Enabled = True Then
'“现在我们正在全屏模式下工作,我们必须设置
'屏幕分辨率切换为,而不是使用默认屏幕
'解析度。
Display_Mode.Width = Fullscreen_Width
Display_Mode.Height = Fullscreen_Height
Display_Mode.Format = COLOR_DEPTH_16_BIT
Direct3D_Window.Windowed = False '该应用程序将处于全屏模式。
Direct3D_Window.BackBufferCount = 1 '仅1个后缓冲
Direct3D_Window.BackBufferWidth = Display_Mode.Width '使后缓冲宽度与显示宽度匹配
Direct3D_Window.BackBufferHeight = Display_Mode.Height '使后缓冲高度与显示高度匹配
Direct3D_Window.hDeviceWindow = frmMain.hWnd '使用frmMain作为设备窗口。
Scalar.X = Fullscreen_Width / frmMain.ScaleWidth
Scalar.Y = Fullscreen_Height / frmMain.ScaleHeight
Else
Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode '使用您当前使用的显示模式
'已经在。 如果您感到困惑,我是
'在谈论您当前的屏幕分辨率。 ;)
Direct3D_Window.Windowed = True '该应用程序将处于窗口模式。
Scalar.X = 1
Scalar.Y = 1
End If
Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC '监视器运行时刷新。
Direct3D_Window.BackBufferFormat = Display_Mode.Format '设置检索到后缓冲区中的格式。
'使用一些有用的信息以及信息创建渲染设备
'我们已经设置了Direct3D_Window。
Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
Frame.Left = 0 * Scalar.X
Frame.Top = 0 * Scalar.Y
Frame.Right = 100 * Scalar.X
Frame.Bottom = 100 * Scalar.Y
'创建多边形。
'---------------------------------------------------------------
With Frame
Vertex_List(0) = Create_TLVertex(.Left, .Top, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
Vertex_List(1) = Create_TLVertex(.Right, .Top, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
Vertex_List(2) = Create_TLVertex(.Left, .Bottom, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
Vertex_List(3) = Create_TLVertex(.Right, .Bottom, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)
End With
'---------------------------------------------------------------
Direct3D_Device.SetVertexShader FVF_TLVERTEX '设置顶点着色的类型(必需)
Running = True '全部初始化。 现在可以激活游戏循环了。
Do While Running = True
DoEvents '允许事件发生,以便程序不会锁定。
'------------------------------------------------- ---
'DirectX会自动为您处理帧速率
'这使其运行(最多)与监视器一样快
' 刷新率高,因此您无需在其中添加额外的代码
' 降低循环速度并以一定数量的帧运行
'每秒。
'------------------------------------------------- ---
'清除后缓冲区。
Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 0), 1#, 0
Direct3D_Device.BeginScene
'渲染代码在这里
Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
Direct3D_Device.EndScene
'将后缓冲区翻转到窗体窗口中。
Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
Loop
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then '如果用户按Esc键...
Running = False '帮助程序退出游戏循环。
'卸载所有DirectX对象
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DirectX8 = Nothing
Unload Me '卸载窗口
End '结束程序
'尽管上方的Unload语句退出了程序,但是您
'这样做后将导致自动化错误?
'END 命令 将有助于防止这种情况,并彻底结束该应用程序。
End If
End Sub
Private Sub Form_Load() '窗口初始化
'窗口完全加载之前将触发此事件
If MsgBox("单击“是”进入全屏(推荐)", vbQuestion Or vbYesNo, "选项") = vbYes Then Fullscreen_Enabled = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
Running = False '帮助程序退出游戏循环。
'卸载所有DirectX对象
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DirectX8 = Nothing
Unload Me '卸载窗口
End '结束程序
'尽管上方的Unload语句退出了程序,但是您
'这样做后将导致自动化错误?
'END 命令 将有助于防止这种情况,并彻底结束该应用程序。
End Sub
Source Analysis:
① type create a vertex, the vertex buffer and the like in front of the reference log
② define a variable D3DVECTOR2 Scalar type , Type D3DVECTOR2 members are X, Y two parameters.
Private Scalar As D3DVECTOR2
When in full screen, the assignment of Scalar is like this
Scalar.X = Fullscreen_Width / frmMain.ScaleWidth
Scalar.Y = Fullscreen_Height / frmMain.ScaleHeight
In windowing, the assignment of Scalar is like this
Scalar.X = 1
Scalar.Y = 1
③ define a variable Frame RECT_SINGLE
Private Frame As RECT_SINGLE
The definition data of RECT_SINGLE is as follows:
Private Type RECT_SINGLE
Left As Single'左边坐标
Top As Single'顶部坐标
Right As Single'右边坐标
Bottom As Single'底部坐标
End Type
In this example, the assignment of Frame is as follows:
Frame.Left = 0 * Scalar.X
Frame.Top = 0 * Scalar.Y
Frame.Right = 100 * Scalar.X
Frame.Bottom = 100 * Scalar.Y
Create polygons accordingly:
'创建多边形。
'---------------------------------------------------------------
With Frame
Vertex_List(0) = Create_TLVertex(.Left, .Top, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
Vertex_List(1) = Create_TLVertex(.Right, .Top, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
Vertex_List(2) = Create_TLVertex(.Left, .Bottom, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
Vertex_List(3) = Create_TLVertex(.Right, .Bottom, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)
End With