Sunday, February 06, 2011

Despaire by Iguana ported to Mac OSX

One of my favourite old demos is Despair by Iguana. It features a number of nice effects including realtime raytracing, blended particles, and many more effects that were ahead of its time (It was released in 1996!). The source code for the demo was released and ported to 32bit by David 'mac' from ThreePixels. So I took it upon myself to port it from 32bit windows to 64bit OSX / *nix.
  1. First I translated David's version to VS2010 and ported it to use SDL and the latest BASS, compiling it on windows, to ensure it was all running.
  2. I stripped out all the non-portable assembly code. Unfortunately none of this code was commented, and the few comments that were in there, were all in spanish. (I think)
  3. I installed Intel C++ Composer XE 2011 for Mac OS* X (The Intel C++ compiler on OSX), to keep the code compatible with VS as far as possible, and created a makefile.
  4. I ported the code across to OSX. Nothing too special here, replacing a few windows and DOS function calls with their POSIX equivalents. This part of the port followed the same process as porting any other windows application to another platform. (I'd already done the port to SDL)
  5. I requested (and received! Thanks Ian!) a 64bit copy of BASS for OSX.
  6. Success! A stripped down version of Despair ran on OSX, very few effects actually worked. Now the fun (pain?) could begin.
  7. I looked through all the assembly code and realised quickly that I would never be able to port it all. Instead, I figured I would take an educated guess at the functionality.
  8. I noted some assembly code "RenderBandScan" and took a quick look and spotted a:
    and eax,0F0F0F0F0h
    
    Immediately, I recalled the old trick of using bitwise and and shifts to quickly sum 4 pixels at once, and re-implemented this in a simple C loop.
  9. The next routine was "AADump", a little trickier this time, but again a well known pattern:
     mov cl,[esi+1]
     mov bl,[esi+320]
     add eax,ecx
     mov dl,[esi+321]
     add eax,ebx
     add ecx,edx
     add eax,edx
     mov dl,[esi+2]
     shr eax,2
    
    The pattern of adds and a shift for quickly "blurring" an image, from that basis I just re-implemented a blur-copy routine, and all was well.
  10. I came across the cryptic "DumpScanASM" routine. Luckily, there was a section of c-code commented out that I could just drop back in to replace this. Phew!
  11. The next function was "RenderScan". The name was quite self-explanatory, however I noted a fair bit of odd '127' in there. Knowing the effect was alpha blending some blobs, I deduced the C code:

    for (i=0;i127)
    sum = 127;
    dest[i]=sum;
    }

  12. The second last effect that needed translation from assembly to C was "FB_Draw320x200". This was a bit cryptic, but essentially it was a fixed-point texture sampling routine with a number of clever bit-shifts. I didn't quite get this one converted correctly.
  13. Finally, the greets section is a rotozoomer, texture mapping and blending routine written in pure assembly. This is where I call it a day. I'm not converting this beast! (If anyone else wants to you will need to re-code: DrawGrScan, and VSTM_DumpScan2000.. good luck!)
Here is a zip of the OSX executable for the Despair/Iguana demo along with the ported source code and make files. Youtube video below:

No comments: