]> Shamusworld >> Repos - thunder/blobdiff - src/screen.cpp
Small refactoring to sprite draw code in anticipation of major refactoring
[thunder] / src / screen.cpp
index b95858268db0d0f785013542f67934a56b53ad95..fef69a0a7a1653e77e273d60d8534bb8ee3b2f7a 100755 (executable)
@@ -27,6 +27,7 @@
 
 void DrawSprites(uint8 priority);
 int FindPCXName(void);
+static void DrawChar(uint8 * chr, uint8 * ram, uint8 sx, uint8 sy, uint16 scp, uint32 baseAddr, uint32 scrollOffset, bool transparent = true);
 
 // Private global variables
 
@@ -35,10 +36,10 @@ uint8 palette[768];                                                                         // Screen palette
 uint8 ccolor[256][8];                                                                  // Character colors
 uint8 scolor[128][16];                                                                 // Sprite colors
 bool charbase;                                                                                 // Character base pointer...
-uint8 hScrollOffset;                                                                           // Horizontal scroll offset
-uint8 vScrollOffset;                                                                           // Vertical scroll offset
+uint8 hScrollOffset;                                                                   // Horizontal scroll offset
+uint8 vScrollOffset;                                                                   // Vertical scroll offset
 uint8 spr_color_index;                                                                 // Sprite color index
-uint32 offsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };                        // Scroll offsets...
+uint32 hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };               // Scroll offsets...
 uint32 voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
 
 extern bool show_text;                                                                 // Whether or not to show text
@@ -49,11 +50,17 @@ extern bool show_scr;                                                                       // Whether or not to show screen
 //
 void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
 {
+// Screen structure:
+// Screen is 288 x 224 pixels, with character and independent sprites. Tiles are 36 x 28.
+// There are four tile planes, three of them affected by the h/vscroll values, the last
+// one is a static display (also has highest priority). Screens are 128 bytes wide by
+// 32 bytes high.
+
        if (show_scr)
        {
                int sx, sy;
                uint32 sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4;        // Adjust hscroll val
-               hScrollOffset = sc_base & 0x07;                                 // Horiz. scroll offset
+               hScrollOffset = sc_base & 0x07;                                 // Horiz. fine scroll offset
                sc_base = (sc_base & 0xFFF8) >> 2;                              // Skip odds..
                uint8 vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
                vScrollOffset = ((ram[0x9002] + 1) & 0x07);             // Vertical fine scroll amount
@@ -68,28 +75,10 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
                scp3 += vsc_base * 0x80;
                scp3 = 0x2000 | (scp3 & 0x0FFF);                                // Set vertical scroll addr
 
-               uint32 chBaseOffset = (charbase ? 0x20000 : 0x00000);
-
                for(sy=0; sy<29; sy++)
                {
                        for(sx=0; sx<37; sx++)
-                       {
-                               uint8 scp_lo = (scp1 + (sx << 1)) & 0x7F;// Let LO byte wrap only...
-                               uint16 sp2 = (scp1 & 0xFF80) | scp_lo;
-                               uint8 tile  = ram[sp2++];
-                               uint8 index = ram[sp2] & 0x03;
-                               uint8 color = ram[sp2];
-                               uint32 chind = chBaseOffset + (((index << 8) + tile) * 64);
-                               uint32 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...
-                               }
-                       }
+                               DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x20000 : 0x00000), 0, false);
 
                        scp1 += 0x80;
                        scp1 = 0x0000 | (scp1 & 0x0FFF);
@@ -97,34 +86,10 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
 
                DrawSprites(0x40);                                                              // Draw sprites at lowest layer...
 
-               chBaseOffset = (charbase ? 0x30000 : 0x10000);
-
                for(sy=0; sy<29; sy++)
                {
                        for(sx=0; sx<37; sx++)
-                       {
-                               uint8 scp_lo = (scp2 + (sx << 1)) & 0x7F;  // Let LO byte wrap only...
-                               uint16 sp2 = (scp2 & 0xFF80) | scp_lo;
-                               uint8 tile   = ram[sp2++];
-                               uint8 index  = ram[sp2] & 0x03;
-                               uint8 color  = ram[sp2];
-                               uint32 chind = chBaseOffset + (((index << 8) + tile) * 64);
-                               uint32 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...
-                               }
-                       }
+                               DrawChar(chr, ram, sx, sy, scp2, (charbase ? 0x30000 : 0x10000), 0);
 
                        scp2 += 0x80;
                        scp2 = 0x1000 | (scp2 & 0x0FFF);
@@ -135,31 +100,10 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
                for(sy=0; sy<29; sy++)
                {
                        for(sx=0; sx<37; sx++)
-                       {
-                               uint8 scp_lo = (scp3 + (sx << 1)) & 0x7F;  // Let LO byte wrap only...
-                               uint16 sp2 = (scp3 & 0xFF80) | scp_lo;
-                               uint8 tile  = ram[sp2++];
-                               uint8 index = ram[sp2] & 0x03;
-                               uint8 color = ram[sp2];
-                               uint32 chind = 0x40000 + (((index << 8) + tile) * 64);
-                               uint32 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]];
+                               DrawChar(chr, ram, sx, sy, scp3, 0x40000, 0);
 
-                                               sc_addr++;
-                                               chind++;
-                                       }
-
-                                       sc_addr += 312;  // Do next line...
-                               }
-                       }
-
-                       scp3 += 0x80;  scp3 = 0x2000|(scp3&0x0FFF);
+                       scp3 += 0x80;
+                       scp3 = 0x2000 | (scp3 & 0x0FFF);
                }
 
                DrawSprites(0xC0);                                                              // Draw highest priority sprites...
@@ -167,28 +111,7 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
                for(sy=0; sy<28; sy++)
                {
                        for(sx=0; sx<36; sx++)
-                       {
-                               uint16 sp2 = scp + (sx << 1);
-                               uint8 tile  = ram[sp2++];
-                               uint8 index = ram[sp2] & 0x03;
-                               uint8 color = ram[sp2];
-                               uint32 chind = 0x50000 + (((index << 8) + tile) * 64);
-                               uint32 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...
-                               }
-                       }
+                               DrawChar(chr, ram, sx, sy, scp, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
 
                        scp += 0x80;
                }
@@ -206,24 +129,17 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
 //             exit(2);
        }
 
-       // Screen size is 288 x 224
-
-       // Fast blit
-/*     for(int i=0; i<224; i++)
-       {
-               memcpy((char *)scr->pixels + scr->pitch * i,
-                       my_scr + (uint32)(offsets[hScrollOffset]+voffsets[vScrollOffset]) + i * 320, 320);
-       }//*/
+       // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
 
        // Doubled pixel blit (should be faster now!)
-       uint8 * pMem = (uint8 *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
-       uint32 src = (uint32)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
+       uint8 * pMem = (uint8 *)scr->pixels;
+       uint32 src = (uint32)(hoffsets[hScrollOffset] + voffsets[vScrollOffset]),
                dst1 = 0, dst2 = scr->pitch;
-       uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
+       uint32 srcAdd = 320 - VIRTUAL_SCREEN_WIDTH, dstAdd = (scr->pitch * 2) - (VIRTUAL_SCREEN_WIDTH * 2);
 
-       for(int i=0; i<224; i++)
+       for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
        {
-               for (int j=0; j<288; j++)
+               for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
                {
                        pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
                        dst1 += 2;
@@ -233,30 +149,43 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
                src += srcAdd;
                dst1 += dstAdd;
                dst2 += dstAdd;
-       }//*/
+       }
 
-       // Scanlined pixel blit
-/*     uint8 * pMem = (uint8 *)scr->pixels + ((scr->pitch * 8 + 16) * 2);
-       uint32 src = (uint32)(offsets[hScrollOffset] + voffsets[vScrollOffset]),
-               dst1 = 0, dst2 = scr->pitch;
-       uint32 srcAdd = 320 - 288, dstAdd = (scr->pitch * 2) - (288 * 2);
+       SDL_UnlockSurface(scr);
+       SDL_UpdateRect(scr, 0, 0, 0, 0);
+}
 
-       for(int i=0; i<224; i++)
+//
+// Draw character on screen
+//
+static inline void DrawChar(uint8 * chr, uint8 * ram, uint8 sx, uint8 sy, uint16 scp, uint32 baseAddr, uint32 scrollOffset, bool transparent/*= true*/)
+{
+       uint8 scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
+       uint16 sp2 = (scp & 0xFF80) | scp_lo;
+       uint8 tile  = ram[sp2++];
+       uint8 index = ram[sp2] & 0x03;
+       uint8 color = ram[sp2];
+       uint32 chind = baseAddr + (((index << 8) + tile) * 64);
+       uint32 sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
+
+       for(int y=0; y<8; y++)
        {
-               for (int j=0; j<288; j++)
+               for(int x=0; x<8; x++)
                {
-                       pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = my_scr[src++];
-                       dst1 += 2;
-                       dst2 += 2;
-               }
+                       if (transparent)
+                       {
+                               if (chr[chind] != 7)
+                                       my_scr[sc_addr] = ccolor[color][chr[chind]];
+                       }
+                       else
+                               my_scr[sc_addr] = ccolor[color][chr[chind]];
 
-               src += srcAdd;
-               dst1 += dstAdd;
-               dst2 += dstAdd;
-       }//*/
+                       sc_addr++;
+                       chind++;
+               }
 
-       SDL_UnlockSurface(scr);
-       SDL_UpdateRect(scr, 0, 0, 0, 0);
+               sc_addr += 312;                                         // Do next line of char...
+       }
 }
 
 //
@@ -264,24 +193,36 @@ void BlitChar(SDL_Surface * scr, uint8 * chr, uint8 * ram)
 //
 void DrawSprites(uint8 priority)
 {
-       extern uint8 gram1[];                                                           // Game RAM space
+       // 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 gram1[];                                                   // Game RAM space
 
        for(uint16 i=0x5800; i<0x6000; i+=0x10)
        {
-               if ((gram1[i + 8] & 0xC0) == priority)                  // Check for correct layer...
+               if ((gram1[i + 8] & 0xC0) == priority)          // Check for correct layer...
                {
-                       spr_color_index = gram1[i + 6] >> 1;            // Set color...
+                       spr_color_index = gram1[i + 6] >> 1;    // Set color...
                        uint16 x = ((gram1[i + 6] & 0x01) << 8) | gram1[i + 7];
 
                        if (x > 512 - 32)
-                               x -= 512;                                                               // Handle neg x values
+                               x -= 512;                                                       // Handle neg x values
 
                        uint16 y = 192 - gram1[i + 9];
-                       uint16 hdr = (gram1[i + 4] & 0x90) << 8 | (gram1[i + 8] & 0x14);
-                       uint8 flip = gram1[i + 4] & 0x20;                       // Horizontal flip
+                       uint8 flip = gram1[i + 4] & 0x20;               // Horizontal flip
                        uint32 spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2);
+                       spr_num += ((gram1[i + 4] & 0x10) >> 4) + ((gram1[i + 8] & 0x10) >> 3);
 
-                       Sprite(spr_num, x, y, flip, hdr);                       // Draw sprite...
+                       // Draw sprite...
+                       Sprite(spr_num, x, y, flip, (gram1[i + 4] & 0x80 ? true : false), (gram1[i + 8] & 0x04 ? true : false));
                }
        }
 }
@@ -289,54 +230,19 @@ void DrawSprites(uint8 priority)
 //
 // Sprite handler
 //
-void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, uint16 spr_id)
+//void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, uint16 spr_id)
+void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, bool horiz_bl, bool vert_bl)
 {
        extern uint8 spr_rom[];
-       // To show or not to show a 16x16 block in the 4x4 grid...
-       bool horiz_bl = false, vert_bl = false;
        uint32 sc_addr;
 
-       x += hScrollOffset;                                                                     // Adjust x-coord
-       y += vScrollOffset;                                                                     // Adjust y-coord
+       x += hScrollOffset;                                                     // Adjust x-coord
+       y += vScrollOffset;                                                     // Adjust y-coord
 
-       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)
-       {
-               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
 
-       sprnum <<= 7;                                                                   // 128 bytes per sprite
+       if (!vert_bl)
+               y += 16;
 
        if (!flip)
        {