#include "Background.h"
#include "Screen.h"
#include "glfuncs.h"
#include "Level.h"

/**Backgrounds don't register themselves in SPRITES, thus they need to have a destructor that removes them
   from texture memory, see below*/
Background::Background(std::string filename, double depth)
:Sprite( filename ),
 _depth( depth )
{
}

Background::Background(std::string filename, int width, int height, double depth)
:Sprite( filename, width, height ),
 _depth( depth )
{
}

void Background::draw()
{
	SDL_Rect screen = Screen::instance()->getScreenRect();
	int height = Level::instance()->getHeight();
	int width = Level::instance()->getWidth();

	GLfloat texcoords[4];

	texcoords[ 0 ] = ((GLfloat)screen.x/width)*_coords[2]*_depth;
	texcoords[ 2 ] = texcoords[ 0 ] + ((GLfloat)screen.w/width)*_coords[2];
	texcoords[ 1 ] = ((GLfloat)screen.y/height)*_coords[3]*_depth;
	texcoords[ 3 ] = texcoords[ 1 ] + ((GLfloat)screen.h/height)*_coords[3];

	glfuncs::instance()->glBindTexture(GL_TEXTURE_2D, _hTexture);
	glfuncs::instance()->glBegin(GL_QUADS);
	glfuncs::instance()->glTexCoord2f(texcoords[0],texcoords[1]);	glfuncs::instance()->glVertex2f(0,0);
	glfuncs::instance()->glTexCoord2f(texcoords[2],texcoords[1]);	glfuncs::instance()->glVertex2f(screen.w,0);
	glfuncs::instance()->glTexCoord2f(texcoords[2],texcoords[3]);	glfuncs::instance()->glVertex2f(screen.w,screen.h);
	glfuncs::instance()->glTexCoord2f(texcoords[0],texcoords[3]);	glfuncs::instance()->glVertex2f(0,screen.h);
	glfuncs::instance()->glEnd();
}

Background::~Background()
{
	glfuncs::instance()->glDeleteTextures(1,&_hTexture);
}