Guest User

OpenXcom inception

a guest
Sep 3rd, 2014
324
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SDL.h>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. SDL_Surface *loadScr(string filename)
  7. {
  8.     SDL_Surface *surface;
  9.  
  10.     // Create surface
  11.     surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 200, 8, 0, 0, 0, 0);
  12.    
  13.     if (surface == NULL)
  14.     {
  15.         throw SDL_GetError();
  16.     }
  17.    
  18.     // Lock the surface
  19.     SDL_LockSurface(surface);
  20.  
  21.     Uint8 *index = (Uint8 *)surface->pixels;
  22.    
  23.     // Load file and put pixels in surface
  24.     ifstream imgFile (filename.c_str(), ios::in | ios::binary);
  25.     if (!imgFile)
  26.     {
  27.         throw "Failed to load SCR";
  28.     }
  29.    
  30.     char value;
  31.  
  32.     while (imgFile.read(&value, 1))
  33.     {
  34.         *index = Uint8(value);
  35.         index++;
  36.     }
  37.  
  38.     if (!imgFile.eof())
  39.     {
  40.         throw "Invalid data from file";
  41.     }
  42.  
  43.     // Unlock the surface
  44.     SDL_UnlockSurface(surface);
  45.  
  46.     imgFile.close();
  47.    
  48.     return surface;
  49. }
  50.  
  51. SDL_Color *loadPalette(string filename, int ncolors, int offset)
  52. {
  53.     SDL_Color *colors = (SDL_Color *)malloc(sizeof(SDL_Color) * ncolors);
  54.  
  55.     // Load file and put colors in pallete
  56.     ifstream palFile (filename.c_str(), ios::in | ios::binary);
  57.     if (!palFile)
  58.     {
  59.         throw "Failed to load palette";
  60.     }
  61.  
  62.     // Move pointer to proper pallete
  63.     palFile.seekg(offset, ios::beg);
  64.    
  65.     char value[3];
  66.  
  67.     for (int j = 0; j < ncolors && palFile.read(value, 3); j++)
  68.     {
  69.         // Correct X-Com colors to RGB colors
  70.         colors[j].r = Uint8(value[0])*4;
  71.         colors[j].g = Uint8(value[1])*4;
  72.         colors[j].b = Uint8(value[2])*4;
  73.     }
  74.  
  75.     /*
  76.     if (!palFile.eof())
  77.     {
  78.         throw "Invalid data from file";
  79.     }
  80.     */
  81.  
  82.     palFile.close();
  83.    
  84.     return colors;
  85. }
  86.  
  87. SDL_Surface *testSurface()
  88. {
  89.     SDL_Surface *surface;
  90.  
  91.     // Create surface
  92.     surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 256, 25, 8, 0, 0, 0, 0);
  93.    
  94.     if (surface == NULL)
  95.     {
  96.         throw SDL_GetError();
  97.     }
  98.  
  99.     // Lock the surface
  100.     SDL_LockSurface(surface);
  101.  
  102.     Uint8 *index = (Uint8 *)surface->pixels;
  103.    
  104.     for (int j = 0; j < 25; j++)
  105.         for (int i = 0; i < 256; i++, index++)
  106.             *index = i;
  107.  
  108.     // Unlock the surface
  109.     SDL_UnlockSurface(surface);
  110.  
  111.     return surface;
  112. }
  113.  
  114. int main(int argc, char** args)
  115. {
  116.     // Initialize SDL
  117.     if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
  118.     {
  119.         throw SDL_GetError();
  120.     }
  121.  
  122.     // Set the window caption
  123.     SDL_WM_SetCaption("X-Com Open Source", NULL);
  124.  
  125.     // Create display
  126.     SDL_Surface *screen = SDL_SetVideoMode(320, 200, 8, SDL_DOUBLEBUF|SDL_HWPALETTE);
  127.    
  128.     // Create test surface
  129.     SDL_Surface *surface = loadScr("GEODATA\\BACK01.SCR");
  130.    
  131.     // Apply palette
  132.     SDL_Color *palette = loadPal("GEODATA\\PALETTES.DAT", 256, 0);
  133.     SDL_SetColors(screen, palette, 0, 256);
  134.     SDL_SetColors(surface, palette, 0, 256);
  135.    
  136.     // Game loop   
  137.     bool quit = false;
  138.     SDL_Event event;
  139.     while (quit)
  140.     {      
  141.         // Process events
  142.         while (SDL_PollEvent(&event))
  143.         {
  144.             if (event.type == SDL_QUIT)
  145.             {
  146.                 quit = true;
  147.             }
  148.         }
  149.        
  150.         // Process rendering
  151.         SDL_BlitSurface(surface, NULL, screen, NULL);      
  152.         if (SDL_Flip(screen) == -1)
  153.         {
  154.             throw SDL_GetError();
  155.         }
  156.     }
  157.    
  158.     // RIP
  159.     SDL_Quit();
  160.  
  161.     return 0;
  162. }
RAW Paste Data