//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

/** @file
 * デバッグヘッドアップディスプレイ実装
 * @author Junpee
 */

#include "Core/Utility/stdafx.h"
#include "Core/Utility/DebugHUD.h"
#include "Core/System/StringMethod.h" // StdStrcpy

// インスタンス
DebugHUD* DebugHUD::instance_ = NULL;

//------------------------------------------------------------------------------
// 初期化、後始末
//------------------------------------------------------------------------------
// 初期化
void DebugHUD::initialize(HWND windowHandle){
	Assert(instance_ == NULL);
	instance_ = new DebugHUD(windowHandle);
}
//------------------------------------------------------------------------------
// 後始末
void DebugHUD::finalize(){
	Assert(instance_ != NULL);
	SafeDelete(instance_);
}
//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
DebugHUD::DebugHUD(HWND windowHandle) :
	windowHandle_(windowHandle), font_(NULL),
	topLeftColor_(Color4c::white), bottomLeftColor_(Color4c::white),
	topRightColor_(Color4c::white), bottomRightColor_(Color4c::white){
	// フォント記述初期化
	::memset(&fontDescription_, 0, sizeof(D3DXFONT_DESC));
	fontDescription_.Height = 23;
	fontDescription_.Weight = FW_NORMAL; 
	fontDescription_.Weight = FW_BOLD; 
	fontDescription_.CharSet = SHIFTJIS_CHARSET;
	StdStrcpy(fontDescription_.FaceName, "ＭＳ ゴシック");
//	StdStrcpy(fontDescription_.FaceName, "ＭＳ Ｐ ゴシック");
	// デバイスオブジェクトホルダ登録
	LampGraphics::addDeviceObjectHolder(this);
}
//------------------------------------------------------------------------------
// デストラクタ
DebugHUD::~DebugHUD(){
	// デバイスオブジェクトホルダ登録解除
	LampGraphics::removeDeviceObjectHolder(this);
}
//------------------------------------------------------------------------------
// 描画
//------------------------------------------------------------------------------
// 描画実装
void DebugHUD::drawImplement(){
	if(font_ == NULL){ return; }
	RECT rect_;
	::GetClientRect(windowHandle_, &rect_);
	int gap = 5;
	rect_.top += gap;
	rect_.bottom -= gap;
	rect_.left += gap * 2;
	rect_.right -= gap * 2;
	RenderingDevice* device = RenderingDevice::getInstance();
	if(!device->beginScene()){ return; }
	// 左上
	if(!topLeft_.isEmpty()){
		font_->DrawText(NULL, topLeft_.getBytes(), -1, &rect_,
			(DT_LEFT | DT_TOP), topLeftColor_.getARGB());
	}
	// 左下
	if(!bottomLeft_.isEmpty()){
		font_->DrawText(NULL, bottomLeft_.getBytes(), -1, &rect_,
			(DT_LEFT | DT_BOTTOM | DT_SINGLELINE),
			bottomLeftColor_.getARGB());
	}
	// 右上
	if(!topRight_.isEmpty()){
		font_->DrawText(NULL, topRight_.getBytes(), -1, &rect_,
			(DT_RIGHT | DT_TOP | DT_SINGLELINE), topRightColor_.getARGB());
	}
	// 右下
	if(!bottomRight_.isEmpty()){
		font_->DrawText(NULL, bottomRight_.getBytes(), -1, &rect_,
			(DT_RIGHT | DT_BOTTOM | DT_SINGLELINE),
			bottomRightColor_.getARGB());
	}
	device->endScene();
	topLeft_ = bottomLeft_ = topRight_ = bottomRight_ = "";
}
//------------------------------------------------------------------------------
// デバイスオブジェクト関連
//------------------------------------------------------------------------------
// デバイスオブジェクトの初期化
bool DebugHUD::initializeGraphicsDeviceObjects(){
	// フォント初期化
	if(DirectXFailed(D3DXCreateFontIndirect(
		LampGraphics::getDirect3DDevice(), &fontDescription_, &font_))){
		ErrorOut("DebugHUD::initializeGraphicsDeviceObjects() "
			"フォントの作成に失敗しました");
		SafeRelease(font_);
		return false;
	}
	return true;
}
//------------------------------------------------------------------------------
// デバイスオブジェクトの削除
void DebugHUD::deleteGraphicsDeviceObjects(){
	// フォント後始末
	SafeRelease(font_);
}
//------------------------------------------------------------------------------
// デバイスオブジェクトのリストア
bool DebugHUD::restoreGraphicsDeviceObjects(){
	// フォント
	if(font_ != NULL){
		if(DirectXFailed(font_->OnResetDevice())){
			ErrorOut("DebugHUD::restoreGraphicsDeviceObjects() "
				"フォントの作成に失敗しました");
			SafeRelease(font_);
			return false;
		}
	}
	return true;
}
//------------------------------------------------------------------------------
// デバイスオブジェクトの無効化
void DebugHUD::invalidateGraphicsDeviceObjects(){
	// フォント
	if(font_ != NULL){ font_->OnLostDevice(); }
}
//------------------------------------------------------------------------------
