Results 1 to 3 of 3

Thread: Game animation: some advices

Threaded View

  1. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Game animation: some advices

    I'll show ya how. However I understand you are using C++ and not DX to do it, but if you would have taken a look at my Massive DX Tutorial a long time ago, you would have seen the solution. Just that I been so busy working every single day constantly covering for my coworkers wanting days off

    Note that due to a lack of time since its brutally late for me to stay up, I'm gonna show you some VB6 code that you can easily bring to C++. Setup your structures kinda like so. If you need to change a few things, some DX stuff such as D3DVECTOR2 can be made into a different structure with whatever name you choose with just X and Y declared as a float:

    Code:
    Private Type RECT
        Left As Single
        Top As Single
        Right As Single
        Bottom As Single
    End Type
    
    Private Type Animation_Type
        
        Frame_Size() As RECT
        Number_Of_Frames As Long
        Number_Of_Textures As Long
        Current_Frame As Single
        Frame_Counter As Long
        Speed As Single
        Initial_Frame As Long
        Mode As Long 'Single Shot, Loop, etc.
        Offset() As D3DVECTOR2
        Texture_Number() As Long
        
    End Type
    
    Private Type Sprite_Type
    
        X As Single
        Y As Single
        Animation_State As Animation_Type
        Texture_Path() As String
        Texture_List() As Direct3DTexture8
        Total_Number_Of_Textures As Long
        
    End Type
    
    Private Const ANIMATION_MODE_SINGLE_SHOT As Long = 0
    Private Const ANIMATION_MODE_LOOP As Long = 1
    This is a very basic setup for animation. Animation_State however can be an array since it can involve multiple animation states on the same sprite, such as walking, running, ducking, jumping, attacking, shooting, dieing, etc etc etc. Unlimited endless possibilities. But in this example Im only gonna use one animation state, so no array is there.

    Next load all your textures into an array. The texture array can be a part of the animation structure or even the sprite structure if you wish. Although this is DX code I'm using, if not using DX, the idea is the same. Store all the frames into a texture array:

    Code:
    Private Sub Load_Textures()
    
        Dim Width As Long
        Dim Height As Long
        Dim Transparency_Color As Long
        Dim Current_Texture As Long
        
        With Sprite
        
            .Total_Number_Of_Textures = 4
            
            ReDim .Texture_Path(.Total_Number_Of_Textures) As String
            ReDim .Texture_List(.Total_Number_Of_Textures) As Direct3DTexture8
            
            .Texture_Path(0) = App.Path & "\Graphics\Ken1.bmp"
            .Texture_Path(1) = App.Path & "\Graphics\Ken2.bmp"
            .Texture_Path(2) = App.Path & "\Graphics\Ken3.bmp"
            .Texture_Path(3) = App.Path & "\Graphics\Ken4.bmp"
        
            Width = 1024
            Height = 1024
            
            Transparency_Color = D3DColorRGBA(0, 0, 0, 255)
            
            For Current_Texture = 0 To .Total_Number_Of_Textures - 1
        
                Set .Texture_List(Current_Texture) = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, _
                                                                .Texture_Path(Current_Texture), _
                                                                Width, Height, _
                                                                0, _
                                                                0, _
                                                                D3DFMT_A8R8G8B8, _
                                                                D3DPOOL_MANAGED, _
                                                                D3DX_FILTER_POINT, _
                                                                D3DX_FILTER_POINT, _
                                                                Transparency_Color, _
                                                                ByVal 0, _
                                                                ByVal 0)
                                                                
            Next Current_Texture
    
        End With
    
    End Sub
    Next step is to setup your sprite and setup the frames for the animation. This can be done when loading your program:
    Code:
    Private Sub Setup_Sprite()
        
        With Sprite
        
            .X = 50
            .Y = 100
            
            With .Animation_State
            
                .Number_Of_Frames = 6
                .Number_Of_Textures = 4
                .Speed = 1.25
                .Initial_Frame = 0
                .Mode = ANIMATION_MODE_LOOP
                
                ReDim .Frame_Size(.Number_Of_Textures) As RECT
                ReDim .Offset(.Number_Of_Textures) As D3DVECTOR2
                ReDim .Texture_Number(.Number_Of_Frames) As Long
                
                .Frame_Size(0).Left = 0: .Frame_Size(0).Top = 0: .Frame_Size(0).Right = 60: .Frame_Size(0).Bottom = 92
                .Frame_Size(1).Left = 0: .Frame_Size(1).Top = 0: .Frame_Size(1).Right = 61: .Frame_Size(1).Bottom = 91
                .Frame_Size(2).Left = 0: .Frame_Size(2).Top = 0: .Frame_Size(2).Right = 59: .Frame_Size(2).Bottom = 94
                .Frame_Size(3).Left = 0: .Frame_Size(3).Top = 0: .Frame_Size(3).Right = 56: .Frame_Size(3).Bottom = 95
                
                .Offset(0).X = 0: .Offset(0).Y = 3
                .Offset(1).X = -1: .Offset(1).Y = 4
                .Offset(2).X = 1: .Offset(2).Y = 1
                .Offset(3).X = 2: .Offset(3).Y = 0
                
                .Texture_Number(0) = 0
                .Texture_Number(1) = 1
                .Texture_Number(2) = 0
                .Texture_Number(3) = 2
                .Texture_Number(4) = 3
                .Texture_Number(5) = 2
            
            End With
        
        End With
        
    End Sub
    This next sub routine is where it all happens. When used in a game loop before rendering it, this will animate your sprite!

    Code:
    Private Sub Animate_Sprite()
        
        Dim X As Single, Y As Single
        Dim Current_Texture As Long
        Dim Transparency_Color As Long
        
        'Right here will alphablend the polygon
        Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, False
    
        'Right here will give the polygon transparency
        Direct3D_Device.SetRenderState D3DRS_ALPHATESTENABLE, True
            
        With Sprite
        
            X = .X
            Y = .Y
            
            With .Animation_State
                
                If .Number_Of_Frames <> 0 Then
                    
                    If .Current_Frame <= 0 Then .Current_Frame = 0
                    
                    Select Case .Mode
            
                        Case ANIMATION_MODE_SINGLE_SHOT
                
                            If .Current_Frame > (.Number_Of_Frames - 1) Then
                    
                                .Current_Frame = (.Number_Of_Frames - 1)
                    
                            End If
                    
                        Case ANIMATION_MODE_LOOP
                        
                            If .Current_Frame > (.Number_Of_Frames - 1) Then
                            
                                .Current_Frame = 0
                            
                            End If
                    
                    End Select
                
                End If
    
                Current_Texture = .Texture_Number(.Current_Frame)
                
                Transparency_Color = D3DColorRGBA(255, 255, 255, 255)
        
                Frame.Left = (X + .Frame_Size(Current_Texture).Left + .Offset(Current_Texture).X) * Scalar.X
                Frame.Top = (Y + .Frame_Size(Current_Texture).Top + .Offset(Current_Texture).Y) * Scalar.Y
                Frame.Right = (X + .Frame_Size(Current_Texture).Right + .Offset(Current_Texture).X) * Scalar.X
                Frame.Bottom = (Y + .Frame_Size(Current_Texture).Bottom + .Offset(Current_Texture).Y) * Scalar.Y
        
                'Creates the 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, Sprite.Texture_List(Current_Texture)
                
                .Current_Frame = .Current_Frame + .Speed
                
            End With
        
        End With
        
    End Sub
    And obviously youll need your game loop and stuff to clear the screen, animate sprite, and render it

    Code:
    Private Sub Game_Loop()
    
        Do While Running = True
            
            '----------------------------------------------------
            'DirectX automatically handles the framerate for you
            'which makes it run (at most) as fast as the monitors
            'refresh rate, so you don't need to add extra code to
            'slow down the loop and run at a certain number of frames
            'per second.
            '----------------------------------------------------
            
            'Clears the backbuffer.
            Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 0), 1#, 0
                
                Direct3D_Device.BeginScene
                    
                    Animate_Sprite
                
                    'Draw the polygon
                    Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
                
                Direct3D_Device.EndScene
            
            'Flips the backbuffer into the form window.
            Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
            
            DoEvents
        Loop
    
    End Sub
    Now I know you don't know DX, but this code can easily be converted to whatever language or graphics library youre using. And with this code you have full control over the speed of the animation. If you need the whole source code, itll be located in my Massive DX Tutorial in my sig. But what I shown ya are just the key parts youll need. The sprite structure is of your choosing. You can have whatever you want. I just led you in the right direction for animation. And wish me luck at work tommorow. Youre welcome

    If you have any more questions, let me know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width