Loading Custom Images inside OnCreate
Monkey Forums/Monkey Code/Loading Custom Images inside OnCreate
| ||
[edit] forgot to mention it requires v63 to work. This is an example with a work around that allows you to load images from either a txt file or a string declared in your code. This first bit of code Prints the pixel data, I was going to use SaveString but I didn't want it GLFW dependant as it takes too long to compile. Copy the outputted text and use for your own stuff. Change the file to match your own and you can also change the background_r, g, b. This is the colour that is used to mask the background. [monkeycode] Strict Import mojo Class MyApp Extends App '// Background Color to mask Global background_r : Int = 255 Global background_g : Int = 0 Global background_b : Int = 128 Field sprite : Image Field saved : Bool = False Method OnCreate : int() SetUpdateRate( 60 ) '// Loads your image sprite = LoadImage( "sonic.png" ) Return 0 End Method Method OnUpdate : int() Return 0 End Method Method OnRender : int() Cls background_r, background_g, background_b DrawImage sprite, 0, 0 If saved = False saved = True Local w : Int = sprite.Width() Local h : Int = sprite.Height() Local pixels : Int[ w * h ] ReadPixels( pixels, 0, 0, w, h ) '// Prints pixel string PixelArrayPrintData( pixels, w, h ) Endif Return 0 End Method End Class '// Saves pixels to file Function PixelArrayPrintData : Void( pixels : Int[], w : Int, h : int ) Local out : String = w + "," + h + "," Local size : Int = pixels.Length For Local i : Int = 0 Until size out = out + pixels[ i ] If i < size - 1 Then out = out + "," Next Print out End Function Function Main : Int() New MyApp() Return 0 End Function [/monkeycode] This next bit loads the data up (functions are provided to load either from a string in code or from a txt file) and provided is a few functions to replace the colours. This can be used to create infinite versions on the same sprite!! The example below requires no media to run. Thanks to therevills for writing the colour masking code, gave me this idea and saved me writing it myself [monkeycode] Strict Import mojo Class MyApp Extends App '// stores our sprite color information w,h,ARGB... Global sprite : String = "27,39,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14663488,-14663488,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14663488,-14663488,-14663488,-14663488,-12566304,-12566304,-14663488,-14663488,-14663488,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14663488,-14663488,-14663488,-14663488,-14663488,-14671712,-14671712,-12566304,-12566304,-12566304,-14663488,-14663488,16711808,16711808,16711808,16711808,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14671712,-14663488,-14663488,-14671712,-2056064,-14671712,-12566304,-12566304,-12566304,-12566304,-14663488,-14663488,16711808,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-6266816,-2056064,-2056064,-14671712,-12566304,-10460960,-12566304,-12566304,-12566304,-14663488,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14663488,-14671712,-6266816,-2056064,-2056064,-2056064,-14663488,-10460960,-10460960,-10460960,-12566304,-12566304,-14663488,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14671712,-6266816,-6266816,-12566304,-12566304,-10460960,-10460960,-10460960,-10460960,-10460960,-12566304,-12566304,-14663488,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14671712,-14663488,-14663488,-14663488,-12566304,-12566304,-10460960,-12566304,-12566304,-12566304,-12566304,-10460960,-12566304,-12566304,-14663488,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14663488,-14671712,-14671712,-14663488,-12566304,-10460960,-10460960,-12566304,-12566304,-12566304,-2039584,-2039584,-10460960,-12566304,-10460960,-12566304,-12566304,-14663488,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14663488,-14663488,-14671712,-14671712,-14663488,-10460960,-2039584,-10460960,-12566304,-12566304,-10460960,-2039584,-2039584,-2039584,-10460960,-12566304,-12566304,-12566304,-10460960,16711808,16711808,16711808,16711808,-14663488,-14663488,-14671712,-14671712,-14671712,-14663488,-14671712,-14671712,-14663488,-12566304,-10460960,-10460960,-12566304,-12566304,-10460960,-2039584,-2039584,-2039584,-16644604,-14663488,-12566304,-12566304,-16644604,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14663488,-12566304,-12566304,-12566304,-12566304,-10460960,-2039584,-2039584,-2039584,-16644604,-16644604,-14663488,-12566304,-16644604,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14671712,-14663488,-14663488,-14663488,-14663488,-12566304,-12566304,-2039584,-2039584,-2039584,-16644604,-16644604,-10460960,-16644604,-16644604,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14663488,-14663488,-14663488,-14663488,-14663488,-12566304,-2039584,-2039584,-16644604,-16644604,-2039584,-16644604,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14671712,-14671712,-14671712,-14671712,-14663488,-6266816,-2039584,-2039584,-14663488,-10460960,-2039584,-2039584,-2039584,-6266816,-8355712,-16644604,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14663488,-14671712,-14671712,-14671712,-14671712,-6266816,-2056064,-2056064,-2056064,-2236959,-2039584,-2039584,-2039584,-2056064,-2056064,-16644604,-16644604,-16644604,16711808,16711808,16711808,16711808,16711808,-14663488,-14663488,-14663488,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-6266816,-2056064,-2056064,-2056064,-2056064,-2056064,-2056064,-2056064,-2056064,-2056064,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-6266816,-2056064,-2056064,-2056064,-2056064,-2056064,-6266816,-6266816,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14663488,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-14671712,-16644604,-14671712,-14671712,-6266816,-6266816,-6266816,-6266816,-6266816,-8388608,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-14671712,-14671712,-16644604,-16644604,-14671712,-14671712,-14663488,-8388608,-8388608,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-2056064,-2056064,-2056064,-2056064,-14663488,-6266816,-2056064,-2056064,-6266816,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-2056064,-2056064,-6266816,-6266816,-6266816,-14663488,-6266816,-2056064,-2056064,-2056064,-2056064,-6266816,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-2056064,-6266816,-6266816,-14671712,-14671712,-14671712,-14663488,-6266816,-2056064,-2056064,-2056064,-2056064,-6266816,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-2056064,-6266816,-14671712,-14671712,-14671712,-14671712,-14663488,-6266816,-6266816,-2056064,-2056064,-6266816,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-6266816,-2039584,-2039584,-8355712,-2039584,-8355712,-14663488,-14663488,-6266816,-6266816,-6266816,-8388608,-16644604,-12566464,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-8388608,-2039584,-8355712,-2039584,-2039584,-2039584,-14671712,-14663488,-14663488,-14663488,-14663488,-14671712,-12566464,-8355712,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-14671712,-14671712,-8355712,-12566464,-8355712,-8355712,-8355712,-14671712,-14663488,-14671712,-16644604,-14671712,-16644604,-12566464,-8355712,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,16711808,16711808,16711808,-8355712,-8355712,-8355712,-8355712,-12566304,-14663488,16711808,-14671712,-14671712,-16644604,-16644604,-12566464,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-8355712,-8355712,-14671712,-12566304,16711808,16711808,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-12566304,16711808,16711808,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-12566304,16711808,16711808,16711808,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-14671712,-12566304,16711808,16711808,16711808,-14671712,-14671712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-8355712,-6250336,-2039584,-2039584,-2039584,16711808,-12566464,-8355712,-8355712,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-8355712,-8355712,-8355712,-6250336,-6250336,16711808,-12566464,-12566464,-12566464,-8355712,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-8388608,-8355712,-8355712,-8388608,-8388608,-2039584,-8355712,-12566464,-12566464,-8355712,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-8388608,-8388608,-8388608,-6250336,-2039584,-8355712,-8355712,-2097152,-16644604,-16644604,-12566464,-8355712,-8355712,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-8388608,-8388608,-8388608,-8355712,-6250336,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-16644604,-16644604,-16644604,-8388608,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-8388608,-8388608,-8388608,-8355712,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-2097152,-16644604,-16644604,-8388608,-8388608,16711808,16711808,16711808,16711808,16711808,16711808,16711808,16711808,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604,-16644604" '// stores two images we will create from the string Field sprite1 : Image Field sprite2 : Image Method OnCreate : int() SetUpdateRate( 60 ) '// converts string into a pixel array that we can use Local dimensions : Int[ 2 ] Local pixels : Int[] = PixelArrayFromString( sprite, dimensions ) '// creates 1st sprite from the pixel array sprite1 = PixelArrayCreateImage( pixels, dimensions[ 0 ], dimensions[ 1 ] ) '// modify the pixels to create a different coloured sprite '// this is swapping a RGB for a different RGB '// you could store these colours to create different coloured outfits using the same sprite PixelArrayReplaceColour( pixels, 32, 32, 160, 160, 160, 32 ) PixelArrayReplaceColour( pixels, 32, 64, 192, 192, 192, 64 ) PixelArrayReplaceColour( pixels, 64, 64, 224, 224, 224, 64 ) PixelArrayReplaceColour( pixels, 96, 96, 224, 224, 224, 96 ) '// creates 2nd sprite from the pixel array sprite2 = PixelArrayCreateImage( pixels, dimensions[ 0 ], dimensions[ 1 ] ) Return 0 End Method Method OnUpdate : int() Return 0 End Method Method OnRender : int() Cls 0, 0, 0 '// scale them up a bit so they are more visible PushMatrix() Scale 2, 2 '// draws the created sprites SetColor 255, 255, 255 DrawImage sprite1, 0, 0 DrawImage sprite2, sprite1.Width(), 0 PopMatrix() Return 0 End Method End Class Function Main : Int() New MyApp() Return 0 End Function '// Creates an image from the pixel array Function PixelArrayCreateImage : Image( pixels : Int[], w : Int, h : Int ) Local this : Image = CreateImage( w, h ) this.WritePixels( pixels, 0, 0, w, h ) Return this End Function '// Converts a comma separate string to pixel array Function PixelArrayFromString : Int[]( s : String, size : Int[] ) Local this : String[] = s.Split(",") Local pixels : Int[ this.Length ] size[ 0 ] = Int( this[ 0 ]) size[ 1 ] = Int( this[ 1 ]) For Local i : Int = 2 Until this.Length pixels[ i - 2 ] = Int( this[ i ]) Next Return pixels End Function '// Loads a comma separate string to pixel array Function PixelArrayLoadString : Int[]( file : String, size : Int[] ) Local this : String[] = LoadString( file ).Split(",") Local pixels : Int[ this.Length ] size[ 0 ] = Int( this[ 0 ]) size[ 1 ] = Int( this[ 1 ]) For Local i : Int = 2 Until this.Length pixels[ i - 2 ] = Int( this[ i ]) Next Return pixels End Function '// Replaces a Pixel Colour RGB Function PixelArrayReplaceColour : void( pixels : Int[], old_r : Int, old_g : Int, old_b : Int, new_r : Int, new_g : Int, new_b : Int ) For Local i : Int = 0 Until pixels.Length Local argb : Int = pixels[ i ] Local a : Int = ( argb Shr 24 ) & $ff Local r : Int = ( argb Shr 16 ) & $ff Local g : Int = ( argb Shr 8 ) & $ff Local b : Int = argb & $ff If r = old_r And g = old_g And b = old_b r = new_r g = new_g b = new_b argb = ( a Shl 24 ) | ( r Shl 16 ) | ( g Shl 8 ) | b pixels[ i ] = argb Endif Next End Function '// Replaces a Pixel Colour ARGB Function PixelArrayReplaceColour : void( pixels : Int[], old_a : Int, old_r : Int, old_g : Int, old_b : Int, new_a : Int, new_r : Int, new_g : Int, new_b : Int ) For Local i : Int = 0 Until pixels.Length Local argb : Int = pixels[ i ] Local a : Int = ( argb Shr 24 ) & $ff Local r : Int = ( argb Shr 16 ) & $ff Local g : Int = ( argb Shr 8 ) & $ff Local b : Int = argb & $ff If a = old_a And r = old_r And g = old_g And b = old_b a = new_a r = new_r g = new_g b = new_b argb = ( a Shl 24 ) | ( r Shl 16 ) | ( g Shl 8 ) | b pixels[ i ] = argb Endif Next End Function [/monkeycode] Thought this was kind of cool so sharing :) |
| ||
not working on html5! |
| ||
It works fine here on HTML5 using Monkey v63b... but I did find out you got an error if you run it in debug mode: http://www.monkeycoder.co.nz/Community/posts.php?topic=3492 |
| ||
ok, that was the reason. I used Debug mode. |