#include "plugin.h"

IDirect3DDevice9* Device;
LPD3DXLINE Line;
D3DXVECTOR3 D3DLine[2];

void Init(IDirect3DDevice9* device);
void Quit(void);
void Render(PluginData* Data);

Manifest MosaicPlugin = {
	PLUGIN_VER, 
	PLUGIN_NAME,
	PLUGIN_DESC,
	Init,        
	Quit,        
	Render,
	&Properties
};
 
void Init(IDirect3DDevice9* device) 
{
	Device = device;

	if (Device != NULL) 
	{
		D3DXCreateLine(Device, &Line);
		Line->SetAntialias(true);
	}
}

 
void Quit() 
{
	if (Line != NULL)
	{
		Line->Release();
		Line = NULL;
	}
}

void Render(PluginData* Data)
{
	if (Device != NULL) 
	{
		float heatResolution = 0.0f;

		switch (Properties.Resolution)
		{
			case RES_SIMPLE:
					Line->SetWidth(8.0f);
					heatResolution = 10.0f;
				break;
			case RES_DETAILED:
					Line->SetWidth(1.0f);
					heatResolution = 1.0f;
				break;
			default:
					Line->SetWidth(2.5f);
					heatResolution = 5.0f;
				break;
		}

		D3DVIEWPORT9 Viewport;
		Device->GetViewport(&Viewport);

		for(int i = 0; i < Viewport.Height/heatResolution; i++)
		{
			D3DLine[0].x = -(Viewport.Width/2.0f);
			D3DLine[0].y = -(Viewport.Height/2.0f) + (i*heatResolution);
			D3DLine[0].z = 0.0f;
			D3DLine[1].x = (Viewport.Width/2.0f);
			D3DLine[1].y = -(Viewport.Height/2.0f) + (i*heatResolution);
			D3DLine[1].z = 0.0f;

			if (Data->Audio.Spectrum[i]*100.0f > 0.5f)
			{
				Line->Begin();
				Line->DrawTransform(D3DLine, 2, &Data->World, D3DXCOLOR(
						Data->Audio.Spectrum[i]*(50.0f*Properties.Color.R),
						Data->Audio.Spectrum[i]*(50.0f*Properties.Color.G),
						Data->Audio.Spectrum[i]*(50.0f*Properties.Color.B), 
						Data->Audio.Spectrum[i]*(50.0f*Properties.Color.A)));
				Line->End();
			}
		}
	}
}
 
extern "C" __declspec(dllexport) Manifest* Plugin() {
	return &MosaicPlugin;
}