
#pragma once
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9tex.h>
#include <iostream>
#include "UIcomponent.h"

LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;    // the pointer to the vertex buffer
LPDIRECT3DINDEXBUFFER9 i_buffer = NULL;

LPDIRECT3DTEXTURE9 tex;

void init_graphics(LPDIRECT3DDEVICE9);    // 3D declarations

struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; FLOAT U, V;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

// this is the function that puts the 3D models into video RAM
void init_graphics(LPDIRECT3DDEVICE9 device)
{
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX vertices[] =
    {
        { -1.0f, -1.0f, -1.0f, 0, 0, 1, 0.0f, 0.0f, },
        { -1.0f, 1.0f, -1.0f, 0, 0, 1, 0.0f, 1.0f, },
        { 1.0f, 1.0f, -1.0f, 0, 0, 1, 1.0f, 1.0f, },
        { 1.0f, -1.0f, -1.0f, 1, 0, 0, 1.0f, 0.0f, },
        { -1.0f, -1.0f, 1.0f, 1, 0, 0, 0.0f, 0.0f, },
        { 1.0f, -1.0f, 1.0f, 1, 0, 0, 0.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 1, 0, 0, 1.0f, 1.0f, },
        { -1.0f, 1.0f, 1.0f, 1, 0, 0, 1.0f, 0.0f, },
    };

	WORD indices[] = 
	{
		0, 1, 2,
		2, 3, 0,
		4, 5, 6,
		6, 7, 4,
		0, 3, 5,
		5, 4, 0,
		3, 2, 6,
		6, 5, 3,
		2, 1, 7,
		7, 6, 2,
		1, 0, 4,
		4, 7, 1
	};

	device->CreateIndexBuffer(12*3*sizeof(WORD),
								D3DUSAGE_WRITEONLY,
								D3DFMT_INDEX16,
								D3DPOOL_DEFAULT,
								&i_buffer,
								NULL);
	
    VOID* pVoid;    // a void pointer

	i_buffer->Lock(0, 3*12*sizeof(WORD), (void**)&pVoid, 0);
	memcpy(pVoid, indices, sizeof(indices));
	i_buffer->Unlock();


    // create a vertex buffer interface called v_buffer
    device->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,
                               NULL);


    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 8*sizeof(CUSTOMVERTEX), (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();


	HRESULT hr = D3DXCreateTextureFromFile(device, L"directx-logo.jpg", &tex);
	if(hr == D3DERR_NOTAVAILABLE)
	{
		int x = 1;
	}
	if(hr == D3DERR_OUTOFVIDEOMEMORY)
	{
		int x = 1;
	}
	if( hr == D3DERR_INVALIDCALL)
	{
		int x = 1;
	}
	if( hr == D3DXERR_INVALIDDATA)
	{
		int x = 1;
	}
	if(hr == E_OUTOFMEMORY)
	{
		int x = 1;
	}

}

void init_graphics_unindexed(LPDIRECT3DDEVICE9 device)
{
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX vertices[] =
    {
        { 400.0f, 62.5f, 0.5f, D3DCOLOR_XRGB(0, 0, 255), 0.0f, 0.0f, },
        { 650.0f, 500.0f, 0.5f, D3DCOLOR_XRGB(0, 255, 0), 0.0f, 0.0f, },
        { 150.0f, 500.0f, 0.5f, D3DCOLOR_XRGB(255, 0, 0), 0.0f, 0.0f, },
    };

    // create a vertex buffer interface called v_buffer
    device->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &v_buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    v_buffer->Lock(0, 3*sizeof(CUSTOMVERTEX), (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();
}

void setLight(PDIRECT3DDEVICE9 pd3dDevice)
{
	pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
	//pd3dDevice->SetRenderState(D3DRS_COLORVERTEX, TRUE); // not sure if needed
	pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_ARGB(22, 64, 64, 64)); // no visible difference on A
	//pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); // probably not needed yet, can't harm, probably also default.


	D3DMATERIAL9 material;

	D3DCOLORVALUE zeroColor;
	D3DCOLORVALUE oneColor;

	zeroColor.a = 0;
	zeroColor.r = 0;
	zeroColor.g = 0;
	zeroColor.b = 0;

	oneColor.a = 1;
	oneColor.r = 1;
	oneColor.g = 1;
	oneColor.b = 1;

	material.Ambient = oneColor;
	material.Diffuse = oneColor; //setting it to oneColor has no effect
	material.Emissive = zeroColor;
	material.Power = 0; //no difference if 0 or 1
	material.Specular = zeroColor;

	pd3dDevice->SetMaterial(&material);

	D3DLIGHT9 light;    // create the light struct
    ZeroMemory(&light, sizeof(light));    // clear out the light struct for use
    light.Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
    light.Diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);    // set the light's color
    light.Direction = D3DXVECTOR3(-1.0f, -0.3f, -1.0f);
    pd3dDevice->SetLight(0, &light);    // send the light struct properties to light #0
	pd3dDevice->LightEnable(0, TRUE);
}
