📄 oasisgraphicssystem.cpp
字号:
/******************************************************************************
* This source file is part of Bad Camel Gaming
* Copyright (C) 2003 Zephie Greyvenstein
* See Readme.html for acknowledgements
*
* 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
*****************************************************************************/
/******************************************************************************
* FILENAME : oasisGraphicsSystem.cpp
* DESCRIPTION : Graphics system
* AUTHOR : Zephie Greyvenstein
*****************************************************************************/
#include "oasisGraphicsSystem.h"
#include "oasisConfiguration.h"
#include "oasisEntity.h"
#include "oasisConvert.h"
#include "oasisScriptSystem.h"
/// Include Ogre stuff
#include "OgreRoot.h"
#include "OgreCamera.h"
#include "OgreRenderWindow.h"
#include "OgreStringConverter.h"
/// Include optional profiler
#if USE_PROFILER == 1
#include "OgreProfiler.h"
#endif
namespace Oasis {
template< > graphicsSystem *singleton< graphicsSystem >::singletonPtr = 0;
graphicsSystem::graphicsSystem( ) : system( "Graphics" ),
pool( "graphics" ) {
// Try to restore previous config
if ( !Ogre::Root::getSingleton( ).restoreConfig( ) ) {
// Show the config setup
Ogre::Root::getSingleton( ).showConfigDialog( );
// Save the config
Ogre::Root::getSingleton( ).saveConfig( );
}
// Setup the window
window = Ogre::Root::getSingleton( ).initialise( true );
if ( !window ) {
oasisError( exception::ET_NULL_POINTER,
"Window for graphics system failed to initialise",
"graphicsSystem::( )" );
}
// Get the default scenemanager
scene = Ogre::Root::getSingleton( ).
getSceneManager( Ogre::ST_GENERIC );
if ( !scene ) {
oasisError( exception::ET_NULL_POINTER,
"Scenemanager could not be fetched",
"graphicsSystem::( )" );
}
// Create camera
camera = scene->createCamera( "mainCamera" );
if ( !camera ) {
oasisError( exception::ET_NULL_POINTER,
"Could not create camera for graphics system",
"graphicsSystem::( )" );
}
// Attach camera to window hence creating viewport
viewport = window->addViewport( camera );
if ( !viewport ) {
oasisError( exception::ET_NULL_POINTER,
"Could not add viewport for graphics system",
"graphicsSystem::( )" );
}
// Set some default values
camera->setNearClipDistance( 5 );
camera->setPosition( Ogre::Vector3( 0, 0, 0 ) );
camera->lookAt( Ogre::Vector3( 0, 0, -1 ) );
// Start the profiler if it was asked for
#if USE_PROFILER == 1
Ogre::Profiler::getSingleton( ).setEnabled( true );
#endif
}
graphicsSystem::~graphicsSystem( ) {
// Ogre takes care of all this for us
window = NULL;
camera = NULL;
scene = NULL;
viewport = NULL;
}
graphicsSystem &graphicsSystem::get( void ) {
return singleton< graphicsSystem >::get( );
}
graphicsSystem *graphicsSystem::getPtr( void ) {
return singleton< graphicsSystem >::getPtr( );
}
void graphicsSystem::reset( void ) {
// Clea all entities
clearPool( );
shotCounter = 0;
setDetailLevel( DL_SOLID );
}
void graphicsSystem::update( real time ) {
window->update( );
}
void graphicsSystem::setSkyBox( bool active,
const string &material,
real distance,
bool drawFirst ) {
scene->setSkyBox( active, material, distance, drawFirst );
}
void graphicsSystem::setFog( FogMode fogMode,
const colour &fogColour,
real expDensity,
real linearStart,
real linearEnd ) {
Ogre::FogMode ogreMode;
switch( fogMode ) {
case FM_NONE:
ogreMode = Ogre::FOG_NONE;
break;
case FM_EXP:
ogreMode = Ogre::FOG_EXP;
break;
case FM_EXP2:
ogreMode = Ogre::FOG_EXP2;
break;
case FM_LINEAR:
ogreMode = Ogre::FOG_LINEAR;
break;
};
scene->setFog( ogreMode,
convert::toOgreColour( fogColour ),
expDensity,
linearStart,
linearEnd );
}
void graphicsSystem::setBackgroundColour( real r, real g, real b ) {
setBackgroundColour( colour( r, g, b ) );
}
void graphicsSystem::setBackgroundColour( const colour &newColour ) {
viewport->setBackgroundColour( convert::toOgreColour( newColour ) );
}
void graphicsSystem::setAmbientLight( real r, real g, real b ) {
setAmbientLight( colour( r, g, b ) );
}
void graphicsSystem::setAmbientLight( const colour &newColour ) {
scene->setAmbientLight( convert::toOgreColour( newColour ) );
}
void graphicsSystem::takeShot( const string &filename ) {
window->writeContentsToFile( filename + ".png" );
}
void graphicsSystem::takeShot( void ) {
takeShot( "shot" +
Ogre::StringConverter::toString( ++shotCounter ) );
}
void graphicsSystem::setDetailLevel( const DetailLevel newLevel ) {
detailLevel = newLevel;
Ogre::SceneDetailLevel level;
switch( detailLevel ) {
case DL_POINTS:
level = Ogre::SDL_POINTS;
break;
case DL_WIREFRAME:
level = Ogre::SDL_WIREFRAME;
break;
case DL_SOLID:
level = Ogre::SDL_SOLID;
break;
};
camera->setDetailLevel( level );
}
const graphicsSystem::DetailLevel graphicsSystem::getDetailLevel( void ) const {
return detailLevel;
}
entity *graphicsSystem::createEntity( const string &entityName,
const string &meshName ) {
// Get an entity from the pool
entity *newEntity = static_cast< entity* >( _getFreePoolable( entityName ) );
// Set it up
newEntity->setEntity( meshName, entityName );
return newEntity;
}
void graphicsSystem::removeEntity( entity *oldEntity ) {
// Return entity to pool
_consumePoolable( oldEntity );
}
void graphicsSystem::removeEntity( const string &entityName ) {
// Return entity to pool
_consumePoolable( entityName );
}
entity *graphicsSystem::getEntity( const string &entityName ) {
entity *ent = static_cast< entity* >( _getUsedPoolable( entityName ) );
if ( !ent ) {
oasisError( exception::ET_ITEM_NOT_FOUND,
"Entity " + entityName + " does not exist!",
"graphicsSystem::getEntity" );
}
return ent;
}
const uint16 graphicsSystem::getWindowHeight( void ) const {
return window->getHeight( );
}
const uint16 graphicsSystem::getWindowWidth( void ) const {
return window->getWidth( );
}
Ogre::RenderWindow *graphicsSystem::getWindow( void ) const {
return window;
}
Ogre::SceneManager *graphicsSystem::getSceneManager( void ) const {
return scene;
}
Ogre::Camera *graphicsSystem::getCamera( void ) const {
return camera;
}
poolable *graphicsSystem::_createPoolable( void ) {
entity *newEntity = new entity( );
return newEntity;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -