B3D or BP :: Static Data Loader

Blitz3D Forums/Blitz3D Tutorials/B3D or BP :: Static Data Loader

darklordz(Posted 2004) [#1]
This is a simple tutorial on how to create a loading bar and use it to load static data like images or sounds. It can be improoved greatly but it's just a v0.0.2 so i'll update it alot i think. It contains a basic set of functions that display the use of types and how to contain ur loaded static media in 1 place. I created this mostly for noobs because most of you already know this...

First Copy past this example then scroll down and change the Dload_Add_Item("xxxxx",FILE_IMG%) lines to any image(s),sounds,meshes etc... in your pc that blitz supports. Then execute the demo. you'll see that it actualy loads your data...

Now to understand what i have done you need to view the functions and types. You can get all kinds of data regarding size filename and more with the use of the functions....Try and understand what each functions is if you have questions or don't know what the f**k is written there lemme know and i'll try to elaborate...

P.S: I used images i scanned in the total size of images were 45 MB and it loaded super fast :P

demo of v0.0.1 here : *clicky* 600 KB
demo of v0.0.2 here : *clicky* <- 2.6 MB

the theory is very simple. we first create a type to store all entity attributes (filename, size, type, parent, flags etc...) then we use this type as a sort of database to work from. We can get all kinds of information from this type. The basic set of function helps generate sutch usefull info. Study each function to get a basic grip of whats happening. They specifies type initiates a specified loading routine during the Do_Load Process. And resturns it's handle. Then you can use this to load anything and be sure that you don't have to mess up anymore....

note: customize the progresbar as you please bu modifying the way it's drawn in Dload_Draw_ProgressBar%(ID%), customize the way things are loaded in Dload_Do_Load(), other functions arent relevant to ya...

; Static Data Loader v0.0.2
; By R.Baldewsingh (03.2004)

Const FILE_XXX% =  0
Const FILE_IMG% =  1 ; Static Image
Const FILE_SND% =  2 ; Sound
Const FILE_S3D% =  3 ; 3D Sound
Const FILE_MSH% =  4 ; Static Mesh
Const FILE_AMS% =  5 ; Animated Mesh
Const FILE_TER% =  6 ; Terrain
Const FILE_MD2% =  7 ; MD2 Model
Const FILE_TEX% =  8 ; Texture
Const FILE_SPR% =  9 ; Sprite

Const RET_B%    = 0
Const RET_KB%   = 1
Const RET_MB%   = 2

Type DLOAD
	Field ID%
	Field FILE$
	Field SIZE#
	Field FTYP%
	Field HWND%
	Field PRNT%
	Field FLAG%
	Field STAT%
	Field PERC#
End Type

Type PRGBAR
	Field ID%
	Field XPOS%
	Field YPOS%
	Field WIDTH%
	Field HEIGHT%
	Field PVAL%
	Field MIN%
	Field MAX%
End Type

Function Dload_Add_Item(FILE$,FTYP%,PRNT%=0,FLAG%=0)
	If FileType(FILE$) = 1
		D.DLOAD = New DLOAD
		D\ID% = Dload_Get_LastID%()
		D\FILE$ = FILE$
		D\FTYP% = FTYP%
		D\SIZE# = FileSize(D\FILE$)
		D\STAT% = 0
		D\PERC% = 0
		D\PRNT% = PRNT%
		D\FLAG% = FLAG%
		Return D\ID%
	Else
		Return 0
	EndIf
End Function

Function Dload_Remove_Item%(ID%)
	For D.DLOAD = Each DLOAD
		If ID% = D\ID%
			If Dload_Get_Loaded(D\ID%) = 1
				Select FTYP%
					Case FILE_IMG%
						FreeImage(D\HWND%)
					Case FILE_SND%
						FreeSound(D\HWND%)
					Case FILE_MSH%
						FreeEntity(D\HWND%)
					Case FILE_AMS%
						FreeEntity(D\HWND%)
					Case FILE_TER%
						FreeEntity(D\HWND%)
					Case FILE_MD2%
						FreeEntity(D\HWND%)
					Case FILE_TEX%
						FreeTexture(D\HWND%)
					Case FILE_SPR%
						FreeEntity(D\HWND%)
				End Select
			EndIf
			Delete D
			Return 1
		EndIf
	Next
End Function

Function Dload_Remove_AllItems%()
	For D.DLOAD = Each DLOAD
		If Dload_Get_Loaded(D\ID%) = 1
			Select FTYP%
				Case FILE_IMG%
					FreeImage(D\HWND%)
				Case FILE_SND%
					FreeSound(D\HWND%)
				Case FILE_MSH%
					FreeEntity(D\HWND%)
				Case FILE_AMS%
					FreeEntity(D\HWND%)
				Case FILE_TER%
					FreeEntity(D\HWND%)
				Case FILE_MD2%
					FreeEntity(D\HWND%)
				Case FILE_TEX%
					FreeTexture(D\HWND%)
				Case FILE_SPR%
					FreeEntity(D\HWND%)
			End Select
		EndIf
		Delete D
		Return 1
	Next
End Function


Function Dload_Get_LastID%()
	For D.DLOAD = Each DLOAD
		X% = X% + 1
	Next
	Return X%
End Function

Function Dload_Get_Size#(ID%,RETVAL%=0)
	For D.DLOAD = Each DLOAD
		If ID% = D\ID%
			Select RETVAL%
				Case 0
					X# = X#
				Case 1
					X# = D\SIZE#/1024
					Z# = Instr(Str(X#),".")
					X# = Float(Left(Str(X#),Z#+2))
				Case 2
					X# = (D\SIZE#/1024/1024)
					Z# = Instr(Str(X#),".")
					X# = Float(Left(Str(X#),Z#+2))
				Case 3 
					X# = Dload_Get_Size#(ID%,2)/1000
					Z# = Instr(Str(X#),".")
					X# = Float(Left(Str(X#),Z#+3))
			End Select
		EndIf
	Next
	Return X#
End Function

Function Dload_Get_TotalSize#(RETVAL%=0)
	For D.DLOAD = Each DLOAD
		X# = X# + FileSize(D\FILE$)
	Next
	Select RETVAL%
		Case 0
			X# = X#
		Case 1
			X# = X#/1024
			Z# = Instr(Str(X#),".")
			X# = Float(Left(Str(X#),Z#+3))
		Case 2
			X# = (X#/1024/1024)
			Z# = Instr(Str(X#),".")
			X# = Float(Left(Str(X#),Z#+2))
		Case 3 
			X# = Dload_Get_TotalSize#(2)/1000
			Z# = Instr(Str(X#),".")
			X# = Float(Left(Str(X#),Z#+3))
	End Select
	Return X#
End Function

Function Dload_Get_Loaded(ID%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID%
			Return D\STAT%
		EndIf
	Next
End Function

Function Dload_Set_Loaded(ID%,VAL%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID%
			D\STAT% = VAL%
		EndIf
	Next
End Function

Function Dload_Set_GlobalPercentage#()
	For D.DLOAD = Each DLOAD
		D\PERC# = (Dload_Get_TotalSize#(RET_KB%)/100)*(FileSize(D\FILE$)/1024)
	Next
End Function

Function Dload_Get_GlobalPercentage#()
	For D.DLOAD = Each DLOAD
		X# = X# + D\PERC#
	Next
	Return X#
End Function

Function Dload_Set_Precentage#(ID%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID%
			D\PERC# = (Dload_Get_TotalSize#(RET_KB%)/100)*(FileSize(D\FILE$)/1024)
		EndIf
	Next
End Function

Function Dload_Get_Percentage#(ID%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID%
			Return D\PERC#
		EndIf
	Next
End Function

Function Dload_Get_VirtualPercentage#(ID%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID%
			Return (Dload_Get_Percentage#(D\ID%)*100)/Dload_Get_GlobalPercentage#()
		EndIf
	Next
End Function

Function Dload_Get_TotalVirtualPercentage#()
	For D.DLOAD = Each DLOAD
		 X# = X# + (Dload_Get_Percentage#(D\ID%)*100)/Dload_Get_GlobalPercentage#()
	Next
	Return X#
End Function

Function Dload_Create_ProgressBar(X%,Y%,W%,H%,IV%,MN%,MX%)
	P.PRGBAR = New PRGBAR
	P\ID%     = Dload_Get_LastProgressBar%()
	P\XPOS%   = X%
	P\YPOS%   = Y%
	P\WIDTH%  = W%
	P\HEIGHT% = H%
	P\PVAL%   = VI%
	P\MIN%    = MN%
	P\MAX%    = MX%
End Function

Function Dload_Get_LastProgressBar%()
	For P.PRGBAR = Each PRGBAR 
		X% = X% + 1
	Next
	Return X%
End Function

Function Dload_Draw_ProgressBar%(ID%)
	For P.PRGBAR = Each PRGBAR 
		If P\ID% = ID%
			Flip:Cls
			R% = ColorRed()
			G% = ColorGreen()
			B% = ColorBlue()
			Rect P\XPOS%,P\YPOS%,P\WIDTH%,P\HEIGHT%,1
			Color(0,0,0)
			Rect P\XPOS%+5,P\YPOS%+5,P\WIDTH%-10,P\HEIGHT%-10,1
			Color(R%,G%,B%)
			Rect P\XPOS%+8,P\YPOS%+8,(P\WIDTH%/100)*P\PVAL%-16,P\HEIGHT%-16,1
			Text 20,20,Dload_Get_ProgressBarValue(1) ; Comment this
		EndIf
	Next
End Function

Function Dload_Update_ProgressBar()
	For P.PRGBAR = Each PRGBAR
		Dload_Draw_ProgressBar%(P\ID%)
	Next
End Function

Function Dload_Set_ProgressBarValue(ID%,VAL%)
	For P.PRGBAR = Each PRGBAR
		If P\ID% = ID%
			P\PVAL% = VAL%
		EndIf
	Next
End Function

Function Dload_Get_ProgressBarValue(ID%)
	For P.PRGBAR = Each PRGBAR
		If P\ID% = ID%
			Return P\PVAL%
		EndIf
	Next
End Function

Function Dload_Get_Handle(ID%)
	For D.DLOAD = Each DLOAD
		If D\ID% = ID% And Dload_Get_Loaded(ID%) = 1
			Return D\HWND%
		EndIf
	Next
End Function

Function Dload_Do_Load()
	Dload_Set_GlobalPercentage()
	Dload_Create_ProgressBar(20,GraphicsHeight()-60,GraphicsWidth()-36,40,0,0,100)
	For D.DLOAD = Each DLOAD
		Select D\FTYP%
			Case FILE_IMG%
				D\HWND% = LoadImage(D\FILE$)
			Case FILE_SND%
				D\HWND% = LoadSound(D\FILE$)
			Case FILE_S3D%
				D\HWND% = Load3DSound(D\FILE$)
			Case FILE_MSH%
				D\HWND% = LoadMesh(D\FILE$,D\PRNT%)
			Case FILE_AMS%
				D\HWND% = LoadAnimMesh(D\FILE$,D\PRNT%)
			Case FILE_TER%
				D\HWND% = LoadTerrain(D\FILE$,D\PRNT%)
			Case FILE_MD2%
				D\HWND% = LoadMD2(D\FILE$,D\PRNT%)
			Case FILE_TEX%
				D\HWND% = LoadTexture(D\FILE$,D\FLAG%)
			Case FILE_SPR%
				D\HWND% = LoadSprite(D\FILE$,D\FLAG%,D\PRNT%)
		End Select
		Dload_Set_Loaded(D\ID%,1)
		Dload_Set_ProgressBarValue(1,Dload_Get_ProgressBarValue(1)+Dload_Get_VirtualPercentage#(D\ID%))
		Dload_Update_ProgressBar()
	Next 
	While I < 50
		Dload_Update_ProgressBar()
		I = I + 1
	Wend
End Function

Function Dload_DisplayInfo%()
	Dload_Set_GlobalPercentage()
	Text 20,20,"FILES: "+Dload_Get_LastID%()
	Text 20,35,"TOTALSIZE: "+Dload_Get_TotalSize#(RET_MB%)+" MB"
	Z% = 50 : I% = 1
	For D.DLOAD = Each DLOAD
		If D\STAT% = 1
			STATS$ = "LOADED"
		Else
			STATS$ = "NOT LOADED"
		EndIf
		Text 20,Z%,"FILESIZE "+I%+": "+Dload_Get_Size#(D\ID%,RET_KB%)+" KB ("+STATS$+") "+Dload_Get_VirtualPercentage#(D\ID%)+"%"
		I% = I% + 1
		Z% = Z% + 15
	Next
	Text 20,Z%+15,"MOUSE CLICK TO CONTINUE..."
End Function
AppTitle "Static Data Loader Demo"
Graphics3D 640,480,32,2
SetBuffer BackBuffer()

Include "Static Data Loader v0.0.2.bb"

	CAMPIVOT% = CreatePivot()
	CAMERA% = CreateCamera(CAMPIVOT%) 
	LIGHT% = CreateLight()

	CreateListener(CAMERA%)
	MoveEntity CAMERA,0,0,-5
	RotateEntity LIGHT%,90,0,0 

	Dload_Add_Item("media\rovkuken.jpg",FILE_IMG%)
	Dload_Add_Item("media\testsnd.wav",FILE_SND%)
	Dload_Add_Item("media\oildrum.3ds",FILE_MSH%)
	Dload_Add_Item("media\BCM_cardbase.png",FILE_SPR%)

	Dload_DisplayInfo()
	WaitMouse
	Dload_Do_Load()

	PlaySound Dload_Get_Handle(2)
	PositionEntity Dload_Get_Handle(3),0,0,MeshDepth(Dload_Get_Handle(3))*2 
	SpriteViewMode Dload_Get_Handle(4),4

	While Not KeyHit(1)
		TurnEntity Dload_Get_Handle(3),+1,+2,+3
		UpdateWorld
		RenderWorld
		DrawImage Dload_Get_Handle(1),MouseX()-ImageWidth(Dload_Get_Handle(1))/2,MouseY()-ImageHeight(Dload_Get_Handle(1))/2
		Dload_DisplayInfo()
		Flip
	Wend

	Dload_Remove_AllItems%()

End



darklordz(Posted 2004) [#2]
Dont tell me nobody found this usefull cuz i didn't just waist my time writing it :S


darklordz(Posted 2004) [#3]
Updated to V0.0.2
Added
----------------------
-Loading for 3d Entities like meshes/md2/sprites etc.
-Improved DoLoad
-Added GetInfo
-Updated Filesize Calculation to be more acurate...

works like a charm :P


maximo(Posted 2004) [#4]
Give it up for mah man darklorz. I've been looking for this tool :) it's excelet ;)


Fred(Posted 2004) [#5]
very useful. Not sure why people didnt post back to you. this is useful for almost any game or project..

Thanks


CyberHeater(Posted 2004) [#6]
Looks great. Thanks.


Picklesworth(Posted 2004) [#7]
people didn't post back because they were so excited by it!
Looks very useful, thanks a lot!


BlackTower(Posted 2004) [#8]
I don't know about this. I'm not trying to say this is completely useless, but there are a few downsides.

First, it uses more memory to load files (since it stores information in certain types).

Second, getting their handles like Dload_Get_Handle(2) isn't very well programming practise. In the long run, it will be frustrating.

Why not just use a variable?