GrabImage problem

Monkey Forums/Monkey Programming/GrabImage problem

Supertino(Posted 2012) [#1]
Not sure why this throws an error? "Image Frame outside surface"

What I am doing is using a custom function LoadAnimImage to save images into an array. That works fine.

I then have a class that needs to take a copy of an image from the array but when it tries to do that via GrabImage I get the error, works fine for array index 0 but any other it throws an error.

Problem is around the GrabImage call in the class, but it I all looks fine to me?!

Import mojo
Import monkey

Global Tiles:Image[]

' -----------------------------------------------------------------------------
' Tiles
' -----------------------------------------------------------------------------
Global Wall:cTile
Global Floor:cTile
Global Slime:cTile

' -----------------------------------------------------------------------------
' Main Loop
' -----------------------------------------------------------------------------
Function Main () ; New Game ; End
Class Game Extends App
	
	Method OnCreate ()
		SetUpdateRate 30
		Tiles = LoadAnimImage( "tiles.png" , 80 , 80 , 10 )
		Wall = New cTile( Tiles[1] )
		Slime = New cTile( Tiles[0] )
		Floor = New cTile( Tiles[4] )
	End
	
	Method OnUpdate ()

	End
	
	Method OnRender()
		Cls()
		Wall.Render( 0 , 0 )
		Floor.Render( 80 , 0 )
		Slime.Render( 160 , 0 )
	End
	
End

' -----------------------------------------------------------------------------
' Classic LoadAnimImage function
' -----------------------------------------------------------------------------
Function LoadAnimImage:Image[]( File:String , Width:Int , Height:Int , Count:Int = 1 )
	Local graphic:Image = LoadImage(File)	
	Local tmp:Image[Count]	
	For Local i = 0 until Count
		tmp[i] = graphic.GrabImage( i * Width , 0 , Width , Height)
	Next	
	Return tmp
End

' -----------------------------------------------------------------------------
' A Class used to create the tiles our game will be made from
' -----------------------------------------------------------------------------
Class cTile
	Field graphic:Image

	' -----------------------------------------------------------------------------
	' Create a new object for a specific tile type, copy the image from the
	' image supplied
	' -----------------------------------------------------------------------------
	Method New ( Source:Image)
		graphic = Source.GrabImage( 0 , 0 , Source.Width , Source.Height , 1 )	
	End
	
	' -----------------------------------------------------------------------------
	' Render the tile\image to screen using the x and y provided
	' -----------------------------------------------------------------------------
	Method Render( X , Y ) ; DrawImage graphic , X , Y ; End
		
End



muddy_shoes(Posted 2012) [#2]
Oops, read wrong...


AdamRedwoods(Posted 2012) [#3]
	For Local i = 0 until Count-1
		tmp[i] = graphic.GrabImage( i * Width , 0 , Width , Height)
	Next



muddy_shoes(Posted 2012) [#4]
Okay, it looks like a mojo bug or something about the intentions of the code that I'm not following.

When you do a GrabImage, mojo creates a new Image object that references the original surface. The offsets into this surface are stored in an array of Frame objects.

The problem seems to be when grab bounds are checked. The mojo code takes the offset of the source image into the surface adding the height and width and then tests against the height and width of that source image. This is wrong, as the offset plus the width/height can obviously be greater than the width/height of a sub-section.

The thing is that the code works fine if you only grab from an image object that isn't itself a previously grabbed image. In your code you're grabbing images and then grabbing from those images again when you create your cTile objects and that's where the problems start.

You could just do:

 
Method New ( Source:Image)
    graphic = Source
End


But if you intend to manipulate the cTile images that could cause issues.

Edit: Not tested much, but this rewrite of the Grab method seems to fix it. Take it with a large pinch of salt though as the intended behaviour with multiple frame images is less than clear to me.




Supertino(Posted 2012) [#5]
Hi Muddy - yes I am taking copies as I need to manipulate the graphics such as changing the handles etc.

So do you think this is a bug or working as intended? I cannot see any benefit for it working the way it apparently does at the moment.

Thanks for looking.

[edit]
Thanks for the fix, I'll try it now.


muddy_shoes(Posted 2012) [#6]
So do you think this is a bug or working as intended?


I couldn't say for sure. It looks wrong to me but my thoughts are often not in line with the Monkey design choices. I don't use this stuff. I just grab from an atlas once and build my own anim objects from individual images.


Supertino(Posted 2012) [#7]
Thanks, Muddy seems to work fine, I'll play some more and maybe but it in the bug section with your fix ofc :D