mem/speed use Height() or save/use height once.
Monkey Forums/Monkey Beginners/mem/speed use Height() or save/use height once.
| ||
| From the doc. Method Height : Int () Returns the height of the image, in pixels. But does this method calculate the img.Height() every time you use it ? Or is it just giving a 'static' value back, what is only calculate once (the image is loaded) ? I ask this because i'm probably going to use many image's with calculations. So I can choose to program like this: Class Sprite Field img:Image End OnUpdate() _mx = _mx - img.Height() / 2 and many other multiple img.Height() & Width() stuff Or store the Height() and Width() once and use that
Class Sprite
Field img:Image
Field height:Float = img.Height() ' using a init method
Field halfHeight:Float = img.Height() / 2' using a init method
End
OnUpdate()
_mx = _mx - img.halfHeight
and many other multiple img.height & img.width stuff
The halfHeight will be faster a little faster, but the height:Float is the question. |
| ||
| It returns only the field height from Class Image. So no extra calculation. |
| ||
| You can check the MonkeyX code to make sure, but on my quick look when an Image is loaded the image data, width, height and depth are stored and not recalculated. Within Diddy we do something like this to save on typing and an extra method call:
Local gi:GameImage = New GameImage
gi.Load("player.png")
Local s:Sprite = New Sprite(gi, 100, 100)
s.x += s.image.h4
Class Sprite
Field image:GameImage
Field x:Float, y:Float
Method New(img:GameImage, x:Float, y:Float)
Self.image = img
Self.x = x
Self.y = y
End
End
Class GameImage
Field image:Image
Field w:Int
Field h:Int
Field w2:Float
Field h2:Float
Field w4:Float
Field h4:Float
Method Load:Void(file:String, flags:Int, midhandle:Bool = True)
Self.midhandle = midhandle
image = LoadBitmap(file, flags)
CalcSize()
MidHandle(midhandle)
End
Method CalcSize:Void()
If image <> Null Then
w = image.Width()
h = image.Height()
w2 = w / 2
h2 = h / 2
w4 = w / 4
h4 = h / 4
End
End
End
Function LoadBitmap:Image(path$, flags%=0)
Local pointer:Image = LoadImage(path, 1, flags)
If pointer = Null Then Error ("Error loading bitmap "+path)
Return pointer
End
|