sprload.cpp
author Mohammed Khoory
Wed Jul 07 19:11:38 2010 -0400 (22 months ago)
changeset 5 bcf3d6f89c62
parent 4 6366beccd211
permissions -rw-r--r--
Keyboard support, code movement, and TestManager

Major Changes
=============
- Added "testman.h" and "testman.cpp" to be used exclusively to test stuff (from a
GameManager perspective)
- Moved all the testing stuff to testman.cpp. 9game is just an empty class now to
manage other GameManagers
- Moved all the initializing stuff from 9Game to main. 9Game is like any other
GameManager now (just that it manages other GameManagers)
- Added keyboard.h and keyboard.c to handle keyboard interrupts (by replacing the
keyboard ISR)
- Keyboard is working :D can read more than one key at a time. (tested in
TestManager with WASD)

Minor Changes
=============
- Removed the exception throwing in SpriteLoader::unloadFile() (because it just
makes things unnecessarily complicated)
- added finished() to GameManager to indicate whether a GameManager is finished or
not
- Animation test + key control test. Sprite moves around depending on wasd key
input. (program ends when off screen to right)
- Set 0x00 as transparency color with drawSprite (TODO: change to make this
customizeable on runtime)
- Edited Makefile to reflect new changes

Notes
=====
- Animation seems to work nice and smooth in dosbox, slow in dosemu, so maybe I
should start testing in both dosemu and dosbox
- TODO: fix bug with drawSprite. When a sprite is drawn on the right of the screen
it "wraps around" to the other side by one pixel in width
     1 /* Implementation of SpriteLoader by Mohammed Khoory */
     2 
     3 #include "sprload.h"
     4 
     5 SpriteLoader::SpriteLoader()
     6 {
     7 	resetHeader();
     8 }
     9 
    10 SpriteLoader::~SpriteLoader()
    11 {
    12 	if(rawBitmapFile.is_open())
    13 		rawBitmapFile.close();
    14 	
    15 	if(paletteFile.is_open())
    16 		paletteFile.close();
    17 }
    18 
    19 void SpriteLoader::resetHeader()
    20 {
    21 	width = 0;
    22 	height = 0;
    23 	size = 0;
    24 }
    25 
    26 // string filename must not have an extension!
    27 void SpriteLoader::loadFile(const string& filename) throw(int, ios_base::failure)
    28 {
    29 	if(rawBitmapFile.is_open() || paletteFile.is_open())
    30 		this->unloadFile();
    31 	
    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);
    43 	
    44 	// open and read the header 
    45 	ifstream headerFile;
    46 	string headerFileName = filename + ".hdr";
    47 	headerFile.open(headerFileName.c_str(), ifstream::in );
    48 	
    49 	if(!headerFile.is_open())
    50 		throw BadFileNameException;
    51 	
    52 	headerFile.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
    53 	
    54 	try
    55 	{
    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());
    63 		headerFile.close();
    64 	}
    65 	catch(ios_base::failure& e)
    66 	{
    67 		resetHeader();
    68 		bool eof = headerFile.eof();
    69 
    70 		headerFile.close();
    71 		rawBitmapFile.close();
    72 		paletteFile.close();
    73 		
    74 		if(eof)
    75 			throw BadFileException;
    76 		else
    77 			throw e;
    78 	}
    79 	
    80 }
    81 
    82 void SpriteLoader::unloadFile()
    83 {
    84 	if(paletteFile.is_open())
    85 		paletteFile.close();
    86 	
    87 	if(rawBitmapFile.is_open())
    88 		rawBitmapFile.close();
    89 	
    90 	resetHeader();
    91 }
    92 
    93 unsigned char * SpriteLoader::getBitmap() throw(int, bad_alloc, ios_base::failure)
    94 {
    95 	if(!rawBitmapFile.is_open() || !paletteFile.is_open())
    96 		throw FileNotLoadedException;
    97 	
    98 	unsigned char * bitmapData = new unsigned char[size];
    99 
   100 	try
   101 	{
   102 		rawBitmapFile.read((char *)bitmapData, size);
   103 	}
   104 	catch(ios_base::failure e)
   105 	{
   106 		delete[] bitmapData;
   107 		if(rawBitmapFile.eof())
   108 			throw BadFileException;
   109 		else
   110 			throw e;
   111 	}	
   112 	
   113 	return bitmapData;
   114 }
   115 
   116 struct sprite * SpriteLoader::getSprite(int x1, int y1, int x2, int y2) throw(int, bad_alloc, ios_base::failure)
   117 {
   118 	if(!paletteFile.is_open() || !rawBitmapFile.is_open())
   119 		throw FileNotLoadedException;
   120 	
   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;
   126 	
   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;
   132 	
   133 	// if x1 > x2, switch, if y1 > y2, switch
   134 	if( x1 > x2)
   135 	{
   136 		int temp = x1;
   137 		x1 = x2;
   138 		x2 = temp;
   139 	}
   140 	if( y1 > y2)
   141 	{
   142 		int temp = y1;
   143 		y1 = y2;
   144 		y2 = temp;
   145 	}
   146 	
   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];
   151 	
   152 	// begin reading
   153 	try
   154 	{
   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++)
   158 		{
   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);
   162 		}
   163 	}
   164 	catch (ios_base::failure& e)
   165 	{
   166 		if(theSprite != NULL)
   167 		{
   168 			if(theSprite->bitmap != NULL)
   169 				delete[] theSprite->bitmap;
   170 			delete theSprite;
   171 		}
   172 		if(rawBitmapFile.eof())
   173 			throw BadFileException;
   174 		else
   175 			throw e;
   176 	}
   177 	
   178 	return theSprite;
   179 }
   180 
   181 void SpriteLoader::setPalette(GraphicsDevice& graphics) throw (int, bad_alloc, ios_base::failure)
   182 {
   183 	if(!paletteFile.is_open())
   184 		throw FileNotLoadedException;
   185 	
   186 	char * palette = new char[768];
   187 	paletteFile.seekg(0);
   188 	paletteFile.read(palette, 768);
   189 	graphics.setPalette(palette, 768);
   190 	delete[] palette;
   191 	
   192 }
   193 
   194 unsigned long int SpriteLoader::getWidth() throw(int)
   195 {
   196 	if(!paletteFile.is_open() && !rawBitmapFile.is_open())
   197 		throw FileNotLoadedException;
   198 		
   199 	return width;
   200 }
   201 
   202 unsigned long int SpriteLoader::getHeight() throw(int)
   203 {
   204 	if(!paletteFile.is_open() && !rawBitmapFile.is_open())
   205 		throw FileNotLoadedException;	
   206 	return height;
   207 }
   208 
   209 unsigned long int SpriteLoader::getSize() throw(int)
   210 {
   211 	if(!paletteFile.is_open() && !rawBitmapFile.is_open())
   212 		throw FileNotLoadedException;	
   213 	return size;
   214 }
   215 
   216 bool SpriteLoader::isLoaded()
   217 {
   218 	return paletteFile.is_open() && rawBitmapFile.is_open();
   219 }
   220