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

#include <Windows.h>
#include <atlbase.h>
#include <iostream>

void init_graphics(LPDIRECT3DDEVICE9);    // 3D declarations

LPD3DXMESH mesh;
LPD3DXBUFFER adj;
LPD3DXBUFFER mat;
LPD3DXBUFFER effectInstances;

DWORD pNumMaterials;
D3DMATERIAL9* pMeshMaterials;
LPDIRECT3DTEXTURE9* pMeshTextures;

LPDIRECT3DTEXTURE9 pParticleTexture;
IDirect3DVertexBuffer9* pParticleBuffer;
int particleCount = 20;

#define D3DFVF_PARTICLE (D3DFVF_XYZ|D3DFVF_DIFFUSE)
struct PARTICLE_VERTEX {
						D3DXVECTOR3 psPosition;
						D3DCOLOR color;
						};

// this is the function that puts the 3D models into video RAM
void init_graphics(LPDIRECT3DDEVICE9 device)
{
	device->CreateVertexBuffer(
								20*sizeof(PARTICLE_VERTEX),
								D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS,
								D3DFVF_PARTICLE,
								D3DPOOL_DEFAULT,
								&pParticleBuffer,
								NULL
								);



	HRESULT hr = D3DXLoadMeshFromX(
		L"D3DBox.x",
		D3DXMESH_SYSTEMMEM,
		device,
		&adj,
		&mat, 
		NULL, //can't create a material buffer 'till we know how many materials
		&pNumMaterials, //so we find out!
		&mesh);

	//Get the location of the materials
	D3DXMATERIAL* materials = (D3DXMATERIAL*)mat->GetBufferPointer();
	//
	pMeshMaterials = new D3DMATERIAL9[pNumMaterials];
	pMeshTextures = new LPDIRECT3DTEXTURE9[pNumMaterials];

	for (DWORD i = 0; i < pNumMaterials; i++)
	{
		pMeshMaterials[i] = materials[i].MatD3D; //copy them

		pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse; //D3DX does not guarantee ambient is to be set

		USES_CONVERSION;
		LPWSTR fileName = A2W(materials[i].pTextureFilename);

		hr = D3DXCreateTextureFromFile(
			device,
			fileName,
			&pMeshTextures[i]);

		
		hr = D3DXCreateTextureFromFile(
			device, 
			L"particleTexture.png", 
			&pParticleTexture);

		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)
{
    
}

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);
}
