Log In  

Cart #gfx_import_example-0 | 2019-03-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

For my game, Witch n Wiz (https://www.lexaloffle.com/bbs/?pid=38117#p), I implemented as simple technique for loading a secondary spritesheet at runtime. This allowed me to have a fullscreen title graphic, but not use up any of the sprite sheet memory (needed for game sprites and map data).

The cart at the top of this post is an ultra simple example of it in use. As you can see, you are able to switch between 2 sprite sheets that would each normally take up the entire sprite memory.

Credit for the num2hex function goes to felice (https://www.lexaloffle.com/bbs/?tid=30910)


You can still only have 128x128 worth of sprites in memory at a time. So this is really best for things like the title screen, or major switches in gameplay (say switching worlds where you don't need any of the original sprites anymore).

The runtime code for this is very light in tokens (~100) but the additional sprite sheet is stored as a large (although compressed) string, so that is going to each into your character count and compressed size (around 8000 chars in a typical case, and 25% of the compressed cart limit).


1) Export

The first thing you need to do, is export your second sprite sheet into a compressed string format the runtime code expects. To do this, you simply load up this cart in Pico-8:

Cart #gfx_export-0 | 2019-03-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Fill the sprite sheet with the content you want to dynamically load at run-time, and hit "run".

This will copy the compressed data to your computers clipboard as a string. Paste that somewhere, like notepad.

2) Copy Runtime Code

Next, open up the import example (or view the code here in the broswer):

Cart #gfx_import_example-0 | 2019-03-31 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

There is a small section of code you will need to add to your cart. Everything between "gfx import runtime begin" and "gfx import runtime end".

This is the code here for reference, if you don't want to load up the cart:

--gfx import runtime begin.
--copy and paste this into your
--game cart.
--example usage after "end".

-- converts hex string
-- to actual number
function hex2num(str)
    return ("0x"..str)+0

--call this when you want to
--load your fullscreen 
--this assumes a compressed
--format of <count>..<color>..
function load_stored_gfx(gfx)
    for i=1,#gfx,2 do
        for j=1,count do

--call this to go back to 
--the original graphics stored
--on the cart.
--eg.after displaying title
function restore_gfx()

--gfx import runtime end.

3) Paste Export Data

Go an get that string that got exported in step 1, and paste it into your game cart as a variable somewhere.

--something like this.
--it will be very large, so I just truncated the string in my example here.
local my_gfx="f51d151d151d151d151"

4) Load/Unload As Needed

There are only 2 functions you need to call for the entire library.

First, when you want to use the stored graphics, call load_stored_gfx() and pass it your compressed graphics string (eg. my_gfx) in this example.

When you are done with it, simply call restore_gfx() to return the sprite sheet to its original form.

P#63133 2019-03-31 04:18 ( Edited 2019-03-31 04:22)

Just excellent and easy!

P#65740 2019-07-10 15:47

@Skulhhead1924 Are you calling restore_gfx()?

P#65993 2019-07-21 04:18

Your awesome! Thank you for this! :)

P#75037 2020-04-20 10:29

not sure what im doing wrong but this isnt working for me. I filled sprite sheet w/ what I want to dynamically load at run-time and run the gfx_import cart but it doesn't copy to my clipboard. any help appreciated.

P#77671 2020-06-04 14:38

@rocco42 you need to do that step in the gfx_export cart, not the gfx_import cart.

P#77673 2020-06-04 14:54

oh ... well that makes total sense now doesn't it. lol thanks

P#77674 2020-06-04 15:23

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-09-25 22:38:44 | 0.041s | Q:23