Archive for June, 2008

More file preloading

Posted in programming with tags on June 8, 2008 by bbarrett

This is a bit of a follow up from my previous post.

In the end I decided to do preloading for Lua scripts too. This is because of the nature of my game – there are a number of times a Lua state (or interpreter instance) can be created. There is one for every “game”, plus a different one for configuration. When the executable loads, there is a dummy “game” playing behind the menu that creates a nice background animation. When the user selects “new game” – a new “game” instance is created.

Because most of the scripts are referenced dynamically from Lua via the “require” standard library function I wrote a function that replaces the vanilla require – it will look in the cache first and if it fails it falls back to the standard library function (which is saved in the Lua registry – a neat little hiding place for Lua values that the C API can access but the Lua scripts can’t. I haven’t tested it fully, but it appears to work well so far. Which obviously means there is something hideously wrong with it…

Advertisements

Freedom

Posted in nonsense, programming with tags on June 5, 2008 by bbarrett

Long time no post. Well, I am finished my degree now, so that – theoretically – should leave time for some projects.

Content for once

So, I made a little progress during the week. I was playing my Asteroids game from a USB pen and it really showed me how my dynamic loading of files was slowing me down. Due to the way I wrote the game, nothing is preloaded (because all graphics and audio files are referenced from external scripts). They are loaded on demand. They were cached once loaded however. On my laptop hard drive, the delay is slight, but still perceptible. I must have gotten immune to it somehow – but since the USB pen thing I started to notice it more and it irritated me.

I wrote a little function that can be called from Lua (a running theme) that takes a directory and a function. It applies the function to every file in the directory and its subdirectories. The function is called with two arguments, the path to the file and a file size. The code for doing this is quite simple – I used the opendir() and readdir() POSIX functions, along with stat() to determine whether to recurse into a directory or to get the byte size of a regular file.

From Lua then I just loop across the data directory, and preload files that look like images or sound files. I haven’t yet implemented preloading lua files, and I’m not sure if I will (given that the majority of the are referenced by the script running the preloader). I’m thinking of breaking the script in two – one that is run at program launch and another that is run at “new game”. At the moment the same script handles a bit of both. I might consider using file watches (or listeners, or whatever they’re called) so I can be notified when a Lua script changes and reload the data, but that is far less likely.

Next up, some kind of graphical progress bar, rather than just using the window title text…