//i like the penis
#include "d3dUtility.h"

#define Width (640.0f)
#define Height (480.0f)
// A structure for our custom vertex type
#define D3DFVF_CUSTOMVERTEX   D3DFVF_XYZ  | D3DFVF_DIFFUSE
struct CUSTOMVERTEX
{
	CUSTOMVERTEX(){}	
	CUSTOMVERTEX(float _x, float _y, float _z, D3DCOLOR _c )
	{
		x = _x;	 y = _y;  z = _z; 
		colour = _c;	
	}
	
	float x, y, z;
	D3DCOLOR colour;
};

//
// Globals
//

IDirect3DDevice9* Device = 0; 
IDirect3DVertexBuffer9*  pVB; 
D3DXMATRIX WorldTri1;;
D3DXMATRIX world_matrix;
D3DXMATRIX mWorld;
int control;



//
// Framework Functions
//

bool Setup()
{

Device->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                  0, 
												  D3DFVF_CUSTOMVERTEX,
												  D3DPOOL_MANAGED,
												  &pVB, NULL );
struct high
{
	float x,y,z;
	int n;
}high;
control = 10;
int Bcontrol = control;
int Btable = 0;
int t;

	CUSTOMVERTEX *v;
	CUSTOMVERTEX *Bezier[10];
	pVB->Lock( 0, 0, (void**)&v, 0 );
	/*for(int n =0; n>control; n++)
	{
		v[n].x = rand()%100-50;
		v[n].y = rand()%100-50;
		v[n].z = rand()%100-50;
		v[n].colour = D3DCOLOR_XRGB(255,0,0);

	}
	high.x = v[0].x;
	high.y = v[0].y;
	high.z = v[0].z;
	high.n = 0;
	for(int n=0; n>control; n++)
	{
		if(v[0].y > high.y)
		{
			high.x = v[n].x;
			high.y = v[n].y;
			high.z = v[n].z;
			high.n = n;
		}
	}
	v[high.n].x = v[0].x;
	v[high.n].y = v[0].y;
	v[high.n].z = v[0].z;
	v[0].x = high.x;
	v[0].y = high.y;
	v[0].z = high.z;*/
	t= 0.1;
	for(int n =0; n>control; n++)
	{
		Bezier[n][Btable].x = rand()%100-50;
		Bezier[n][Btable].y = rand()%100-50;
		Bezier[n][Btable].z = rand()%100-50;
		Bezier[n][Btable].colour = D3DCOLOR_XRGB(255,0,0);

	}
	high.x = Bezier[0][Btable].x;
	high.y = Bezier[0][Btable].y;
	high.z = Bezier[0][Btable].z;
	high.n = 0;
	for(int n=0; n>control; n++)
	{
		if(Bezier[0][Btable].y > high.y)
		{
			high.x = Bezier[n][Btable].x;
			high.y = Bezier[n][Btable].y;
			high.z = Bezier[n][Btable].z;
			high.n = n;
		}
	}
	Bezier[high.n][Btable].x = Bezier[0][Btable].x;
	Bezier[high.n][Btable].y = Bezier[0][Btable].y;
	Bezier[high.n][Btable].z = Bezier[0][Btable].z;
	Bezier[0][Btable].x = high.x;
	Bezier[0][Btable].y = high.y;
	Bezier[0][Btable].z = high.z;
	do
	{
	Bcontrol -= 1;
	Btable += 1;
	do{
	for(int n=0; n<Bcontrol; n++)
	{
		Bezier[n][Btable].x = (1-t)*Bezier[n][Btable-1].x + t*Bezier[n+1][Btable-1].x;
		Bezier[n][Btable].y = (1-t)*Bezier[n][Btable-1].y + t*Bezier[n+1][Btable-1].y;
		Bezier[n][Btable].z = (1-t)*Bezier[n][Btable-1].z + t*Bezier[n+1][Btable-1].z;
	}
	Bcontrol -= 1;
	Btable += 1;
	}while (Bcontrol>0);
	Bcontrol = control;
	Btable = 0;
	t +=0.1;
	}while (t<1);
	t=0.1;
	/*v[0]=CUSTOMVERTEX(0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[1]=CUSTOMVERTEX(1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[2]=CUSTOMVERTEX(0.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));

	v[3]=CUSTOMVERTEX(0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[4]=CUSTOMVERTEX(1.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[5]=CUSTOMVERTEX(1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));

	v[6]=CUSTOMVERTEX(0.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[7]=CUSTOMVERTEX(0.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255,0,0));
	v[8]=CUSTOMVERTEX(0.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));

	v[9]=CUSTOMVERTEX(0.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255,0,0));
	v[10]=CUSTOMVERTEX(0.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255,0,0));
	v[11]=CUSTOMVERTEX(0.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255,0,0));
	//v[8]=CUSTOMVERTEX(0.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255,0,0));*/
	pVB->Unlock();


	//position and aim camera
	D3DXVECTOR3 position (0.0f, 0.0f,-8.0f);
	D3DXVECTOR3 target (0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up (0.0f, 1.0f, 0.0f);
	D3DXMATRIX V;
	D3DXMatrixLookAtLH(&V, &position, &target, &up);
	Device->SetTransform(D3DTS_VIEW, &V);

	//set projection matrix
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj,                        // result
		D3DX_PI * 0.25f,               // 90 - degrees
		(float)Width / (float)Height, // aspect ratio
		1.0f,                         // near plane
		100.0f);                     // far plane  	

	
   //The World Matrix transforms Model Coordinates into World Space coordinates.
   //Setting it to Identity means there is no transformation, so Model Space is directly
   //mapped onto World Space.
 	D3DXMATRIX mTranslate;
	D3DXMatrixTranslation( &mTranslate, -4.0f, 0.0f, 0.0f );
	mWorld=mWorld*mTranslate;
	Device->SetTransform(D3DTS_WORLD, &mWorld);
	D3DXMatrixIdentity(&world_matrix);
   Device->SetTransform(D3DTS_WORLD,&world_matrix);
	
	Device->SetTransform(D3DTS_PROJECTION, &proj);
	Device->SetRenderState(D3DRS_LIGHTING, FALSE);

	return true;	

}

void Cleanup()
{
	// Nothing to cleanup in this sample.
}



void ClearBuffer(DWORD dwColor)
{		
Device->Clear( 0, NULL, D3DCLEAR_TARGET, dwColor, 1.0f, 0 );
}
bool Display(float timeDelta)
{

	if( Device )
	{
		ClearBuffer(D3DCOLOR_XRGB(0,0,0));

		// Begin the scene
		Device->BeginScene();

		Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		Device->SetStreamSource( 0, pVB, 0, sizeof(CUSTOMVERTEX) );
		Device->SetFVF( D3DFVF_CUSTOMVERTEX );
		D3DXMatrixIdentity(&world_matrix);
		Device->SetTransform(D3DTS_WORLD,&world_matrix);
		Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
		Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
		Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 3 );
		Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 4 );
		
		Device->EndScene();
		Device->Present(0,0,0,0); 
	}
	return true;
}

//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;
		
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			::DestroyWindow(hwnd);
		break;
	}
	return ::DefWindowProc(hwnd, msg, wParam, lParam);
}

//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
				   HINSTANCE prevInstance, 
				   PSTR cmdLine,
				   int showCmd)
{
	bool NotfullScreen;

	NotfullScreen = !(MessageBox(NULL, "Run in fullscreen Mode?", "Display Mode", MB_YESNO)==IDYES);

	if(!d3d::InitD3D(hinstance,
		Width, Height, NotfullScreen, D3DDEVTYPE_HAL, &Device))
	{
		::MessageBox(0, "InitD3D() - FAILED", 0, 0);
		return 0;
	}
		
	if(!Setup())
	{
		::MessageBox(0, "Setup() - FAILED", 0, 0);
		return 0;
	}

	d3d::EnterMsgLoop( Display );

	Cleanup();

	Device->Release();

	return 0;
}