Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroPython Example of loading sprites #58

Merged
merged 8 commits into from
Apr 2, 2025

Conversation

fatfingers23
Copy link
Contributor

  • Added a flag to Presto for sprites if set to true, sets the pen type to PEN_RGB332 to allow for RGB332 sprites that work with PicoGraphics
  • Added awesome_game.py, which is a port of the same name from the tutfy2040 example

The game uses a touch screen instead of a gamepad, so it's easier for everyone to try it; also, every time you get a treasure chest, a backlight turns on as gold.

@fatfingers23 fatfingers23 changed the title Micropython Example of loading sprites MicroPython Example of loading sprites Feb 18, 2025
@Gadgetoid
Copy link
Member

Gadgetoid commented Feb 18, 2025

Awesome, thank you, going to run and test this!

I have a feeling that the only reason larger pen types don't support sprites is the RAM constraints. I wonder if that should be fixed now we have Pico 2 and... y'know... ALL THE PSRAM 😆

Edit: it might also be that the sprite functions are implemented in some eldritch horror fashion that doesn't bear thinking about and doesn't lend itself to other pen types. I'm not going to stare into that abyss just yet though.

@fatfingers23
Copy link
Contributor Author

It was a fun one to work on!

And it does look like the RGB565 pen can load in sprites, I may play with that some and see about changing the parameter to just be an overwritable pen type?

I went with RGB332 cause there were sprite sheets for it, and I had no idea what magic was happening in spritesheet-to-rgb332.py 😅. I figured a known working state would be a good start.

@fatfingers23
Copy link
Contributor Author

I did some more digging on RGB565, and at this point, it's over my head, and I'm not sure why the colors are off. But I did do some testing.

I found the pirate themed RGB565 sprite sheets in the picosystem repo and set the device to RGB565 and loaded it up. Everything is a bit pink. Did move from 30fps to 60fps though. Not sure if that's because of different pens or fewer colors. Those pictures are at the bottom.

I also found another magic python file. I got the same results when trying to convert a spritesheet I had that was working with RGB332

IMG_1418
IMG_1413

@Gadgetoid
Copy link
Member

I had no idea what magic was happening in spritesheet-to-rgb332.py

Surprisingly little! I think it's probably rendered horribly cryptic by numpy operations IIRC but the principle is simple enough.

Take an 8bit per pixel image... and just chop 3, 3 and 2 bits off for red, green and blue respectively, pack 'em into a byte and repeat. RGB565 is the same but two bytes. RGB888 ... well you get the idea 😆

@fatfingers23
Copy link
Contributor Author

I ended up asking a friend (Okay, maybe it was AI) and was able to get the script working to convert the sprite sheets over. Working perfectly with the RGB565 pen and even running at 60fps+

  • Added the new script to a root folder called tools mimicking what was done in the picosystem repo. May want to double check the comments in the image_to_data method since it was written by AI.
  • Reverted the sprites param that was added to presto.py since RGB565 sprites work fine
  • I found the pngs for the sprites, so I converted them with the new script so it's no longer a pink hue
  • Added a fps counter to the top right that runs during the game play loop. Not 100% sure if it's accurate, but I think it's close enough

@Gadgetoid
Copy link
Member

Sorry for the radio silence here, been focussed on pushing the rest of Presto over the not-quite-finished-but-good-enough line.

Would you mind giving this one last rebase, please?

@fatfingers23 fatfingers23 force-pushed the feature/SpriteExample branch from e3d5441 to 778a111 Compare April 1, 2025 15:37
@fatfingers23
Copy link
Contributor Author

No problem at all @Gadgetoid !

I just rebased, and it should be good to go, but I will pull the uf2 down once my repo makes the build to double check it still works with the changes from the rebase

@Gadgetoid
Copy link
Member

Awesome, thank you!

If you want to get ahead of the latest QA/linting fixes in dev, you can also run:

pip install ruff
ruff check --select "F,Q,W,E,B,COM,BLE,C4,ISC,ICN,PIE,RSE,RET,SLF,ARG" --ignore "E501,E402,COM812" examples/

If it complains about anything that doesn't make sense to fix - unused args you really don't need to use for example - then use a # noqa <ERRNO> to squash a particular error on the line.

Note: If you don't want to run the gauntlet I'll run a QA pass when I merge and rebase dev onto main anyway!

@fatfingers23
Copy link
Contributor Author

Sweet! I went through and ran that and took the fixes. The only issue I ran into is the manifest.txt does not seem to be picking up the .16bpp files to add to the firmware build, but other than that should be good to go!

@Gadgetoid
Copy link
Member

The only issue I ran into is the manifest.txt does not seem to be picking up the .16bpp files

Oooh, perhaps you have found an obscure bug 😆 I'll look into that.

Thanks for adding the fixes, saves me some hassle later!

@Gadgetoid
Copy link
Member

Okay the *.16bpp problem is because there's a trailing space right after it which gets used as part of the wildcard and doesn't match any files. I should probably fix dir2uf2 to handle that more gracefully. Nice fuzz test 😆 Delete the space and you're gold!

@Gadgetoid
Copy link
Member

Oh also:

# ICON [[(-6.09, 20.0), (-7.14, 19.96), (-8.63, 19.78), (-10.29, 19.37), (-11.02, 19.11), (-12.48, 18.43), (-14.22, 17.27), (-15.52, 16.12), (-16.4, 15.14), (-17.1, 14.22), (-18.16, 12.38), (-18.65, 11.2), (-19.11, 9.61), (-19.36, 7.97), (-19.42, 6.62), (-19.38, 5.51), (-19.18, 3.92), (-18.88, 2.66), (-18.41, 1.33), (-17.67, -0.14), (-16.69, -1.58), (-15.84, -2.55), (-14.4, -3.87), (-13.45, -4.56), (-12.16, -5.31), (-10.14, -6.11), (-9.01, -6.39), (-7.41, -6.62), (-5.64, -6.67), (-4.29, -8.95), (-3.72, -9.57), (-3.13, -9.89), (-1.99, -10.1), (-0.79, -9.76), (0.58, -8.98), (1.09, -9.81), (2.19, -10.97), (3.05, -11.48), (4.52, -11.93), (5.79, -12.0), (6.89, -11.81), (7.63, -11.53), (9.64, -10.4), (7.87, -7.33), (6.31, -8.22), (5.76, -8.42), (4.89, -8.34), (4.09, -7.86), (3.64, -7.2), (5.42, -6.18), (6.24, -5.44), (6.6, -4.67), (6.71, -3.51), (6.42, -2.62), (5.2, -0.44), (5.84, 0.67), (6.61, 2.64), (6.97, 4.01), (7.21, 5.68), (7.24, 6.93), (7.07, 8.92), (6.58, 10.95), (6.05, 12.29), (5.46, 13.42), (4.79, 14.43), (3.52, 15.94), (1.73, 17.51), (0.37, 18.39), (-1.78, 19.33), (-3.76, 19.82), (-6.04, 20.0)], [(-6.09, 16.44), (-4.82, 16.37), (-3.91, 16.22), (-2.71, 15.87), (-1.8, 15.49), (-0.58, 14.77), (0.12, 14.22), (1.31, 13.05), (1.98, 12.2), (2.87, 10.64), (3.33, 9.36), (3.66, 7.41), (3.63, 5.64), (3.46, 4.6), (3.06, 3.23), (2.2, 1.46), (1.02, -0.4), (2.89, -3.6), (-1.73, -6.27), (-3.6, -3.07), (-5.56, -3.07), (-6.96, -2.99), (-8.83, -2.61), (-10.73, -1.84), (-12.3, -0.84), (-13.31, 0.04), (-14.04, 0.85), (-14.84, 2.07), (-15.34, 3.18), (-15.77, 4.78), (-15.9, 6.13), (-15.8, 8.14), (-15.36, 9.93), (-14.76, 11.29), (-14.07, 12.38), (-13.1, 13.52), (-12.35, 14.21), (-10.96, 15.18), (-9.77, 15.76), (-8.63, 16.13), (-7.05, 16.4), (-6.13, 16.44)], [(14.09, -4.89), (14.09, -8.44), (19.42, -8.44), (19.42, -4.89), (14.09, -4.89)], [(4.31, -14.67), (4.31, -20.0), (7.87, -20.0), (7.87, -14.67), (4.31, -14.67)], [(12.98, -11.07), (10.49, -13.56), (14.27, -17.33), (16.76, -14.84), (12.98, -11.07)]]

And

# NAME Awesome Game

@fatfingers23
Copy link
Contributor Author

D'oh. Worked perfectly! Thank you so much and the icon is da bomb.

I think it is ready to go. Thanks for all the help!

@Gadgetoid
Copy link
Member

No worries, it's a fantastic example and dangerously addictive 😆 absolute shoe-in. Thank you for your efforts and responsiveness here!

@Gadgetoid Gadgetoid merged commit 6b3ec85 into pimoroni:main Apr 2, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants