Zal0 / ZGB

Game Boy / Color engine with lots of features
MIT License
708 stars 50 forks source link

InitScroll() does not reset last_bg_pal_loaded (palettes get appended) #44

Closed mhughson closed 1 year ago

mhughson commented 2 years ago

If multiple maps are loaded with InitScroll(), it is possible to run out of palettes (and I assume have the wrong palettes assigned to the wrong tile).

I believe in Scroll.c, InitScrollWithTiles should reset last_bg_pal_loaded back to 0, prior to loading the tile and map data.

https://discord.com/channels/790342889318252555/790346049377927168/928058473692954674

mhughson commented 2 years ago

Actually, it looks like last_bg_pal_loaded could just be completely removed.

Zal0 commented 2 years ago

Yep, there was a few problems with this, and also with how attributes were being handled. From this commit to this one I have been changing how tiles are loaded for maps.

Tiles are loaded in ZGB using ScrollSetTiles, which is being called by InitScroll (for scrolled backgrounds bigger than 32x32 and containnig enemy spawners) and LoadMap (for regular backgrounds, like splash screens or huds)

Whenever new tiles are loaded using ScrollSetTiles an offset is returned (containing both the first index and the first palette where the new tiles have their info). Users doesn't really need to know what this offset contains but they need to keep it and pass it back to UpdateMapTile if they want to update any of the tiles (this is very common on the hud when the player gets hit and you need to update the life bar)

In ZGB maps are used for backgrounds, huds and fonts. For performance reasons I have taken the decission of ignoring offsets for the backgrounds. This means, the background should always be loaded first in any State (I can't find any use case where this could be a problem but if there is something I am missing I'll be happy to discuss it). Then the user can load some extra maps for either the Font or the HUD

InitScroll does 4 things:

You can of course tweak the whole engine and make InitScroll work within the same state (by resetting all the vars, doing fades and all the whole stuff that is done in main.c when swapping states). But that's up to you. It's not how ZGB is mean to be used. One thing that is posible to do is to load a new map using the same tiles using ScrollSetMap

I have also added a few useful macros

I hope you find this whole thing useful. It's been a lot of extra work ;)

mhughson commented 2 years ago

LoadMap (for regular backgrounds, like splash screens or huds)

Where is LoadMap? I searched this repo and the GBDK docs, and I can't find anything.

Zal0 commented 2 years ago

It's one of the new additions. You need to check the develop branch https://github.com/Zal0/ZGB/blob/6fd59298a98a6f0f7877c96dfb5acc40e2ebc09a/common/include/Scroll.h#L57

Zal0 commented 1 year ago

I think I can close this one