Log In  

Cart #39973 | 2017-04-27 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
14

So I was running out of sprites and I thought "there must be a decent way around this"

a few hours later I came up with this. This cart is just showing 3 frames of a 128x128 animation - three times the size of the sprite sheet. You can peek at the code to see the relevant function - string_to_sprite() - but if you want to know the pipeline, here's how I did it.

-I just searched for some anime gif at 128x128 to show this off, picked the first one I thought looked neat, opened it in Aseprite.
-I switched over to the pico-8 palette and remapped the image to it
-Then I went to pico-8 and typed the command "export img.png" - this gives you a 128x128 image of the spritesheet.
-Then I went back into Aseprite, opened img.png and copied the first frame of animation to it, and saved.
-Back in pico-8, typed the command "import img.png" - now we have the first frame of animation.
-In the sprite sheet editor view, I copied a 16x4 selection of sprites by holding shift + clicking and dragging, ctrl+c
-I then went into notepad, and pasted the selection. Now, if you do this you'll see a long string, starting with
[0x0]
. I did the same for the other 3 sequences (is there a way to copy the entire spritesheet?) and in notepad, I got rid of the [gfx] tags and the 4 digits in the front for each pasted section. Then I was left with a string 16,384 letters long. That's frame 1.
-I repeated those steps to get 3 more frames.
-So that is what those huge strings at the top of the code are. I couldn't fit the last frame in since I think the character limit is exactly 4 frames large, so to fit the rest of the code in I sacrificed a frame of animation :)

This is kinda dumb, but if you need to swap out tilemaps for extra levels and stuff it might come in handy!

P#39974 2017-04-27 01:41 ( Edited 2017-04-29 00:16)

nice and easy trick!

  • there's the semi-fullscreen icon at the top left. then you can pan & select more. not the the whole frame height though, the UI gets in the way. (an autoscroll or ctrl-a would be nice, zep!). or maybe there's another way I'm not aware of...

  • you can replace all the if statements by n=("0x"..s)+0

  • get your spritesheet back with reload(0,0,0x2000) (you could keep a fourth frame in there)

I think I could be using this for a title or help screen. thanks a lot! :)

(edit)
btw, the four characters after the tag are the width and height of the data:
width, height = ("0x"..sub(str,1,2))+0, ("0x"..sub(str,3,4))+0
also, if you keep the tags, you can easily copy back and forth between the code and the sprite sheet.

P#39976 2017-04-27 05:49 ( Edited 2017-04-28 08:49)

Nice! Could you fit more stuff in there if you used characters beyond 0..f?

P#39988 2017-04-28 02:02 ( Edited 2017-04-28 06:02)

copy&paste :)

[hidden]
bana={
"[gfz]212366666666666666666666666666666666666666666666666666666666666666666666666666666666666006666666666666666666666666666660990666666666666666666666666666660440666666666666666666666666666660940066666666666666666666666666600aa906666666666666666666666666660aaa900666666666666666666666666660aaaa906666666666666666666666666000a00006666666666666666666666660777077700666666666666666666666660707070700666666666666666600066660777077700666660006666666077006666000a000906666007706666660777066660aaaaa400666607770666666077006666000000800666600770666666600006666088888090666600006666666666600666008880a906660066666666666666600660a000aa906600666666666666666660000aaaaa9900006666666666666666666660aaaaa9006666666666666666666666660aaaaa9066666666666666666666666660aaaa99066666666666666666666666660aaaa90066666666666666666666666660aaa990666666666666666666666666660aa9900666666666666666666666666660009006666666666666666666666666660000006666666666666666666666666660666606666666666666666666666666600666600666666666666666666666666606666660666666666666666666666600006666660006666666666666666660077700666007770066666666666666660777770666077777066666666666666660000006666600000066666666[/gfx]",
"[gfz]2123666666666666666006666666666666666666666666666660940666666666666666666666666666660440666666666666666666666666666600490666666666666666666666666666609aa0066666666666666666666666666009aaa06666666666666666666666666609aaaa0666666666666666000666666660000a00066666666000660770066666600777077706666660077060777066666600707070706666660777060770066666600777077706666660077066000066666609000a000666666600006666660006666004aaaaa066666000666666666660006600800000066600066666666666666600609088888060006666666666666666660009a08880000666666666666666666666609aa000a0666666666666666666666666099aaaaa0666666666666666666666666099aaaaa0666666666666666666666666009aaaaa0666666666666666666666666609aaaaa06666666666666666666666666099aaaa06666666666666666666666666009aaaa066666666666666666666666666099aaa0666666666666666666666666660099aa06666666666666666666666666660090006666666666666666666666666660000066666666666666666666666666660666066666666666666666666666666600666006666666666666666666666666606666606666666666666666666666666606666606666666666666666666666600000666000006666666666666666660077770666077770066666666666666660777700666007777066666666666666660000066666660000066666666[/gfx]",
"[gfz]212366666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666006666666666666666666666666666660000666666666666666666666666666660440666666666666666666666666666600a4066666666666666666666666666600aaa066666666666666666666666666009aaa066666666666666666666666666099aaa06666666666666666666666666009aa0000066666666666666666666666099a07770706666666666666666666666099a07700006666666666666666666666099a07770706666666666666666666666090aa000006666666666666666666666609000aaa0666666666666666666666666090880000666666666666666666666666099088806666666666666666666666666099a008066666666666666666666666660a9aaa000666666666666666666000666099aaaaa06666600066666666607700660099aaaa066660077066666666077706660a9aaaa006660777066666666077006660099aaaa0666007706666666660000060009a9aaa0060000066666666666666000600999aa00006666666666666666666666600a9aaa06666666666666666666666666600099000666666666666666666666666006000000066666666666666666666666066666666066666666666666666666000066666660000666666666666666660777700666007777066666666666666660777770666077777066666666666666666000006666600000666666666[/gfx]",
"[gfz]212366666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666006666666666666666666666666666660000666666666666666666666666666660440666666666666666666666666666600a4066666666666666666666666666600aaa06666666666666666666666666600aaaa0666666666666666666666666660a9aaa0666666666666666666666666600aaa00000666666666666666666666660a9a077707066666666666666666666660a9a07700006666666666666666666666099a07770706666666666666666666666090aa000006666666666666666666666609000aaa06666666666666666666666660a0880000666666666666666666666666099088806666666666666666666666666099a00806666666666666666666666666099aaa0006666666666666666666600060099aaaa06660006666666666666077006099aaaa0060077066666666666607770600a9aaaa06077706666666666660770066099aaaa0600770666666666666600006600a99aa00600066666666666666666006000a99aa06066666666666666666666000600099000066666666666666666666600000000000006666666666666666666000066666666000666666666666666660777000666000777066666666666666660777770666077777066666666666666666000000666000000666666666[/gfx]",
"[gfz]212366666666666666666666666666666666666666666666666666666666666666666666666666666666006666666666666666666666666666660990666666666666666666666666666660440666666666666666666666666666600990666666666666666666666666666609aa0066666666666666666666666666009aaa06666666666666666666666666609aaaa0666666666666666666666666660000a00066666666666666666666666600777077706666666666666666666666600707070706666666666666660066666600777077706666000666666607700666609000a000666600770666666077706666004aaaaa066660777066666607700666600800000066660077066666660000666609088888066660000666666666660066609a08880066600666666666666666006609aa000a0660066666666666666666000099aaaaa0000666666666666666666666009aaaaa0666666666666666666666666609aaaaa06666666666666666666666666099aaaa06666666666666666666666666009aaaa066666666666666666666666666099aaa0666666666666666666666666660099aa066666666666666666666666666600900066666666666666666666666666600000066666666666666666666666666606666066666666666666666666666666006666006666666666666666666666666066666606666666666666666666666600066666600006666666666666666660077700666007770066666666666666660777770666077777066666666666666660000006666600000066666666[/gfx]",
"[gfz]21236666666666666666006666666666666666666666666666660440666666666666666666666666666660440666666666666666666666666666660940066666666666666666666666666600aa906666666666666666666666666660aaa900666666666666666666666666660aaaa906666666666666600066666666000a00006666666600066077006666660777077700666666007706077706666660707070700666666077706077006666660777077700666666007706600006666666000a000906666660000666666000666660aaaaa400666600066666666666000666000000800660006666666666666660006088888090600666666666666666666600008880a900066666666666666666666660a000aa906666666666666666666666660aaaaa9906666666666666666666666660aaaaa9906666666666666666666666660aaaaa9006666666666666666666666660aaaaa9066666666666666666666666660aaaa99066666666666666666666666660aaaa90066666666666666666666666660aaa990666666666666666666666666660aa99006666666666666666666666666600090066666666666666666666666666660000066666666666666666666666666660666066666666666666666666666666600666006666666666666666666666666606666606666666666666666666666666606666606666666666666666666666600000666000006666666666666666660077770666077770066666666666666660777700666007777066666666666666660000066666660000066666666[/gfx]",
"[gfz]2123666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666660066666666666666666666666666666600006666666666666666666666666666604406666666666666666666666666666604a0066666666666666666666666666660aaa006666666666666666666666666660aaa900666666666666666666666666660aaa99066666666666666666666666600000aa90066666666666666666666660707770a99066666666666666666666660000770a99066666666666666666666660707770a9906666666666666666666666600000aa0a06666666666666666666666660aaa0009066666666666666666666666600008809066666666666666666666666660888099066666666666666666666666660800a990666666666666666666666666000aaa9906666666666666666000666660aaaaa9906660006666666660770066660aaaa99006600770666666660777066600aaaa9906660777066666666077006660aaaa99006660077066666666600000600aaa9a9000600000666666666666660000aa999006000666666666666666666660aa99a00666666666666666666666666000990006666666666666666666666660000000600666666666666666666666660666666660666666666666666666666000066666660000666666666666666660777700666007777066666666666666660777770666077777066666666666666666000006666600000666666666[/gfx]",
"[gfz]2123666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666660066666666666666666666666666666600006666666666666666666666666666604406666666666666666666666666666604a0066666666666666666666666666660aaa006666666666666666666666666660aaa900666666666666666666666666660aaa99066666666666666666666666600000aa90066666666666666666666660707770a99066666666666666666666660000770a99066666666666666666666660707770a9906666666666666666666666600000aa0906666666666666666666666660aaa00090666666666666666666666666000088090666666666666666666666666608880a9066666666666666666666666660800a990666666666666666666666666000aaa9906666666666666666660006660aaaa99006000666666666666607700600aaaa9906007706666666666660777060aaaaa9006077706666666666660770060aaaa99066007706666666666666000600aa999006600006666666666666666060aa99900060066666666666666666660000990006000666666666666666666600000000000006666666666666666666000666666660000666666666666666660777000666000777066666666666666660777770666077777066666666666666666000000666000000666666666[/gfx]" 
}
-- note: had to change "gfx" to "gfz"
-- else the graphics showed up on the bbs!!

function str2spr(s,x,y)
    local w,h=("0x"..sub(s,6,7))+0, ("0x"..sub(s,8,9))+0
    local n=10
    for v=0,h-1 do
        for u=0,w-1 do
            sset(x+u,y+v,("0x"..sub(s,n,n))+0)
            n+=1
        end
    end
end

frame=0
function _update()
    str2spr(bana[1+flr(frame)%#bana],0,0)
    frame+=0.25
end

function _draw()
    cls(6)palt(0,false)
    sspr(0,0,0x21,0x23,64-0x21/2,64-0x23/2)
end
[/hidden]

@ianh: sure, but that's an extra uneasy step. the beauty here is the simplicity of a copy/paste.

P#39990 2017-04-28 06:11 ( Edited 2017-04-28 10:16)
6

Hi what's up I needed some sprite compression myself so I made this!


'compress_spsh()' returns the string with the compressed data in it.
You can copy it to the clipboard with 'printh(str,"@clip")'.
Decompress the data to the sprite-sheet with 'decompress_spsh(str)'.
Or decompress it directly to the screen with 'decompress_spsh(str, true)'.

The compression depends a lot on the complexity of the sprite-sheet being compressed. It doesn't like dithering very much.

On this little demo there are 13 different 128x128 frames, but they are not very complex. Still that's better than just 3! ;D

It can definitely be optimized further to get much shorter strings but then the decompressing would also take a lot more time. :/ (if I end up doing it anyway I'll put it here)

Use it however, wherever you want, it's yours! (credit me to make me happy)

enjoy, much love!

P#40008 2017-04-28 20:16 ( Edited 2017-04-29 00:16)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 15:19:13 | 0.018s | Q:24