Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

CSBitmap.cpp

Go to the documentation of this file.
00001 #ifdef WIN32
00002 #pragma warning(disable : 4786 )
00003 #endif
00004 #include "CSBitmap.h"
00005 #include "SDLMain.h"
00006 #include "CSXMLHelper.h"
00007 
00008 using namespace std;
00009 
00010 CSBitmapLoader CSBitmapLoader::INSTANCE;
00011 CSBitmapDirectLoader CSBitmapDirectLoader::INSTANCE;
00012 
00013 CSBitmap *CSBitmapLoader::create(const std::string &filename)
00014 {
00015     CSXMLHelper xmlSupport(filename, "BITMAP");
00016     char *id;
00017     char *bitmapFilename;
00018     signed int r,g,b;
00019     try
00020     {
00021         if (xmlSupport.getError())
00022         {
00023             throw "error";
00024         }
00025 
00026         id = strdup(xmlSupport.getString("ID").c_str());
00027         bitmapFilename = strdup(xmlSupport.getString("NAME").c_str());
00028         r = xmlSupport.getInt("TRANSPARENT_COLOR/RED");
00029         g = xmlSupport.getInt("TRANSPARENT_COLOR/GREEN");
00030         b = xmlSupport.getInt("TRANSPARENT_COLOR/BLUE");
00031     }
00032     catch(...)
00033     {
00034         LOG_EXIT
00035         SDLMain::shutdown((std::string)"XML error \"" + filename + "\": " + xmlSupport.getErrorMessage().c_str(), 1);
00036     }
00037 
00038     SDL_Surface *image = 0;
00039 
00040     /* Load the BMP file into a surface */
00041     image = SDL_LoadBMP(bitmapFilename);
00042 
00043     if (image == 0) 
00044     {
00045         LOG_EXIT
00046         SDLMain::shutdown((std::string)"Couldn't load \"" + bitmapFilename + "\": " + SDL_GetError(), 1);
00047     }
00048 
00049     // free loaded parse entities!
00050     free (bitmapFilename);
00051     bitmapFilename = 0;
00052     free (id);
00053     id = 0;
00054 
00055     CSBitmap *bitmap = new CSBitmap();
00056     bitmap->r = r;
00057     bitmap->g = g;
00058     bitmap->b = b;
00059     bitmap->picture = image;
00060     return bitmap;
00061 }
00062 
00063 void CSBitmapLoader::destroy(CSBitmap *image)
00064 {
00065     SDLMain::freeSurface(image->picture);
00066     delete image;
00067 }
00068 
00069 
00070 typedef std::map<std::string, CSBitmap *> BitmapMap;
00071 
00072 void CSBitmapLoader::scale(char *name, int width, int divideX, int height, int divideY)
00073 {
00074     BitmapMap *map = CSBitmapLoader::INSTANCE.getMap();
00075     BitmapMap::iterator iter = map->find(name);
00076     if (iter == map->end())
00077     {
00078         return;
00079     }
00080     CSBitmap *bitmap = iter->second;
00081     SDL_Surface *surface = bitmap->picture;
00082     bitmap->picture = SDLMain::getInstance()->scale(surface, width, divideX, height, divideY);
00083     SDLMain::freeSurface(surface);
00084 }
00085 
00086 CSBitmap *CSBitmapDirectLoader::create(const std::string &filename)
00087 {
00088     SDL_Surface *image = 0;
00089 
00090     /* Load the BMP file into a surface */
00091     image = SDL_LoadBMP(filename.c_str());
00092 
00093     if (image == 0) 
00094     {
00095         LOG_EXIT
00096         SDLMain::shutdown((std::string)"Couldn't load \"" + filename + "\": " + SDL_GetError(), 1);
00097     }
00098 
00099     // free loaded parse entities!
00100     CSBitmap *bitmap = new CSBitmap();
00101     bitmap->r = 255;
00102     bitmap->g = 0;
00103     bitmap->b = 255;
00104     bitmap->picture = image;
00105     return bitmap;
00106 }

Generated on Wed Jul 14 00:43:28 2004 for CSLib by doxygen 1.3.6