/***************************************************************************/
/*                                                                         */
/* FileName: main.cpp                       ***  **  ~~                    */
/*                                          *** * *~                       */
/* Details: Sound! (prt9)                   ****  *    ~~                  */
/*                                          ***   * ~                      */
/* Author: Ben_3D & Wishi                   ****  *  ~~~                   */
/*                                          *** * *   ~~~                  */
/* URL: www.xfactordev.net                  ***  ** ~~~                    */
/*                                                                         */
/***************************************************************************/


// Main header file for the XDK
#include <xtl.h>

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// All our sound loading code and playing code is all nice and tidy in     //
// sound.h/.cpp...so it should be easy for you to use it and understand    //
// it without getting mixed up with loads of other problem code.           //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
#include "sound.h" // So we can use CSound!

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// This is just a file I use in the splash screen...its the location of    //
// the jpg file which will display on screen while we listen to our music  //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

#define szTexFile "D:\\media\\xfactordev.jpg"

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// These are used for our directX initilisation and so I can display my    //
// wonderful splash image on the screen.                                   //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

IDirect3D8*       g_pD3D = NULL;                // DirectX Object
IDirect3DDevice8* g_pD3DDevice = NULL;          // Screen Object

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
//  Our sound class...well I create a single global instance of it here    //
//  then call Create etc for it later on...and we can play our sound .wav  //
//  file which I have in the media folder!                                 //
//  Don't forget to include the media folder, which as the .wav file which //
//  you need!.. or else it will CRASH!                                     //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
CSound g_Madonna;                            // CSound is defined in sound.h


/////////////////////////////////////////////////////////////////////////////
//                                                                         //
//  Well this is our DirectX initilsation code, we call this once when     //
//  our program starts up.                                                 //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

void InitialiseD3D()
{
    //First of all, create the main D3D object. If it is created successfully we 
    //should get a pointer to an IDirect3D8 interface.
    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

    //Create a structure to hold the settings for our device
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    //Fill the structure. 
    //Set fullscreen 640x480x32 mode

	d3dpp.BackBufferWidth = 640;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;


	// Create one backbuffer and a zbuffer
	d3dpp.BackBufferCount = 1;

	// Set up how the backbuffer is "presented" to the frontbuffer each time
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

    //Create a Direct3D device.
    g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL, 
                                   D3DCREATE_HARDWARE_VERTEXPROCESSING, 
                                                   &d3dpp, &g_pD3DDevice);


}

 

void CleanUp()
{
    g_pD3DDevice->Release();
    g_pD3D->Release();
}


/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// This little function is nothing to do with the sound!  I just added it  //
// in, so that while your listening to the mega cool .wav sound I gave you //
// the screen would display more than just a blank screen or a triangle.   //
// Its the basic's...and it loads in file which I've specified above as    //
// "D:\\xfactodev.net" with a define (so you can change it later... and    //
// displays it.                                                            //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////


void XFactorTextureAdvert()
{
	g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	D3DVertexBuffer* g_pVertexBuffer = NULL; // Vertices Buffer
	IDirect3DTexture8* pTexture = NULL;

    VOID* pVertices;

    struct CUSTOMVERTEX
	{
		FLOAT x, y, z; // The transformed position for the vertex.
		FLOAT tu, tv;  // The vertex texture coordinates
	};

	DWORD D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ|D3DFVF_TEX1);


    //Store each point of the triangle together with it's colour
    CUSTOMVERTEX cvVertices[] =
    {
        { -1.0f, -1.0f, 0.0f    ,0.0f, 1.0f }, // x, y, z, textures (tu, tv) 
		{ -1.0f,  1.0f, 0.0f    ,0.0f, 0.0f }, 
		{  1.0f,  1.0f, 0.0f    ,1.0f, 0.0f },

		{ -1.0f, -1.0f, 0.0f    ,0.0f, 1.0f },
		{  1.0f,  1.0f, 0.0f    ,1.0f, 0.0f },
		{  1.0f, -1.0f, 0.0f    ,1.0f, 1.0f }

    };
	
	//Load our texture in
	D3DXCreateTextureFromFile(g_pD3DDevice, szTexFile, &pTexture);

    //Create the vertex buffer from our device
    g_pD3DDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX),
                                               0, 
											   D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, 
											   &g_pVertexBuffer);

    //Get a pointer to the vertex buffer vertices and lock the vertex buffer
    g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0);

    //Copy our stored vertices values into the vertex buffer
    memcpy(pVertices, cvVertices, sizeof(cvVertices));

    //Unlock the vertex buffer
    g_pVertexBuffer->Unlock();

	//Rendering our triangle
    g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
    g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
	g_pD3DDevice->SetTexture(0, pTexture);
    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

	g_pVertexBuffer->Release();
	pTexture->Release();


}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// Our applicatoin entry point!                                            //
// Now note the **NEW** comments...so you can't get mixed up with where    //
// sound code is being initilised and used :)                              //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

//Application entry point
void __cdecl main()
{

	InitialiseD3D();

	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
	g_Madonna.Create( "D:\\media\\do_it_with_madonna.wav" );

	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **
	//** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** 

	
	g_Madonna.playsound();

      while(true)
      {


            //Clear the backbuffer to black
            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
            //Begin the scene
            g_pD3DDevice->BeginScene();

			XFactorTextureAdvert();

            //End the scene
            g_pD3DDevice->EndScene();

            //Filp the back and front buffers so that whatever has been rendered on the back buffer
            //will now be visible on screen (front buffer).

            g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
      }     

	  g_Madonna.Release();

      CleanUp();
}




