Basic Animation Stuff . . . ^_^;;

Source-code only - please, don't post questions here.
Pritchard
Posts: 5425
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Basic Animation Stuff . . . ^_^;;

Postby Pritchard » Feb 01, 2007 21:32

Basically, it's Charlie. He's all cool and likes toothpaste. Did I mention this is ASCII animation? Move around with arrow keys... Hit Enter to Animate Charlie!

EDIT: This is old, but still kinda nice. Has quite a few features like timer rollover I think :-)

Code: Select all

#include "fbgfx.bi"
Namespace ASCII
 
  Enum ASCII_OPTIONS
    ASCII_LOOP = 1
    ASCII_COLOR = 2
    ASCII_KEY = 4
  End Enum

  Type Frame
    Text                  As zstring ptr
    Delay                 As Double
    Declare Destructor()
  End Type

  Type Anim
    Color                 As Uinteger
   
    FrameSet              As Frame Ptr ptr
    NumFrames             As Integer
   
    CurFrame              As Integer
    LastTime              As Double
   
    Options               As Integer = 0
   
    Key                   As Integer = -1
    Moving                As Integer = 0
   
    X                     As Integer
    Y                     As Integer
   
    Declare Constructor()
    Declare Destructor()
   
    Declare Sub Draw( Byval _X_ As Integer, Byval _Y_ As Integer )
    Declare Sub GetInput()
      Declare Sub Move ( Byval _X_ As Integer, Byval _Y_ As Integer)
    Declare Sub NewFrame( Byref Char As zString ptr = @"", Byval Length As Integer = 50 )
    Declare Sub SetOption( Byval OptionVal As Integer, Byval Switch As Integer = 0 )
    Declare Sub Update()
      Declare Sub UpdateFrame()
  End Type
 
End Namespace

Dim As ASCII.Anim Ptr Charlie = New ASCII.Anim
 
  ' SetOption( Switch, SwitchValue ).  If there's two switches here that require value
  ' THEY ARE NOT COMPATABLE TO CALL AT THE SAME TIME!!!!
 
  ' Set the ascii Charlie's color, and set it so he animates once through when you hit Enter
Charlie->SetOption( ASCII.ASCII_LOOP Or ASCII.ASCII_COLOR, rgb(255, 0, 0) )
Charlie->SetOption( ASCII.ASCII_KEY, FB.SC_ENTER )

  ' His ASCII Animation... Time is in Milliseconds.  Feel free to change his anim.
Charlie->NewFrame( "d^_^=====b   d(>_<d==)", 500 )
Charlie->NewFrame( "d=^_^====b   d(>_<d==)" )
Charlie->NewFrame( "d==^_^===b   d(>_<d==)" )
Charlie->NewFrame( "d===^_^==b   d(>_<d==)" )
Charlie->NewFrame( "d====^_^=b  d=(>_<d==)" )
Charlie->NewFrame( "d=====^_^b d==(>_<d==)", 500 )
Charlie->NewFrame( "d====^_^=bd===(>_<d==)" )
Charlie->NewFrame( "d===^_^==b d==(>_<d==)" )
Charlie->NewFrame( "d==^_^===b  d=(>_<d==)" )
Charlie->NewFrame( "d=^_^====b   d(>_<d==)" )

Screenres 640, 480, 32
screenlock
Do
 
  Charlie->GetInput()
  Charlie->Update()
  Charlie->Draw(Charlie->X, Charlie->Y)
 
  Locate 1, 20
  Print "Hit Enter to Animate Charlie"
    ' Flip page fyi...
  Screenunlock
  Screenlock
  Cls
  Sleep 5, 1
 
Loop Until Multikey(&h01)
Screenunlock  ' ALWAYS UNLOCK SCREEN!
Delete Charlie


Namespace ASCII
 
  Destructor Frame()
    If This.Text <> 0 then Deallocate This.Text
  End Destructor
 
  Constructor Anim
    This.LastTime = Timer ' Who knows what this shall end up if we didn't have this.
  End Constructor
 
  Destructor Anim
    For DoVar as Integer = 0 to This.NumFrames - 1
      Delete This.FrameSet[DoVar]
    Next
    Deallocate This.FrameSet
  End Destructor
 
  Sub Anim.Draw( Byval _X_ As Integer, Byval _Y_ As Integer )
   
      '' Draw Charlie! ^_^;;=b  He defaults to white...
    If ( This.Options And ASCII_COLOR ) <> 0 Then
      ..Draw String (_X_, _Y_), *This.FrameSet[This.CurFrame]->Text, This.Color
    Else
      ..Draw String (_X_, _Y_), *This.FrameSet[This.CurFrame]->Text, Rgb(255, 255, 255)
    End If
   
  End Sub
 
  Sub Anim.GetInput()
   
    Dim As Integer _X_, _Y_
   
      '' Yup, get input... :D
    If ( This.Options And ASCII_KEY ) <> 0 Then
      If Multikey(This.Key) Then This.Moving = -1
    End If
    If Multikey(FB.SC_LEFT) Then _X_ -= 1
    If Multikey(FB.SC_RIGHT) Then _X_ += 1
    If Multikey(FB.SC_UP) Then _Y_ -= 1
    If Multikey(FB.SC_DOWN) Then _Y_ += 1
   
      '' A little optimization
    If _X_ <> 0 Then
      This.Move( _X_, _Y_ )
    Else
      If _Y_ <> 0 Then
        This.Move( 0, _Y_ )
      End If
    End If
   
  End Sub
 
    Sub Anim.Move( Byval _X_ As Integer, Byval _Y_ As Integer)
     
      This.X += _X_
      This.Y += _Y_
     
    End Sub
 
  Sub Anim.NewFrame( Byref Char As zString ptr = @"", Byval Length As Integer  = 50 )
     
      '' Resize charlie (plastic asurcceryii)
    This.NumFrames += 1
   
    This.FrameSet = Reallocate( This.FrameSet, sizeof(Frame ptr) * This.NumFrames )
    This.FrameSet[This.NumFrames - 1] = New Frame
    If Char <> 0 then
      This.FrameSet[This.NumFrames - 1]->Text = Callocate(sizeof(Zstring)*(Len(*Char)+1))
      *This.FrameSet[This.NumFrames - 1]->Text = *Char
    End If
    This.FrameSet[This.NumFrames - 1]->Delay = Length
   
  End Sub
 
  Sub Anim.SetOption( Byval OptionVal As Integer, Byval Switch As Integer = 0 )
   

    This.Options Or = OptionVal
   
      '' We can only set one of certain options at once, hence the else's
    If ( OptionVal And ASCII_COLOR ) <> 0 Then
      This.Color = Switch
    Else
      If ( OptionVal And ASCII_KEY ) Then
        This.Key = Switch
      End If
    End If
   
  End Sub
 
  Sub Anim.Update()
   
      '' If Time since last update > Time frame lasts, move up one frame
    If ( Timer - This.LastTime ) >= This.FrameSet[This.CurFrame]->Delay / 1000 Then
      If ( This.Options And ASCII_KEY ) <> 0 Then
        If This.Moving <> 0 Then
          This.UpdateFrame()
        End If
      Else
        This.UpdateFrame()
      End If
    End If
   
  End Sub
 
    Sub Anim.UpdateFrame()
     
        '' Move up frame.  Loop if necessary.  Reset moving variable.
      This.CurFrame += 1
      This.LastTime = Timer
      If ( This.Options And ASCII_LOOP ) <> 0 Then
        If This.CurFrame > This.NumFrames - 1 Then
          This.CurFrame = 0
          This.Moving = 0
        End If
      Else
        If This.CurFrame > This.NumFrames - 1 Then
          This.CurFrame = This.NumFrames - 1
          This.Moving = 0
        End If
      End If
     
    End Sub
 
End Namespace
Last edited by Pritchard on Aug 01, 2007 6:26, edited 2 times in total.
acetoline
Posts: 228
Joined: Oct 27, 2006 6:50
Contact:

Postby acetoline » Feb 02, 2007 5:19

This is a good prog for those of us who don't know nada about animation. Good stuff, Pritchard.
Pritchard
Posts: 5425
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Aug 01, 2007 6:26

bump (updated) - twas a memory leak

Return to “Tips and Tricks”

Who is online

Users browsing this forum: Google [Bot], Kuan Hsu and 0 guests