]> Shamusworld >> Repos - thunder/blobdiff - src/thunder.cpp
Initial conversion to SDL 2.
[thunder] / src / thunder.cpp
index 4cd035c2304fb993c21ebd587a86bc37d0d41996..59824249291e8b3aacf52ed39f61f851a79dd39b 100644 (file)
@@ -10,6 +10,7 @@
 // ---  ----------  -----------------------------------------------------------
 // JLH  07/23/2009  Added changelog ;-)
 // JLH  08/12/2009  Stabilized emulation so that it works
+// JLH  04/04/2014  Converted to SDL 2
 //
 
 #define THUNDER_VERSION                "1.0.0"
 //#include <conio.h>                   // For getch()
 #include <curses.h>                    // For getch()
 #include <time.h>
-#include "SDL.h"                                                               // Get yer SDL out...!
+#include <SDL2/SDL.h>          // Get yer SDL out...!
 #include "v6809.h"
 #include "screen.h"
 #include "gui.h"
 #include "log.h"
+#include "video.h"
 
-using namespace std;                                                   // Yes!
+using namespace std;
 
 
 #define ROM1   "rt3-1b.9c"
@@ -99,7 +101,8 @@ uint8_t * fm_adrs[14];
 fstream tr;                                                    // Tracelog hook
 uint16_t pcx;                                          // Where we at?
 
-static uint8_t * keys;                         // SDL raw keyboard matrix
+//kludge, for now
+//static uint8_t keys[0x1000];                         // SDL raw keyboard matrix
 
 static char op_mat1[256] = {
   1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
@@ -657,17 +660,11 @@ uint16_t htod(char * str)
        for(int i=0; i<len; i++)
        {
                if (str[i] >= '0' && str[i] <= '9')
-               {
                        value = (value << 4) | (unsigned)(str[i] - '0');
-               }
                else if (str[i] >= 'a' && str[i] <= 'f')
-               {
                        value = (value << 4) | (unsigned)((str[i] - 'a') + 10);
-               }
                else if (str[i] >= 'A' && str[i] <= 'F')
-               {
                        value = (value << 4) | (unsigned)((str[i] - 'A') + 10);
-               }
        }
 
        return value;
@@ -679,23 +676,35 @@ uint16_t htod(char * str)
 //
 bool Load32KImg(char * filename, uint16_t address)
 {
+#if 0
        ifstream ff;
        char ch;
 
-       ff.open(filename, ios::binary | ios::in);  // Open 'da file...
+       ff.open(filename, ios::binary | ios::in);
 
        if (ff)
        {
-               for(long i=0; i<32768; i++)                // Read it in...
+               // Read it in...
+               for(long i=0; i<32768; i++)
                {
                        ff.get(ch);
                        grom[address+i] = ch;
                }
 
-               ff.close();                                // Close 'da file...
+               ff.close();
        }
 
        return ff;
+#else
+       FILE * file = fopen(filename, "rb");
+
+       if (!file)
+               return false;
+
+       fread(&grom[address], 1, 0x8000, file);
+
+       return true;
+#endif
 }
 
 
@@ -737,11 +746,12 @@ bool ReadColorPROMs(void)
        fstream ff1, ff2;
        //  uint8_t ch;
        char ch;
-       extern uint8_t palette[768];     // Screen physical palette
+//     extern uint8_t palette[768];     // Screen physical palette
+       extern uint32_t palette[256];     // Screen physical palette
        extern uint8_t ccolor[256][8];   // Character color PROM values
        extern uint8_t scolor[128][16];  // Sprite color PROM values
 
-       ff1.open("./ROMs/"PROM3, ios::binary|ios::in);
+       ff1.open("./ROMs/"PROM3, ios::binary | ios::in);
 
        if (ff1)
        {
@@ -757,7 +767,7 @@ bool ReadColorPROMs(void)
                ff1.close();
        }
 
-       ff1.open("./ROMs/"PROM4, ios::binary|ios::in);
+       ff1.open("./ROMs/"PROM4, ios::binary | ios::in);
 
        if (ff1)
        {
@@ -773,29 +783,33 @@ bool ReadColorPROMs(void)
                ff1.close();
        }
 
-       ff1.open("./ROMs/"PROM1, ios::binary|ios::in);
-       ff2.open("./ROMs/"PROM2, ios::binary|ios::in);
+       ff1.open("./ROMs/"PROM1, ios::binary | ios::in);
+       ff2.open("./ROMs/"PROM2, ios::binary | ios::in);
 
-       if (ff1)    // If open was successful...
+       // If open was successful...
+       if (ff1)
        {
-               for(int i=0; i<768; i+=3)
+               // Palette is 12-bit RGB, we stretch it to 24-bit
+               for(int i=0; i<256; i++)
                {
-                       ff1.get(ch);
-                       palette[i]   = (uint8_t)(ch&0x0F);
-                       palette[i+1] = (uint8_t)(ch>>4);
-                       ff2.get(ch);
-                       palette[i+2] = (uint8_t)ch;
+                       char c1, c2;
+                       uint8_t r, g, b;
+                       ff1.get(c1);
+                       ff2.get(c2);
+                       r = (uint8_t)c1 & 0x0F;
+                       g = (uint8_t)c1 >> 4;
+                       b = (uint8_t)c2;
+                       palette[i] = 0xFF000000 | (b << 20) | (b << 16) | (g << 12) | (g << 8) | (r << 4) | r;
                }
 
-               // Do palette stretching here... I.e. add 0 to hinyb 0, 1 to hinyb 1, etc.
-
-               for(int i=0; i<768; i++)
-               palette[i] = ((palette[i]<<4)&0xF0) | (palette[i]&0x0F);
-
                ff1.close();
                ff2.close();
        }
 
+       // PROM5 has the following in it (tile address decoder):
+       // 00:  00 20 40 60 02 22 42 62  04 24 44 64 06 26 46 66
+       // 10:  88 A8 C8 E8 8A AA CA EA  8C AC CC EC 8E AE CE EE 
+
        return ff1;
 }
 
@@ -1011,6 +1025,7 @@ extern bool disasm;       // From 'V6809.CPP'
        cout << "This emulator is free software. If you paid for it you were RIPPED OFF"
                << endl << endl;
 
+#if 0
        cout << "Initializing SDL..." << endl;
 
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
@@ -1018,18 +1033,26 @@ extern bool disasm;     // From 'V6809.CPP'
                cout << "Couldn't initialize SDL: " << SDL_GetError() << endl;
                return -1;
        }
+#endif
 
-       SDL_WM_SetCaption("Thunder v"THUNDER_VERSION" ", "Thunder");
+//     SDL_WM_SetCaption("Thunder v"THUNDER_VERSION" ", "Thunder");
 
-       keys = SDL_GetKeyState(NULL);                           // Get the SDL keyboard matrix
+//     keys = SDL_GetKeyState(NULL);                           // Get the SDL keyboard matrix
 
        gram = gram1;  grom = grom1;           // Needed only for debugger
 
+#if 0
        for(long i=0; i<0x10000; i++)
        {
                gram[i] = 0;  grom[i] = 0;           // Zero out memory
                gram2[i] = 0;  grom2[i] = 0;
        }
+#else
+       memset(gram, 0, 0x10000);
+       memset(grom, 0, 0x10000);
+       memset(gram2, 0, 0x10000);
+       memset(grom2, 0, 0x10000);
+#endif
 
        game_over_switch = 0;   // Init game over delay
 //  cpu1.a = 0; cpu1.b = 0; cpu1.cc = 0; cpu1.dp = 0; cpu1.x = 0; cpu1.y = 0; cpu1.s = 0; ur = 0; cpu1.pc = 0;
@@ -1269,13 +1292,18 @@ WriteLog("--> CPU clock #1: %u\n", cpu1.clock);
                        }
 
 WriteLog("About to set up screen...\n");
+#if 0
                        screen = SDL_SetVideoMode(VIRTUAL_SCREEN_WIDTH * 2, VIRTUAL_SCREEN_HEIGHT * 2, 8, SDL_SWSURFACE  | SDL_DOUBLEBUF);
                        if (screen == NULL)
                        {
                                cout << "Failed to initialize screen!" << endl;
                                running = false;
                        }
+#else
+                       InitVideo();
+#endif
 
+#if 0
                        SDL_Color colors[256];
 
                        for(int i=0; i<256; i++)
@@ -1286,28 +1314,17 @@ WriteLog("About to set up screen...\n");
                        }
 
                        SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL, colors, 0, 256);
-
-#if 0
-       // This confirms that we're getting video to the screen...
-       SDL_LockSurface(screen);
-
-       uint8_t pixel = 0;
-       uint8_t * pixels = (uint8_t *)(screen->pixels);
-
-       for(uint32_t y=0; y<480; y++)
-               for(uint32_t x=0; x<640; x++)
-                       pixels[(y * 640) + x] = pixel++;
-
-       SDL_UnlockSurface(screen);
-       SDL_UpdateRect(screen, 0, 0, 0, 0);
 #endif
 
+#if 0
                        for(int i=0; i<256; i++)
                                keys[i] = 0;                            // Clear keyboard buffer...
+#endif
 
                        oldTicks = SDL_GetTicks();
 
 WriteLog("About to set up audio...\n");
+#if 1
                        // This crap SHOULD be in sound.cpp (not yet created)...
                        SDL_AudioSpec desired, obtained;
                        desired.freq = 22050;
@@ -1324,17 +1341,23 @@ WriteLog("About to set up audio...\n");
                        }
 
                        SDL_PauseAudio(0);                                                      // Get that audio going!
+#endif
 
+memset(scrBuffer, 0xFF, VIRTUAL_SCREEN_WIDTH*VIRTUAL_SCREEN_HEIGHT*sizeof(uint32_t));
+RenderScreenBuffer();
 WriteLog("About to enter main loop...\n");
                        while (running)
                        {
                                HandleGUIDebounce();                                    // Debounce GUI keys
+
                                if (game_over_switch)
                                {
                                        game_over_switch--;  // Countdown...
+
                                        if (game_over_switch == 0)
                                                gram1[0x4380] = 0; // Kill music!
                                }
+
 //testing... (works)
 //gram1[0x423D] = 1;
                                //gram1[0x423D] = self_test;                    // Reset DSW1-1
@@ -1350,8 +1373,20 @@ WriteLog("About to enter main loop...\n");
 
                                // SDL key handling...
 
-                               SDL_PumpEvents();                               // Force key events into the buffer.
+//                             SDL_PumpEvents();                               // Force key events into the buffer.
+                               SDL_Event event;
+
+                               while (SDL_PollEvent(&event))
+                               {
+                                       switch (event.type)
+                                       {
+                                       case SDL_KEYDOWN:
+                                               if (event.key.keysym.sym == SDLK_ESCAPE)
+                                                       running = false;
+                                       }
+                               }
 
+#if 0
                                if (keys[SDLK_ESCAPE])
                                        running = false;                     // ESC to exit...
 
@@ -1562,7 +1597,7 @@ WriteLog("About to enter main loop...\n");
                WriteLog("\nMAIN: Stuffed $02 in $5FF3!!!\n\n");
        }
 #endif
-
+#endif
 
                                if (enable_cpu)
 //                             if (true)