]> Shamusworld >> Repos - thunder/blobdiff - src/screen.cpp
Added DIP switch fungibility, misc. code cleanups.
[thunder] / src / screen.cpp
old mode 100755 (executable)
new mode 100644 (file)
index bfd0f89..d3f5672
 //
 // Screen Handler
 //
-// This sets screen to VESA 320x240 LFB so we can use the WHOLE laptop screen
-// Now with VESA2 support!
-// Also, support routines for video hardware emulation are included
-//
-// by James L. Hammons
+// This emulates the NAMCO tile/sprite hardware
 //
+// by James Hammons
 // (C) 2003 Underground Software
 //
-// JLH = James L. Hammons <jlhamm@acm.org>
+// JLH = James Hammons <jlhamm@acm.org>
 //
 // Who  When        What
-// ---  ----------  ------------------------------------------------------------
-// JLH  03/12/2003  Ported this crud to Simple Directmedia Layer
+// ---  ----------  -----------------------------------------------------------
+// JLH  03/12/2003  Ported this steaming pile of crap from VESA to SDL
+// JLH  04/04/2014  Ported to SDL 2. Much less crappy.
 //
 
+#include "screen.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string>                     // For memset()
-#include "SDL.h"
 #include "gui.h"
-#include "screen.h"
+#include "video.h"
 
 // Private function prototypes
 
-void DrawSprites(BYTE priority);
+void DrawSprites(uint8_t priority);
 int FindPCXName(void);
+static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t baseAddr, bool transparent = true);
+static void DrawChar(uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll, uint32_t yScroll, bool transparent = true);
+void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl);
 
 // Private global variables
 
-BYTE my_scr[0x14000];                                                                  // Screen buffer...
-BYTE palette[768];                                                                             // Screen palette
-BYTE ccolor[256][8];                                                                   // Character colors
-BYTE scolor[128][16];                                                                  // Sprite colors
-bool charbase;                                                                                 // Character base pointer...
-BYTE hScrollOffset;                                                                            // Horizontal scroll offset
-BYTE vScrollOffset;                                                                            // Vertical scroll offset
-BYTE spr_color_index;                                                                  // Sprite color index
-DWORD offsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };                 // Scroll offsets...
-DWORD voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
+uint8_t my_scr[0x14000];               // Screen buffer...
+uint32_t palette[256];                 // Screen palette
+uint8_t ccolor[256][8];                        // Character colors
+uint8_t scolor[128][16];               // Sprite colors
+bool charBankSwitch;                   // Character bank switch
+uint8_t spr_color_index;               // Sprite color index
+
+extern bool show_text;                 // Whether or not to show text
 
-extern bool show_text;                                                                 // Whether or not to show text
-extern bool show_scr;                                                                  // Whether or not to show screen
 
 //
 // Render the NAMCO screen
 //
-void BlitChar(SDL_Surface * scr, BYTE * chr, BYTE * ram)
+void BlitChar(uint8_t * chr, uint8_t * ram)
 {
-       if (show_scr)
-       {
-               int sx, sy;
-               DWORD sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
-               hScrollOffset = sc_base & 0x07;                                 // Horiz. scroll offset
-               sc_base = (sc_base & 0xFFF8) >> 2;                              // Skip odds..
-               BYTE vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
-               vScrollOffset = ((ram[0x9002] + 1) & 0x07);             // Vertical fine scroll amount
-               DWORD scp1 = 0x0180 | ((sc_base + 0x04) & 0x7F);        /*0x0188;*/
-               DWORD scp2 = 0x1180 | ((sc_base + 0x04) & 0x7F);        /*0x1188;*/
-               DWORD scp3 = 0x2180 | ((sc_base + 0x04) & 0x7F);        /*0x2188;*/
-               DWORD scp = 0x3208;
-               scp1 += vsc_base * 0x80;
-               scp1 &= 0x0FFF;                                                                 // Set vertical scroll addr
-               scp2 += vsc_base * 0x80;
-               scp2 = 0x1000 | (scp2 & 0x0FFF);                                // Set vertical scroll addr
-               scp3 += vsc_base * 0x80;
-               scp3 = 0x2000 | (scp3 & 0x0FFF);                                // Set vertical scroll addr
-
-               DWORD chBaseOffset = (charbase ? 0x20000 : 0x00000);
-
-               for(sy=0; sy<29; sy++)
-               {
-                       for(sx=0; sx<37; sx++)
-                       {
-                               BYTE scp_lo = (scp1 + (sx << 1)) & 0x7F;// Let LO byte wrap only...
-                               WORD sp2 = (scp1 & 0xFF80) | scp_lo;
-                               BYTE tile  = ram[sp2++];
-                               BYTE index = ram[sp2] & 0x03;
-                               BYTE color = ram[sp2];
-                               DWORD chind = chBaseOffset + (((index << 8) + tile) * 64);
-                               DWORD sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
-
-                               for(int y=0; y<8; y++)
-                               {
-                                       for(int x=0; x<8; x++)
-                                               my_scr[sc_addr++] = ccolor[color][chr[chind++]];
-
-                                       sc_addr += 312;                                         // Do next line...
-                               }
-                       }
-
-                       scp1 += 0x80;
-                       scp1 = 0x0000 | (scp1 & 0x0FFF);
-               }
+       // Screen structure:
+       //
+       // Each screen RAM is 4K in size, from lowest to highest priority:
+       // $0000-$0FFF, $1000-$1FFF, $2000-$2FFF, $3000-$3FFF
+       //
+       // Screen is 288 x 224 pixels, with character and independent sprites.
+       // Tiles are 36 x 28. There are four tile planes, all of them affected by
+       // their own h/vscroll values. Screens are 128 bytes wide by 32 bytes high.
+       //
+       // Also note that tiles are 16-bits wide, meaning the screen is really
+       // only 64 characters wide!
+
+       // Base address is $9000-1 (top 13 bits) + $9002 >> 3 << 7 (top 5 bits)
+/*
+$9000-2: $02 $0C $FF  0000 0010  0000 1100  1111 1111
+$9004-6: $06 $0E $FF  0000 0110  0000 1110  1111 1111
+$9400-2: $0A $0B $FF  0000 1010  0000 1011  1111 1111
+$9404-6: $0E $0D $07  0000 1110  0000 1101  0000 0111
+                      ---- PP?H  HHHH Hhhh  VVVV Vvvv
+? = refresh layer bit?
+PP = which 4K block to write to?
+*/
+       uint16_t screenX0 = (((ram[0x9000] << 8) | ram[0x9001]) + 20) & 0x1FF;
+       uint16_t screenX1 = (((ram[0x9004] << 8) | ram[0x9005]) + 18) & 0x1FF;
+       uint16_t screenX2 = (((ram[0x9400] << 8) | ram[0x9401]) + 21) & 0x1FF;
+       uint16_t screenX3 = (((ram[0x9404] << 8) | ram[0x9405]) + 19) & 0x1FF;
+
+       uint16_t screenY0 = ram[0x9002] + 25;
+       uint16_t screenY1 = ram[0x9006] + 25;
+       uint16_t screenY2 = ram[0x9402] + 25;
+       uint16_t screenY3 = ram[0x9406] + 25;
+
+       uint16_t ramBlock0 = (ram[0x9000] >> 2) & 0x03;
+       uint16_t ramBlock1 = (ram[0x9004] >> 2) & 0x03;
+       uint16_t ramBlock2 = (ram[0x9400] >> 2) & 0x03;
+       uint16_t ramBlock3 = (ram[0x9404] >> 2) & 0x03;
+
+       DrawScreen(ramBlock0, screenX0, screenY0, (charBankSwitch ? 2 : 0), false);
+       DrawSprites(0x40);
+       DrawScreen(ramBlock1, screenX1, screenY1, (charBankSwitch ? 3 : 1));
+       DrawSprites(0x80);
+       DrawScreen(ramBlock2, screenX2, screenY2, 4);
+       DrawSprites(0xC0);
+       DrawScreen(ramBlock3, screenX3, screenY3, 5);
+
+       // Draw a msg if needed...
+       if (show_text)
+               DrawText();
 
-               DrawSprites(0x40);                                                              // Draw sprites at lowest layer...
+       // Show GUI if active...
+       if (ShowGUI())
+               DrawGUI();
 
-               chBaseOffset = (charbase ? 0x30000 : 0x10000);
+       // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not
+       // be... (and we don't have to care about that, the OpenGL backend takes
+       // care of it.)
+       for(uint32_t i=0; i<VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT; i++)
+               scrBuffer[i] = palette[my_scr[i]];
 
-               for(sy=0; sy<29; sy++)
-               {
-                       for(sx=0; sx<37; sx++)
-                       {
-                               BYTE scp_lo = (scp2 + (sx << 1)) & 0x7F;  // Let LO byte wrap only...
-                               WORD sp2 = (scp2 & 0xFF80) | scp_lo;
-                               BYTE tile   = ram[sp2++];
-                               BYTE index  = ram[sp2] & 0x03;
-                               BYTE color  = ram[sp2];
-                               DWORD chind = chBaseOffset + (((index << 8) + tile) * 64);
-                               DWORD sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
-
-                               for(int y=0; y<8; y++)
-                               {
-                                       for(int x=0; x<8; x++)
-                                       {
-                                               if (chr[chind] != 7)
-                                                       my_scr[sc_addr] = ccolor[color][chr[chind]];
-
-                                               sc_addr++;
-                                               chind++;
-                                       }
-
-                                       sc_addr += 312;                                         // Do next line...
-                               }
-                       }
+       RenderScreenBuffer();
+}
 
-                       scp2 += 0x80;
-                       scp2 = 0x1000 | (scp2 & 0x0FFF);
-               }
 
-               DrawSprites(0x80);                                                              // Draw sprites under layer #2...
+//
+// Render tilemap
+//
+static inline void DrawScreen(uint16_t ramBlock, uint16_t xAddress, uint16_t yAddress, uint32_t tileBase, bool transparent/*= true*/)
+{
+       uint16_t ramBase = (ramBlock << 12) | ((yAddress << 4) & 0xF80)
+               | ((xAddress >> 2) & 0x7E);
 
-               for(sy=0; sy<29; sy++)
+       for(uint8_t sy=0; sy<29; sy++)
+       {
+               for(uint8_t sx=0; sx<37; sx++)
                {
-                       for(sx=0; sx<37; sx++)
-                       {
-                               BYTE scp_lo = (scp3 + (sx << 1)) & 0x7F;  // Let LO byte wrap only...
-                               WORD sp2 = (scp3 & 0xFF80) | scp_lo;
-                               BYTE tile  = ram[sp2++];
-                               BYTE index = ram[sp2] & 0x03;
-                               BYTE color = ram[sp2];
-                               DWORD chind = 0x40000 + (((index << 8) + tile) * 64);
-                               DWORD sc_addr = (sx * 8) + (sy * 2560); // Start addr in my_scr[]
-
-                               for(int y=0; y<8; y++)
-                               {
-                                       for(int x=0; x<8; x++)
-                                       {
-                                               if (chr[chind] != 7)
-                                                       my_scr[sc_addr] = ccolor[color][chr[chind]];
-
-                                               sc_addr++;
-                                               chind++;
-                                       }
-
-                                       sc_addr += 312;  // Do next line...
-                               }
-                       }
 
-                       scp3 += 0x80;  scp3 = 0x2000|(scp3&0x0FFF);
+                       DrawChar(sx, sy, ramBase, tileBase << 16, xAddress & 0x07, yAddress & 0x07, transparent);
                }
+       }
+}
 
-               DrawSprites(0xC0);                                                              // Draw highest priority sprites...
 
-               for(sy=0; sy<28; sy++)
+//
+// Draw character on screen
+//
+static inline void DrawChar(uint8_t sx, uint8_t sy, uint16_t ramBase, uint32_t tileBase, uint32_t xScroll, uint32_t yScroll, bool transparent/*= true*/)
+{
+       extern uint8_t charROM[];
+       extern uint8_t gram1[];
+
+       // Calculate address in RAM of tile to draw
+       uint16_t addr = (ramBase & 0xF000) | ((ramBase + (sy << 7)) & 0x0F80)
+               | ((ramBase + (sx << 1)) & 0x7F);
+       uint16_t tile  = ((gram1[addr + 1] << 8) | gram1[addr]) & 0x03FF;
+       // Yes, it really requires all 8 bits... even though the bottom 3 are used
+       // as a tile address!
+       uint8_t color = gram1[addr + 1];
+       uint32_t chind = tileBase + (tile << 6);
+
+       int xStart = (int)(sx * 8) - xScroll;
+       int yStart = (int)(sy * 8) - yScroll;
+       int32_t sc_addr = xStart + (yStart * 288);
+
+       for(int y=0; y<8; y++)
+       {
+               for(int x=0; x<8; x++)
                {
-                       for(sx=0; sx<36; sx++)
+                       // Clipping...
+                       if (((xStart + x) < 0) || ((xStart + x) >= 288)
+                               || ((yStart + y) < 0) || ((yStart + y) >= 224))
                        {
-                               WORD sp2 = scp + (sx << 1);
-                               BYTE tile  = ram[sp2++];
-                               BYTE index = ram[sp2] & 0x03;
-                               BYTE color = ram[sp2];
-                               DWORD chind = 0x50000 + (((index << 8) + tile) * 64);
-                               DWORD sc_addr = (sx * 8) + (sy * 2560) + hScrollOffset + voffsets[vScrollOffset];       // Start addr in my_scr[]
-
-                               for(int y=0; y<8; y++)
-                               {
-                                       for(int x=0; x<8; x++)
-                                       {
-                                               if (chr[chind] != 7)
-                                                       my_scr[sc_addr] = ccolor[color][chr[chind]]; 
-
-                                               sc_addr++;
-                                               chind++;
-                                       }
-
-                                       sc_addr += 312;                                         // Do next line of char...
-                               }
+                               sc_addr++;
+                               chind++;
+                               continue;
                        }
 
-                       scp += 0x80;
+                       if (!(transparent && (charROM[chind] == 7)))
+                               my_scr[sc_addr] = ccolor[color][charROM[chind]];
+
+                       sc_addr++;
+                       chind++;
                }
+
+               sc_addr += (288 - 8);           // Do next line of char...
        }
+}
 
-       if (show_text)
-               DrawText();                                                                             // Draw a msg if needed...
 
-       if (ShowGUI())
-               DrawGUI();                                                                              // Show GUI if active...
+//
+// Copy sprites in sprite RAM from positions 4-9 to 10-15
+// (N.B.: This still doesn't solve the shifting signs on the walls problem...)
+//
+void CopySprites(void)
+{
+       extern uint8_t gram1[];
 
-       if (SDL_LockSurface(scr) < 0)
+       for(uint16_t i=0x5800; i<0x6000; i+=0x10)
        {
-//             fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
-//             exit(2);
+               for(uint16_t j=4; j<=9; j++)
+               {
+                       gram1[i + j + 6] = gram1[i + j];
+               }
        }
+}
 
-       // Screen size is 288 x 224
 
-       // Fast blit
-/*     for(int i=0; i<224; i++)
+//
+// Draw sprites at priority level (new code)
+//
+void DrawSprites(uint8_t priority)
+{
+// Sprite blocks:
+//
+// Offset  Note
+// ------  --------------------------------------------------------------------
+// 4       h.fb .nnn (f = horz. flip, h = horz. expand, b = sprite offset lo
+//         bit, nnn = upper bits of sprite #)
+// 5       Lower 7 bits of sprite #
+// 6       Sprite color index (top 7 bits only), bottom bit is bit 8 of X
+//         position
+// 7       Sprite X position (bits 0-7)
+// 8       Top two bits are sprite priority, bits 4 & 2 are sprite offset hi
+//         bit, vert. expand
+// 9       Sprite Y position (192 - value)
+
+       extern uint8_t gram1[];                                                 // Game RAM space
+
+       for(uint16_t i=0x5800; i<0x6000; i+=0x10)
        {
-               memcpy((char *)scr->pixels + scr->pitch * i,
-                       my_scr + (DWORD)(offsets[hScrollOffset]+voffsets[vScrollOffset]) + i * 320, 320);
-       }//*/
+               if ((gram1[i + 8 + 6] & 0xC0) == priority)              // Check for correct layer...
+               {
+                       spr_color_index = gram1[i + 6 + 6] >> 1;        // Set color...
+                       uint16_t x = ((gram1[i + 6 + 6] & 0x01) << 8) | gram1[i + 7 + 6];
 
-       // Doubled pixel blit (should be faster now!)
-       BYTE * pMem = (BYTE *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
-       DWORD src = (DWORD)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
-               dst1 = 0, dst2 = scr->pitch;
-       DWORD srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
+                       if (x > 512 - 32)
+                               x -= 512;                                                       // Handle neg x values
 
-       for(int i=0; i<224; i++)
-       {
-               for (int j=0; j<288; j++)
-               {
-                       pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
-                       dst1 += 2;
-                       dst2 += 2;
+                       uint16_t y = 192 - gram1[i + 9 + 6];
+                       uint8_t flip = gram1[i + 4 + 6] & 0x20;         // Horizontal flip
+                       uint32_t spr_num = ((gram1[i + 4 + 6] & 0x07) << 9)
+                               | ((gram1[i + 5 + 6] & 0x7F) << 2)
+                               | ((gram1[i + 4 + 6] & 0x10) >> 4)
+                               | ((gram1[i + 8 + 6] & 0x10) >> 3);
+
+                       // Draw sprite...
+                       Sprite(spr_num, x, y, flip, gram1[i + 4 + 6] & 0x80, gram1[i + 8 + 6] & 0x04);
                }
+       }
+}
 
-               src += srcAdd;
-               dst1 += dstAdd;
-               dst2 += dstAdd;
-       }//*/
 
-       // Scanlined pixel blit
-/*     BYTE * pMem = (BYTE *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
-       DWORD src = (DWORD)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
-               dst1 = 0, dst2 = scr->pitch;
-       DWORD srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
+static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
+{
+       extern uint8_t spr_rom[];
 
-       for(int i=0; i<224; i++)
+       for(uint16_t sy=0; sy<16; sy++)
        {
-               for (int j=0; j<288; j++)
+               for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
                {
-                       pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = my_scr[src++];
-                       dst1 += 2;
-                       dst2 += 2;
-               }
+                       uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
+                       uint16_t spy = y + sy, spx = x + sx;    // Need to optimize this clipping! [eh?]
 
-               src += srcAdd;
-               dst1 += dstAdd;
-               dst2 += dstAdd;
-       }//*/
+                       // This handles negative values, by casting as unsigned
+                       uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
 
-       SDL_UnlockSurface(scr);
-       SDL_UpdateRect(scr, 0, 0, 0, 0);
+                       if (b1 != 15)
+                               my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
+
+                       sc_addr++;
+
+                       if (b2 != 15)
+                               my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
+               }
+       }
 }
 
-//
-// Draw sprites at priority level (new code)
-//
-void DrawSprites(BYTE priority)
+
+static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
 {
-       extern BYTE * gram1;                                                            // Game RAM space
+       extern uint8_t spr_rom[];
 
-       for(WORD i=0x5800; i<0x6000; i+=0x10)
+       for(uint16_t sy=0; sy<16; sy++)
        {
-               if ((gram1[i + 8] & 0xC0) == priority)                  // Check for correct layer...
+               for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
                {
-                       spr_color_index = gram1[i + 6] >> 1;            // Set color...
-                       WORD x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
+                       uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
+                       uint16_t spy = y + sy, spx = x + sx;    // Need to optimize this clipping! [eh?]
 
-                       if (x > 512 - 32)
-                               x -= 512;                                                               // Handle neg x values
+                       // This handles negative values, by casting as unsigned
+                       uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
+
+                       if (b2 != 15)
+                               my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
 
-                       WORD y = 192 - gram1[i + 9];
-                       WORD hdr = (gram1[i + 4] & 0x90) << 8 | (gram1[i + 8] & 0x14);
-                       BYTE flip = gram1[i + 4] & 0x20;                        // Horizontal flip
-                       DWORD spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2);
+                       sc_addr++;
 
-                       Sprite(spr_num, x, y, flip, hdr);                       // Draw sprite...
+                       if (b1 != 15)
+                               my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
                }
        }
 }
 
+
 //
 // Sprite handler
 //
-void Sprite(DWORD sprnum, WORD x, WORD y, BYTE flip, WORD spr_id)
+void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
+       uint8_t horiz_bl, uint8_t vert_bl)
 {
-       extern BYTE * spr_rom;
-       // To show or not to show a 16x16 block in the 4x4 grid...
-       bool horiz_bl = false, vert_bl = false;
-       DWORD sc_addr;
-
-       x += hScrollOffset;                                                                     // Adjust x-coord
-       y += vScrollOffset;                                                                     // Adjust y-coord
+       // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
+       sprnum <<= 7;
 
-       if (spr_id == 0x8004)
-       {
-               horiz_bl = true;  vert_bl = true;
-       }
-       if (spr_id == 0x8000)
-       {
-               horiz_bl = true;  y += 16;
-       }
-       if (spr_id == 0x8010)
-       {
-               horiz_bl = true;  y += 16;  sprnum += 2;
-       }
-       if (spr_id == 0x0004)
-       {
-               vert_bl = true;
-       }
-       if (spr_id == 0x1004)
-       {
-               vert_bl = true;  sprnum++;
-       }
-       if (spr_id == 0x0000)
-       {
+       if (!vert_bl)
                y += 16;
-       }
-       if (spr_id == 0x1000)
-       {
-               y += 16;  sprnum++;
-       }
-       if (spr_id == 0x0010)
-       {
-               y += 16;  sprnum += 2;
-       }
-       if (spr_id == 0x1010)
-       {
-               y += 16;  sprnum += 3;
-       }
-
-       sprnum <<= 7;                                                                   // 128 bytes per sprite
 
        if (!flip)
        {
-               for(WORD sy=0; sy<16; sy++)
-               {
-                       for(WORD sx=0; sx<16; sx+=2)
-                       {
-                               BYTE b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
-                               WORD spy = y + sy, spx = x + sx;        // Need to optimize this clipping!
-
-                               if (spy > 223 || spx > 299)
-                                       sc_addr = 0x13FFE;
-                               else
-                                       sc_addr = spx + spy * 320;
-
-                               if (b1 != 15)
-                                       my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
-
-                               sc_addr++;
-
-                               if (b2 != 15)
-                                       my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
-                       }
-               }
+               DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
 
                if (horiz_bl)
-               {
-                       for(WORD sy=0; sy<16; sy++)
-                       {
-                               for(WORD sx=16; sx<32; sx+=2)
-                               {
-                                       BYTE b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
-                                       WORD spy = y + sy, spx = x + sx;
-
-                                       if (spy > 223 || spx > 299)
-                                               sc_addr = 0x13FFE;
-                                       else
-                                               sc_addr = spx + spy * 320;
-
-                                       if (b1 != 15)
-                                               my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
-
-                                       sc_addr++;
-
-                                       if (b2 != 15)
-                                               my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
-                               }
-                       }
-               }
+                       DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
                else
-                       sprnum += 128;                                                          // Advance to next...
+                       sprnum += 128;  // Advance to next...
 
                if (vert_bl)
                {
-                       y += 16;                                                                        // Do next row...
+                       y += 16;                // Do next row...
 
-                       for(WORD sy=0; sy<16; sy++)
-                       {
-                               for(WORD sx=0; sx<16; sx+=2)
-                               {
-                                       BYTE b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
-                                       WORD spy = y + sy, spx = x + sx;
+                       DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
 
-                                       if (spy > 223 || spx > 299)
-                                               sc_addr = 0x13FFE;
-                                       else
-                                               sc_addr = spx + spy * 320;
+                       if (horiz_bl)
+                               DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
+               }
+       }
+       else    // Flip
+       {
+               if (horiz_bl)
+                       DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
 
-                                       if (b1 != 15)
-                                               my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
+               DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
 
-                                       sc_addr++;
+               if (!horiz_bl)
+                       sprnum += 128;  // If single, skip sprite...
 
-                                       if (b2 != 15)
-                                               my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
-                               }
-                       }
+               if (vert_bl)
+               {
+                       y += 16;                // Adjust Y coord...
 
                        if (horiz_bl)
-                       {
-                               for(WORD sy=0; sy<16; sy++)
-                               {
-                                       for(WORD sx=16; sx<32; sx+=2)
-                                       {
-                                               BYTE b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
-                                               WORD spy = y + sy, spx = x + sx;
-
-                                               if (spy > 223 || spx > 299)
-                                                       sc_addr = 0x13FFE;
-                                               else
-                                                       sc_addr = spx + spy * 320;
-
-                                               if (b1 != 15)
-                                                       my_scr[sc_addr] = scolor[spr_color_index][b1];  // Store it
-
-                                               sc_addr++;
-
-                                               if (b2 != 15)
-                                                       my_scr[sc_addr] = scolor[spr_color_index][b2];  // Store it
-                                       }
-                               }
-                       }
+                               DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
+
+                       DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
                }
        }
-       else    // Flip
-       {
-    if (horiz_bl)
-    {
-      for(WORD sy=0; sy<16; sy++)
-      {
-        for(WORD sx=30; sx!=14; sx-=2)
-        {
-          BYTE b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
-        WORD spy = y+sy, spx = x+sx;
-        if ((spy>223) || (spx>299))
-          sc_addr = 0x13FFE;
-        else
-          sc_addr = spx+spy*320;
-          if (b2 != 15)
-            my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
-          sc_addr++;
-          if (b1 != 15)
-            my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
-        }
-      }
-    }
-    for(WORD sy=0; sy<16; sy++)
-    {
-      for(WORD sx=14; sx!=0xFFFE; sx-=2)
-      {
-        BYTE b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
-        WORD spy = y+sy, spx = x+sx;
-        if ((spy>223) || (spx>299))
-          sc_addr = 0x13FFE;
-        else
-          sc_addr = spx+spy*320;
-        if (b2 != 15)
-          my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
-        sc_addr++;
-        if (b1 != 15)
-          my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
-      }
-    }
-    if (!horiz_bl)  sprnum += 128;  // If single, skip sprite...
-    if (vert_bl)
-    {
-      y += 16;      // Adjust Y coord...
-      if (horiz_bl)
-      {
-        for(WORD sy=0; sy<16; sy++)
-        {
-          for(WORD sx=30; sx!=14; sx-=2)
-          {
-            BYTE b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
-        WORD spy = y+sy, spx = x+sx;
-        if ((spy>223) || (spx>299))
-          sc_addr = 0x13FFE;
-        else
-          sc_addr = spx+spy*320;
-            if (b2 != 15)
-              my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
-            sc_addr++;
-            if (b1 != 15)
-               my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
-          }
-        }
-      }
-      for(WORD sy=0; sy<16; sy++)
-      {
-        for(WORD sx=14; sx!=0xFFFE; sx-=2)
-        {
-          BYTE b1 = spr_rom[sprnum]>>4, b2 = spr_rom[sprnum++]&0x0F;
-        WORD spy = y+sy, spx = x+sx;
-        if ((spy>223) || (spx>299))
-          sc_addr = 0x13FFE;
-        else
-          sc_addr = spx+spy*320;
-          if (b2 != 15)
-            my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
-          sc_addr++;
-          if (b1 != 15)
-            my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
-        }
-      }
-    }
-  }
 }
 
-int FindPCXName()
+
+int FindPCXName(void)
 {
-       static int pcxNum = -1;                                                         // This needs to go elsewhere... (or does it?)
+       static int pcxNum = -1; // This needs to go elsewhere... (or does it?)
        char filename[30];
        FILE * fr;
 
@@ -537,8 +356,9 @@ int FindPCXName()
        {
                sprintf(filename, "thnd%04i.pcx", pcxNum);
 
+               // file does not exist - we can create it
                if ((fr = fopen(filename, "r")) == NULL)
-                       return pcxNum;                                                          // file does not exist - we can create it
+                       return pcxNum;
 
                pcxNum++;
        }
@@ -546,80 +366,103 @@ int FindPCXName()
        return -1;
 }
 
-void SnapPCX(SDL_Surface * scr)
+
+void SavePCXSnapshot(void)
 {
        char filename[30];
-       int i, line;
-       FILE * fw;
-       int XMax = 319;       // Need to adjust this to handle 288 bytes per line
-       int YMax = 223;
-       int bytesPerLine = 320;
+       int xMax = 287;
+       int yMax = 223;
+       int bytesPerLine = 288;
+       int i = FindPCXName();
 
-       if ((i = FindPCXName()) < 0)
-               return;               // failed...
+       if (i < 0)
+               return;
 
        sprintf(filename, "thnd%04i.pcx", i);
+       FILE * fw = fopen(filename, "wb");
 
-       if ((fw = fopen(filename, "wb")) == NULL)
-               return;  // failed...
+       if (fw == NULL)
+               return;
 
        // Write the header
 
-       fputc(0xA, fw); // pcx signature
-       fputc(0x5, fw); // version 5
-       fputc(0x1, fw); // RLE encoding
-       fputc(0x8, fw); // bits per pixel
-       fputc(0, fw);  fputc(0, fw);  fputc(0, fw);  fputc(0, fw); // XMin=0,YMin=0
-       fputc(XMax&0xFF, fw);  fputc(XMax>>8, fw);
-       fputc(YMax&0xFF, fw);  fputc(YMax>>8, fw);
-       fputc(0, fw);  fputc(0, fw);  fputc(0, fw);  fputc(0,fw); // unknown DPI
-       for (i=0; i<48; i++)  fputc(0, fw);                 // EGA color palette
-       fputc(0, fw); // reserved
-       fputc(1, fw); // number of bit planes
-       fputc(bytesPerLine&0xFF, fw);  fputc(bytesPerLine>>8, fw);
-       fputc(1, fw);  fputc(0, fw); // palette info - unused
-       fputc((XMax+1)&0xFF, fw);  fputc((XMax+1)>>8, fw);
-       fputc((YMax+1)&0xFF, fw);  fputc((YMax+1)>>8, fw); // screen resolution
-       for (i=0; i<54; i++)  fputc(0, fw); // unused
-
-       // Instead of using the screen, we should use our internal buffer...
-       SDL_LockSurface(scr);
-
-       DWORD mem = scr->pitch * 8; // Skip first line... WAS:320*8;
-       for (line=0; line<=YMax; line++)
+       fputc(0x0A, fw);        // PCX signature
+       fputc(0x05, fw);        // Version 5
+       fputc(0x01, fw);        // RLE encoding
+       fputc(0x08, fw);        // Bits per pixel
+       fputc(0, fw);
+       fputc(0, fw);
+       fputc(0, fw);
+       fputc(0, fw);           // XMin=0,YMin=0
+       fputc(xMax & 0xFF, fw);
+       fputc(xMax >> 8, fw);
+       fputc(yMax & 0xFF, fw);
+       fputc(yMax >> 8, fw);
+       fputc(0, fw);
+       fputc(0, fw);
+       fputc(0, fw);
+       fputc(0, fw);           // Unknown DPI
+
+       // EGA color palette
+       for(i=0; i<48; i++)
+               fputc(0, fw);
+
+       fputc(0, fw);           // Reserved
+       fputc(1, fw);           // Number of bit planes
+       fputc(bytesPerLine & 0xFF, fw);
+       fputc(bytesPerLine >> 8, fw);
+       fputc(1, fw);
+       fputc(0, fw);           // Palette info - unused
+       fputc((xMax + 1) & 0xFF, fw);
+       fputc((xMax + 1) >> 8, fw);
+       fputc((yMax + 1) & 0xFF, fw);
+       fputc((yMax + 1) >> 8, fw);     // Screen resolution
+
+       // Unused
+       for (i=0; i<54; i++)
+               fputc(0, fw);
+
+       uint8_t * mem = my_scr;
+
+       for(int line=0; line<=yMax; line++)
        {
-               int count;
-               int last;
-               int xpos;
-               BYTE * pMem = (BYTE *)scr->pixels;
+               int xpos = 0;
 
-               xpos = 0;
                while (xpos < bytesPerLine)
                {
-                       last = pMem[mem++];
+                       uint8_t count = 1;
+                       uint8_t last = *mem;
+                       mem++;
                        xpos++;
-                       count = 1;
-                       while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
+
+                       while ((*mem == last) && (xpos < bytesPerLine) && (count < 63))
                        {
-                               mem++;  count++;  xpos++;
+                               mem++;
+                               count++;
+                               xpos++;
                        }
 
-                       if (count > 1 || (last&0xC0) == 0xC0)
+                       if ((count > 1) || ((last & 0xC0) == 0xC0))
                        {
-                               fputc(0xC0 | (count & 0x3F), fw);  fputc(last & 0xFF, fw);
+                               fputc(0xC0 | (count & 0x3F), fw);
+                               fputc(last & 0xFF, fw);
                        }
-                       else fputc(last & 0xFF, fw);
+                       else
+                               fputc(last & 0xFF, fw);
                }
-               mem += (scr->pitch - 320);  // Handle non-standard line lengths...
        }
 
-       SDL_UnlockSurface(scr);
-
-       // Write the palette
-
+       // Write out the palette
        fputc(0x0C, fw);
-       for (i=0; i<768; i++)
-               fputc(palette[i], fw);
 
-       fclose(fw);    // success!
+       for(int i=0; i<256; i++)
+       {
+               fputc(palette[i] & 0xFF, fw);
+               fputc((palette[i] >> 8) & 0xFF, fw);
+               fputc((palette[i] >> 16) & 0xFF, fw);
+       }
+
+       // Success!
+       fclose(fw);
 }
+