RenderEngine.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "O3dPre.hpp"
00023 #include "RenderEngine.hpp"
00024 #include "./area/O3dManager.hpp"
00025 #include "./config/o3d_config.hpp"
00026 #include "./event/RenderEventListeners.hpp"
00027 #include "./input/O3dInputBridge.hpp"
00028 #include "./input/InputHandler.hpp"
00029 #include "./input/InputManager.hpp"
00030 #include "./input/Console.hpp"
00031 #include "./schema/O3dSchema.hpp"
00032 #include "./thing/ThingEntity.hpp"
00033 #include "./widget/all.hpp"
00034 #include "./io/all.hpp"
00035 #include "util/task/TaskList.hpp"
00036 #include <OgreRoot.h>
00037 #include <OgreRenderSystem.h>
00038 #include <OgreRenderWindow.h>
00039 #include <OgreAnimation.h>
00040 #include <OgreConfigFile.h>
00041 
00042 using namespace Ogre;
00043 using namespace se_core;
00044 
00045 
00046 namespace se_ogre {
00047     RenderEngine* RenderEngine
00048     ::singleton() {
00049         Assert(O3dSchema::renderEngine);
00050         return O3dSchema::renderEngine;
00051     }
00052 
00053 
00054     RenderEngine
00055     ::RenderEngine(se_ogre::ConsoleHandler* consoleHandler)
00056             : inputBridge_(0), levelResourceCount_(0), skip_(0) {
00057 #ifndef _DEBUG
00058         O3dSchema::root = new Root("plugins.cfg", "ogre.cfg", "Ogre.log");
00059 #else
00060         O3dSchema::root = new Root("plugins_d.cfg", "ogre.cfg", "Ogre.log");
00061 #endif
00062         LogDetail("Created Ogre root");
00063 
00064         // Create speech listener object
00065         O3dSchema::speechBubble = new SpeechBubble();
00066         LogDetail("Created speech bubble handler");
00067 
00068         if(IS_CONSOLE_ENABLED) {
00069             O3dSchema::console = new Console();
00070             if(consoleHandler) {
00071                 O3dSchema::console->setConsoleHandler(consoleHandler);
00072             }
00073             LogDetail("Created console");
00074         }
00075     }
00076 
00077 
00078     RenderEngine
00079     ::~RenderEngine() {
00080         delete O3dSchema::console;
00081         O3dSchema::console = 0;
00082         LogDetail("Destroyed console");
00083 
00084         delete O3dSchema::speechBubble;
00085         O3dSchema::speechBubble = 0;
00086         LogDetail("Destroyed speech bubble handler");
00087 
00088         delete O3dSchema::root;
00089         O3dSchema::root = 0;
00090         LogDetail("Destroyed ogre root");
00091     }
00092 
00093 
00094     void RenderEngine
00095     ::postSimTickEvent(long when) {
00096         if(skip_ > 0) {
00097             --skip_;
00098         }
00099         O3dSchema::taskList.perform(1);
00100     }
00101 
00102 
00103     void RenderEngine
00104     ::renderFrame() {
00105         if(skip_ > 0) {
00106             return;
00107         }
00108         // WorldManager::frameStarted is called before rendering
00109         if(O3dSchema::window && O3dSchema::window->isActive())
00110             O3dSchema::root->renderOneFrame();
00111         // WorldManager::frameEnded is called after rendering
00112         if(inputBridge_) {
00113             inputBridge_->step();
00114         }
00115     }
00116 
00117 
00118     void RenderEngine
00119     ::screenshot(int screenShotId) {
00120         // Will render frame buffer to file
00121         char buffer[256];
00122         sprintf(buffer, "screenshot%05d.jpg", screenShotId);
00123         Ogre::Root::getSingleton().getRenderSystem()->_getViewport()->getTarget()->writeContentsToFile(buffer);
00124     }
00125 
00126 
00127     void RenderEngine
00128     ::screenshot(const char* prefix) {
00129         Ogre::Root::getSingleton().getRenderSystem()->_getViewport()->getTarget()->writeContentsToTimestampedFile(prefix, "jpg");
00130     }
00131 
00132 
00133     void RenderEngine
00134     ::resetGameClock(void) {
00135         O3dSchema::gameClock = 0;
00136     }
00137 
00138 
00140     bool RenderEngine
00141     ::setup(void) {
00142         setupResources();
00143 
00144         bool carryOn = configure();
00145         if (!carryOn) return false;
00146 
00147         // Configure this from file
00148         //chooseSceneManager();
00149         //createCamera();
00150         //createViewports();
00151 
00152         TextureManager::getSingleton().setDefaultNumMipmaps(5);
00153 
00154         // Create any resource listeners (for loading screens)
00155         createResourceListener();
00156 
00157         // Load resources
00158         loadResources();
00159 
00160         /*
00161         const Ogre::RenderSystemCapabilities* caps = O3dSchema::root->getRenderSystem()->getCapabilities();
00162         if (!caps->hasCapability(RSC_VERTEX_PROGRAM)) {
00163             LogDetail("Your card does not support vertex programs.");
00164         }
00165 
00166         // Setup animation default
00167         Animation::setDefaultInterpolationMode(Animation::IM_LINEAR);
00168         Animation::setDefaultRotationInterpolationMode(Animation::RIM_LINEAR);
00169         LogDetail("Initialized interpolation of animations.");
00170 
00171         if(IS_CONSOLE_ENABLED) {
00172             try {
00173                 O3dSchema::console->setupGuiSystem();
00174                 LogDetail("Setup GUI system");
00175             }
00176             catch(...) {
00177                 LogDetail("Failed initializeing console window. Console unavailable.");
00178                 delete O3dSchema::console;
00179                 O3dSchema::console = 0;
00180             }
00181         }
00182 
00183         if(O3dSchema::console) {
00184             // Give application chance to create gui from
00185             // xml in a renderEventListener before creating
00186             // console window
00187             O3dSchema::console->createConsoleWindow();
00188             LogDetail("Created applications CEGUI.");
00189         }
00190         */
00191 
00192         // Create bridge between input
00193         createInputBridge();
00194 
00195         return true;
00196     }
00197 
00198 
00199     void RenderEngine
00200 	::cleanup(void) {
00201         LogDetail("Shutting down resource group manager");
00202         ResourceGroupManager::getSingleton().shutdownAll();
00203 
00204         delete inputBridge_;
00205         inputBridge_ = 0;
00206         LogDetail("Destroyed input bridge");
00207 
00208         O3dSchema::sceneManager = 0;
00209         LogDetail("Destroyed world manager and scene manager");
00210     }
00211 
00212 
00213 
00214     // Create new frame listener
00215     void RenderEngine
00216     ::createInputBridge(void) {
00217         inputBridge_= new O3dInputBridge(O3dSchema::window);
00218         LogDetail("Created input bridge");
00219     }
00220 
00221 
00224     bool RenderEngine
00225     ::configure(void) {
00226         // Show the configuration dialog and initialise the system
00227         // You can skip this and use root.restoreConfig() to load configuration
00228         // settings if you were sure there are valid ones saved in ogre.cfg
00229         if(O3dSchema::root->restoreConfig()) {
00230             LogDetail("Loaded config");
00231         }
00232         bool gotConfig = false;
00233         try {
00234             gotConfig = O3dSchema::root->showConfigDialog();
00235         }
00236         catch(...) {
00237             // Probably failed to write config
00238             gotConfig = true;
00239         }
00240         if(gotConfig) {
00241             // If returned true, user clicked OK so initialise
00242             // Here we choose to let the system create a default rendering window by
00243             // passing 'true'
00244             LogDetail("Got requested config");
00245             try {
00246                 O3dSchema::root->saveConfig();
00247                 LogDetail("Saved config");
00248             }
00249             catch(...) {
00250                 // Probably running from non-writeable media
00251                 LogDetail("Couldn't write config");
00252             }
00253             O3dSchema::window = O3dSchema::root->initialise(true);
00254             return true;
00255         }
00256         else {
00257             LogDetail("Config canceled by user");
00258             return false;
00259         }
00260     }
00261 
00262 
00264     void RenderEngine
00265     ::setupResources(void) {
00266         // Load resource paths from config file
00267         ConfigFile cf;
00268         Ogre::String dataPath;
00269         if(IoSchema::dataPath)
00270             dataPath.append(IoSchema::dataPath);
00271         cf.load(dataPath + "/ogre/resources.cfg");
00272 
00273         // Go through all sections & settings in the file
00274         ConfigFile::SectionIterator seci = cf.getSectionIterator();
00275 
00276         Ogre::String secName, typeName, archName;
00277         while (seci.hasMoreElements()) {            
00278             secName = seci.peekNextKey();
00279             ConfigFile::SettingsMultiMap *settings = seci.getNext();
00280             ConfigFile::SettingsMultiMap::iterator i;
00281             for (i = settings->begin(); i != settings->end(); ++i) {
00282                 typeName = i->first;
00283                 archName = dataPath + i->second;
00284                 ResourceGroupManager::getSingleton()
00285                     .addResourceLocation(archName, typeName, secName);
00286             }
00287         }
00288         LogDetail("Initialised resources.");
00289     }
00290 
00291 
00294     void RenderEngine
00295     ::loadResources(void) {
00296         const char* sections[] = { "bootstrap", "common", "particle", 0 };
00297         int i = 0;
00298         while(sections[i] != 0) {
00299             try {
00300                 ResourceGroupManager::getSingleton().initialiseResourceGroup(sections[i]);
00301                 //ResourceGroupManager::getSingleton().loadResourceGroup(sections[i]);
00302             } catch(...) {
00303             }
00304             ++i;
00305         }
00306     }
00307 
00308 
00309     void RenderEngine
00310     ::loadLevelResources(const char** sections) {
00311         bool isSame = true;
00312         for(int i = 0; i < levelResourceCount_ && sections[i] != 0; ++i) {
00313             if(strcmp(sections[i], levelResources_[i]) != 0) {
00314                 isSame = false;
00315                 break;
00316             }
00317         }
00318         if(isSame && sections[ levelResourceCount_ ] != 0) {
00319             isSame = false;
00320         }
00321         if(isSame)
00322             return;
00323 
00324         resetLevelResources();
00325 
00326         const char** sec = sections;
00327         while(*sec != 0) {
00328             try {
00329                 ResourceGroupManager::getSingleton().initialiseResourceGroup(*sec);
00330                 //ResourceGroupManager::getSingleton().loadResourceGroup(*sec);
00331             }
00332             catch(...) {
00333             }
00334             Assert(levelResourceCount_ < MAX_LEVEL_RESOURCE_SECTIONS);
00335             levelResources_[ levelResourceCount_++ ] = *sec;
00336             ++sec;
00337         }
00338     }
00339 
00340 
00341     void RenderEngine
00342     ::resetLevelResources() {
00343         LogWarning(levelResourceCount_);
00344         while(levelResourceCount_ > 0) {
00345             const char* sec = levelResources_ [ --levelResourceCount_ ];
00346             ResourceGroupManager::getSingleton().unloadResourceGroup(sec);
00347             ResourceGroupManager::getSingleton().clearResourceGroup(sec);
00348         }
00349     }
00350 
00351 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:06 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo