So I have made a resource manager for my game I think will work out pretty good. My full code relies on random mojo additions I have done, but here is a quick and dirty version of it. It provides an optimal way to load assets between levels, only loading the difference, and not causing a memory spike. It also provides an elegant loading screen solution that has a loading progress bar. It also provides a way to cache recently loaded assets and unload them if you are low on memory.
Now in your game code you would have to define a resource set like this:
import mojo.app
import mojo.graphics
import resources ' the file from above
class MenuResourceSet extends ResourceSet
field buttonImage:ImageRef
field fontImage:ImageRef
method new()
buttonImage = AddImage("button.png")
fontImage = AddImage("font.png")
end method
end class
global MenuResources:MenuResourceSet = new MenuResourceSet
class MyApp extends App
method OnCreate()
LoadSet( MenuResources )
SetUpdateRate(2)
end method
method OnRender()
Cls(0,0,0)
if UpdateLoading() then
DrawText("Loading..." + Int(100*GetLoadPercentage()), 0, 0)
else
if MenuResources.buttonImage.get() <> null then
DrawImage( MenuResources.buttonImage.get(), 0, 0 )
end if
end if
end method
end class
Function Main:Int()
New MyApp()
End Function
You can create lists of objects we want loaded at any given time, i'm just using pre-defined global ones for now. Instead of referencing Image or Sound objects, you reference MenuImages.buttonImage, and you call MenuImages.buttonImage.get() to get the real reference to it. You can have multiple sets that all reference the same resources, and you can dynamically create these resource sets from a level by just calling add* for everything it will not matter.
Now you have a function that you call at the start of your render loop, UpdateLoading. If it returns true then you render your loading screen with the GetLoadPercentage. If it returns false then you update your game as normal. I am doing all of my game update in the render loop with delta times, not the update, because it uses less resources and makes sense for me. You could set a boolean to tell your update loop you are loading and to abort however.
|