* Loads an X-Com palette from a file. X-Com palettes are just a set * of RGB colors in a row, on a 0-63 scale, which have to be adjusted * for modern computers (0-255 scale). * @param filename Filename of the palette. * @param ncolors Number of colors in the palette. * @param offset Position of the palette in the file (in bytes). * @sa http://www.ufopaedia.org/index.php?title=PALETTES.DAT *
| 48 | * @sa http://www.ufopaedia.org/index.php?title=PALETTES.DAT |
| 49 | */ |
| 50 | void Palette::loadDat(const std::string &filename, int ncolors, int offset) |
| 51 | { |
| 52 | if(_colors != 0) |
| 53 | throw Exception("loadDat can be run only once"); |
| 54 | _count = ncolors; |
| 55 | _colors = new SDL_Color[_count]; |
| 56 | memset(_colors, 0, sizeof(SDL_Color) * _count); |
| 57 | |
| 58 | // Load file and put colors in pallete |
| 59 | std::ifstream palFile (filename.c_str(), std::ios::in | std::ios::binary); |
| 60 | if (!palFile) |
| 61 | { |
| 62 | throw Exception(filename + " not found"); |
| 63 | } |
| 64 | |
| 65 | // Move pointer to proper pallete |
| 66 | palFile.seekg(offset, std::ios::beg); |
| 67 | |
| 68 | Uint8 value[3]; |
| 69 | |
| 70 | for (int i = 0; i < _count && palFile.read((char*)value, 3); ++i) |
| 71 | { |
| 72 | // Correct X-Com colors to RGB colors |
| 73 | _colors[i].r = value[0] * 4; |
| 74 | _colors[i].g = value[1] * 4; |
| 75 | _colors[i].b = value[2] * 4; |
| 76 | _colors[i].unused = 255; |
| 77 | } |
| 78 | _colors[0].unused = 0; |
| 79 | |
| 80 | palFile.close(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Provides access to colors contained in the palette. |
no test coverage detected