- Main Page
- Classes
- Files

- File List
- File Members

# C:/Users/Public/AGS Final Folder/BFLETCHER D4010283/Source/Game/Source/Visualisation.cpp

Go to the documentation of this file.

    00001 #include "Visualisation.h"
    00002
    00003 #include "Sprite.h"
    00004 #include "BoxSprite.h"
    00005 #include "BallSprite.h"
    00006 #include "BackgroundSprite.h"
    00007 #include "TextSprite.h"
    00008
    00009 #include "Colour.h"
    00010 #include "DebugUtil.h"
    00011
    00012 #include "SDL.h"
    00013
    00014 #include "GameException.h"
    00015
    00016 template<> Visualisation* TSingleton<Visualisation>::m_pInstance = NULL;
    00017
    00018 Visualisation::Visualisation()
    00019 : m_pScreen(0), m_lastUniqueID(0), m_clearBG(0)
    00020 {
    00021         m_transColourKey.r = 0;
    00022         m_transColourKey.g = 255;
    00023         m_transColourKey.b = 0;
    00024 }
    00025
    00026 Visualisation::~Visualisation()
    00027 {
    00028 }
    00029
    00030 void Visualisation::Initialise(const int &w, const int &h, const int &bpp)
    00031 {
    00032         m_pScreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE);
    00033
    00034         if(!m_pScreen)
    00035         {
    00036                 throw GameException(eFatalError, "Visualisation: SDL_SetVideoMode() failed.");
    00037                 return;
    00038         }
    00039
    00040         SDL_SetCursor(0);
    00041         SDL_ShowCursor(SDL_DISABLE);
    00042         Uint32 key = SDL_MapRGB(m_pScreen->format, m_transColourKey.r, m_transColourKey.g, m_transColourKey.b);
    00043         SDL_SetColorKey(m_pScreen, SDL_SRCCOLORKEY, key);
    00044
    00045         m_clearBG = CreateSpriteBox(w, h, ColourRGB(0,0,0), ColourRGB(0,0,0));
    00046 }
    00047
    00048 void Visualisation::Shutdown()
    00049 {
    00050         for(size_t i = 0; i < m_vSprites.size(); i++)
    00051         {
    00052                 if(m_vSprites[i])
    00053                         delete m_vSprites[i];
    00054         }
    00055
    00056         m_vSprites.clear();
    00057 }
    00058
    00059 SpriteID Visualisation::CreateSpriteBall(const int &radius, const ColourRGB &centreColour, const ColourRGB &outerColour)
    00060 {
    00061         BallSprite* pSprite = new BallSprite(++m_lastUniqueID, radius, centreColour, outerColour);
    00062         m_vSprites.push_back(pSprite);
    00063
    00064         return m_lastUniqueID;
    00065 }
    00066
    00067 SpriteID Visualisation::CreateSpriteBox(const unsigned int &w, const unsigned int &h, const ColourRGB &borderColour, const ColourRGB &fillColour, unsigned int borderSize)
    00068 {
    00069         BoxSprite* pSprite = new BoxSprite(++m_lastUniqueID, w, h, borderColour, fillColour, borderSize);
    00070         m_vSprites.push_back(pSprite);
    00071
    00072         return m_lastUniqueID;
    00073 }
    00074
    00075 SpriteID Visualisation::CreateSpriteFromFile(char *filename)
    00076 {
    00077         Sprite* pSprite = new Sprite(++m_lastUniqueID);
    00078         pSprite->InitFromFile(filename);
    00079
    00080         m_vSprites.push_back(pSprite);
    00081
    00082         return m_lastUniqueID;
    00083 }
    00084
    00085 SpriteID Visualisation::CreateSpriteStarryBackground(const unsigned int &w, const unsigned int &h, const unsigned int &numStars)
    00086 {
    00087         BackgroundSprite* pSprite = new BackgroundSprite(++m_lastUniqueID, w, h, numStars);
    00088         DebugUtil::outputDebugText("Visualisation: Background sprite created.\n");
    00089         m_vSprites.push_back(pSprite);
    00090
    00091         return m_lastUniqueID;
    00092 }
    00093
    00094 SpriteID Visualisation::CreateSpriteText(const std::string &message, const ColourRGB &colour)
    00095 {
    00096         TextSprite* pSprite = new TextSprite(++m_lastUniqueID, message, colour);
    00097         m_vSprites.push_back(pSprite);
    00098
    00099         return m_lastUniqueID;
    00100 }
    00101
    00102 void Visualisation::RemoveSprite(const SpriteID &id)
    00103 {
    00104         std::vector<Sprite*>::iterator it = m_vSprites.begin();
    00105         while(it != m_vSprites.end())
    00106         {
    00107                 if((*it)->GetID() == id)
    00108                 {
    00109                         (*it)->Shutdown();
    00110                         delete (*it);
    00111                         it = m_vSprites.erase(it);
    00112                 }
    00113                 else
    00114                         it++;
    00115         }
    00116
    00117         throw GameException(eMinorError, "Visualisation: RemoveSprite() failed due to incorrect ID: %d", (int)id);
    00118 }
    00119
    00120 void Visualisation::DrawSprite(SpriteID id, const unsigned int &x, const unsigned int &y, unsigned int frame)
    00121 {
    00122         std::vector<Sprite*>::iterator it = m_vSprites.begin();
    00123         while(it != m_vSprites.end())
    00124         {
    00125                 if((*it)->GetID() == id)
    00126                 {
    00127                         (*it)->Draw(x, y, frame);
    00128                         return;
    00129                 }
    00130
    00131                 it++;
    00132         }
    00133
    00134         throw GameException(eMinorError, "Visualisation: DrawSprite() failed due to incorrect ID: %d", (int)id);
    00135 }
    00136
    00137 void Visualisation::ClearScreen()
    00138 {
    00139         DrawSprite(m_clearBG, 0, 0);
    00140 }
    00141
    00142 unsigned int Visualisation::GetSpriteWidth(const SpriteID &id)
    00143 {
    00144         std::vector<Sprite*>::iterator it = m_vSprites.begin();
    00145         while(it != m_vSprites.end())
    00146         {
    00147                 if((*it)->GetID() == id)
    00148                         return (*it)->GetWidth();
    00149
    00150                 it++;
    00151         }
    00152
    00153         throw GameException(eMinorError, "Visualisation: GetSpriteWidth() failed due to incorrect ID: %d", (int)id);
    00154         return 0;
    00155 }
    00156
    00157 unsigned int Visualisation::GetSpriteHeight(const SpriteID &id)
    00158 {
    00159         std::vector<Sprite*>::iterator it = m_vSprites.begin();
    00160         while(it != m_vSprites.end())
    00161         {
    00162                 if((*it)->GetID() == id)
    00163                         return (*it)->GetHeight();
    00164
    00165                 it++;
    00166         }
    00167
    00168         throw GameException(eMinorError, "Visualisation: GetSpriteHeight() failed due to incorrect ID: %d", (int)id);
    00169         return 0;
    00170 }