Another “short but sweet” update for the day, due to some after Christmas errands and get togethers with family.
I fixed the bug from yesterday, which turned out to be an incredibly simple rendering bug, of all things. I never tested the blitting API for images when I ported it to TypeScript (shame on me!) and the method for rendering a bitmap centered at a location was broken. The method cheaply translates the canvas to the location to render to make everything simpler but it was not properly restoring the canvas context afterwards.
Once that was squashed I modified the preloader a bit so that when you attempt to load the same image multiple times (as determined by the name you are trying to preload) you just get the same image tag back that was returned on the prior attempt. Although I’m sure (and hope) that the browser would be intelligent enough to only keep one copy of the image in memory even if multiple references to it are made, we do save a bit on memory footprint by not having redundant tag objects.
I also implemented audio preloading as well, although here we are using the “canplaythrough” event, which means that we don’t REALLY know that the audio is all the way loaded, just that the browser thinks that it will be able to play the whole thing if you try. This system uses a bit of jiggery behind the scenes so that you provide the audio name without the extension and it appends the extension for the audio format that the browser says that it supports.
I used the same “reuse tag” mechanism for the audio as I did for the images, but I don’t think that that’s necessarily a good idea since audio tags need to be explicitly told to restart themselves if you try to play them when they’re already playing. I’m not sure if the best thing to do is have multiple tags with the same source to allow for “mixing” or if just an Audio class of some sort would be better and take care of that detail for you.
A little more thought needs to go into that one.