About Monkey 2 › Forums › Monkey 2 Programming Help › LoadAnimImage missing?
Tagged: LoadAnimImage
This topic contains 11 replies, has 5 voices, and was last updated by
Amon
2 years, 1 month ago.
-
AuthorPosts
-
February 9, 2017 at 12:07 pm #7056
Coming from mojo1 I cannot seem to find the LoadAnimImage command to load in sprite sheets.
Is this a oversight or is there a new way of doing it in MonkeyX2?
February 9, 2017 at 2:49 pm #7060It’s not programed in to mojo. you will have to implement it yourself.
February 9, 2017 at 3:35 pm #7061Hmm is there any way to scale images or grab sections of them as I can’t see that either, or do I have to write the OpenGL code for that?
February 9, 2017 at 3:48 pm #7062to scale:
[/crayon]Monkey123[crayon-5cb9dfd26f288804833313 inline="true" ]canvas.Scale = new Vec2f(x,y)or
[/crayon]Monkey123[crayon-5cb9dfd26f28e561960392 inline="true" ]image.Scale = New Vec2f(x,y)to grab a section of an image:
[/crayon]Monkey123[crayon-5cb9dfd26f293434826617 inline="true" ]local img:Image = new Image(image,x,y,width,height)February 9, 2017 at 4:00 pm #7063Ah brilliant thanks for that I will use that in my conversions
February 10, 2017 at 2:00 am #7069Also u can write “sprite” class that contains reference to the source image and drawing rect. With that + some addon code you can change source image on the fly and so that change all sprites.
February 10, 2017 at 6:55 am #7071@nerobot that sounds interesting
Will look into it more and see what I can do with MonkeyX2
February 10, 2017 at 7:56 am #7072Here ya go:
Monkey123456789101112131415161718192021222324Function LoadSpriteSheet:Image[] ( path:String, numFrames:Int, cellWidth:Int, cellHeight:Int, filter:Bool = True, preScale:Float = 1.0, padding:Int = 0, border:Int = 0 )Local atlasTexture := Texture.Load( path, Null )Assert( atlasTexture, " ~n ~nGameGraphics: Image " + path + " not found.~n ~n" )Local imgs := New Image[ numFrames ]Local atlasImg := New Image( atlasTexture )If Not filter Then atlasImg.TextureFilter = TextureFilter.NearestLocal paddedWidth:= cellWidth + ( padding * 2 )Local paddedHeight:= cellHeight + ( padding * 2 )Local columns:Int = ( atlasImg.Width - border - border ) / paddedWidthFor Local i:= 0 Until numFramesLocal col := i Mod columnsLocal x := ( col * paddedWidth ) + padding + borderLocal y := ( ( i / columns ) * paddedHeight ) + padding + borderimgs[i] = New Image( atlasImg, New Recti( x , y, x + cellWidth, y + cellHeight ) )imgs[i].Scale = New Vec2f( preScale, preScale )NextatlasImg = NullReturn imgsEndThe “padding” and “border” arguments are designed to work with the way Aseprite exports sprite sheets. I haven’t tested this out of my framework, but it looks like it should work. Let me know.
I think I based this on some code someone posted on the Monkey-X forum. It’s been too long, can’t remember now…
February 10, 2017 at 8:01 am #7073@ethernaut thank you very much will test it once I get the latest build and recompile
February 10, 2017 at 8:18 am #7077Tested, works here. Here’s a sample usage with a 5 frame 32×32 sprite (160×32 sprite sheet, no border or padding):
(requires previously posted LoadSpriteSheet function)[/crayon]Monkey1234567891011121314151617181920212223242526272829303132[crayon-5cb9dfd28152a276286991 inline="true" ]#Import "<mojo>"#Import "<std>"#Import "images/testSprite.png"Using mojo..Using std..Class SpriteSheet Extends WindowField sprite:Image[]Method New()Super.New( "Test", 256, 256 )sprite = LoadSpriteSheet( "asset::testSprite.png", 5, 32, 32 )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.Clear( Color.Black )For Local n := 0 To 4canvas.DrawImage( sprite[n], n * 32, n * 32 )NextEndEndFunction Main()New AppInstanceNew SpriteSheet()App.Run()EndFebruary 10, 2017 at 9:08 am #7080Amazing stuff haven’t even downloaded the latest build yet lol
February 19, 2017 at 8:07 pm #7212 -
AuthorPosts
You must be logged in to reply to this topic.