/*
	Wheels Of Steel
	Copyright (c) 2006, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   Background
// Purpose:      Background generation, cloud movement, drawing.. etc...
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "Background.h"

//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------

// rate at which clouds move

const float Background::CLOUDS_SPEED = 19.5f;

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::Background
Purpose:  Background constructor, loads relevant bitmaps, initialises the clouds
Params:   Game window
Returns:  n/a

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

Background::Background(Window & window)
{
    // Load the sky and cloud bitmaps:

    bmpSky      = window.loadBitmap("gfx\\bgr\\sky.bmp",false);
    bmpCloud    = window.loadBitmap("gfx\\bgr\\cloud.bmp",true);

    // Create the clouds

    cloudsInit();
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::~Background
Purpose:  Background destructor, frees images
Params:   n/a
Returns:  n/a

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

Background::~Background()
{
    // Free up the images used

    delete bmpSky;
    delete bmpCloud;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::cloudsInit
Purpose:  Initialises the cloud positions for the background
Params:   none
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Background::cloudsInit()
{
    // Could have used random generation here but manual generation looks far better

    clouds_x[0] = -60;      clouds_y[0] = 20;
    clouds_x[1] =  120;     clouds_y[1] = 90;
    clouds_x[2] =  300;     clouds_y[2] = 40;
    clouds_x[3] =  520;     clouds_y[3] = 110;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::cloudsDraw
Purpose:  Draws the clouds to the screen
Params:   Window to draw to
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Background::cloudsDraw(Window & window)
{
    // draw every single cloud

    for ( register int i = CLOUDS_COUNT-1 ; i >= 0  ; i-- )
    {
        // Draw this cloud at its position

        window.drawBitmap
        (
            bmpCloud,
            (int)(clouds_x[i]),
            (int)(clouds_y[i])
        );
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::cloudsMove
Purpose:  Moves every single cloud across the screen and wraps them around
Params:   Time the last frame took in seconsd
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Background::cloudsMove( float frameTime )
{
    // Get the width of the clouds

    register float cloudWidth = (float) bmpCloud->getWidth();

    // Get the amount to move the clouds by

    float amount = CLOUDS_SPEED * frameTime;

    // Clamp move amount to the screen width

    amount = amount < -Game::RESOLUTION_X ? -Game::RESOLUTION_X : amount;
    amount = amount >  Game::RESOLUTION_X ?  Game::RESOLUTION_X : amount;

    // Move all clouds

    for ( int i = 0 ; i < CLOUDS_COUNT ; i++ )
    {
        // Move the cloud

        clouds_x[i] += amount;

        // If cloud is past the right side of the screen then correct

        if ( clouds_x[i] >= Game::RESOLUTION_X )
        {
             clouds_x[i] -= Game::RESOLUTION_X + cloudWidth;
        }

        // If cloud is past the left side of the screen then correct

        if ( clouds_x[i] < -cloudWidth )
        {
            clouds_x[i] += Game::RESOLUTION_X + cloudWidth;
        }
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     Background::draw
Purpose:  Draws the background to the screen
Params:   Window to draw to, Time the last frame took in seconds
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void Background::draw( Window & window, float frameTime )
{
    // Move all the clouds along first

    cloudsMove(frameTime);

    // Draw the background bitmap

    window.drawBitmap(bmpSky,0,0);

    // Draw all the clouds

    cloudsDraw(window);
}
