- Main Page
- Classes
- Files

- File List
- File Members

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

Go to the documentation of this file.

    00001 #include "BallSprite.h"
    00002
    00003 #include "SDL.h"
    00004 #include "Visualisation.h"
    00005 #include <math.h>
    00006 #include "GameException.h"
    00007
    00008 #define PI 3.14159265f
    00009 #define DEGTORAD(deg) (deg * (PI / 180.0f))
    00010
    00011 BallSprite::BallSprite(const SpriteID &id, const int &radius, const ColourRGB &outerColour, const ColourRGB &centreColour)
    00012 : Sprite(id)
    00013 {
    00014         m_width = m_height = radius * 2;
    00015
    00016         m_numFrames = 1; //Only one frame for procedural balls so far, animation could be added later.
    00017         SDL_Surface* pScreen = Visualisation::getSingleton()->GetScreen();
    00018         m_pFrames = SDL_CreateRGBSurface(SDL_HWSURFACE, m_width, m_height, pScreen->format->BitsPerPixel, pScreen->format->Rmask, pScreen->format->Gmask, pScreen->format->Bmask, pScreen->format->Amask);
    00019
    00020         if(!m_pFrames)
    00021         {
    00022                 throw GameException(eMajorError, "BallSprite: Constructor failed due to failed SDL_CreateRGBSurface() call.");
    00023                 return;
    00024         }
    00025
    00026
    00027         if(SDL_MUSTLOCK(m_pFrames))
    00028                 SDL_LockSurface(m_pFrames);
    00029
    00030         //Add clearing entire surface to transparent colour key here
    00031         ColourRGB transColour = Visualisation::getSingleton()->GetTransparencyKey();
    00032         Uint32 key = SDL_MapRGB(m_pFrames->format, transColour.r, transColour.g, transColour.b);
    00033         SDL_SetColorKey(m_pFrames, SDL_SRCCOLORKEY, key);
    00034
    00035         for(size_t x = 0; x < m_width; x++)
    00036         {
    00037                 for (size_t y = 0; y < m_height; y++)
    00038                 {
    00039                         WritePixel(m_pFrames, x, y, transColour);
    00040                 }
    00041         }
    00042
    00043         ColourRGB colour;
    00044         float fRad = (float)radius;
    00045
    00046         for(int r = 0; r < radius; r++)
    00047         {
    00048                 fRad = static_cast<float>(r);
    00049                 for(float i = 0; i < 360; i += 0.5f)
    00050                 {
    00051                         float degInRad = DEGTORAD(i);
    00052
    00053                         //Radius here is centre x and centre y
    00054                         int x = radius - (int)(cos(degInRad) * fRad);
    00055                         int y = radius - (int)(sin(degInRad) * fRad);
    00056
    00057                         float fLerpVal = fRad / (float)radius;
    00058                         colour.r = outerColour.r + (unsigned char)((float)(centreColour.r - outerColour.r) * fLerpVal);
    00059                         colour.g = outerColour.g + (unsigned char)((float)(centreColour.g - outerColour.g) * fLerpVal);
    00060                         colour.b = outerColour.b + (unsigned char)((float)(centreColour.b - outerColour.b) * fLerpVal);
    00061
    00062                         WritePixel(m_pFrames, x, y, colour);
    00063                 }
    00064         }
    00065
    00066         if(SDL_MUSTLOCK(m_pFrames))
    00067                 SDL_UnlockSurface(m_pFrames);
    00068 }