]> Shamusworld >> Repos - thunder/blobdiff - src/screen.cpp
Added MCU to execution loop and it works.
[thunder] / src / screen.cpp
index bc733601fa4eda7620ce1030ddcd4e65a6ee897e..dda0f6e665eb42f349723213b4b688e370e8384c 100644 (file)
@@ -14,6 +14,7 @@
 // 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"
@@ -43,11 +44,9 @@ 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 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 };
-//n.b.: 320 = $140
-// $000, $140, $280, $3C0, $500, $640, $780, $8C0
 
 extern bool show_text;                         // Whether or not to show text
 extern bool show_scr;                          // Whether or not to show screen
@@ -88,11 +87,7 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
                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);
@@ -105,11 +100,7 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
                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);
@@ -122,11 +113,7 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
                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);
@@ -139,11 +126,7 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
                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;
                }
@@ -157,27 +140,19 @@ void BlitChar(SDL_Surface * scr, uint8_t * chr, uint8_t * ram)
        if (ShowGUI())
                DrawGUI();
 
-#if 0
-       if (SDL_LockSurface(scr) < 0)
-       {
-//             fprintf(stderr, "Couldn't lock the display surface: %s\n", SDL_GetError());
-//             exit(2);
-       }
-#endif
-
-       // Rolling Thunder screen size is 288 x 224. Virtual is this, real may not be...
+       // 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.)
 
-       uint32_t src = 0;//(uint32_t)(hoffsets[hScrollOffset] + voffsets[vScrollOffset]);
-//     uint32_t srcAdd = 320 - VIRTUAL_SCREEN_WIDTH;
+       uint32_t src = 0;
 
        for(int i=0; i<VIRTUAL_SCREEN_HEIGHT; i++)
        {
                for (int j=0; j<VIRTUAL_SCREEN_WIDTH; j++)
                {
-                       scrBuffer[(i * VIRTUAL_SCREEN_WIDTH) + j] = palette[my_scr[src++]];
+                       scrBuffer[src] = palette[my_scr[src]];
+                       src++;
                }
-
-//             src += srcAdd;
        }
 
        RenderScreenBuffer();
@@ -196,13 +171,8 @@ static inline void DrawChar(uint8_t * chr, uint8_t * ram, uint8_t sx, uint8_t sy
        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 * 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++)
@@ -282,11 +252,9 @@ void DrawSprites(uint8_t priority)
 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;
 
        for(uint16_t sy=0; sy<16; sy++)
        {
-//             for(uint16_t sx=0; sx<16; sx+=2)
                for(uint16_t sx=xStart; sx<xEnd; sx+=xInc)
                {
                        uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
@@ -310,11 +278,9 @@ static inline void DrawSpriteBlock(uint32_t & sprnum, uint16_t x, uint16_t y, ui
 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[];
-//     uint32_t sc_addr;
 
        for(uint16_t sy=0; sy<16; sy++)
        {
-//             for(uint16_t sx=0; sx<16; sx+=2)
                for(uint16_t sx=xStart; sx!=xEnd; sx+=xInc)
                {
                        uint8_t b1 = spr_rom[sprnum] >> 4, b2 = spr_rom[sprnum++] & 0x0F;
@@ -341,270 +307,49 @@ static inline void DrawSpriteBlock2(uint32_t & sprnum, uint16_t x, uint16_t y, u
 void Sprite(uint32_t sprnum, uint16_t x, uint16_t y, uint8_t flip,
        uint8_t horiz_bl, uint8_t vert_bl)
 {
-       extern uint8_t spr_rom[];
-       uint32_t sc_addr;
-       sprnum <<= 7;                           // 128 bytes per sprite
+       // 128 bytes per sprite (16 x 16 chunks, 4 bits per pixel)
+       sprnum <<= 7;
 
        if (!vert_bl)
                y += 16;
 
        if (!flip)
        {
-#if 0
-               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;    // Need to optimize this clipping!
-
-                               // This handles negative values, by casting as unsigned
-#if 0
-//                             if (spy > 223 || spx > 299)
-                               if (spy >= 224 || spx >= 288)
-                                       sc_addr = 0x13FFE;
-                               else
-//                                     sc_addr = spx + spy * 320;
-                                       sc_addr = spx + (spy * 288);
-#else
-                               sc_addr = ((spy >= 224) || (spx >= 288) ? 0x13FFE : spx + (spy * 288));
-#endif
-                               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
-                       }
-               }
-#else
                DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
-#endif
 
                if (horiz_bl)
-               {
-#if 0
-                       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)
-                                       if (spy > 223 || spx > 287)
-                                               sc_addr = 0x13FFE;
-                                       else
-//                                             sc_addr = spx + spy * 320;
-                                               sc_addr = spx + (spy * 288);
-
-                                       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
-                               }
-                       }
-#else
                        DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
-#endif
-               }
                else
-                       sprnum += 128;                                                          // Advance to next...
+                       sprnum += 128;  // Advance to next...
 
                if (vert_bl)
                {
-                       y += 16;                                                                        // Do next row...
+                       y += 16;                // Do next row...
 
-#if 0
-                       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)
-                                       if (spy > 223 || spx > 287)
-                                               sc_addr = 0x13FFE;
-                                       else
-//                                             sc_addr = spx + spy * 320;
-                                               sc_addr = spx + (spy * 288);
-
-                                       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
-                               }
-                       }
-#else
                        DrawSpriteBlock(sprnum, x, y, 0, 16, 2);
-#endif
 
                        if (horiz_bl)
-                       {
-#if 0
-                               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)
-                                               if (spy > 223 || spx > 287)
-                                                       sc_addr = 0x13FFE;
-                                               else
-//                                                     sc_addr = spx + spy * 320;
-                                                       sc_addr = spx + (spy * 288);
-
-                                               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
-                                       }
-                               }
-#else
                                DrawSpriteBlock(sprnum, x, y, 16, 32, 2);
-#endif
-                       }
                }
        }
        else    // Flip
        {
                if (horiz_bl)
-               {
-#if 0
-                       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))
-                                       if ((spy > 223) || (spx > 287))
-                                               sc_addr = 0x13FFE;
-                                       else
-//                                             sc_addr = spx + spy * 320;
-                                               sc_addr = spx + (spy * 288);
-
-                                       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
-                               }
-                       }
-#else
                        DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
-#endif
-               }
-
-#if 0
-               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))
-                               if ((spy > 223) || (spx > 287))
-                                       sc_addr = 0x13FFE;
-                               else
-//                                     sc_addr = spx + spy * 320;
-                                       sc_addr = spx + (spy * 288);
 
-                               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
-                       }
-               }
-#else
                DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
-#endif
 
                if (!horiz_bl)
-                       sprnum += 128;  // If single, skip sprite...
+                       sprnum += 128;  // If single, skip sprite...
 
                if (vert_bl)
                {
-                       y += 16;      // Adjust Y coord...
+                       y += 16;                // Adjust Y coord...
 
                        if (horiz_bl)
-                       {
-#if 0
-                               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))
-                                               if ((spy > 223) || (spx > 287))
-                                                       sc_addr = 0x13FFE;
-                                               else
-//                                                     sc_addr = spx + spy * 320;
-                                                       sc_addr = spx + (spy * 288);
-
-                                               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
-                                       }
-                               }
-#else
                                DrawSpriteBlock2(sprnum, x, y, 30, 14, -2);
-#endif
-                       }
 
-#if 0
-                       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))
-                                       if ((spy > 223) || (spx > 287))
-                                               sc_addr = 0x13FFE;
-                                       else
-//                                             sc_addr = spx + spy * 320;
-                                               sc_addr = spx + (spy * 288);
-
-                                       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
-                               }
-                       }
-#else
                        DrawSpriteBlock2(sprnum, x, y, 14, 0xFFFE, -2);
-#endif
                }
        }
 }
@@ -633,88 +378,82 @@ int FindPCXName(void)
 }
 
 
-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;
-
-       // Return if failed...
-       if ((i = FindPCXName()) < 0)
+       int xMax = 287;
+       int yMax = 223;
+       int bytesPerLine = 288;
+       int i = FindPCXName();
+
+       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(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);           // 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
+       fputc(0, fw);           // Unknown DPI
 
-       for (i=0; i<48; i++)
-               fputc(0, fw);                 // EGA color palette
+       // EGA color palette
+       for(i=0; i<48; i++)
+               fputc(0, fw);
 
-       fputc(0, fw); // reserved
-       fputc(1, fw); // number of bit planes
+       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
+       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); // unused
-
-       // Instead of using the screen, we should use our internal buffer...
-       SDL_LockSurface(scr);
+               fputc(0, fw);
 
-       uint32_t mem = scr->pitch * 8; // Skip first line... WAS:320*8;
+       uint8_t * mem = my_scr;
 
-       for (line=0; line<=YMax; line++)
+       for(int line=0; line<=yMax; line++)
        {
-               int count;
-               int last;
-               int xpos;
-               uint8_t * pMem = (uint8_t *)scr->pixels;
-
-               xpos = 0;
+               int 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++;
                        }
 
-                       if (count > 1 || (last & 0xC0) == 0xC0)
+                       if ((count > 1) || ((last & 0xC0) == 0xC0))
                        {
                                fputc(0xC0 | (count & 0x3F), fw);
                                fputc(last & 0xFF, fw);
@@ -722,18 +461,19 @@ void SnapPCX(SDL_Surface * scr)
                        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);
+       for(int i=0; i<256; i++)
+       {
+               fputc(palette[i] & 0xFF, fw);
+               fputc((palette[i] >> 8) & 0xFF, fw);
+               fputc((palette[i] >> 16) & 0xFF, fw);
+       }
 
-       fclose(fw);    // success!
+       // Success!
+       fclose(fw);
 }