More foreground/background fun

I was so fascinated by the cookie cutter technique, that I decided to take it a bit further and implement a complete dynamic foreground/background mechanic. The theory is not really complicated, but in this case the actual implementation was challenging. The only way to influence if a sprite is displayed before or behind another sprite on the C64 is to sort them accordingly. Sprite 0 is always displayed on top of all sprites, then comes sprite 1 and so on until sprite 7. So for every movement of the player, all sprites that need to be displayed are determined (remember that for the cookie cutter mechanism only the sprites that overlap with the player are required to be active) and then dependent of the position of the player they need to be sorted either before or after the player sprites (I am using 2 player sprites, one as black overlay over the other).  Sounds not too difficult, but I needed 3 attempts to get to an implementation that I found acceptable. The first was simply wrong, because I forgot that I must not change the order of sprites as they are listed in my room descriptions, or overlay sprites will suddenly disappear behind the sprites they are supposed to overlay. The second one was so twisted and unmaintainable, that my honor as a programmer was seriously impacted, so I had to do it a third time. The final implementation is still not super nice, I think the problem is simply that there are not enough registers on the C64 to do this really in a nice way. I split up the decision of the order of sprites and the actual handling of the related information (like screen positions, color, etc.) in two loops. This resulted probably in a bit slower code, but due to my task mechanism I described in an earlier post this is not an issue and can be done in the background during the rendering of 4 frames. Here is what you can do with the new code in another small video.

Leave a Reply

Your email address will not be published. Required fields are marked *