Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# SceneFramework.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * シーンフレームワーク実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Framework/System/SceneFramework.h"
    00027 #include "Graphics/System/LampGraphics.h"
    00028 #include "Graphics/Renderer/Renderer.h"
    00029 #include "Graphics/Renderer/InformationRenderer.h"
    00030 #include "Graphics/Camera/CameraManager.h"
    00031 #include "Graphics/Fog/Fog.h"
    00032 #include "Graphics/MeshData/MeshDataManager.h"
    00033 #include "Framework/Utility/PS2PadCameraController.h"
    00034 #include "Animation/System/AnimationManager.h"
    00035 #include "Animation/System/Animation.h"
    00036 #include "Input/System/LampInput.h"
    00037 #include "Input/Pad/PS2Pad.h"
    00038
    00039 // ファイル読み込み
    00040 #include "Core/InputOutput/FilePath.h"
    00041 #include "Graphics/InputOutput/TextSceneLoader.h"
    00042 #include "Graphics/InputOutput/BinarySceneLoader.h"
    00043 #include "Animation/InputOutput/TextAnimationLoader.h"
    00044 #include "Animation/InputOutput/BinaryAnimationLoader.h"
    00045
    00046 namespace Lamp{
    00047
    00048 //------------------------------------------------------------------------------
    00049 // コンストラクタ
    00050 SceneFramework::SceneFramework(const String& name) :
    00051     BasicFramework(name), renderer_(NULL), informationRenderer_(NULL),
    00052     sceneName_(""), scene_(NULL), camera_(NULL), cameraController_(NULL),
    00053     animationManager_(NULL), animation_(NULL), runCount_(0), pad_(NULL){
    00054 }
    00055 //------------------------------------------------------------------------------
    00056 // デストラクタ
    00057 SceneFramework::~SceneFramework(){
    00058 }
    00059 //------------------------------------------------------------------------------
    00060 // フレームワークの初期化
    00061 bool SceneFramework::frameworkInitialize(HINSTANCE instanceHandle){
    00062     // 基本フレームワーク呼び出し
    00063     if(!BasicFramework::frameworkInitialize(instanceHandle)){ return false; }
    00064     // レンダラ初期化
    00065     renderer_ = new Renderer();
    00066     informationRenderer_ = new InformationRenderer();
    00067     // カメラコントローラ初期化
    00068     cameraController_ = new PS2PadCameraController();
    00069     Joystick* cameraJoystick = cameraController_->searchJoystick();
    00070     // パッド初期化
    00071     int joystickCount = LampInput::getJoystickCount();
    00072     for(int i = 0; i < joystickCount; i++){
    00073         Joystick* joystick = LampInput::getJoystick(i);
    00074         if(joystick == cameraJoystick){ continue; }
    00075         if(!PS2Pad::checkCompatibility(joystick)){ continue; }
    00076         pad_ = new PS2Pad(joystick);
    00077         break;
    00078     }
    00079
    00080     // シーン初期化
    00081     scene_ = LampGraphics::createScene(name_);
    00082     // カメラの作成
    00083     camera_ = scene_->getCameraManager()->createCamera("camera");
    00084     scene_->setCurrentCamera(camera_);
    00085     cameraController_->setCamera(camera_);
    00086
    00087     // アニメーションマネージャ初期化
    00088     animationManager_ = new AnimationManager();
    00089     return true;
    00090 }
    00091 //------------------------------------------------------------------------------
    00092 // フレームワークの後始末
    00093 void SceneFramework::frameworkFinalize(){
    00094     // アニメーションマネージャの破棄
    00095     animationManager_->clear();
    00096     delete animationManager_;
    00097
    00098     // シーンの破棄
    00099     scene_->clear();
    00100     LampGraphics::destroyScene(scene_);
    00101
    00102     // パッドの破棄
    00103     SafeDelete(pad_);
    00104     // カメラコントローラの破棄
    00105     SafeDelete(cameraController_);
    00106     // レンダラの破棄
    00107     SafeDelete(informationRenderer_);
    00108     SafeDelete(renderer_);
    00109     // 基本フレームワーク呼び出し
    00110     BasicFramework::frameworkFinalize();
    00111 }
    00112 //------------------------------------------------------------------------------
    00113 // フレームワーク実行
    00114 void SceneFramework::frameworkRun(){
    00115     BasicFramework::frameworkRun();
    00116     runCount_++;
    00117     // カメラ操作
    00118     cameraController_->control();
    00119 }
    00120 //------------------------------------------------------------------------------
    00121 // フレームワークレンダリング準備
    00122 void SceneFramework::frameworkRenderSetup(){
    00123     BasicFramework::frameworkRenderSetup();
    00124     // 時間計測
    00125     float animationTime = (float)runCount_;// 時間の単位が16.6666fmsが1なので
    00126     runCount_ = 0;
    00127
    00128     // カリング前アニメーション。シーンノード
    00129     if(animation_ != NULL){
    00130         animation_->animate(animationTime, Animation::maskPreCulling);
    00131     }
    00132
    00133     // トラバース、先にカメラの設定を終えている必要がある
    00134     scene_->traverse();
    00135
    00136     // レンダリング準備
    00137     renderer_->renderingSetup(scene_);
    00138     informationRenderer_->renderingSetup(scene_);
    00139
    00140     // カリング後アニメーション。キャラクタ、パーティクル等
    00141     if(animation_ != NULL){
    00142         animation_->animate(animationTime, Animation::maskPostCulling);
    00143     }
    00144 }
    00145 //------------------------------------------------------------------------------
    00146 // フレームワークレンダリング
    00147 void SceneFramework::frameworkRender(){
    00148     BasicFramework::frameworkRender();
    00149     // レンダリング
    00150     renderer_->rendering();
    00151     informationRenderer_->rendering();
    00152 }
    00153 //------------------------------------------------------------------------------
    00154 // シーンのロード
    00155 bool SceneFramework::loadScene(const String& sceneName){
    00156     // シーンのクリア
    00157     clearScene();
    00158     // シーンのロード
    00159     bool textFlag_;
    00160     sceneName_ = sceneName;
    00161     if(sceneName_.getSize() == 0){ return false; }
    00162     FilePath sceneFilePath(sceneName_);
    00163     if(!sceneFilePath.existFile()){ return false; }
    00164     if(sceneFilePath.getExtension() == "tsn"){
    00165         TextSceneLoader* loader = new TextSceneLoader();
    00166         loader->load(sceneFilePath.getPath(), scene_);
    00167         delete loader;
    00168         textFlag_ = true;
    00169     }else if(sceneFilePath.getExtension() == "bsn"){
    00170         BinarySceneLoader* loader = new BinarySceneLoader();
    00171         loader->load(sceneFilePath.getPath(), scene_);
    00172         delete loader;
    00173         textFlag_ = false;
    00174     }else{
    00175         return false;
    00176     }
    00177     // アニメーションのロード
    00178     String animationName = sceneName_.getSubstring(0, sceneName_.getSize() - 3);
    00179     FilePath animationFilePath;
    00180     if(textFlag_){ animationName += "tam"; }
    00181     else{ animationName += "bam"; }
    00182     animationFilePath.setPath(animationName);
    00183     if(animationFilePath.existFile()){
    00184         if(textFlag_){
    00185             TextAnimationLoader* animationLoader =
    00186                 new TextAnimationLoader();
    00187             animationLoader->load(
    00188                 animationFilePath.getPath(), animationManager_);
    00189             delete animationLoader;
    00190         }else{
    00191             BinaryAnimationLoader* animationLoader =
    00192                 new BinaryAnimationLoader();
    00193             animationLoader->load(
    00194                 animationFilePath.getPath(), animationManager_);
    00195             delete animationLoader;
    00196         }
    00197         // アニメーションのバインド
    00198         animation_ = animationManager_->search(animationFilePath.getName());
    00199         if(animation_ != NULL){
    00200             if(!animation_->bind(scene_)){
    00201                 animationManager_->clear();
    00202                 animation_ = NULL;
    00203             }
    00204         }
    00205     }
    00206 /*
    00207     FilePath animationFilePath;
    00208     animationFilePath.setPath(
    00209         sceneName_.getSubstring(0, sceneName_.getSize() - 3) + "tam");
    00210     if(animationFilePath.existFile()){
    00211         TextAnimationLoader* animationLoader = new TextAnimationLoader();
    00212         animationLoader->load(
    00213             animationFilePath.getPath(), animationManager_);
    00214         delete animationLoader;
    00215         // アニメーションのバインド
    00216         animation_ = animationManager_->search(animationFilePath.getName());
    00217         if(animation_ != NULL){
    00218             if(!animation_->bind(scene_)){
    00219                 animationManager_->clear();
    00220                 animation_ = NULL;
    00221             }
    00222         }
    00223     }
    00224 */
    00225     // 背景色設定
    00226     backGroundColor_ = scene_->getFog()->getColor();
    00227     // カメラ位置の設定
    00228     float distance = 2.f;
    00229     MeshDataManager* meshDataManager = scene_->getMeshDataManager();
    00230     int meshDataCount = meshDataManager->getCount();
    00231     for(int i = 0; i < meshDataCount; i++){
    00232         MeshData* meshData = meshDataManager->get(i);
    00233         float radius = meshData->getBoundingSphere().getRadius();
    00234         if(radius > distance){ distance = radius; }
    00235     }
    00236     camera_->setTransformation(
    00237         Vector3::zero, Vector3(0.f, distance * 0.5f, distance * 1.5f));
    00238     cameraController_->setTranslationSensibility(distance * 0.05f);
    00239     return true;
    00240 }
    00241 //------------------------------------------------------------------------------
    00242 // シーンのクリア
    00243 void SceneFramework::clearScene(){
    00244     scene_->clear();
    00245     camera_ = scene_->getCameraManager()->createCamera("camera");
    00246     camera_->setTransformation(Vector3::zero, Vector3::zero);
    00247     scene_->setCurrentCamera(camera_);
    00248     cameraController_->setCamera(camera_);
    00249     // アニメーションのクリア
    00250     animationManager_->clear();
    00251     animation_ = NULL;
    00252 }
    00253 //------------------------------------------------------------------------------
    00254 } // End of namespace Lamp
    00255 //------------------------------------------------------------------------------