#include "titlescreen.h"
#include "drawutils.h"
#include "game.h"
#include "debug.h"

TitleScreen::TitleScreen(stack<GameState*>* state_stack,
			 SDL_Surface* display)
  : GameState(state_stack, display)
{
    background = NULL;
    title_banner = NULL;
}


TitleScreen::~TitleScreen()
{
    if (background != NULL)
	SDL_FreeSurface(background);

    if (background != NULL)
	SDL_FreeSurface(title_banner);
}


bool TitleScreen::init()
{
    background = DrawUtils::load("background.bmp");

    if (background == NULL)
    {
	debug("TitleScreen::init(): error: Couldn't load background image");
	return false;
    }

    title_banner = DrawUtils::load("title_banner.bmp");

    if (title_banner == NULL)
    {
	debug("TitleScreen::init(): error: Couldn't load title banner");
	return false;	
    }

    DrawUtils::transpare(title_banner);

    return GameState::init();
}


void TitleScreen::render()
{
    DrawUtils::draw(display, background, 0, 0);

    int x = display->w / 2 - (title_banner->w / 2);
    int y = display->h / 2 - (title_banner->h / 2);

    DrawUtils::draw(display, title_banner, x, y);

    SDL_Flip(display);
}


void TitleScreen::on_lbutton_down(int x, int y)
{
    state_stack->push(new Game(state_stack, display));
}


void TitleScreen::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
{
    if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit();
}