//------------------------------------------------------------------------------
// 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 "LampBasic.h"
#include "Graphics2D/Renderer/SpriteRenderState.h"
#include "Graphics2D/Renderer/SpriteGraphicsBuffer.h"
#include "Graphics2D/Sprite/SpritePicture.h"
#include "Graphics/Renderer/RenderingDevice.h"

namespace Lamp{

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
SpriteRenderState::SpriteRenderState(SpriteGraphicsBuffer* graphicsBuffer) :
	graphicsBuffer_(graphicsBuffer), picture_(NULL){
	// レンダーターゲットサイズを取得する
	RenderingDevice* device = RenderingDevice::getInstance();
	renderTargetSize_.set(device->getRenderTargetSize());
}
//------------------------------------------------------------------------------
// デストラクタ
SpriteRenderState::~SpriteRenderState(){
}
//------------------------------------------------------------------------------
// 描画
//------------------------------------------------------------------------------
// リクエスト
void SpriteRenderState::request(SpritePicture* picture,
	const Point2f& minPosition, const Point2f& maxPosition,
	const TexCoord2& minUV, const TexCoord2& maxUV){
	// ピクチャに変更があればバッファを書き出す
	if(picture_ != picture){
		render();
		picture_ = picture;
		RenderingDevice* device = RenderingDevice::getInstance();
		if(picture_ == NULL){
			device->setTexture(0, (Direct3DTexture*)NULL);
		}else{
			device->setTexture(0, picture_->getD3DTexture());
		}
	}
	graphicsBuffer_->request(minPosition, maxPosition, minUV, maxUV);
}
//------------------------------------------------------------------------------
// レンダリング
void SpriteRenderState::render(){
	graphicsBuffer_->render();
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
