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

Sprite system #5

Open
tansiret opened this issue Dec 13, 2024 · 15 comments
Open

Sprite system #5

tansiret opened this issue Dec 13, 2024 · 15 comments

Comments

@tansiret
Copy link

Hi! First of all thanks for the library, its very helpful. I am trying to make a Super Mario demo from scratch using your library. I would ask for 2 features:

  • In BMP drawing, setting a color as transparent so other tiles written before it could be visible rather than a rectangle.
    -Some solution to decrease refresh blinking (maybe already doable with the current state of the library, I am a bit newbie to this so help is appreciated)
    Both you can see in the photos
    IMG_20241213_203357.jpg

IMG_20241213_203339.jpg

Have a good rest of your day!

@gavinlyonsrepo
Copy link
Owner

I will look into the sprites.

Regarding the "decrease refresh blinking " you can increase the speed of SPI maybe that will help
in the example file Setup() function

uint32_t TFT_SCLK_FREQ = 8000 ; // Spi freq in KiloHertz , 1000 = 1Mhz

Try increasing this 8000 to 12000 or 16000 or higher

@gavinlyonsrepo
Copy link
Owner

gavinlyonsrepo commented Dec 14, 2024

Hi

I have added a new function 'TFTdrawSpriteData' which draws sprites with a transparent background
you pass in the 16-bit 565 color number background of the sprite as a argument. It then ignores this color.
The sprites must be 16 bit color.
It is demonstrated in test400 of the ST7735_TFT_BMP_DATA example.
It seems to work. This is experimental not sure how robust this is going to be in a real program.

1

regards

@tansiret
Copy link
Author

tansiret commented Dec 14, 2024

It works on plain color backgrounds but on a bitmap one like this one (I haven't made a tile system yet so I'm just using a fullscreen bitmap image) messes it up
VID_20241215_002210.gif

VID_20241215_000858.gif

@tansiret
Copy link
Author

tansiret commented Dec 15, 2024

I have just implemented a simple tilesystem and found what happens, and figured out a way to "fix it" (sorta, for now, as there are few layers for the moment). When sprite background is deleted, its replaces with the last drawn thing inside. I fixed it by filling the screen just before mario and putting the delay before the fill so everything seem visible at the same time to the human eye.

Before:
photo_2024-12-15_01-51-42
After:
VID_20241215_015013

Code before:
myTFT.TFTfillScreen(BACKGR);
for (int k = 0; k < 160; k+=16) {
xdk = xd2 + k;
myTFT.TFTdrawBitmap16Data(xdk, yd2, (uint8_t *)pGrass, 16, 8);
}
myTFT.TFTdrawSpriteData(xd1, y, (uint8_t *)Mario, 16, 24, BACKGR);
TFT_MILLISEC_DELAY(65);

Code after:
for (int k = 0; k < 160; k+=16) {
xdk = xd2 + k;
myTFT.TFTdrawBitmap16Data(xdk, yd2, (uint8_t *)pGrass, 16, 8);
}
TFT_MILLISEC_DELAY(65);
myTFT.TFTfillScreen(BACKGR);
myTFT.TFTdrawSpriteData(xd1, y, (uint8_t *)Mario, 16, 24, BACKGR);

PS: I will update you if I encounter problems as I implement more tiles and entities.

@tansiret
Copy link
Author

I just added more entities and its not fixable anymore. If I keep doing this way then the entities appear ahead of Mario. If I put them before Mario then they appear in background. If I add a screen fill between them then it becomes blinky.

@gavinlyonsrepo
Copy link
Owner

Hi

I have changed the 'TFTdrawSpriteData' function. with new commit.

The first version used 'Malloc' to create a buffer and then drew to this buffer the data and then sent that buffer in one SPI transaction to device. I believe the use of malloc was causing memory fragmentation and optimisations with pico reusing the same block of data.

The second version uses 'TFTdrawPixel' function to directly write the sprite into the VRAM of ST7735. pixel by pixel.
The disadvantage of this method is it is slower but the sprites are so small the speed might be acceptable.

See if it works and see if it is fast enough.

regards

@tansiret
Copy link
Author

tansiret commented Dec 15, 2024

It works, thanks!
VID_20241215_232052.gif

Refresh rate is still an issue but I can live withbit (as visible on gif). I maxxed it to 62500 and its still like that. Maybe for such a project I shall buy and use ST7789 instead, even if its not that big of a problem.

@gavinlyonsrepo
Copy link
Owner

Hi

I have a library for the St7789 as well https://github.com/gavinlyonsrepo/ST7789_TFT_PICO I will add the sprite function to it.

RE the refresh rate
If you implement a frame rate per second counter you will be able get an idea if its speed of library.
See example files Test 701 in ST7735_TFT_BMP_DATA or Test702 in ST7735_TFT_FUNCTIONS_FPS.
I have recorded a frame rate of 22 FPS for drawing full screen bitmaps(128x128) at 8Mhz, so I think it might be something else in your code.

How are you drawing the background, TFTfillRectangle or TFTfillRect. or TFTFillscreen?
TFTfillRectangle uses Malloc and a SPI Buffer Write and is faster.
TFTFillRect draws pixels direct.
TFTFillscreen wraps TFTfillRectangle for full screen rectangle.

@tansiret
Copy link
Author

tansiret commented Dec 18, 2024

Hi

I have a library for the St7789 as well https://github.com/gavinlyonsrepo/ST7789_TFT_PICO I will add the sprite function to it.

RE the refresh rate
If you implement a frame rate per second counter you will be able get an idea if its speed of library.
See example files Test 701 in ST7735_TFT_BMP_DATA or Test702 in ST7735_TFT_FUNCTIONS_FPS.
I have recorded a frame rate of 22 FPS for drawing full screen bitmaps(128x128) at 8Mhz, so I think it might be something else in your code.

How are you drawing the background, TFTfillRectangle or TFTfillRect. or TFTFillscreen?
TFTfillRectangle uses Malloc and a SPI Buffer Write and is faster.
TFTFillRect draws pixels direct.
TFTFillscreen wraps TFTfillRectangle for full screen rectangle.

This gave me an idea. I will fill the screen with rectangles only in sections where there are no tiles tile by tile.

Btw I realized when I draw tiles with bitmap function, when they are sliding from right to left, they come distorted. But then I changed them to sprites and they come clean but makes fps worse. I know I askee a lot from you already, but can you implement a tile function too that uses the same drawing technique but without background filling as in sprites? Here are some screenshots of before and after to elaborate what I mean (focus on the far right of the screen):

Tiles drawn with bitmap function:
IMG_20241218_073516.jpg

With sprite function:
IMG_20241218_073626.jpg

@gavinlyonsrepo
Copy link
Owner

gavinlyonsrepo commented Dec 18, 2024

Hi

Working on the assumption that MALLOC is the problem and the resulting heap memory fragmentation.
I have removed it from the 'TFTdrawBitmap16Data' function and replaced in with a row by row write system( not pixel by pixel)
I got same FPS results for this.
So their is no buffer memory allocation heap or static in this function now, and according to my FPS test it is working at same speed.

Dec 2024 V1.72

  1. 701 Bitmap + FPS Test :: HW SPI 8MHZ :: 22 fps
  2. 701 Bitmap + FPS Test :: HW SPI 32MHZ :: 71 fps
  3. 701 Bitmap + FPS Test :: SW SPI :: 9 fps

See how it works now on your demo.

Regards

@tansiret
Copy link
Author

Hi, I returned to my home country and left it behind as I was scared that they don't allow such stuff on airport. I will do it after 9 January and let you know.

@tansiret
Copy link
Author

Yay, it works! Thank you! I will upload the code on a github repo and let you know when the game becomes playable.

@tansiret tansiret reopened this Jan 15, 2025
@tansiret
Copy link
Author

tansiret commented Jan 15, 2025

Hi @gavinlyonsrepo, I just realized a bug in the new bitmap drawing function. Its not visible on grass but very much so on luck/rotation blocks when approached from leftIMG_20250115_182432.jpg

IMG_20250115_182443.jpg

@gavinlyonsrepo
Copy link
Owner

Hi

I am busy at moment , might be able to look at this in 1-2 weeks .

@gavinlyonsrepo
Copy link
Owner

Hi

Have you made any progress since last week?
I am not seeing any problems with function( TFTdrawBitmap16Data) with my tests.
If you exclude error handling the function is only four lines long.

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

No branches or pull requests

2 participants