Procedures for Sprite animated

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Procedures for Sprite animated

Post by dobro »

2 procedures to animate a sprite that would "board Sprite"
Style :
Image

the board is charged with :
LoadSprite2(id.l, file_name.s, taille_x, taille_y, nbr_frame_par_line,nbr_ligne)
id.l = number of sprite
file_name.s = the Path to the sprite-board
taille_x = size X each cell (64 ) for our case with birds
taille_y = size y each cell (64 )
nbr_frame_par_line = number of images per line (ici 4 )
nbr_ligne = number of the line, into the board ... ben 4 aussi :)


for displaying animation
the function:
Anim_sprite(id.l,num_ligne,x_spr,y_spr,mode,vitesse)
id.l= number of sprite
num_ligne = number of online animation that we want to see display
x_spr = X coordinate of the sprite on the screen
y_spr = Y coordinate of the sprite on the screen
mode = loop mode if = 0; PingPong mode if = 1
Vitesse = smaller it is, the more it's fast ... 0 = Stop animation


here is the sample code with the board Sprite birds blue above :)

the advantage of this function is that the Sprite will always have number of reference (the iD)
whatever the animation, since it is with the procedure Anim_sprite () we chose that our Sprite # xxxx must :D

Code: Select all

;***********************************************
;Titre  :*Sprite_animé_procedure
;Auteur  : Dobro
;Date  :13/03/2014
;Heure  :16:59:30
;Version Purebasic :  PureBasic 5.21 LTS (Windows - x86)
;Version de l'editeur :EPB V2.54
; Libairies necessaire : Aucune 
;***********************************************

UsePNGImageDecoder()
Declare  LoadSprite2(id.l, file_name.s, taille_x, taille_y, nb_frame_par_line,nbr_ligne)
Declare  Anim_sprite(id.l,num_ligne,x_spr,y_spr,mode,vitesse)
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
	MessageRequester("Error", "Sprite system can't be initialized", 0)
	End
EndIf
Enumeration 1
	#Bird
	#image_fond
Endenumeration
;-image de fond (degradé )
If CreateImage(#image_fond, 800, 600) 
	if StartDrawing(ImageOutput(#image_fond))
		DrawingMode(#PB_2DDrawing_Gradient)      
		BackColor($F8A106)
		FrontColor($3470F)
		GradientColor(0.16,RGB(125,125,255))
		LinearGradient(0, 0, 0, 600)    
		Box(0, 0, 800, 600, $FFFFFF)
		StopDrawing() 
	Endif
EndIf
;*******
OpenWindow(0, 0, 0, 800, 600, "2D Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
	
	;-je charge la planche de sprite
	taille_x=64 ; la taille des cellule
	taille_y=64; la taille des cellule
	nbr_frame_par_line=4 ; il y a 4 image 64x64 par ligne
	nbr_ligne=4 ; il y a 4 lignes
	File_name.s=getcurrentdirectory()+"bird.png" ; le chemin de la planche
	LoadSprite2(#bird, File_name.s, taille_x, taille_y, nbr_frame_par_line,nbr_ligne)
	
	Repeat
		WaitWindowEvent(20)
		ExamineKeyboard()
		ExamineMouse()
		StartDrawing(ScreenOutput())
			DrawImage(imageid(#image_fond),1,1)
		StopDrawing()
		;-animation du sprite
		Anim_sprite(#bird,0,100,100,1,30) ; ici j'affiche la premiere ligne de la planche en Mode PingPong, avec une vitesse de 30 (plus c'est grand , plus c'est lent )
		Anim_sprite(#bird,1,200,100,0,5) ; ici j'affiche la Deuxieme ligne de la planche en mode Boucle avec une vitesse de 5
		Anim_sprite(#bird,2,300,100,1,10) ; ici j'affiche la troisieme ligne de la planche  en Mode PingPong avec une vitesse de 10
		Anim_sprite(#bird,3,400,100,1,0) ; ici j'affiche la 4em ligne de la planche en mode PingPong l'animation est stopé par la vitesse =0
		
		FlipBuffers()
		ClearScreen(RGB(0,0,0))
	Until KeyboardPushed(#PB_Key_Escape)
	Else
	MessageRequester("Error", "Can't open a 800*600 - 32 bit screen !", 0)
EndIf
End   



; *************** zone procedure  **********************
;-procedures Zone
Procedure LoadSprite2(id.l, file_name.s, taille_x, taille_y, nbr_frame_par_line,nbr_ligne)
	; by Dobro
	structure Spr
		id.l ; id
		x.l ; pos x
		y.l; posy
		taille_x.l ; taille x cellule
		taille_y.l ; taille y cellule
		nbr_frame_par_line.l ; nombre d'image par ligne
		nbr_ligne.l ; nombre de ligne dans la planche
		compteur.l [20] ; compteur du pointeur d'image de l'animation
		Pas.l[20]  ; le pas de progression de l'image en cours pour l'animation
		Frame.l[20]  ; l'image pointé de l'animation
		num_ligne.l[20]  ; ligne actuel de l'animation
		compteur_aff.l[20]
		vitesse.l[20]
	Endstructure
	global dim Spr.Spr(id.l)
	Spr(id.l)\id.l=id.l
	Spr(id.l)\taille_x.l=taille_x
	Spr(id.l)\taille_y.l=taille_y
	Spr(id.l)\nbr_frame_par_line.l=nbr_frame_par_line
	Spr(id.l)\nbr_ligne.l=nbr_ligne
	for i=0 to 19
		Spr(id.l)\compteur.l[i] =0
		Spr(id.l)\Pas.l [i]  =1
		Spr(id.l)\Frame.l[i]=1
		spr(id.l)\pas [i]=1
	Next i
	LoadSprite(id.l,file_name.s,#PB_Sprite_AlphaBlending)
Endprocedure


Procedure Anim_sprite(id.l,num_ligne,x_spr,y_spr,mode,vitesse)
	; by Dobro
	Spr(id.l)\vitesse=vitesse
	select mode
		Case 0 ; boucle
		spr(id.l)\compteur_aff.l[num_ligne]=spr(id.l)\compteur_aff.l[num_ligne]+spr(id.l)\pas [num_ligne] 
		;deplacement du pointeur de lecture
		if spr(id.l)\compteur_aff.l[num_ligne]=Spr(id.l)\vitesse
			spr(id.l)\compteur_aff.l[num_ligne]=0
			Spr(id.l)\compteur[num_ligne]=Spr(id.l)\compteur[num_ligne]+spr(id.l)\pas [num_ligne] ;avance continuel du pointeur de lecture
			Spr(id.l)\Frame.l[num_ligne]=Spr(id.l)\Frame.l[num_ligne]+spr(id.l)\pas [num_ligne] * Spr(id.l)\taille_x.l
		Endif
		;*********************************
		; calcul depassement
		if Spr(id.l)\compteur[num_ligne]=Spr(id.l)\nbr_frame_par_line.l
			Spr(id.l)\Compteur[num_ligne]=0
			Spr(id.l)\Frame.l[num_ligne]=0
		Endif
		Case 1 ; pingpong
		spr(id.l)\compteur_aff.l[num_ligne]=spr(id.l)\compteur_aff.l[num_ligne]+1
		;deplacement du pointeur de lecture
		if spr(id.l)\compteur_aff.l[num_ligne]=Spr(id.l)\vitesse
			spr(id.l)\compteur_aff.l[num_ligne]=0
			Spr(id.l)\compteur[num_ligne]=Spr(id.l)\compteur[num_ligne]+spr(id.l)\pas [num_ligne] ;avance continuel du compteur de lecture dans la ligne d'animation
			Spr(id.l)\Frame.l[num_ligne]=(Spr(id.l)\compteur[num_ligne] * Spr(id.l)\taille_x.l) ; calcul du pointeur de cellule
			; calcul depassement
			if Spr(id.l)\compteur[num_ligne]=Spr(id.l)\nbr_frame_par_line.l or Spr(id.l)\compteur[num_ligne]=0
				if Spr(id.l)\compteur[num_ligne]=Spr(id.l)\nbr_frame_par_line.l
				Endif
				spr(id.l)\pas [num_ligne]=-spr(id.l)\pas [num_ligne] ; inversion de pas
				if Spr(id.l)\compteur[num_ligne]=Spr(id.l)\nbr_frame_par_line.l
					Spr(id.l)\compteur[num_ligne]=Spr(id.l)\compteur[num_ligne]-1
					Spr(id.l)\Frame.l[num_ligne]=Spr(id.l)\compteur[num_ligne] * Spr(id.l)\taille_x.l
				Endif
			Endif
		Endif
		;*********************************
	EndSelect
	Spr(id.l)\num_ligne.l=num_ligne * Spr(id.l)\taille_y.l ; la ligne actuellement affichée
	ClipSprite(Spr(id.l)\id.l,#PB_Default ,#PB_Default ,#PB_Default ,#PB_Default )
	ClipSprite(Spr(id.l)\id.l,Spr(id.l)\Frame.l[num_ligne],Spr(id.l)\num_ligne.l,Spr(id.l)\taille_x.l,Spr(id.l)\taille_y.l)
	DisplayTransparentSprite(Spr(id.l)\id.l,x_spr,y_spr)
	
EndProcedure

; Epb

Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/