VB6 programming: DirectX 2D graphics learning log 23 cartoon animation summary

VB6 programming: DirectX 2D graphics learning log 23 cartoon animation summary
tutorial download address : https://download.csdn.net/download/gosub60/13696651
program analysis
1. Initialization process:
1. A function Window_Setup() is used to set the window ,

Private Sub Window_Setup(Window As Form, [ByVal X As Long = -1], [ByVal Y As Long = -1], [ByVal Width As Long = -1], [ByVal Height As Long = -1], [Caption As String = " "], [Auto_Redraw As Boolean = False], [ByVal Back_Color As Long = -1])

The parameters are as follows:
parameter 1: Window: window name, here is frmMain
parameter 2/3: x, y: coordinates, default -1
parameter 4/5: Width, Height: the width and height of the window when windowing (Default -1)
Parameter 6: Caption: Window title
Parameter 7: Optional
Parameter 8: Background color, default -1, RGB can be specified by RGB (0, 0, 0)

Then, call the subroutine as before: DirectX_Initialize'initialize DirectX and Direct3D.

2. Load the texture image. Call the subroutine as before: Load_Texture'Load texture from file.

Write 11 cartoon pictures into vertex buffer

3. Call the subroutine Setup_Sprite: set the coordinate position, size, speed and other attributes of the cartoon sprite.
Use the command below to set the number of cartoon frames. There are 11 cartoon pictures in total, so there are 11 frames

Sprite.Animation_State.Number_Of_Frames = 11

When using the above definition, you need to first:
define a cartoon structure type

Private Type Sprite_Type
    X As Single'卡通X坐标
    Y As Single'卡通Y坐标
    Animation_State As Animation_Type'动画状态
   End Type

Define an animation state structure type

Private Type Animation_Type
    Number_Of_Frames As Long '帧数
    Current_Frame As Single '当前帧
    Frame_Counter As Long '帧_计数器
    Speed As Single '速度
    Frame_Size As RECT '框架尺寸
 End Type

Define a RECT_SINGLE

Private Type RECT_SINGLE
    Left As Single
    Top As Single
    Right As Single
    Bottom As Single
End Type

2. After completing the above initialization, enter the Game_Loop subroutine' game loop.
In the Game_Loop loop, call Animate_Sprite to draw cartoon dynamics
①Set the renderer state

'就在这里将alphablend多边形
    Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, False

    '就在这里将赋予多边形透明度
    Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True

②Get the current frame

If Int(Sprite.Animation_State.Current_Frame) > (Sprite.Animation_State.Number_Of_Frames - 1) Then
    
        Sprite.Animation_State.Current_Frame = 0
    
    End If
    
    Current_Frame = Int(Sprite.Animation_State.Current_Frame)'获得当前帧

③Set the coordinates of the current frame

Private Frame As RECT_SINGLE
    Frame.Left = (Sprite.X + Sprite.Animation_State.Frame_Size.Left) * Scalar.X
    Frame.Top = (Sprite.Y + Sprite.Animation_State.Frame_Size.Top) * Scalar.Y
    Frame.Right = (Sprite.X + Sprite.Animation_State.Frame_Size.Right) * Scalar.X
    Frame.Bottom = (Sprite.Y + Sprite.Animation_State.Frame_Size.Bottom) * Scalar.Y

④Create polygon

'创建多边形。
    '---------------------------------------------------------------
    With Frame
    
        Vertex_List(0) = Create_TLVertex(.Left, .Top, 0, 1, Transparency_Color, 0, 0, 0)
        Vertex_List(1) = Create_TLVertex(.Right, .Top, 0, 1, Transparency_Color, 0, 1, 0)
        Vertex_List(2) = Create_TLVertex(.Left, .Bottom, 0, 1, Transparency_Color, 0, 0, 1)
        Vertex_List(3) = Create_TLVertex(.Right, .Bottom, 0, 1, Transparency_Color, 0, 1, 1)
    
    End With
    '---------------------------------------------------------------

⑤Set the texture

'设置纹理
    Direct3D_Device.SetTexture 0, Texture(Current_Frame)
    
    Sprite.Animation_State.Current_Frame = Sprite.Animation_State.Current_Frame + Sprite.Animation_State.Speed

Guess you like

Origin blog.csdn.net/gosub60/article/details/111224425