Monday, 31 August 2009

Thank you curl, Thurl.

Today was a bit of engine work. (Not a bank holiday as some other people here in the UK seem think, too wet!)
For Embiggen we will need to fetch some data from the web. Namely, from a database which stores all our matches (and user profiles). A simple PHP script can convert the required match database row into some XML and pump it out - specifically into our asset XML format - and we can download it, convert it to binary asset data and load it in-game. Shimples!
Anyway, the UIKit in the iPhone SDK provides us with some functionality to do this for us (NSURLConnection) so we need a solution in windows to replicate this for easier testing. (and maybe for us on the Wii or Mac or whatever else we might port to)

I looked through a few open source projects, libs etc before settling on LibCurl which seems to do everything we require in a nice simple, supported, threadsafe, documented, active open source package. After some initial linking trouble with unresolved externals all starting with __imp__ I had a look through the example .vcproj for some pre-processor defines which sure enough I missed out on. Got it all going, setup a simple asynchronous base connection class (TLNetwork::TConnection), setup a new TestApp example which shows one texture asset on the left and another on the right. I uploaded a compiled .asset file to the web (Which is freely availible to download if you're curious, although it's binary data rather than XML representation, I'll show an example of that when it's ready)
Everything seemed to be setup nicely but when the internal thread was cleaned up I kept getting some corruption in the heap. I started looking into CRT issues and conflicts and found a full rebuild of an all debug build of the app didn't corrupt the heap. I setup all our projects to use Debug MT CRT but was still getting an occasional crash. I overloaded all the LibCurl memory allocation functions to use our own memory allocation functions which I can track much more easily. The remaining crash seemed more consistent (Instead of random crashes), looking further into it an asset kept being overwritten and corrupted... I stepped through the code and it turned out I was corrupting one of my assets myself, turning a c-pointer into a smart pointer and then releasing it, Oops.
Now I'm a little unsure if the change in the memory allocations, or the CRT changes were neccessary. Nevermind, it works!
The image is the file download test app. The image on the left is local, the image on the right is downloaded, converted and then set. Not the most exciting screenshot :)

I've put together an IPod/IPhone version, but untested. Now that this is stable I can see if it works on the iPhone and get back to game stuff :)

0 comments:

Post a Comment