1 /* Implementation of SpriteLoader by Mohammed Khoory */
5 SpriteLoader::SpriteLoader()
10 SpriteLoader::~SpriteLoader()
12 if(rawBitmapFile.is_open())
13 rawBitmapFile.close();
15 if(paletteFile.is_open())
19 void SpriteLoader::resetHeader()
26 // string filename must not have an extension!
27 void SpriteLoader::loadFile(const string& filename) throw(int, ios_base::failure)
29 if(rawBitmapFile.is_open() || paletteFile.is_open())
32 // open rawBitmapFile and paletteFile (don't do anything)
33 string rawFileName = filename + ".spr";
34 string paletteFileName = filename + ".pal";
35 rawBitmapFile.open(rawFileName.c_str(), ifstream::in);
36 if(!rawBitmapFile.is_open())
37 throw BadFileNameException;
38 paletteFile.open(paletteFileName.c_str(), ifstream::in);
39 if(!paletteFile.is_open())
40 throw BadFileNameException;
41 rawBitmapFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
42 paletteFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
44 // open and read the header
46 string headerFileName = filename + ".hdr";
47 headerFile.open(headerFileName.c_str(), ifstream::in );
49 if(!headerFile.is_open())
50 throw BadFileNameException;
52 headerFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
56 string widthString, heightString, sizeString;
57 getline(headerFile, widthString);
58 getline(headerFile, heightString);
59 getline(headerFile, sizeString);
60 width = atol(widthString.c_str());
61 height = atol(heightString.c_str());
62 size = atol(sizeString.c_str());
65 catch(ios_base::failure& e)
68 bool eof = headerFile.eof();
71 rawBitmapFile.close();
75 throw BadFileException;
82 void SpriteLoader::unloadFile()
84 if(paletteFile.is_open())
87 if(rawBitmapFile.is_open())
88 rawBitmapFile.close();
93 unsigned char * SpriteLoader::getBitmap() throw(int, bad_alloc, ios_base::failure)
95 if(!rawBitmapFile.is_open() || !paletteFile.is_open())
96 throw FileNotLoadedException;
98 unsigned char * bitmapData = new unsigned char[size];
102 rawBitmapFile.read((char *)bitmapData, size);
104 catch(ios_base::failure e)
107 if(rawBitmapFile.eof())
108 throw BadFileException;
116 struct sprite * SpriteLoader::getSprite(int x1, int y1, int x2, int y2) throw(int, bad_alloc, ios_base::failure)
118 if(!paletteFile.is_open() || !rawBitmapFile.is_open())
119 throw FileNotLoadedException;
121 // if x1 or y1 is negative set to 0, if x2 or y2 is negative set to the last index in row/column
122 x1 = x1 < 0 ? 0 : x1;
123 y1 = y1 < 0 ? 0 : y1;
124 x2 = x2 < 0 ? width-1 : x2;
125 y2 = y2 < 0 ? height-1 : y2;
127 // check if anything is out of bounds, set it to largest size
128 x1 = x1 > width-1 ? width-1 : x1;
129 x2 = x2 > width-1 ? width-1 : x2;
130 y1 = y1 > height-1 ? height-1 : y1;
131 y2 = y2 > height-1 ? height-1 : y2;
133 // if x1 > x2, switch, if y1 > y2, switch
147 struct sprite * theSprite = new struct sprite;
148 theSprite->width = x2 - x1 +1;
149 theSprite->height = y2 - y1 +1;
150 theSprite->bitmap = new unsigned char[theSprite->width * theSprite->height];
155 // go to initial offset (at (x1,y1))
156 unsigned long int initialOffset = x1 + width*y1;
157 for(int i = 0; i < y2 - y1; i++)
159 // i is considered to be the current "row" we're reading
160 rawBitmapFile.seekg(initialOffset + (i *width));
161 rawBitmapFile.read((char*)theSprite->bitmap + (i*theSprite->width), theSprite->width);
164 catch (ios_base::failure& e)
166 if(theSprite != NULL)
168 if(theSprite->bitmap != NULL)
169 delete[] theSprite->bitmap;
172 if(rawBitmapFile.eof())
173 throw BadFileException;
181 void SpriteLoader::setPalette(GraphicsDevice& graphics) throw (int, bad_alloc, ios_base::failure)
183 if(!paletteFile.is_open())
184 throw FileNotLoadedException;
186 char * palette = new char[768];
187 paletteFile.seekg(0);
188 paletteFile.read(palette, 768);
189 graphics.setPalette(palette, 768);
194 unsigned long int SpriteLoader::getWidth() throw(int)
196 if(!paletteFile.is_open() && !rawBitmapFile.is_open())
197 throw FileNotLoadedException;
202 unsigned long int SpriteLoader::getHeight() throw(int)
204 if(!paletteFile.is_open() && !rawBitmapFile.is_open())
205 throw FileNotLoadedException;
209 unsigned long int SpriteLoader::getSize() throw(int)
211 if(!paletteFile.is_open() && !rawBitmapFile.is_open())
212 throw FileNotLoadedException;
216 bool SpriteLoader::isLoaded()
218 return paletteFile.is_open() && rawBitmapFile.is_open();