00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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_;
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
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
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 }
00255