Anyone have animation code for the Sprite library?

Advanced game related topics
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Anyone have animation code for the Sprite library?

Post by dracflamloc »

Please do not link me to ANY pb libs unless they are open source.

I just wondered if anyone had any code they'd like to share with me for playing/controlling sprite animations. No API either please =)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Me! Me! Me!

Works under linux too :D

Code: Select all


Structure anim
  x.l
  y.l
  first.l
  last.l
  Delay.l
  current.l
  time.l
EndStructure

NewList Animation.anim()

Procedure Add_Animation(first,last,Delay.l);firstframe , lastframe, delay between frames
  AddElement(Animation())
  Animation()\x=0
  Animation()\y=0
  Animation()\first=first
  Animation()\current=first
  Animation()\last=last
  Animation()\Delay=Delay
  Animation()\time=ElapsedMilliseconds()
  ProcedureReturn ListIndex(Animation())
EndProcedure

Procedure Animate()
  
  ResetList(Animation())
  While NextElement(Animation())
    
    now=ElapsedMilliseconds()
    
    If now-Animation()\time>Animation()\Delay
      x=Animation()\current
      Animation()\current+1
      If x+1>Animation()\last
        Animation()\current=Animation()\first
      EndIf
      Animation()\time=now
    EndIf
    
  Wend
  
EndProcedure

Procedure Display_Animation(number,x,y)
  
  SelectElement(Animation(),number)
  Animation()\x=x
  Animation()\y=y
  DisplayTransparentSprite(Animation()\current,Animation()\x,Animation()\y)
  Animate()
  
EndProcedure

Procedure Destroy_Animation(number)
  SelectElement(Animation(),number)
  DeleteElement(Animation())
EndProcedure

Works like this ***pseudo code ***:

Code: Select all


loadyoursprites
Guy_Moving_Up=AddAnimation(1,6,250)

Repeat

cls
If keyhit=Move_Up
 for a=1 to 10
  Display_Anim(Guy_Moving_Up, x,y+a)
 next
endif

flipbuffer
Forever

Post Reply