D:/Development/Projects/X-Clone/X-Clone/trunk/Display.cpp

Go to the documentation of this file.
00001 #include "Display.h"
00002 #include "ConfigOptions.h"
00003 
00004 Display::Display() :
00005    _screen(NULL),
00006    _font(NULL)
00007 {
00008    // empty
00009 }
00010 
00011 void Display::init()
00012 {
00013    ConfigOptions &o = ConfigOptions::instance();
00014 
00015    _screenWidth = o.get<int>(WIDTH);
00016    _screenHeight = o.get<int>(HEIGHT);
00017 
00018    _screen = SDL_SetVideoMode(_screenWidth,
00019                               _screenHeight,
00020                               o.get<int>(COLOR_DEPTH),
00021                               SDL_HWSURFACE | SDL_DOUBLEBUF);
00022    SDL_WM_SetCaption("X-CLONE", NULL);
00023 }
00024 
00025 void Display::deinit()
00026 {
00027    if (_screen)
00028    {
00029       SDL_FreeSurface(_screen);
00030    }
00031 
00032    if (_font)
00033    {
00034       TTF_CloseFont(_font);
00035    }
00036 }
00037 
00038 int Display::getScreenWidth() const
00039 {
00040    return _screenWidth;
00041 }
00042 
00043 int Display::getScreenHeight() const
00044 {
00045    return _screenHeight;
00046 }
00047 
00048 SDL_Surface* Display::loadImage( string filename, bool useAlpha ) 
00049 {
00050    SDL_Surface* loadedImage = NULL;
00051    SDL_Surface* optimizedImage = NULL;
00052    //Load the image using SDL_image
00053    cout << filename << endl;
00054    loadedImage = IMG_Load( filename.c_str() );
00055 
00056    if(loadedImage != NULL)
00057    {
00058       //Create an optimized image
00059       if (useAlpha)
00060       {
00061          optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
00062       }
00063       else 
00064       {
00065          optimizedImage = SDL_DisplayFormat( loadedImage );
00066       }
00067       SDL_FreeSurface( loadedImage );
00068    }
00069    else
00070    {
00071       cout << SDL_GetError() << endl;
00072    }
00073    return optimizedImage;
00074     
00075    if(optimizedImage != NULL)
00076    {
00077       //Map the color key
00078       Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );          
00079       SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
00080    }
00081    else
00082    {
00083       cout << SDL_GetError() << endl;
00084    }
00085 
00086    return optimizedImage;
00087 }
00088 
00089 void Display::draw(int x, int y, string text)
00090 {
00091    if (_font)
00092    {
00093       SDL_Color color = {255, 255, 255};
00094       SDL_Surface* message = TTF_RenderText_Solid(_font, text.c_str(), color);
00095 
00096       if (message)
00097       {
00098          draw(x, y, message);
00099          SDL_FreeSurface(message);
00100       }
00101    }
00102 }
00103 
00104 void Display::draw(int x, int y, SDL_Surface* source)
00105 {
00106    //Rectangle to hold the offsets
00107    SDL_Rect offset;
00108    //Get offsets
00109    offset.x = x;
00110    offset.y = y;
00111    //Blit the surface
00112    SDL_BlitSurface( source, NULL, _screen, &offset );
00113 }
00114 
00115 void Display::drawRect(SDL_Rect* rect, SDL_Color* color)
00116 {
00117     SDL_FillRect( _screen, rect, SDL_MapRGB(_screen->format, color->r, color->g, color->b) );
00118 }
00119 
00120 void Display::render()
00121 {
00122    SDL_Flip(_screen);
00123    SDL_FillRect(_screen, NULL, 0);
00124 }
00125 
00126 bool Display::loadFont(string fileName)
00127 {
00128    _font = TTF_OpenFont(fileName.c_str(), 14);
00129    if (_font)
00130    {
00131       return true;
00132    }
00133 
00134    return false;
00135 }

Generated on Fri Sep 22 06:00:26 2006 for X-CLONE by  doxygen 1.4.7