Here I present a method for stroking text and sprites.
In looking through solutions for stroked/filled text, I usually see print()on the string called 9 times; i.e. once at each cardinal location with a 1px offset. I realized if pixels were represented by "fat pixels" outlines of various thicknesses could be simulated.
For example, if a pixel is at (x,y),
rectfill(x-thickness, y-thickness, x+thickness, y+thickness,color) |
with a normal pixel draw on top would look like an outlined pixel. I further realized that anything with transparent pixels could be outlined in this same manner, and so applied it to sprites.
This method works in the following:
- Video space is "borrowed" for less than a frame (?) to render the text and capture it's pixel data to user data space. (the specific address space used for this caching can be set to wherever you like; sprites are read as-is from sprite memory space)
- The bytes are walked and used to draw fat pixels (via rectfill()).
- The string is then rendered "as normal" at the location requested. (print()is only called twice: once to grab the pixel data cache, once to draw the string on top of the effects)
- Seems to be at 300 tokens currently.
This method has the following benefits:
- Visual effects are defined as a table (list) of inner tables, where each inner table defines:
{fill_color, stroke_color, line_thickness, effect_x_offset, effect_y_offset} |
- Effects in a list are applied in the "painter method" from [1]..[#effect] where each subsequent effect is drawn on top of the previous. This allows for complex stackings of routines.
- Effects are just tables, and so can be pre-defined and re-used on other elements, including sprites.
- Effects are animatable, by simply swapping colors and offset values.
- Uses no sprites, only Pico-8 native text (though it could be pretty easily adapted to work with mini-font libraries that use custom sprites, I think)
Optimization possibilities
Unlike the "draw a string 9 times" methods, this methodology presents an opportunity for optimization. I'm still very new to Pico-8 and Lua, so this really only represents a starting point toward a better algorithm.
For example, I'm sure there are ways to make it more efficient.
- I believe the call to peek() could be switched to a peek4()?
- I believe the call to rectfill() could be swapped for direct writes to the bytes?
- I'm certain there are ways to reduce the token count from the current 300, but for the purpose of this post I tried to consolidate redundant code without sacrificing (too much) legibility.
(P.S. - I don't see how a name can be attached to a cart? @clip didn't use the file name? I see no tools here for setting title, license, etc.)
![](/gfx/set_like0.png)
![](/gfx/top_drop.png)
![](https://www.lexaloffle.com/bbs/files/15232/me-square.jpg)
Pretty neat ! I wrote a fairly comprehensive library for TEXT years ago, unfortunately, it's spaghetti coded. Could stand to be rewritten with modern methods.
![](/gfx/set_like0.png)
![](/gfx/top_drop.png)
![](https://www.lexaloffle.com/bbs/files/29213/tobias_pico8.png)
Names are attached to carts very easily. Put this comment on the top of your code:
--cartridge title --by your name |
[Please log in to post a comment]