//------------------------------------------------------------------------------
// 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 "Game/Rendering/RenderingSystem.h"

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

//------------------------------------------------------------------------------
// 生成、破棄
//------------------------------------------------------------------------------
// コンストラクタ
RenderingSystem::RenderingSystem() : defaultCameraPosition_(0.f, 40.f, 80.f){
	// インスタンスポインタ設定
	Assert(instance_ == NULL);
	instance_ = this;
	// スプライトレンダラ初期化
	spriteRenderer_ = new SpriteRenderer();
	// レンダラ初期化
	renderer_ = new Renderer();
	// シーンの作成
	scene_ = LampGraphics::createScene("Scene");
	// カメラコントローラ初期化、2番目のパッドが使用できるなら使用する
	cameraController_ = new PS2PadCameraController();
	if(LampInput::getJoystickCount() > 1){
		if(PS2Pad::checkCompatibility(LampInput::getJoystick(1))){
			cameraController_->setJoystick(LampInput::getJoystick(1));
		}
	}
	// カメラの作成
	camera_ = scene_->getCameraManager()->createCamera("camera");
	camera_->setTransformation(Vector3::zero, defaultCameraPosition_);
	scene_->setCurrentCamera(camera_);
	cameraController_->setCamera(camera_);
	// アニメーションマネージャの作成
	animationManager_ = new AnimationManager();
}
//------------------------------------------------------------------------------
// デストラクタ
RenderingSystem::~RenderingSystem(){
	// アニメーションマネージャの破棄
	int animationCount = animationManager_->clear();
	Assert(animationCount == 0);
	delete animationManager_;
	// カメラの破棄
	scene_->getCameraManager()->destroy(camera_);
	// カメラコントローラの破棄
	SafeDelete(cameraController_);
	// シーンの破棄
	int sceneObjectCount = scene_->clear();
	Assert(sceneObjectCount == 0);
	LampGraphics::destroyScene(scene_);
	// レンダラの破棄
	SafeDelete(renderer_);
	// スプライトレンダラの破棄
	SafeDelete(spriteRenderer_);
	// インスタンスポインタ後始末
	Assert(instance_ == this);
	instance_ = NULL;
}
//------------------------------------------------------------------------------
// 背景クリア
void RenderingSystem::clearBackground(Color4c color){
	GraphicsDevice::getInstance()->clear(Color4c::black);
}
//------------------------------------------------------------------------------
// トラバース
void RenderingSystem::traverse(){
	scene_->traverse();
}
//------------------------------------------------------------------------------
// レンダリング準備
void RenderingSystem::renderingSetup(){
	renderer_->renderingSetup(scene_);
}
//------------------------------------------------------------------------------
// レンダリング
void RenderingSystem::rendering(){
	renderer_->rendering();
	spriteRenderer_->render();
}
//------------------------------------------------------------------------------
// クリア
void RenderingSystem::clear(){
	scene_->clear();
	// カメラの作成
	camera_ = scene_->getCameraManager()->createCamera("camera");
	camera_->setTransformation(Vector3::zero, defaultCameraPosition_);
	scene_->setCurrentCamera(camera_);
	cameraController_->setCamera(camera_);
}
//------------------------------------------------------------------------------
// カメラコントローラの実行
void RenderingSystem::runCameraController(){
	cameraController_->control();
}
//------------------------------------------------------------------------------
