Background Animation System

BlitzPlus Forums/BlitzPlus Programming/Background Animation System

This is a quick system for having randomly occuring animations on a static background in your game using image strips.

Thought it may be of use to some and hopefully improved.

Here's how it works.

First create a text file (using wordpad or similar) with the following information:

Path to Anim Strip Image
Id No.
No. of Frames
Frame Width
Frame Height
x_position on background
y_position on background
Animation Speed
Occurance Chance

If you set "Occurance Chance" as 0 it will not animate randomly - instead you can use the function ForceAnimation(id) to start off the animation.

Useful if you want to trigger an animation as a player moves past a certain part of the background.

So your config file would look like this:

gfx\back\tile1.bmp
1
28
124
76
59
376
60
5

gfx\back\tile2.bmp
2
22
115
117
96
252
50
20


And so on, for how many different entities are to be animated. Be sure to leave a blank line between each entity.

Save this as test_cfg.txt

Here is the code that processes the text file and contains the functions that will sit in your main game code.

Save this as bg_anim.bb

;*************************************************************
;* Description... bg_anim.bb
;* Date.......... 24.07.07
;* Author........ Yeshu777
;*************************************************************
 
;*************************************************************
; Animation Object Type Declaration
;*************************************************************

Type	anim_object

	Field	id
	Field	image
	Field	nof_frames
	Field	width
	Field	height
	Field	x_pos
	Field	y_pos

	Field	status
	
	Field	index
	Field	speed
	Field	chance
	Field	timer

End Type

;*************************************************************
; Global variables
;*************************************************************

Global	anim.anim_object;

;*************************************************************
;* Description... Initialise the animation list from the config
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameters.... Path to the config file
;*************************************************************

Function	ProcessConfig(config$)
			
	file = ReadFile(config$);
				
	; Read the config into a list of types
	
	While(Not Eof(file))
		
		anim.anim_object = New anim_object;
			
		path$ = ReadLine(file);

		anim\id = Int(ReadLine(file));
		anim\nof_frames = Int(ReadLine(file)) - 1;
		anim\width = Int(ReadLine(file));
		anim\height = Int(ReadLine(file));
		anim\x_pos = Int(ReadLine(file));
		anim\y_pos = Int(ReadLine(file));
		anim\speed = Int(ReadLine(file));
		anim\chance = Int(ReadLine(file));

		; Load the animation strip
		
		anim\image = LoadAnimImage(path$, anim\width, anim\height, 0, anim\nof_frames);

		; Default Mask 
		MaskImage(anim\image, 255,0,255);
		
		; Set Object Defaults
		anim\status = False;
		anim\index = 0;
		anim\timer = 0;

		; Skip blank line
		ReadLine(file);
				
	Wend
	
	CloseFile(file);

	; Required to setup Random Decision

	SeedRnd(MilliSecs());
			
	Return	

	; For Debug only
	
	For anim.anim_object = Each anim_object

		Print "Id: "+anim\id
		Print "Frames: "+anim\nof_frames;
		Print "Width: "+anim\width;
		Print "Height: "+anim\height;
		Print "X: "+anim\x_pos
		Print "Y: "+anim\y_pos
		Print "Speed: "+anim\speed;
		Print "Chance: "+anim\chance
		Print "Time: "+anim\timer;
		Print "";
		Delay(200);

	Next

End Function

;*************************************************************
;* Description... Reiterate through the animation, activate 
;* .............. update frame & limit check, deactivate
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameter1.... Animation	List of anim_objects
;*************************************************************

Function	ProcessAnimation(list.anim_object)

	For list.anim_object = Each anim_object

		If(list\status = False) Then

			; Chance of animation Starting
			
			If(RandomDec(list\chance)) Then

				list\index = 0;				Restart Animation
				list\timer = MilliSecs();	Update Timer
				list\status = True;			Enable 

			End If

		Else		
			
			; Animation Timing
			
			If(MilliSecs() > list\timer + list\speed) Then

				list\timer = MilliSecs();		Update Timer
				list\index = list\index + 1;	Increment Frame
				
			End If
								
			; Animtion Frame Limiting
			
			If(list\index >= list\nof_frames) Then

				list\index = 0;
				list\status = False;
				
			End If
								
		End If
				
	Next

End Function

;*************************************************************
;* Description... Parse the list and draw any active animations
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameter1.... Animation	List of anim_object
;* Returns....... Nothing
;*************************************************************

Function	DrawAnimation(list.anim_object)

	For list.anim_object = Each anim_object

		If(list\status = True) Then

			DrawImage(list\image, list\x_pos, list\y_pos, list\index);
									
		End If
				
	Next

End Function

;*************************************************************
;* Description... Force a specific list item to animate
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameter1.... Item ID
;* Returns....... Nothing
;*************************************************************

Function	ForceAnimation(id)

	For list.anim_object = Each anim_object

		If(list\id = id) Then
                        list\index = 0;
                        list\timer = MilliSecs();
			list\status = True;
		End If
		
	Next

End Function

;*************************************************************
;* Description... Free the animation list
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameter1.... Animation	List of anim_object
;* Returns....... Nothing
;*************************************************************

Function	FreeAnimation(list.anim_object)

	For list.anim_object = Each anim_object
		Delete(list)				
	Next

End Function

;*************************************************************
;* Description... Random Decision
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameters.... Percenatge chance (1000 = 100%)
;* Returns....... True or False
;*************************************************************

Function	RandomDec(percentage)

	If(Rand(1,1000) < percentage) Then
		Return(True);
	End If
	
	Return(False);

End Function


Here is how it is implemented in the main game:

;*************************************************************
;* Description... Main.bb
;* Date.......... 24.07.07
;* Author........ Yeshu777
;*************************************************************

	Include "bg_anim.bb";			BackGround Animation System

	; Setup Graphic Mode

	Graphics(800,600,32,3);
	SetBuffer(BackBuffer());


;*************************************************************
; Global Vars
;*************************************************************

	Global 	background;

	
;*************************************************************
; Load Gfx
;*************************************************************

	background = LoadImage("backgnd.bmp")
	
	ProcessConfig("test_cfg.txt");


;*************************************************************
; Init Game
;*************************************************************



;*************************************************************
;* Description... Main Program Loop
;* Date.......... 24.07.07
;* Author........ Yeshu777
;* Parameters.... None	
;*************************************************************

	While(Not KeyHit(1))
				
		ProcessAnimation(anim);

		Cls
		
		DrawImage(background,0,0,0);

                DrawAnimation(anim);	
				
		Flip
		
	Wend

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

	FreeAnimation(anim);
	
End


If you want to add more animated entities then you can just add to the config file leaving the game code untouched.