//--------------------------------------------------------------------------------
//
//　ウインドウ表示プログラム
//	Author : Xu Wenjie
//	Date   : 2016-04-26
//--------------------------------------------------------------------------------
//  Update : 
//	
//--------------------------------------------------------------------------------
#include "goal.h"

//--------------------------------------------------------------------------------
//  定数定義
//--------------------------------------------------------------------------------
#define NUM_POLYGON ( 2 )
#define NUM_VERTEX  ( 4 )

#define GOAL_WIDTH (60.0f)//ポリゴンの幅
#define GOAL_HEIGHT (60.0f)//ポリゴンの高さ
#define GOAL_TEXTURENAME_RED "data/TEXTURE/circle_r.png"//ファイル名
#define GOAL_TEXTURENAME_GREEN "data/TEXTURE/circle_g.png"//ファイル名
#define GOAL_TEXTURENAME_BLUE "data/TEXTURE/circle_b.png"//ファイル名
#define GOAL_TEXTURENAME_YELLOW "data/TEXTURE/circle_y.png"//ファイル名
#define GOAL_POS_LEFT (D3DXVECTOR3(69.0f, 325.0f, 0.0f))
#define GOAL_POS_TOP (D3DXVECTOR3(640.0f, 106.0f, 0.0f))
#define GOAL_POS_RIGHT (D3DXVECTOR3(1219.0f, 325.0f, 0.0f))
#define GOAL_POS_BOTTOM (D3DXVECTOR3(640.0f, 555.0f, 0.0f))

//--------------------------------------------------------------------------------
//  構造体定義
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
//  プロトタイプ宣言
//--------------------------------------------------------------------------------
HRESULT MakeVerTexGoal(LPDIRECT3DDEVICE9 pDevice);

//--------------------------------------------------------------------------------
//  グローバル変数
//--------------------------------------------------------------------------------
LPDIRECT3DTEXTURE9 g_pTextureGoal[4];//textureインターフェース
LPDIRECT3DVERTEXBUFFER9 g_pVtxBufferGoal = NULL;//頂点バッファ管理インターフェースポインタ
D3DXVECTOR3 g_aGoalPos[NUM_GOAL];

//--------------------------------------------------------------------------------
//  初期化処理
//--------------------------------------------------------------------------------
void InitGoal(void)
{
	LPDIRECT3DDEVICE9 pDevice = GetDevice();//デバイス取得

	int nCnt = 0;
	for (nCnt = 0;nCnt < TYPE_MAX;nCnt++)
	{
		g_pTextureGoal[nCnt] = NULL;
	}

	g_aGoalPos[0] = GOAL_POS_LEFT;//y
	g_aGoalPos[1] = GOAL_POS_TOP;//r
	g_aGoalPos[2] = GOAL_POS_RIGHT;//g
	g_aGoalPos[3] = GOAL_POS_BOTTOM;//b

	if (FAILED(MakeVerTexGoal(pDevice)))//textureポインタへのポインタ
	{
		MessageBox(NULL, "MakeVerTexPlayer ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}

	//ハードディスクからTextureの読み込み
	//※エラーチェック必須
	if (FAILED(D3DXCreateTextureFromFile(pDevice,
		GOAL_TEXTURENAME_YELLOW,
		&g_pTextureGoal[0])))//textureポインタへのポインタ
	{
		MessageBox(NULL, "D3DXCreateTextureFromFile ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}
	if (FAILED(D3DXCreateTextureFromFile(pDevice,
		GOAL_TEXTURENAME_RED,
		&g_pTextureGoal[1])))//textureポインタへのポインタ
	{
		MessageBox(NULL, "D3DXCreateTextureFromFile ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}
	if (FAILED(D3DXCreateTextureFromFile(pDevice,
		GOAL_TEXTURENAME_GREEN,
		&g_pTextureGoal[2])))//textureポインタへのポインタ
	{
		MessageBox(NULL, "D3DXCreateTextureFromFile ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}
	if (FAILED(D3DXCreateTextureFromFile(pDevice,
		GOAL_TEXTURENAME_BLUE,
		&g_pTextureGoal[3])))//textureポインタへのポインタ
	{
		MessageBox(NULL, "D3DXCreateTextureFromFile ERROR!!", "エラー", MB_OK | MB_ICONWARNING);
	}
}

//--------------------------------------------------------------------------------
//  終了処理
//--------------------------------------------------------------------------------
void UninitGoal(void)
{
	if (g_pVtxBufferGoal != NULL)
	{
		g_pVtxBufferGoal->Release();
		g_pVtxBufferGoal = NULL;
	}

	for (int nCnt = 0;nCnt < NUM_GOAL;nCnt++)
	{
		if (g_pTextureGoal[nCnt] != NULL)
		{
			g_pTextureGoal[nCnt]->Release();
			g_pTextureGoal[nCnt] = NULL;
		}
	}
}

//--------------------------------------------------------------------------------
//  更新処理
//--------------------------------------------------------------------------------
void UpdateGoal(void)
{
}

//--------------------------------------------------------------------------------
//  描画処理
//--------------------------------------------------------------------------------
void DrawGoal(void)
{
	LPDIRECT3DDEVICE9 pDevice = GetDevice();//デバイス取得

	pDevice->SetStreamSource(
		0,//ストリーム番号
		g_pVtxBufferGoal,
		0,//オフセット（開始位置）
		sizeof(VERTEX_2D));//ストライド量

						   //頂点フォーマットの設定
	pDevice->SetFVF(FVF_VERTEX_2D);

	//Textureの設定
	for (int nCnt = 0; nCnt < NUM_GOAL; nCnt++)
	{
		pDevice->SetTexture(0, g_pTextureGoal[nCnt]);

		//プリミティブ描画
		pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,
			nCnt * 4,//オフセット（頂点数）
			NUM_POLYGON);
	}
}

//--------------------------------------------------------------------------------
//  頂点の作成
//--------------------------------------------------------------------------------
HRESULT MakeVerTexGoal(LPDIRECT3DDEVICE9 pDevice)
{
	if (FAILED(pDevice->CreateVertexBuffer(
		sizeof(VERTEX_2D) * NUM_VERTEX * NUM_GOAL,//作成したい頂点バッファのサイズ
		D3DUSAGE_WRITEONLY,//頂点バッファの使用方法
		FVF_VERTEX_2D,//書かなくても大丈夫
		D3DPOOL_MANAGED,//メモリ管理方法(managed：デバイスにお任せ)
		&g_pVtxBufferGoal,//
		NULL//
	)))
	{
		return E_FAIL;
	}

	//仮想アドレスを取得するためのポインタ
	VERTEX_2D *pVtx;
	int nCnt = 0;
	float fLength = 0.0f;
	float fRotation = 0.0f;
	D3DXVECTOR3 vGoalPos;

	//頂点バッファをロックして、仮想アドレスを取得する
	g_pVtxBufferGoal->Lock(
		0,//範囲
		0,//範囲
		(void**)&pVtx,//アドレスが書かれたメモ帳のアドレス
		0);

	for (nCnt = 0; nCnt < NUM_GOAL; nCnt++)
	{
		//頂点座標の設定（2D座標、右回り）
		pVtx[nCnt * 4 + 0].pos = D3DXVECTOR3(g_aGoalPos[nCnt].x - GOAL_WIDTH * 0.5f, g_aGoalPos[nCnt].y - GOAL_HEIGHT * 0.5f, 0.0f);
		pVtx[nCnt * 4 + 1].pos = D3DXVECTOR3(g_aGoalPos[nCnt].x + GOAL_WIDTH * 0.5f, g_aGoalPos[nCnt].y - GOAL_HEIGHT * 0.5f, 0.0f);
		pVtx[nCnt * 4 + 2].pos = D3DXVECTOR3(g_aGoalPos[nCnt].x - GOAL_WIDTH * 0.5f, g_aGoalPos[nCnt].y + GOAL_HEIGHT * 0.5f, 0.0f);
		pVtx[nCnt * 4 + 3].pos = D3DXVECTOR3(g_aGoalPos[nCnt].x + GOAL_WIDTH * 0.5f, g_aGoalPos[nCnt].y + GOAL_HEIGHT * 0.5f, 0.0f);

		//rhwの設定(必ず1.0f)
		pVtx[nCnt * 4 + 0].rhw = 1.0f;
		pVtx[nCnt * 4 + 1].rhw = 1.0f;
		pVtx[nCnt * 4 + 2].rhw = 1.0f;
		pVtx[nCnt * 4 + 3].rhw = 1.0f;

		//頂点カラーの設定(0〜255の整数値)
		pVtx[nCnt * 4 + 0].color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
		pVtx[nCnt * 4 + 1].color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
		pVtx[nCnt * 4 + 2].color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
		pVtx[nCnt * 4 + 3].color = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

		//texture頂点
		pVtx[nCnt * 4 + 0].tex = D3DXVECTOR2(0.0f, 0.0f);
		pVtx[nCnt * 4 + 1].tex = D3DXVECTOR2(1.0f, 0.0f);
		pVtx[nCnt * 4 + 2].tex = D3DXVECTOR2(0.0f, 1.0f);
		pVtx[nCnt * 4 + 3].tex = D3DXVECTOR2(1.0f, 1.0f);
	}

	//仮想アドレス解放
	g_pVtxBufferGoal->Unlock();

	return S_OK;
}

//--------------------------------------------------------------------------------
//  敵アドレス取得
//--------------------------------------------------------------------------------
D3DXVECTOR3 GetGoal(int nType)
{
	return g_aGoalPos[nType];
}