/*
	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:   Menu_BeginRace
// Purpose:      Displays the key layout for the in game parts
//               and waits for the enter key until returning.
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "Menu_BeginRace.h"

//------------------------------------------------------------------------------
// Static initialisers
//------------------------------------------------------------------------------

bool            Menu_BeginRace::runMenu         = 0;   // Run the window and consequently the menu ?
Bitmap *        Menu_BeginRace::bmpKeyLayout    = 0;   // image to display in center of screen
Bitmap *        Menu_BeginRace::bmpMenuBar      = 0;   // The menu bar horizontal rule divider
BitmapFont *    Menu_BeginRace::bitFont         = 0;   // Bitmap font to use for the screen

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

Name:     Menu_BeginRace::display
Purpose:  starts showing the begining of race menu.
Params:   as commented.
Returns:  none

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

void Menu_BeginRace::display(Window * window)
{
    //--------------------------------------------------------------------------
    // Show the menu initially
    //--------------------------------------------------------------------------

    runMenu = true;

    //--------------------------------------------------------------------------
    // Load the key layout image which is displayed centered in the screen
    //--------------------------------------------------------------------------

    bmpKeyLayout = window->loadBitmap("gfx\\menu\\keyLayout.bmp",true);

    //--------------------------------------------------------------------------
    // Load the menu bar resource
    //--------------------------------------------------------------------------

    bmpMenuBar = window->loadBitmap("gfx\\menu\\menuBar1.bmp",true);

    //--------------------------------------------------------------------------
    // Load the bitmap font used for the end of race screen
    //--------------------------------------------------------------------------

    bitFont = new BitmapFont("gfx\\font\\font1sm.bmp",window);

    //--------------------------------------------------------------------------
    // Set the paint and window closed event of the window
    //--------------------------------------------------------------------------

    window->setEvent_paint(event_draw);
    window->setEvent_keyDown(event_keyDown);
    window->setEvent_closed(event_closed);

    //--------------------------------------------------------------------------
    // Continue running the window until the 'runMenu' flag is false
    //--------------------------------------------------------------------------

    while ( runMenu )
    {
        // Request a repaint of the window.

        window->requestRepaint();

        // do message processing of the window

        window->run();
    }

    //--------------------------------------------------------------------------
    // Disengage the event handlers
    //--------------------------------------------------------------------------

    window->setEvent_paint(0);
    window->setEvent_keyDown(0);
    window->setEvent_closed(0);

    //--------------------------------------------------------------------------
    // Free all resources allocated
    //--------------------------------------------------------------------------

    delete bmpMenuBar;          bmpMenuBar      = 0;
    delete bmpKeyLayout;        bmpKeyLayout    = 0;
    delete bitFont;             bitFont         = 0;
}

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

Name:     Menu_BeginRace::event_keyDown
Purpose:  Stops running the menu if enter was pressed.
Params:   Window
Returns:  none

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

// No unreferenced param warning

#pragma warning(disable:4100)

void Menu_BeginRace::event_keyDown ( Window & window , Window::KeyboardKey key )
{
    // If the user has hit the enter key then stop showing the menu

    if ( key == Window::KEYBOARD_RETURN )
    {
        // Stop showing the menu

        runMenu = false;
    }
}

// Restore this warning to normal

#pragma warning(default:4100)

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

Name:     Menu_BeginRace::event_closed
Purpose:  Just stops the execution of the window after it is closed.
Params:   Window
Returns:  none

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

// No unreferenced param warning

#pragma warning(disable:4100)

void Menu_BeginRace::event_closed ( Window & window )
{
    // Stop running the menu

    runMenu = false;
}

// Restore this warning to normal

#pragma warning(default:4100)

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

Name:     Menu_BeginRace::event_draw
Purpose:  Draws the menu screen.
Params:   Window to paint to.
Returns:  none

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

void Menu_BeginRace::event_draw( Window & window )
{
    // Clear the screen to black

    window.clear(0,0,0);

    // Figure out the coordinates of the keyboard layout image. Tis centered in the middle of the screen.

    int lx = Game::RESOLUTION_X / 2 - bmpKeyLayout->getWidth()  / 2;   // left x of image
    int ty = Game::RESOLUTION_Y / 2 - bmpKeyLayout->getHeight() / 2;   // top  y of image

    // Draw the keyboard layout image centered in the middle of the screen

    window.drawBitmap
    (
        bmpKeyLayout,   // bitmap to draw
        lx,             // x pos
        ty              // y pos
    );

    // Draw the title of the screen centered

    bitFont->drawText
    (
        "Game controls",
        Game::RESOLUTION_X / 2 - ( strlen("Game controls") * bitFont->getWidth() ) / 2,
        bitFont->getHeight(),
        &window
    );

    // Draw a the menu bar under the title

    window.drawBitmap
    (
        bmpMenuBar,
        0,
        bitFont->getHeight() * 3
    );

    // Draw a the menu bar above the 'press enter to continue' prompt

    window.drawBitmap
    (
        bmpMenuBar,
        0,
        Game::RESOLUTION_Y - bitFont->getHeight() * 5
    );

    // Draw the 'press enter to continue' prompt

    bitFont->drawText
    (
        "Press enter to continue.",
        Game::RESOLUTION_X / 2 - ( strlen("Press enter to continue.") * bitFont->getWidth() ) / 2,
        Game::RESOLUTION_Y - bitFont->getHeight() * 1,
        &window
    );
}


