
#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;


void init_graphics(LPDIRECT3DDEVICE9);    // 3D declarations

struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

// 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[] =
    {
        { 200.0f, 200.0f, 200.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 200.0f, 400.0f, 200.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 400.0f, 400.0f, 200.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 400.0f, 200.0f, 200.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 200.0f, 200.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 400.0f, 200.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 400.0f, 400.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 200.0f, 400.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
    };

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

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

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