//------------------------------------------------------------------------------
// 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 "SceneToCollisionConverter.h"
#include <Core/InputOutput/FilePath.h>
#include <Graphics/Scene/Scene.h>
#include <Graphics/SceneNode/SceneNode.h>
#include <Collision/Renderer/CollisionRenderer.h>
#include <Collision/System/CollisionScene.h>
#include <Collision/Converter/CollisionConverter.h>
#include <Collision/InputOutput/TextCollisionSaver.h>

/*
#include <Collision/InputOutput/TextCollisionLoader.h>
#include <Collision/System/CollisionNode.h>
#include <Collision/Leaf/StaticSphereCollision.h>
*/
//------------------------------------------------------------------------------
// プログラムエントリポイント
int WINAPI WinMain(
	HINSTANCE instance, HINSTANCE previousInstance, LPSTR command, int show){
	SceneToCollisionConverter sceneToCollisionConverter(command);
	return sceneToCollisionConverter.execute(instance);
}
//------------------------------------------------------------------------------
// コンストラクタ
SceneToCollisionConverter::SceneToCollisionConverter(const String& command) :
	SceneFramework("SceneToCollisionConverter"), command_(command),
	collisionScene_(NULL), collisionRenderer_(NULL){
}
//------------------------------------------------------------------------------
// デストラクタ
SceneToCollisionConverter::~SceneToCollisionConverter(){
}
//------------------------------------------------------------------------------
// 初期化
bool SceneToCollisionConverter::initialize(){
	if(command_.isEmpty()){
		ErrorOut("SceneToCollisionConverter::initialize() "
			"ファイル名を指定してください");
		return false;
	}
	FilePath filePath(command_);
	if(!loadScene(filePath.getPath())){
		ErrorOut("SceneToCollisionConverter::initialize() "
			"シーンのロードに失敗しました %s", filePath.getPath().getBytes());
		return false;
	}

	// コリジョンコンバート
	collisionScene_ = new CollisionScene();
	CollisionConverter converter;
	converter.convert(scene_, collisionScene_);

	// コリジョン保存
	String fileName = filePath.getFolderPath() + filePath.getName() + ".tcl";
	TextCollisionSaver* saver = new TextCollisionSaver();
	saver->save(fileName, collisionScene_);
	delete saver;
/*
	// ローダのテスト
	collisionScene_->clear();
	TextCollisionLoader* loader = new TextCollisionLoader();
	loader->load(fileName, collisionScene_);
	delete loader;

	// 球テスト
	StaticSphereCollision* sphere =
		collisionScene_->createStaticSphereCollision("sphere");
	sphere->setSphere(Vector3(0.f, 1.f, 0.f), 0.5f);
	collisionScene_->getRootNode()->addChild(sphere);
*/
	// コリジョンレンダラ初期化
	collisionRenderer_ = new CollisionRenderer();

	// 通常のシーンを表示しないようにする
	scene_->getRootNode()->setEnabled(false);
	return true;
}
//------------------------------------------------------------------------------
// 後始末
void SceneToCollisionConverter::finalize(){
	// コリジョンレンダラ削除
	SafeDelete(collisionRenderer_);
	// コリジョンの削除
	if(collisionScene_ != NULL){
		collisionScene_->clear();
		delete collisionScene_;
	}
}
//------------------------------------------------------------------------------
// 実行
void SceneToCollisionConverter::run(){
	// 通常シーンの表示切替
	if(keyboard_->down(Keyboard::keyS)){
		scene_->getRootNode()->setEnabled(!scene_->getRootNode()->isEnabled());
	}
	// コリジョンシーンのトラバース
	collisionScene_->traverse();
}
//------------------------------------------------------------------------------
// レンダリング準備
void SceneToCollisionConverter::renderSetup(){
	collisionRenderer_->renderingSetup(
		collisionScene_, scene_->getCurrentCamera());
}
//------------------------------------------------------------------------------
// レンダリング
void SceneToCollisionConverter::render(){
	collisionRenderer_->rendering();
}
//------------------------------------------------------------------------------
// 情報描画
void SceneToCollisionConverter::drawInformation(){
	SceneFramework::drawInformation();
}
//------------------------------------------------------------------------------
