#include "BackgroundSprite.h"

#include "SDL.h"
#include "Visualisation.h"

#include "GameException.h"

BackgroundSprite::BackgroundSprite(const SpriteID &id, const unsigned int &w, const unsigned int &h, const unsigned int &numStars)
: Sprite(id)
{
	m_numFrames = 1; //Only one frame for procedural boxes so far, animation could be added later.
	SDL_Surface* pScreen = Visualisation::getSingleton()->GetScreen();
	m_pFrames = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, pScreen->format->BitsPerPixel, pScreen->format->Rmask, pScreen->format->Gmask, pScreen->format->Bmask, pScreen->format->Amask);

	if(!m_pFrames)
	{
		throw GameException(eMajorError, "BackgroundSprite: Constructor failed due to failed SDL_CreateRGBSurface() call.");
		return;
	}

	if(SDL_MUSTLOCK(m_pFrames))
		SDL_LockSurface(m_pFrames);

	for(size_t x = 0; x < w; x++)
	{
		for(size_t y = 0; y < h; y++)
		{
			WritePixel(m_pFrames, x, y, ColourRGB(1,1,1));
		}
	}

	for(size_t i = 0; i < numStars; i++)
	{
		int x = rand() % w;
		int y = rand() % h;
		WritePixel(m_pFrames, x, y, ColourRGB(255,255,255));
	}

	if(SDL_MUSTLOCK(m_pFrames))
		SDL_UnlockSurface(m_pFrames);
}
