- Main Page
- Classes
- Files

- File List
- File Members

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

Go to the documentation of this file.

    00001 #include "Sprite.h"
    00002
    00003 #include "SDL.h"
    00004 #include "Visualisation.h"
    00005
    00006 #include <math.h> //Replace with fixed
    00007
    00008 #define PI 3.14159265
    00009 #define DEGTORAD(deg) (deg * (PI / 180))
    00010
    00011 Sprite::Sprite(const SpriteID &id)
    00012 : m_ID(id), m_pFrames(0), m_numFrames(0), m_width(0), m_height(0)
    00013 {
    00014 }
    00015
    00016 Sprite::~Sprite()
    00017 {
    00018         Shutdown();
    00019 }
    00020
    00021 void Sprite::InitFromFile(char* filename)
    00022 {
    00023
    00024 }
    00025
    00026 void Sprite::Draw(const unsigned int &x, const unsigned int &y, unsigned int frame)
    00027 {
    00028         SDL_Rect dstRect;
    00029         dstRect.x = x; dstRect.y = y;
    00030         SDL_BlitSurface(&m_pFrames[frame], 0, Visualisation::getSingleton()->GetScreen(), &dstRect);
    00031 }
    00032
    00033 void Sprite::Shutdown()
    00034 {
    00035         for(size_t i = 0; i < m_numFrames; i++)
    00036                 SDL_FreeSurface(&m_pFrames[i]);
    00037 }
    00038
    00039 void Sprite::WritePixel(SDL_Surface* pSurface, int x, int y, const ColourRGB &colour)
    00040 {
    00041         Uint32 mapColour = SDL_MapRGB(pSurface->format, colour.r, colour.g, colour.b);
    00042         switch (pSurface->format->BytesPerPixel)
    00043         {
    00044                 case 1: // Assuming 8-bpp
    00045                         Uint8 *p8Buffer;
    00046                         p8Buffer = (Uint8 *)pSurface->pixels + y * pSurface->pitch + x;
    00047                         *p8Buffer = mapColour;
    00048                         break;
    00049
    00050                 case 2: // Probably 15-bpp or 16-bpp
    00051                         Uint16 *p16Buffer;
    00052                         p16Buffer = (Uint16 *)pSurface->pixels + y * (pSurface->pitch / 2) + x;
    00053                         *p16Buffer = mapColour;
    00054                         break;
    00055
    00056                 case 3: // Slow 24-bpp mode, usually not used
    00057                         Uint8 *p24Buffer;
    00058                         p24Buffer = (Uint8 *)pSurface->pixels + y * pSurface->pitch + x * 3;
    00059                         if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
    00060                         {
    00061                                 p24Buffer[0] = mapColour;
    00062                                 p24Buffer[1] = mapColour >> 8;
    00063                                 p24Buffer[2] = mapColour >> 16;
    00064                         }
    00065                         else
    00066                         {
    00067                                 p24Buffer[2] = mapColour;
    00068                                 p24Buffer[1] = mapColour >> 8;
    00069                                 p24Buffer[0] = mapColour >> 16;
    00070                         }
    00071                         break;
    00072
    00073                 case 4: // Probably 32-bpp
    00074                         Uint32 *p32Buffer;
    00075                         p32Buffer = (Uint32 *)pSurface->pixels + y * (pSurface->pitch / 4) + x;
    00076                         *p32Buffer = mapColour;
    00077                         break;
    00078         }
    00079 }