Media Management

Blitz3D Forums/Blitz3D Programming/Media Management

The following will go on the code archives once we have a few testers. Feedback is appreciated. Basically it allows you to store media in memory for faster use. I can see this being used in case where a resource is used repeatedly.

How to:
include "media manager.bb"

Media_Mesh_Get("myfile.b3d") ; loads from disk
Media_Mesh_Get("myfile.b3d") ; loads from memory
Media_Mesh_Get("myfile.b3d") ; loads from memory


You can have a function that will load a level each time called, within you can call all the media manager functions.

Level 1
Media_Reset()
test1 = Media_Mesh_Get("myfile.b3d") ; loads from disk
test2 = Media_Mesh_Get("myfile.b3d") ; loads from memory
test 3 = Media_Mesh_Get("myotherfile.b3d") ; loads from disk
Media_FreeUnused()

Free Level1
frees test1, test2, test3

Level2
Media_Reset()
Media_Mesh_Get("myfile.b3d") ; loads from memory
Media_FreeUnused() ;will free file ""myotherfile.b3d", but keeps "myfile.b3d"

If you have few resources then you don't need to use Media_Reset and Media_FreeUnused() that way you can almost instantly load levels.

Use Media_Clear() if your levels are huge, that way it clears memory usage by freeing everything that is in the media manager.

I suggest you time your load time using this functions compared to using manual loadmesh and loadanimmesh commands.
ml = millisecs()
load level
debuglog "Load time: " + str(millisecs() - ml)

media manager.bb
;Media Manager by Jesse Andersen (jesse_andersengt@...)
;About: With the media manager you load a resource once from disk, and if the resource is needed again
;it will load it from memory, which should be faster than loading from disk each time.
;Credit is appreciated


Type mMesh
	Field h, file$
	Field ustate
End Type

Type mAMesh
	Field h, file$
	Field ustate
End Type

Type mTexture
	Field h, file$
	Field ustate
End Type

Type mSound
	Field h, file$
	Field ustate
End Type

Type m3DSound
	Field h, file$
	Field ustate
End Type


Function Media_Reset()
	For mM.mMesh = Each mMesh
		mM\ustate = 0
	Next
	
	For mAM.mAMesh = Each mAMesh
		mAM\ustate = 0
	Next
	
	For mT.mTexture = Each mTexture
		mT\ustate = 0
	Next
	
	For mS.mSound = Each mSound
		mS\ustate = 0
	Next
	
	For m3DS.m3DSound = Each m3DSound
		m3DS\ustate = 0
	Next
End Function


Function Media_FreeUnused()
	For mM.mMesh = Each mMesh
		If mM\ustate = 0 Then
			FreeEntity mM\h : Delete mM
		EndIf
	Next
	
	For mAM.mAMesh = Each mAMesh
		If mAM\ustate = 0 Then
			FreeEntity mAM\h : Delete mAM
		EndIf
	Next
	
	For mT.mTexture = Each mTexture
		If mT\ustate = 0 Then
			FreeTexture mT\h : Delete mT
		EndIf
	Next
	
	For mS.mSound = Each mSound
		If mS\ustate = 0 Then
			FreeSound mS\h : Delete mS
		EndIf
	Next
	
	For m3DS.m3DSound = Each m3DSound
		If m3DS\ustate = 0 Then
			FreeSound m3DS\h : Delete m3DS
		EndIf
	Next
End Function



Function Media_Clear()
	For mM.mMesh = Each mMesh
		FreeEntity mM\h : Delete mM
	Next
	
	For mAM.mAMesh = Each mAMesh
		FreeEntity mAM\h : Delete mAM
	Next
	
	For mT.mTexture = Each mTexture
		FreeTexture mT\h : Delete mT
	Next
	
	For mS.mSound = Each mSound
		FreeSound mS\h : Delete mS
	Next
	
	For m3DS.m3DSound = Each m3DSound
		FreeSound m3DS\h : Delete m3DS
	Next
End Function


Function Media_Mesh_Get(file$)
	For mM.mMesh = Each mMesh
		If mM\file = file And mM\h <> 0 Then mM\ustate = 1 : Return CopyEntity(mM\h)
	Next
	
	h = LoadMesh(file$)
	If h <> 0 Then
		mM.mMesh = New mMesh
		mM\h = h : mM\file = file : HideEntity(mM\h) : mM\ustate = 1
		Return CopyEntity(mM\h)
	EndIf
End Function


Function Media_AMesh_Get(file$)
	For mAM.mAMesh = Each mAMesh
		If mAM\file = file And mAM\h <> 0 Then mAM\ustate = 1 :  Return CopyEntity(mAM\h)
	Next
	
	h = LoadAnimMesh(file$)
	If h <> 0 Then
		mAM.mAMesh = New mAMesh
		mAM\h = h : mAM\file = file : HideEntity(mAM\h) : mAM\ustate = 1
		Return CopyEntity(mAM\h)
	EndIf
End Function


Function Media_Texture_Get(file$)
	For mT.mTexture = Each mTexture
		If mT\file = file And mT\h <> 0 Then mT\ustate = 1 : Return mT\h
	Next
	
	h = LoadTexture(file$)
	If h <> 0 Then
		mT.mTexture = New mTexture
		mT\h = h : mT\file = file  : mT\ustate = 1
		Return mT\h
	EndIf
End Function


Function Media_Sound_Get(file$)
	For mS.mSound = Each mSound
		If mS\file = file And mS\h <> 0 Then mS\ustate = 1 : Return mS\h
	Next
	
	h = LoadSound(file$)
	If h <> 0 Then
		mS.mSound = New mSound
		mS\h = h : mS\file = file  : mS\ustate = 1
		Return mS\h
	EndIf
End Function


Function Media_3DSound_Get(file$)
	For m3DS.m3DSound = Each m3DSound
		If m3DS\file = file And m3DS\h <> 0 Then m3DS\ustate = 1 : Return m3DS\h
	Next
	
	h = Load3DSound(file$)
	If h <> 0 Then
		m3DS.m3DSound = New m3DSound
		m3DS\h = h : m3DS\file = file  : m3DS\ustate = 1
		Return m3DS\h
	EndIf
End Function