//
// 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
//
#include "screen.h"
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);
+void Sprite(uint32 sprnum, uint16 x, uint16 y, uint8 flip, uint8 horiz_bl, uint8 vert_bl);
// Private global variables
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
//
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
- uint32 scp1 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
- uint32 scp2 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
- uint32 scp3 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
- uint32 scp = 0x3208;
+
+ uint32 scp0 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
+ uint32 scp1 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
+ uint32 scp2 = 0x2180 | ((sc_base + 0x04) & 0x7F); /*0x2188;*/
+ uint32 scp3 = 0x3208;
+
+ scp0 += vsc_base * 0x80;
+ scp0 &= 0x0FFF; // Set vertical scroll addr
scp1 += vsc_base * 0x80;
- scp1 &= 0x0FFF; // Set vertical scroll addr
+ scp1 = 0x1000 | (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
+ scp2 = 0x2000 | (scp2 & 0x0FFF); // Set vertical scroll addr
- uint32 chBaseOffset = (charbase ? 0x20000 : 0x00000);
-
- for(sy=0; sy<29; sy++)
+ // Layer 0 (bottom layer)
+ for(uint8 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++]];
+ for(uint8 sx=0; sx<37; sx++)
+ DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), 0, false);
- sc_addr += 312; // Do next line...
- }
- }
-
- scp1 += 0x80;
- scp1 = 0x0000 | (scp1 & 0x0FFF);
+ scp0 += 0x80;
+ scp0 = 0x0000 | (scp0 & 0x0FFF);
}
DrawSprites(0x40); // Draw sprites at lowest layer...
- chBaseOffset = (charbase ? 0x30000 : 0x10000);
-
- for(sy=0; sy<29; sy++)
+ // Layer 1
+ for(uint8 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++;
- }
+ for(uint8 sx=0; sx<37; sx++)
+ DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), 0);
- sc_addr += 312; // Do next line...
- }
- }
-
- scp2 += 0x80;
- scp2 = 0x1000 | (scp2 & 0x0FFF);
+ scp1 += 0x80;
+ scp1 = 0x1000 | (scp1 & 0x0FFF);
}
DrawSprites(0x80); // Draw sprites under layer #2...
- for(sy=0; sy<29; sy++)
+ // Layer 2
+ for(uint8 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]];
+ for(uint8 sx=0; sx<37; sx++)
+ DrawChar(chr, ram, sx, sy, scp2, 0x40000, 0);
- sc_addr++;
- chind++;
- }
-
- sc_addr += 312; // Do next line...
- }
- }
-
- scp3 += 0x80; scp3 = 0x2000|(scp3&0x0FFF);
+ scp2 += 0x80;
+ scp2 = 0x2000 | (scp2 & 0x0FFF);
}
DrawSprites(0xC0); // Draw highest priority sprites...
- for(sy=0; sy<28; sy++)
+ // Layer 3 (top layer)
+ for(uint8 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++;
- }
+ for(uint8 sx=0; sx<36; sx++)
+ DrawChar(chr, ram, sx, sy, scp3, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
- sc_addr += 312; // Do next line of char...
- }
- }
-
- scp += 0x80;
+ scp3 += 0x80;
}
}
// 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;
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...
+ }
}
//
//
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
- uint32 spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2);
+ uint8 flip = gram1[i + 4] & 0x20; // Horizontal flip
+ uint32 spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2)
+ | ((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, gram1[i + 8] & 0x04);
}
}
}
//
// 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, uint8 horiz_bl, uint8 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)
{