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.
I am currently making comparably good progress with the game engine and the editor. One notable thing that I added was the mechanism to allow fore- and background graphics. It is based on sprites and is making use of the “Cookie Cutter” effect: Sprites can have a lower priority than the normal graphic on the screen. At the same time, a sprite can have a higher priority than the player sprite. The player sprite again has a higher priority than the normal character graphic, as it is supposed to be displayed over the background graphics. This obviously creates a strange situation, where the player is configured to be displayed over the character graphic, the other sprite is displayed over the player sprite, and the character graphic is displayed over the other sprite. The video chip of the C64 solves this dilemma by displaying the character graphic over the player wherever the other sprite is non-transparent. A little bit like a cookie cutter will cut out parts of the dough. Sounds complicated? It is, but it can be used to create effects like the one below, where parts of the character graphic is displayed in the foreground, and other parts are in the background. It needs some dynamic handling of the sprites, because the bridge in the video is clearly too wide to be covered by the available 6 sprites. So the sprites are only enabled when there is an overlap between them and the player. Apologies for the music, I always have some arbitrary SID blaring so that I can check if the available raster time is ok.