My approach to this is a generic image handler type 'TImg' that keeps track of all loaded image files and offers a simple interface to the user to fetch the images without having to worry if the file has already been loaded or not.
With this you can simply call TImg.LoadImg("filename") wherever you wish in the program, and it actually loads the image only if it hasn't been loaded yet. Otherwise it just returns the preloaded TImage.
The idea is not to use it in the main loop, but call it in the creation of the entity represented by the image, and store the returned TImage within the created entity instance.
With this you can simply call TImg.LoadImg("filename") wherever you wish in the program, and it actually loads the image only if it hasn't been loaded yet. Otherwise it just returns the preloaded TImage.
The idea is not to use it in the main loop, but call it in the creation of the entity represented by the image, and store the returned TImage within the created entity instance.
Type TImg Global g_mediaPath:String = "media/" Global g_L_imageFiles:TList Field _image:TImage ' container field for the image itself Field _filename:String ' filename of the media file in the media directory Method GetFileName:String() Return _fileName End Method Method GetImage:TImage() Return _image End Method ' LoadImg returns a TImage matching a filename string. Function LoadImg:TImage(filename:String, automid:Int = True) AutoImageFlags MASKEDIMAGE | FILTEREDIMAGE | MIPMAPPEDIMAGE ' flags For LoadImage() If Not g_L_imageFiles Then g_L_imageFiles = CreateList() ' if the file has already been loaded, return it instead of reloading it For Local img:TImg = EachIn g_L_imageFiles If img.GetFileName() = filename Then Return img.GetImage() Next AutoMidHandle automid Local image:TImage = LoadImage(g_mediaPath + filename) If Not image Then Return Null Local img:TImg = TImg.Create(image, filename) Return img._image End Function ' finds a previously loaded image and removes it from the image list Function UnLoadImg(filename:String) If Not g_L_imageFiles Then Return For Local img:TImg = EachIn g_L_imageFiles If img.GetFileName() = filename Then img._image = Null g_L_imageFiles.Remove(img) EndIf Next EndFunction Function Create:TImg(image:TImage, filename:String) If Not g_L_imageFiles Then g_L_imageFiles = CreateList() Local img:TImg = New TImg img._filename = filename img._image = image g_L_imageFiles.AddLast(img) Return img End Function End Type