Spritesheet To P8SCII

Published 2022-11-03

(v01 09-29-22)

TO LOAD THIS PICO-8 CART, in immediate mode, type: load #spr2p8sci

Hello there.

If you are running this code in Pico-8, you will see that the Spritesheet does not contain this picture ! Instead there is only a single line of code, and a PRINT statement at that to create the display you see above. How is this possible ? Well, we need to go back a day to find this out.

I was taking a look at @GPI's marvelous example of multi-poke using P8SCII code:


And I thought to myself, it would be interesting to write code that could save off an entire 128x128 16-color picture as a single print command. Could it be done ? And yes, it could. The picture above for instance is just 8948-text characters directly in source-code as a single PRINT statement.

It looks a little like this:

?"\^!6000ちちたた▥▥▥ホIお🅾️🅾️🅾️🅾️🅾️🅾️ ...

To do so in your own work and code is quite simple, load up the code I wrote here by bringing up Pico-8 EXE and then loading my cart with: load #spr2p8sci

Change to the directory you have a 128x128 picture you want to convert. From there in immediate mode type, import pic.png which contains the 128x128 picture you want to convert to a single line of P8SCII code.

Call my function, spr2p8sci() which is 230-characters long so it's fast and doesn't take much code space.

Once that is done the current picture in the spritesheet will then be saved to clipboard, ready to paste in your code. And be aware that you can post this single line all by itself with nothing else written in your code and it will still display properly.

So in a newly opened Pico-8 process you want to have this picture appear in, in your source-code, press CTRL+P first to enter puny font mode, then press CTRL+V to paste it. Then CTRL+P to leave puny font mode, and there you have it.

Run that code to see that you do not need any functions or code at all except for that one-line to house your picture.

And there you have it !

Now I also want to mention that this is getting compressed of a sort. That is I am NOT just taking every byte of the spritesheet and converting it over with 4-characters in the format, \000 . Nope, because that would take 32768-characters not even counting the print and quotes and that might not even fit on a single line. I don't know what the limitations are to a single line.

Nor is it doing it via \0\00 and \000 for 3-digits. That method takes exactly 30687-characters for the current picture.

No, instead I am converting every single byte to a readable character if it is permitted, going from 30687-characters down to even smaller. Now considering the actual graphic memory is 8192-bytes and even converting raw 8192-bytes to 6-bit yields 10923-characters, this method of P8SCII is quite the compression at only 8948-characters !

Which is only 9% bigger than raw memory. And doubtless that could be compressed further from there.

So, yes, that picture above is only 8948-typed characters with no other code needed.

Also if you want to change the memory your picture is stored instead of the screen, simply change the print statement to: ?"\^! followed by the 4-digits of hexadecimal memory, currently 6000 you want to put the picture. If if it is 0000 that is the spritesheet, yet you can also store your pictures in high memory such as 8000A000C000 and E000.

Easy to use. Single function, and creates a single line of code close to actual memory size for your picture to display !

Hope This Helps !