global images?

BlitzMax Forums/BlitzMax Programming/global images?

unazebra(Posted 2008) [#1]
hi. just a quick question.
is it faster(cpu wise) to load all images as globals, and then just have any images inside my types reference them? i have a game where there are lots of instances of one type, and i was wondering if its faster to do that than have each instance load the images as fields.
thanks!
una.x


CS_TBL(Posted 2008) [#2]
Dunno, but even if global images are faster, is it worth having it all global? Globals are a major ingredient for messy oldskool code.

I'd say, make a benchmark, and you'll know!


tonyg(Posted 2008) [#3]
... globals within the type might be worthwhile but each object instance you create can share the same TImage instance without having to loadimage it each time.
Obviously, if you make changes to the image/pixmap then it will be reflected in all objects referencing that image.


CS_TBL(Posted 2008) [#4]
And when you insist on globals, at least don't waste your namespace and do something like this:

Type TImagepool
	Field image1:timage=LoadImage("C:\Bmaxdev\oO\1.PNG")
End Type

Global imagepool:Timagepool=New TImagepool

Print ImageWidth(imagepool.image1)


Then it'll all only cost you one variabele in your namespaec. A variable you can easily pass as function argument even.


Czar Flavius(Posted 2008) [#5]
Type Something
   Global image:timage=loadimage(........)
   
   Field x, y
   Method draw()
      drawimage Something.image, x, y
   End Method
End Type


Then the image is stored and loaded only once, in Something.image


Vilu(Posted 2008) [#6]
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.




Czar Flavius(Posted 2008) [#7]
The problem with copying your code like that is it's not as easily understood by others as you may think.


Vilu(Posted 2008) [#8]
The code (which is commented, I might add) is ready to be cut & pasted without anyone having to understand anything but the two interfaces: LoadImg and UnloadImg functions. It really shouldn't be that complicated.


tonyg(Posted 2008) [#9]
I thought the code was very well documented both with comments and good use of function, method and variable names.
The only thing I really do differently is use a tmap rather than a list but then I bundle all media resources into the same functions.


nino(Posted 2008) [#10]
I use something like this but with wrappers around the images like Vilu. You should never call LoadImage more than you need to is I think the short answer.

Type TimageLibrary Extends Tmap
Method get:Timage(path:String)
	Local o:Object = MapValueForKey(Self, path)
	If TImage(o) Return TImage(o)
EndMethod
Method open(path:String, frames:Int = 1) 
	Local i:Timage
	If frames > 1
		Local temp:Timage = LoadImage(path) 
		Local w:Int = ImageWidth(temp) 
		Local h:Int = ImageHeight(temp) 
		i = LoadAnimImage(path, w / frames, h, 0, frames)
	Else
		i = LoadImage(path)
	EndIf
	MapInsert(Self, path, i)
EndMethod
Method list:TMapEnumerator() 
	Return MapKeys:TMapEnumerator( Self )
EndMethod
EndType



Czar Flavius(Posted 2008) [#11]
The code (which is commented, I might add) is ready to be cut & pasted without anyone having to understand anything but the two interfaces: LoadImg and UnloadImg functions

Not really. The only way I could figure that out is to look into the program code and work it out. You should start the code with brief description of its use, that doesn't require the code to be read.


Vilu(Posted 2008) [#12]
You should start the code with brief description of its use, that doesn't require the code to be read.


Sounds to me like you didn't read the post above the code snippet. ;)


MGE(Posted 2008) [#13]
Since the original question was in reference to speed, I'd say none of this will result in faster rendering.