// Who When What
// --- ---------- -----------------------------------------------------------
// 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 <stdlib.h>
#include <string> // For memset()
#include "gui.h"
+#include "video.h"
// Private function prototypes
void DrawSprites(uint8_t priority);
int FindPCXName(void);
-static void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent = true);
+//static void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent = true);
+static void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll = 0, uint32_t yScroll = 0, 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
uint8_t my_scr[0x14000]; // Screen buffer...
-uint8_t palette[768]; // Screen palette
+//uint8_t palette[768]; // Screen palette
+uint32_t palette[256]; // Screen palette
uint8_t ccolor[256][8]; // Character colors
uint8_t scolor[128][16]; // Sprite colors
bool charbase; // Character base pointer...
uint8_t hScrollOffset; // Horizontal scroll offset
uint8_t vScrollOffset; // Vertical scroll offset
uint8_t spr_color_index; // Sprite color index
-uint32_t hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };// Scroll offsets...
-uint32_t voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
+//uint32_t hoffsets[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };// Scroll offsets...
+//uint32_t voffsets[8] = { 0, 320, 640, 960, 1280, 1600, 1920, 2240 };
+uint32_t voffsets[8] = { 288*0, 288*1, 288*2, 288*3, 288*4, 288*5, 288*6, 288*7 };
extern bool show_text; // Whether or not to show text
extern bool show_scr; // Whether or not to show screen
void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * 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.
+ // 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)
{
uint32_t sc_base = ((ram[0x9000] << 8) | ram[0x9001]) + 4; // Adjust hscroll val
- hScrollOffset = sc_base & 0x07; // Horiz. fine scroll offset
- sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
+ hScrollOffset = sc_base & 0x07; // Horiz. fine scroll offset
+ sc_base = (sc_base & 0xFFF8) >> 2; // Skip odds..
uint8_t vsc_base = ((ram[0x9002] + 1) & 0xF8) >> 3;// Vertical scroll addr adjust
- vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
+ vScrollOffset = ((ram[0x9002] + 1) & 0x07); // Vertical fine scroll amount
uint32_t scp0 = 0x0180 | ((sc_base + 0x04) & 0x7F); /*0x0188;*/
uint32_t scp1 = 0x1180 | ((sc_base + 0x04) & 0x7F); /*0x1188;*/
uint32_t scp3 = 0x3208;
scp0 += vsc_base * 0x80;
- scp0 &= 0x0FFF; // Set vertical scroll addr
+ scp0 &= 0x0FFF; // Set vertical scroll addr
scp1 += vsc_base * 0x80;
- scp1 = 0x1000 | (scp1 & 0x0FFF); // Set vertical scroll addr
+ scp1 = 0x1000 | (scp1 & 0x0FFF); // Set vertical scroll addr
scp2 += vsc_base * 0x80;
- scp2 = 0x2000 | (scp2 & 0x0FFF); // Set vertical scroll addr
+ scp2 = 0x2000 | (scp2 & 0x0FFF); // Set vertical scroll addr
// Layer 0 (bottom layer)
for(uint8_t sy=0; sy<29; sy++)
{
for(uint8_t sx=0; sx<37; sx++)
+#if 0
DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), 0, false);
+#else
+ DrawChar(chr, ram, sx, sy, scp0, (charbase ? 0x20000 : 0x00000), hScrollOffset, vScrollOffset, false);
+#endif
scp0 += 0x80;
scp0 = 0x0000 | (scp0 & 0x0FFF);
}
- DrawSprites(0x40); // Draw sprites at lowest layer...
+ // Draw sprites at lowest layer...
+ DrawSprites(0x40);
// Layer 1
for(uint8_t sy=0; sy<29; sy++)
{
for(uint8_t sx=0; sx<37; sx++)
+#if 0
DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), 0);
+#else
+ DrawChar(chr, ram, sx, sy, scp1, (charbase ? 0x30000 : 0x10000), hScrollOffset, vScrollOffset);
+#endif
scp1 += 0x80;
scp1 = 0x1000 | (scp1 & 0x0FFF);
}
- DrawSprites(0x80); // Draw sprites under layer #2...
+ // Draw sprites under layer #2...
+ DrawSprites(0x80);
// Layer 2
for(uint8_t sy=0; sy<29; sy++)
{
for(uint8_t sx=0; sx<37; sx++)
+#if 0
DrawChar(chr, ram, sx, sy, scp2, 0x40000, 0);
+#else
+ DrawChar(chr, ram, sx, sy, scp2, 0x40000, hScrollOffset, vScrollOffset);
+#endif
scp2 += 0x80;
scp2 = 0x2000 | (scp2 & 0x0FFF);
}
- DrawSprites(0xC0); // Draw highest priority sprites...
+ // Draw highest priority sprites...
+ DrawSprites(0xC0);
// Layer 3 (top layer)
for(uint8_t sy=0; sy<28; sy++)
{
for(uint8_t sx=0; sx<36; sx++)
+#if 0
DrawChar(chr, ram, sx, sy, scp3, 0x50000, hScrollOffset + voffsets[vScrollOffset]);
+#else
+ DrawChar(chr, ram, sx, sy, scp3, 0x50000);
+#endif
scp3 += 0x80;
}
}
+ // Draw a msg if needed...
if (show_text)
- DrawText(); // Draw a msg if needed...
+ DrawText();
+ // Show GUI if active...
if (ShowGUI())
- DrawGUI(); // Show GUI if active...
-
- if (SDL_LockSurface(scr) < 0)
- {
-// fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
-// exit(2);
- }
+ DrawGUI();
// Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
- // Doubled pixel blit (should be faster now!)
- uint8_t * pMem = (uint8_t *)scr->pixels;
- uint32_t src = (uint32_t)(hoffsets[hScrollOffset] + voffsets[vScrollOffset]),
- dst1 = 0, dst2 = scr->pitch;
- uint32_t srcAdd = 320 - VIRTUAL_SCREEN_WIDTH, dstAdd = (scr->pitch * 2) - (VIRTUAL_SCREEN_WIDTH * 2);
+ uint32_t src = 0;
for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
- {
for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
- {
- pMem[dst1] = pMem[dst1 + 1] = pMem[dst2] = pMem[dst2 + 1] = my_scr[src++];
- dst1 += 2;
- dst2 += 2;
- }
+// scrBuffer[(i * VIRTUAL_SCREEN_WIDTH) + j] = palette[my_scr[src++]];
+ scrBuffer[src] = palette[my_scr[src++]];
- src += srcAdd;
- dst1 += dstAdd;
- dst2 += dstAdd;
- }
-
- SDL_UnlockSurface(scr);
- SDL_UpdateRect(scr, 0, 0, 0, 0);
+ RenderScreenBuffer();
}
//
// Draw character on screen
//
-static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent/*= true*/)
+//static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t scrollOffset, bool transparent/*= true*/)
+static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy, uint16_t scp, uint32_t baseAddr, uint32_t xScroll/*= 0*/, uint32_t yScroll/*= 0*/, bool transparent/*= true*/)
{
uint8_t scp_lo = (scp + (sx << 1)) & 0x7F;// Let LO byte wrap only...
uint16_t sp2 = (scp & 0xFF80) | scp_lo;
uint8_t index = ram[sp2] & 0x03;
uint8_t color = ram[sp2];
uint32_t chind = baseAddr + (((index << 8) + tile) * 64);
- uint32_t sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
+// uint32_t sc_addr = (sx * 8) + (sy * 2560) + scrollOffset; // Start addr in my_scr[]
+// uint32_t sc_addr = (sx * 8) + (sy * 288 * 8);// + scrollOffset;
+
+ int xStart = (int)(sx * 8) - xScroll;
+ int yStart = (int)(sy * 8) - yScroll;
+
+// int32_t sc_addr = ((sx * 8) - xScroll) + ((sy * 8 * 288) - (yScroll * 288));
+ int32_t sc_addr = xStart + (yStart * 288);
for(int y=0; y<8; y++)
{
for(int x=0; x<8; x++)
{
+ if (((xStart + x) < 0) || ((xStart + x) >= 288)
+ || ((yStart + y) < 0) || ((yStart + y) >= 224))
+ {
+ sc_addr++;
+ chind++;
+ continue;
+ }
+
if (transparent)
{
if (chr[chind] != 7)
chind++;
}
- sc_addr += 312; // Do next line of char...
+ sc_addr += (288 - 8); // Do next line of char...
}
}
//
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)
+// 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
uint16_t y = 192 - gram1[i + 9];
uint8_t flip = gram1[i + 4] & 0x20; // Horizontal flip
- uint32_t spr_num = ((gram1[i + 4] & 0x07) << 9) | ((gram1[i + 5] & 0x7F) << 2)
- | ((gram1[i + 4] & 0x10) >> 4) | ((gram1[i + 8] & 0x10) >> 3);
+ uint32_t spr_num = ((gram1[i + 4] & 0x07) << 9)
+ | ((gram1[i + 5] & 0x7F) << 2)
+ | ((gram1[i + 4] & 0x10) >> 4)
+ | ((gram1[i + 8] & 0x10) >> 3);
// Draw sprite...
Sprite(spr_num, x, y, flip, gram1[i + 4] & 0x80, gram1[i + 8] & 0x04);
}
-//
-// Sprite handler
-//
-void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip, uint8_t horiz_bl, uint8_t vert_bl)
+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[];
- uint32_t sc_addr;
- x += hScrollOffset; // Adjust x-coord
- y += vScrollOffset; // Adjust y-coord
+ for(uint16_t sy=0; sy<16; sy++)
+ {
+ for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
+ {
+ 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?]
- sprnum <<= 7; // 128 bytes per sprite
+ // This handles negative values, by casting as unsigned
+ uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
- if (!vert_bl)
- y += 16;
+ if (b1 != 15)
+ my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
- if (!flip)
+ sc_addr++;
+
+ if (b2 != 15)
+ my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
+ }
+ }
+}
+
+
+static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, uint16_t xStart, uint16_t xEnd, int16_t xInc)
+{
+ extern uint8_t spr_rom[];
+
+ for(uint16_t sy=0; sy<16; sy++)
{
- for(uint16_t sy=0; sy<16; sy++)
+ for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
{
- for(uint16_t sx=0; sx<16; sx+=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!
+ 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 (spy > 223 || spx > 299)
- sc_addr = 0x13FFE;
- else
- sc_addr = spx + spy * 320;
+ // This handles negative values, by casting as unsigned
+ uint32_t sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
- if (b1 != 15)
- my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
+ if (b2 != 15)
+ my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
- sc_addr++;
+ sc_addr++;
- if (b2 != 15)
- my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
- }
+ if (b1 != 15)
+ my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
}
+ }
+}
- if (horiz_bl)
- {
- for(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=16; sx<32; sx+=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t spy = y + sy, spx = x + sx;
- if (spy > 223 || spx > 299)
- sc_addr = 0x13FFE;
- else
- sc_addr = spx + spy * 320;
+//
+// Sprite handler
+//
+void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
+ uint8_t horiz_bl, uint8_t vert_bl)
+{
+ // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
+ sprnum <<= 7;
- if (b1 != 15)
- my_scr[sc_addr] = scolor[spr_color_index][b1]; // Store it
+ if (!vert_bl)
+ y += 16;
- sc_addr++;
+ if (!flip)
+ {
+ DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
- if (b2 != 15)
- my_scr[sc_addr] = scolor[spr_color_index][b2]; // Store it
- }
- }
- }
+ if (horiz_bl)
+ DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
else
sprnum += 128; // Advance to next...
{
y += 16; // Do next row...
- for(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=0; sx<16; sx+=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t 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, 0, 16, 2);
if (horiz_bl)
- {
- for(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=16; sx<32; sx+=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t 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 // Flip
{
if (horiz_bl)
- {
- for(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=30; sx!=14; sx-=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t spy = y + sy, spx = x + sx;
+ DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
- if ((spy > 223) || (spx > 299))
- sc_addr = 0x13FFE;
- else
- sc_addr = spx + spy * 320;
+ DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
- 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(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t 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 (!horiz_bl)
+ sprnum += 128; // If single, skip sprite...
if (vert_bl)
{
y += 16; // Adjust Y coord...
if (horiz_bl)
- {
- for(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=30; sx!=14; sx-=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t 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(uint16_t sy=0; sy<16; sy++)
- {
- for(uint16_t sx=14; sx!=0xFFFE; sx-=2)
- {
- uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
- uint16_t spy = y + sy, spx = x + sx;
+ DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
- 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
- }
- }
+ DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
}
}
}
-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;
{
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++;
}
while (pMem[mem] == last && xpos < bytesPerLine && count < 63)
{
- mem++; count++; xpos++;
+ mem++;
+ count++;
+ xpos++;
}
if (count > 1 || (last & 0xC0) == 0xC0)
else
fputc(last & 0xFF, fw);
}
+
mem += (scr->pitch - 320); // Handle non-standard line lengths...
}