VB6编程:DirectX 2D图形学习日志21旋转图片
教程下载地址:https://download.csdn.net/download/gosub60/13696651
作用:通过按↑和下方向键旋转图片
总结:在源码最后
源码如下:需要一个图片配合。这里提供一个女王给大家按摩一下!
下边上VB6源码:
'---------------------------------
'标题:DirectX教程
'
'描述:
'
'作者:Jacob Roman 翻译:gosub60@163.com QQ:127644712
'
'日期:2005年12月2日
'
'联系人:jacob_c_roman@hotmail.com
'---------------------------------
Option Explicit
''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
Private Const PI As Single = 3.14159265358979
'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 Direct3DX As D3DX8
Private Fullscreen_Enabled As Boolean '帮助确定它是否为全屏模式。
Private Running As Boolean '帮助确定主游戏循环是否正在运行。
Private Vertex_List(3) As TLVERTEX '4个顶点将构成一个正方形
Private Texture As Direct3DTexture8
Private Main_Font As D3DXFont
Private Main_Font_Description As IFont
Private Text_Rect As RECT
Private Angle As 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 Function DirectX_Initialize() As Boolean
On Error GoTo Error_Handler
Dim Display_Mode As D3DDISPLAYMODE '显示模式说明。
Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer和视口说明。
Set DirectX8 = New DirectX8 '创建DirectX对象。
Set Direct3D = DirectX8.Direct3DCreate() '使用DirectX对象创建Direct3D对象
Set Direct3DX = New D3DX8
If Fullscreen_Enabled = True Then
'“现在我们正在全屏模式下工作,我们必须设置
'屏幕分辨率切换为,而不是使用默认屏幕
'解析度。
Display_Mode.Width = 1024
Display_Mode.Height = 768
Display_Mode.Format = COLOR_DEPTH_32_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作为设备窗口。
Else
Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode '使用您当前使用的显示模式
'已经在。 如果您感到困惑,我是
'在谈论您当前的屏幕分辨率。 ;)
Direct3D_Window.Windowed = True '该应用程序将处于窗口模式。
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)
Direct3D_Device.SetVertexShader FVF_TLVERTEX '设置顶点着色的类型。 (需要)
'不需要这些行,但能够过滤掉
'使它们看起来更好的纹理。
Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_POINT
Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_POINT
Exit Function
Error_Handler:
MsgBox "初始化DirectX时发生错误!", vbCritical
Close_Program
DirectX_Initialize = False
End Function
Private Sub Create_Polygon(ByVal X As Single, Y As Single, Size As Single, ByVal Angle As Single)
Dim Vertex(3) As D3DVECTOR2
Dim New_Vertex(3) As D3DVECTOR2
Vertex(0).X = -Size: Vertex(0).Y = -Size
Vertex(1).X = Size: Vertex(1).Y = -Size
Vertex(2).X = -Size: Vertex(2).Y = Size
Vertex(3).X = Size: Vertex(3).Y = Size
New_Vertex(0).X = X + (Vertex(0).X * Cos(Angle * (PI / 180)) - Vertex(0).Y * Sin(Angle * (PI / 180)))
New_Vertex(0).Y = Y + (Vertex(0).X * Sin(Angle * (PI / 180)) + Vertex(0).Y * Cos(Angle * (PI / 180)))
New_Vertex(1).X = X + (Vertex(1).X * Cos(Angle * (PI / 180)) - Vertex(1).Y * Sin(Angle * (PI / 180)))
New_Vertex(1).Y = Y + (Vertex(1).X * Sin(Angle * (PI / 180)) + Vertex(1).Y * Cos(Angle * (PI / 180)))
New_Vertex(2).X = X + (Vertex(2).X * Cos(Angle * (PI / 180)) - Vertex(2).Y * Sin(Angle * (PI / 180)))
New_Vertex(2).Y = Y + (Vertex(2).X * Sin(Angle * (PI / 180)) + Vertex(2).Y * Cos(Angle * (PI / 180)))
New_Vertex(3).X = X + (Vertex(3).X * Cos(Angle * (PI / 180)) - Vertex(3).Y * Sin(Angle * (PI / 180)))
New_Vertex(3).Y = Y + (Vertex(3).X * Sin(Angle * (PI / 180)) + Vertex(3).Y * Cos(Angle * (PI / 180)))
Vertex_List(0) = Create_TLVertex(New_Vertex(0).X, New_Vertex(0).Y, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 0)
Vertex_List(1) = Create_TLVertex(New_Vertex(1).X, New_Vertex(1).Y, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 0)
Vertex_List(2) = Create_TLVertex(New_Vertex(2).X, New_Vertex(2).Y, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 0, 1)
Vertex_List(3) = Create_TLVertex(New_Vertex(3).X, New_Vertex(3).Y, 0, 1, D3DColorRGBA(255, 255, 255, 0), 0, 1, 1)
End Sub
Private Sub Load_Texture()
Dim File_Path As String
Dim Width As Long
Dim Height As Long
Dim Transparency_Color As Long
File_Path = App.Path & "\timg.jpg"
Width = 1024
Height = 768
Transparency_Color = D3DColorRGBA(0, 0, 0, 0)
Set Texture = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, _
File_Path, _
Width, Height, _
0, _
0, _
D3DFMT_X8R8G8B8, _
D3DPOOL_MANAGED, _
D3DX_FILTER_POINT, _
D3DX_FILTER_POINT, _
Transparency_Color, _
ByVal 0, _
ByVal 0)
End Sub
Private Sub Game_Loop()
Do While Running = True
DoEvents '允许事件发生,以便程序不会锁定。
'----------------------------------------------------
'DirectX会自动为您处理帧速率
'这使其运行(最多)与监视器一样快
' 刷新率高,因此您无需在其中添加额外的代码
' 降低循环速度并以一定数量的帧运行
'每秒。
'----------------------------------------------------
'清除后缓冲。
Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 0), 1#, 0
Direct3D_Device.BeginScene
Create_Polygon 500, 300, 150, Angle
'设置纹理
Direct3D_Device.SetTexture 0, Texture
'绘制多边形
Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
If Fullscreen_Enabled = True Then
'绘制文本。
Direct3DX.DrawText Main_Font, D3DColorRGBA(255, 255, 255, 255), "按Up/Down(上/下)键旋转图片 " & vbCrLf & Angle & "角度", Text_Rect, DT_TOP Or DT_LEFT
Else
Direct3DX.DrawText Main_Font, D3DColorRGBA(255, 255, 255, 255), "仅在全屏模式下工作", Text_Rect, DT_TOP Or DT_LEFT
End If
Direct3D_Device.EndScene
'将后缓冲区翻转到窗体窗口中。
Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
Loop
End Sub
Private Sub Close_Program()
Running = False '这有助于程序退出游戏循环。
'卸载所有DirectX对象。
Set Texture = Nothing
Set Direct3DX = Nothing
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DirectX8 = Nothing
Unload Me '卸载窗体
End ''结束程序
'尽管上方的Unload语句退出了程序,但是您
'这样做后将导致自动化错误?
'END 命令 将有助于防止这种情况,并彻底结束该应用程序。
End Sub
Private Sub Form_Activate()
frmMain.Caption = "DirectX教程15:图片旋转"
ScaleMode = 3
DirectX_Initialize '初始化DirectX和Direct3D。
Load_Texture '从文件加载纹理。
Font.Name = "Arial"
Font.Size = 14
Set Main_Font_Description = Font
Set Main_Font = Direct3DX.CreateFont(Direct3D_Device, Main_Font_Description.hFont)
Text_Rect.Left = 20
Text_Rect.Top = 160
Text_Rect.Right = Text_Rect.Left + 300
Text_Rect.bottom = Text_Rect.Top + 50
Running = True '全部初始化。 现在可以激活游戏循环了。
Game_Loop
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyEscape '如果用户按Esc键则退出程序
Close_Program
Case vbKeyLeft
Angle = Angle - 3
Case vbKeyRight
Angle = Angle + 3
End Select
Angle = Angle Mod 360
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)
Close_Program
End Sub
程序分析
一、初始化过程:
① 同前边一样调用子程序:DirectX_Initialize '初始化DirectX和Direct3D。
②加载纹理图片。同前边一样调用子程序:Load_Texture '从文件加载纹理。
③创建文本相关资源
二、 完成以上初始化后,进入Game_Loop子程序’游戏循环
在Game_Loop循环中,根据窗口标志Fullscreen_Enabled来判断窗口状态
根据Angle值绘制多边形, 同前边一样调用子程序:Create_Polygon
三、Form_KeyDown:按键检测
如果按下ESC那么退出程序
如果按下vbKeyUp(向上键),则Angle值-3
如果按下vbKeyDown(向下键),则Angle值+3
以上就是今天的教程总结欢迎大家留言提出宝贵意见。