Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <fstream>
- using namespace std;
- SDL_Surface *loadScr(string filename)
- {
- SDL_Surface *surface;
- // Create surface
- surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 200, 8, 0, 0, 0, 0);
- if (surface == NULL)
- {
- throw SDL_GetError();
- }
- // Lock the surface
- SDL_LockSurface(surface);
- Uint8 *index = (Uint8 *)surface->pixels;
- // Load file and put pixels in surface
- ifstream imgFile (filename.c_str(), ios::in | ios::binary);
- if (!imgFile)
- {
- throw "Failed to load SCR";
- }
- char value;
- while (imgFile.read(&value, 1))
- {
- *index = Uint8(value);
- index++;
- }
- if (!imgFile.eof())
- {
- throw "Invalid data from file";
- }
- // Unlock the surface
- SDL_UnlockSurface(surface);
- imgFile.close();
- return surface;
- }
- SDL_Color *loadPalette(string filename, int ncolors, int offset)
- {
- SDL_Color *colors = (SDL_Color *)malloc(sizeof(SDL_Color) * ncolors);
- // Load file and put colors in pallete
- ifstream palFile (filename.c_str(), ios::in | ios::binary);
- if (!palFile)
- {
- throw "Failed to load palette";
- }
- // Move pointer to proper pallete
- palFile.seekg(offset, ios::beg);
- char value[3];
- for (int j = 0; j < ncolors && palFile.read(value, 3); j++)
- {
- // Correct X-Com colors to RGB colors
- colors[j].r = Uint8(value[0])*4;
- colors[j].g = Uint8(value[1])*4;
- colors[j].b = Uint8(value[2])*4;
- }
- /*
- if (!palFile.eof())
- {
- throw "Invalid data from file";
- }
- */
- palFile.close();
- return colors;
- }
- SDL_Surface *testSurface()
- {
- SDL_Surface *surface;
- // Create surface
- surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 256, 25, 8, 0, 0, 0, 0);
- if (surface == NULL)
- {
- throw SDL_GetError();
- }
- // Lock the surface
- SDL_LockSurface(surface);
- Uint8 *index = (Uint8 *)surface->pixels;
- for (int j = 0; j < 25; j++)
- for (int i = 0; i < 256; i++, index++)
- *index = i;
- // Unlock the surface
- SDL_UnlockSurface(surface);
- return surface;
- }
- int main(int argc, char** args)
- {
- // Initialize SDL
- if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
- {
- throw SDL_GetError();
- }
- // Set the window caption
- SDL_WM_SetCaption("X-Com Open Source", NULL);
- // Create display
- SDL_Surface *screen = SDL_SetVideoMode(320, 200, 8, SDL_DOUBLEBUF|SDL_HWPALETTE);
- // Create test surface
- SDL_Surface *surface = loadScr("GEODATA\\BACK01.SCR");
- // Apply palette
- SDL_Color *palette = loadPal("GEODATA\\PALETTES.DAT", 256, 0);
- SDL_SetColors(screen, palette, 0, 256);
- SDL_SetColors(surface, palette, 0, 256);
- // Game loop
- bool quit = false;
- SDL_Event event;
- while (quit)
- {
- // Process events
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_QUIT)
- {
- quit = true;
- }
- }
- // Process rendering
- SDL_BlitSurface(surface, NULL, screen, NULL);
- if (SDL_Flip(screen) == -1)
- {
- throw SDL_GetError();
- }
- }
- // RIP
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement