Page 1 of 1

Animated Image and Gadget

Posted: Tue May 04, 2004 6:28 am
by Flype
Code updated for 5.20+

here is an include file for my GadAnim in beta state
save and rename it GadAnim.pbi

Code: Select all

;----------------------------------------------------------------------------------------------
;
; Nom :       GadAnim.pbi
; Auteur :    Flype
; Date :      03/05/2004
; Purebasic : 3.90
;
; Explication du programme :
;
;   . Jeu de fonctions pour la création de gadgets animés
;   . L'animation est produite par une image dite "tube"
;     de la même manière que le jeu de fonctions AnimSprite (Librarie PureTools)
;
; Fonctions Publiques :
;
;   . GadAnimButton( #Gadget, x, y, w, h, #Image,ImageNB )
;   . GadAnimImage ( #Gadget, x, y, w, h, #Image,ImageNB, Flags )
;   . GadAnimStart ( hWnd, #Timer, Delay )
;   . GadAnimStop  ( hWnd, #Timer )
;   . GadAnimPause ()
;
; Fonctions Privées :
;
;   . GadAnimRefresh()
;
;----------------------------------------------------------------------------------------------


Declare GadAnimButton(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l)
Declare GadAnimImage(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l, Flags.l)
Declare GadAnimStart(hWindow.l, TimerID.l, Delay.l)
Declare GadAnimStop(hWindow.l, TimerID.l)
Declare GadAnimPause()
Declare GadAnimRefresh()


;----------------------------------------------------------------------------------------------


#GadAnim_MaxImages = 20


;----------------------------------------------------------------------------------------------


Structure GadAnimTimer_Struct
  
  hWindow.l
  TimerID.l
  Delay.l
  
EndStructure

Structure GadAnimList_Struct
  
  GadgetID.l
  Index.l
  Number.l
  Images.l[ #GadAnim_MaxImages ]
  
EndStructure


;----------------------------------------------------------------------------------------------


Global GadAnimTimer.GadAnimTimer_Struct
Global NewList GadAnimList.GadAnimList_Struct()


;----------------------------------------------------------------------------------------------

Procedure GadAnimButton(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l)
  
  hImage.l = ImageID(ImageID)
  
  If hImage=#Null
    ProcedureReturn #Null
  EndIf
  
  imgWidth     = ImageWidth(ImageID)
  imgHeight    = ImageHeight(ImageID)
  imgTileWidth = imgWidth/ImageNB
  
  AddElement(GadAnimList())
  
  GadAnimList()\GadgetID = GadgetID
  GadAnimList()\Number   = ImageNB-1
  GadAnimList()\Index    = 0
  
  For i=0 To ImageNB-1
    GadAnimList()\Images[i] = ImageID(GrabImage(ImageID,#PB_Any,i*imgTileWidth,0,imgTileWidth,imgHeight))
  Next
  
  ProcedureReturn ButtonImageGadget(GadgetID, x, y, w, h, hImage)
  
EndProcedure

Procedure GadAnimImage(GadgetID.l, x.l, y.l, w.l, h.l, ImageID.l, ImageNB.l, Flags.l)
  
  hImage.l = ImageID(ImageID)
  
  If hImage=#Null
    ProcedureReturn #Null
  EndIf
  
  imgWidth     = ImageWidth(ImageID)
  imgHeight    = ImageHeight(ImageID)
  imgTileWidth = imgWidth/ImageNB
  
  AddElement(GadAnimList())
  
  GadAnimList()\GadgetID = GadgetID
  GadAnimList()\Number   = ImageNB-1
  GadAnimList()\Index    = 0
  
  For i=0 To ImageNB-1
    GadAnimList()\Images[i] = ImageID(GrabImage(ImageID,#PB_Any,i*imgTileWidth,0,imgTileWidth,imgHeight))
  Next
  
  ProcedureReturn ImageGadget(GadgetID, x, y, w, h, hImage, Flags)
  
EndProcedure

Procedure GadAnimRefresh()
  
  ForEach GadAnimList()
    
    GadAnimList()\Index + 1
    
    If GadAnimList()\Index > GadAnimList()\Number
      GadAnimList()\Index = 0
    EndIf
    
    SetGadgetState( GadAnimList()\GadgetID, GadAnimList()\Images[GadAnimList()\Index])
    
  Next
  
EndProcedure

Procedure GadAnimStart(hWindow.l, TimerID.l, Delay.l)
  
  If hWindow<>#Null
    
    GadAnimTimer\hWindow = hWindow
    GadAnimTimer\TimerID = TimerID
    GadAnimTimer\Delay   = Delay
    
    SetTimer_(hWindow,TimerID,Delay,@GadAnimRefresh())
    
  EndIf
  
EndProcedure

Procedure GadAnimStop(hWindow.l, TimerID.l)
  
  If hWindow<>#Null
    KillTimer_(hWindow,TimerID.l)
  EndIf
  
EndProcedure

Procedure GadAnimPause()
  
  Static GadAnimSwitch.b
  
  If GadAnimSwitch
    GadAnimStart(GadAnimTimer\hWindow,GadAnimTimer\TimerID,GadAnimTimer\Delay)
  Else
    GadAnimStop(GadAnimTimer\hWindow,GadAnimTimer\TimerID)
  EndIf
  
  GadAnimSwitch = #True - GadAnimSwitch
  
EndProcedure


Posted: Tue May 04, 2004 6:31 am
by Flype
and now an example of how to use it
you should have some "Tube" images like the ones presents
in example folder of the PureTools_Lib

Code: Select all

;----------------------------------------------------------------------------------------------
; 
; Nom :       GadAnim.pb
; Auteur :    Flype
; Date :      03/05/2004
; Purebasic : 3.90
;
; Explication du programme : Exemple d'utilisation du fichier Include GadAnim.pbi
;
;----------------------------------------------------------------------------------------------


IncludeFile "GadAnim.pbi"


;- Initialisation du programme ----------------------------------------------------------------


Enumeration
  #Gad_1
  #Gad_2
  #Gad_3
EndEnumeration

Enumeration
  #Img_1
  #Img_2
  #Img_3
EndEnumeration


;- Chargement des images ----------------------------------------------------------------------


UseJPEGImageDecoder()

If LoadImage(#Img_1, "C:\DEV\PureBasic\Examples\PureTools_I\AnimSprite\Fight\fighter.bmp") = #Null : End : EndIf
If LoadImage(#Img_2, "C:\DEV\PureBasic\Examples\PureTools_I\AnimSprite\Fight\soldier.bmp") = #Null : End : EndIf
If LoadImage(#Img_3, "C:\DEV\PureBasic\Examples\PureTools_I\AnimSprite\Zylinder\zylinder.bmp") = #Null : End : EndIf


;- Création de l'interface --------------------------------------------------------------------


If OpenWindow(0, 0, 0, 338, 215, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "GadAnim") = #Null : End : EndIf
If CreateGadgetList(WindowID()) = #Null : End : EndIf

GadAnimButton(#Gad_1, 5, 5, 120, 100, #Img_1, 17)
GadAnimButton(#Gad_2, 5, 110, 120, 100, #Img_2, 9)
GadAnimImage(#Gad_3, 130, 5, 200, 200, #Img_3, 12, #PB_Image_Border)


;- Boucle Principale --------------------------------------------------------------------------


GadAnimStart(WindowID(), 1, 40)


Repeat
  
  Select WaitWindowEvent()
    Case #WM_CLOSE : Break
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case #Gad_1 : GadAnimPause()
        Case #Gad_2 : Break
      EndSelect
  EndSelect
  
ForEver


GadAnimStop(WindowID(), 1)


;- Fin du programme ---------------------------------------------------------------------------


End

Posted: Tue May 04, 2004 10:39 am
by Henrik
Very cool idea, Flype 8)

Best regards
Henrik

Posted: Tue May 04, 2004 6:25 pm
by Flype
thanx :wink:

does it works well
is there any "flickering" on your config ?

at home it works very well

and i ask pb users if you thinks it could be cool
to make a true userlib of this GadAnim.pbi :idea:

Posted: Tue May 04, 2004 7:42 pm
by Henrik
Flype wrote:thanx :wink:

does it works well
is there any "flickering" on your config ?
It workes very well no flickering :) Win98 SE

and i ask pb users if you thinks it could be cool
to make a true userlib of this GadAnim.pbi :idea
I would use it if it was available :wink:

Best Regards
Henrik

Posted: Tue May 04, 2004 7:48 pm
by Flype
good news :D

as i'm not an ASM coder, i'll just use TailBite
for making a pb userlib of it if pbusers are agree...

but before, have any idea, adds to implement ?

for exemples :

GetGadAnimDelay()

something like that

send me ideas :o