My latest game, Beneath the Depths, is a bit technically complicated. It uses some rather unusual methods for extending the spritsheet space in a normal pico-8 cart without using reload(), and it takes advantage of the nature of memory in pico-8 to acomplish it. So, here is a deep dive into all the things I found out about while making this game:
Introduction to key concepts
First of all, if you are not familiar with the way memory works in pico-8, or you need a refresher, i recomend chacking out LazyDevs'( @Krystman) video on memory or reading the pico-8 wiki for information on the memory layout.
Aditionally, most of the things I'll talk about here I created with information from this post: Upper memory: 4 more spritesheets!. It uses a different method to reach a similar goal to me, but it is a bit more math-heavy, but do check it out!
Now, lets dive into it.
Extended spritesheet space with memcpy() and load()
Since Pico-8 0.2.4, memory now includes a section going from 0x8000 to 0xffff for general use. However, it can't be directly modified with reload(), which can only target regions from 0x0 to 0x4300. So, how can we use all that real state for our extended spritesheet?
Well, a feature of pico-8 is that, when loading a new cart, any general use memory region IS NOT WIPED, meaning it persists between carts. So, if we were to run a cart, say, a title screen, with a full spritsheet, we could copy the region of memory of the spritsheet (0x0-0x0fff) to a "persistent" section (like 0x8000), we now have access to all of those sprites if we were to load() a new cart that can transition between spritsheets during runtime. Aditionally, we can save the sprite flags (0x3000-0x30ff) into a separate persistent memory region (0x4300).
--in the title screen cart:
memcpy(0x8000,0x0,0x2000)
memcpy(0x4300,0x3000,0x100)
if btnp() then
load("gamename")
end
--in the game
mem={{0x8000,0x4300},{0x9000,0x4400}}
function load_spritsheet(index)
memcpy(0x1000,mem[index][1],0x1000)
memcpy(0x3080,mem[index][2],0xff)
end
|
In the actual game, I formated it in a way similar to the example above, where I can use a table to copy certain regions of the stored spritesheet into the lower half of the active spritsheet. This way, I can cycle between tilesets in certain map areas whilst having spritesheet space for animations and such.
However, there are some caveats. First, this only really works well if you are only copying a full spritsheet from cart A to cart B. If you need more than the 255 sprite spaces, well, you can certainly store the data in other memory regions, like the map, but that requires a lot of work on figuring out how to do so. Secondly, if you don't want to load() from one cart to another, or you'd rather use reload() (which has some caveats of its own), this method isn'r the best. However, there is something we can do about both of those things.
Encoding and decoding sprites
Lets say you need one and a half full spritesheets to store your tilemap. Well, you can store one full spritsheet with the method described earlier, and we can encode the remaining sprites from a separate cart into the cart, or, if you want to skip the load() step alltogether, you can encode as many spritesheets as you need and then decode them into the memory region you will be storing them in the actual game.
--in the cart you are going to draw your spritesheet function export() memcpy(0x1000,0x1800,0x800) local str="" for i=0,0x800 do local num=peek(i+0x1800,1) if tonum(num)<10 then num="0"..num end if tonum(num)<100 then num="0"..num end if i!=0x800 then num..="," end str..=num end printh(str,"@clip") end --in the cart (load or not) tileset=split"" --copy from clipboard the exported data for i=0,#tileset do poke(0xa800+i-1,tileset[i]) end |
The code takes a region of memory and it encodes it into a single string of numbers separated by comas. Later, it decodes the string and it esentially transfers the memory between carts without the need to load() or reload().
You can repeat this process for the sprite flags (if you need them) by adjusting the memory locations and lenghts used. The example above is just the process I had to use for my game, but if you want to see more clearly how it works, I recomend checking out the game on SPLORE and looking at the code yourself, since it might be more insightful.
Encoding and decoding images
After programming that, I realized the method I was using of load()-ing to transfer the persistent memory didn't allow me to create a title screen that could use any part of the spritesheet, since it was all ocupied. However, I realised the method I used to encode & decode bytes of memory into stings might hold the key to extending pico-8 limits. If I could use this method to encode, say, whatever drawing I show on a screen of another cart, I can then decode the string into any cart and use said image as if it were a sprite.
hex={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}
-- this table is used to change the sotred values from
-- decimal to hexadecimal, which makes the process
-- of decoding simpler and makes encoding take less space
function encode_img(sx,sy,wd,ht)
local str=""
for x=sx,wd do
for y=sy,ht do
local s=hex[pget(x,y)+1]
str..=s
if y==sy+ht then
if x!=sx+wd then
str..=","
end
end
end
end
printh(str,"@clip")
end
function decode_img(str,sx,sy)
local tbl=split(str,",",false)
for x=1,#tbl do
for y=1,#tbl[x] do
if tbl[x][y]!=0 then
--you can remove this conditional if you don't want to maintain "transparency"
pset(sx+x-1,sy+y-1,"0x"..tbl[x][y])
end
end
end
end |
The result after some more code shenanigans:
This function allowed me to create the cover/title screen art for the cart in a separate cart and paste it into the main cart while leaving the spritesheet alone to be used for the load() spritesheet transfer.
Final thoughts
One WARNING before I go. When I uploaded my game to the LXO forums, something really weird happened. Somehow, it seemed as though the spritesheet from the title screen cart had persisted into the game cart, which is concerning, since only certain memory regions persist, and the main sprite sheet memory (0x0-0xfff) is NOT supposed to be one of them. I only figured out what was happening after a bunch of testing, since I initially thougbt that somehow exporting the carts to the BBS had messed up some code or the memory regions that needed to be targeted.
I found two ways to fix it. One is to Reset Cart from the pause menu, which somehow fixes the issue, but it isn't practical to say: "hey everyone, if you want to play the game you need to reset it manually every time you boot it up, but only after the main menu screen fades". The second method (the one I ended up using) is to reload() the affected memory region AFTER leaving the main menu screen (on _init of the game cart) but BEFORE memcpy()-ing the data from the persistent region to the active spritesheet.
I have no clue why or how this happened but I couldn't find any info of this issue happening to anyone before me, and since it works perfectly fine when exported as and html file and as a local program on my version of pico-8, I didn't catch the bug until I had already uploaded the game on the BBS.
So, thats it, really. I hope this helps people who are in desperate need of extra spritesheet space. I'm not sure i'll use this in future projects, since part of the fun of pico-8 is to work with the limitations, not against them, but it might come in handy for projects that want to push the limits of pico-8. (Which my game definitely didn't try to do, but it lead me down this rabbit hole that you can now use without as much testing and troubleshooting).
[Please log in to post a comment]




