D:/simple_rts/src/LevelStatusVisualizer.cpp

Go to the documentation of this file.
00001 #include "LevelStatusVisualizer.h"
00002 
00003 #include "Unit.h"
00004 
00005 #include "OgreMath.h"
00006 #include "OgrePlane.h"
00007 #include "OgreException.h"
00008 #include "OgreStringConverter.h"
00009 #include "OgreVector3.h"
00010 #include "OgreMeshManager.h"
00011 #include "OgreResourceGroupManager.h"
00012 #include "OgreHardwarePixelBuffer.h"
00013 #include "OgreMaterial.h"
00014 #include "OgreMaterialManager.h"
00015 #include "OgrePixelFormat.h"
00016 #include "OgreTextureManager.h"
00017 
00018 using Ogre::Pass;
00019 using Ogre::TextureManager;
00020 using Ogre::HardwareBuffer;
00021 using Ogre::Math;
00022 using Ogre::Plane;
00023 using Ogre::Vector3;
00024 using Ogre::MeshManager;
00025 using Ogre::MaterialPtr;
00026 using Ogre::MaterialManager;
00027 using Ogre::ResourceGroupManager;
00028 using Ogre::HardwarePixelBufferSharedPtr;
00029 using Ogre::PixelBox;
00030 using Ogre::StringConverter;
00031 using Ogre::TextureUnitState;
00032 
00033 
00034 
00035 namespace ASR
00036 {
00037         // ----------------------------------------------------------------------------
00038         LevelStatusVisualizer::LevelStatusVisualizer( Level* lev )
00039                 : mLevel ( lev )
00040         {
00041                 // Create a dynamic texture
00042                 tex = TextureManager::getSingleton ().createManual (
00043                         "DynamicTexture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
00044                         Ogre::TEX_TYPE_2D, mLevel->mCellsWide, mLevel->mCellsHigh, 0, Ogre::PF_BYTE_BGRA, Ogre::TU_DYNAMIC_WRITE_ONLY );
00045 
00046                 
00047                 // Add another layer onto the terrain to show the occlusion information
00048                 MaterialPtr material = MaterialManager::getSingleton ().getByName ( "ASR/Terrain" );
00049                 TextureUnitState* texState = material->getTechnique (0)->getPass (0)->createTextureUnitState ( "DynamicTexture" );
00050                 texState->setColourOperation ( Ogre::LBO_ADD );
00051 
00052                 texState->setTextureFiltering ( Ogre::FO_POINT, Ogre::FO_POINT, Ogre::FO_NONE );
00053 
00054                 _rebuild();
00055         }
00056 
00057 
00058         // ----------------------------------------------------------------------------
00059         LevelStatusVisualizer::~LevelStatusVisualizer(void)
00060         {
00061         }
00062 
00063 
00064         // ----------------------------------------------------------------------------
00065         bool LevelStatusVisualizer::updateBeforeFrame ( const FrameEvent& e, const InputReader* inputDevice )
00066         {
00067                 _rebuild ();
00068 
00069                 return true;
00070         }
00071 
00072 
00073         // ----------------------------------------------------------------------------
00074         bool LevelStatusVisualizer::updateAfterFrame ( const FrameEvent& e, const InputReader* inputDevice )
00075         {
00076                 return true;
00077         }
00078 
00079 
00080         // ----------------------------------------------------------------------------
00081         void LevelStatusVisualizer::_rebuild ()
00082         {
00083                 HardwarePixelBufferSharedPtr pixBuf = tex->getBuffer ();
00084                 pixBuf->lock ( HardwareBuffer::HBL_DISCARD );
00085                 const PixelBox& pixelBox = pixBuf->getCurrentLock ();
00086 
00087                 unsigned char* pBuf = static_cast<unsigned char*>(pixelBox.data);
00088                 unsigned char* data = pBuf;
00089 
00090                 _paintBlockedTiles ( data, pixelBox.getRowSkip () );
00091                 _paintUnitPaths ( data, pixelBox.getRowSkip () );
00092 
00093                 pixBuf->unlock ();
00094         }
00095 
00096 
00097         // ----------------------------------------------------------------------------
00098         void LevelStatusVisualizer::_paintUnitPaths ( unsigned char* data, unsigned int rowSkip )
00099         {
00100                 Level::TeamIterator teamIter = mLevel->getTeamIterator ();
00101 
00102                 // For each team
00103                 while ( teamIter.hasMoreElements () )
00104                 {
00105                         Team* curTeam = teamIter.getNext ();
00106 
00107                         // For each unit on the team
00108                         TeamUnits::UnitIterator unitIter = curTeam->getUnitIterator ();
00109                         while ( unitIter.hasMoreElements () )
00110                         {
00111                                 Unit* curUnit = unitIter.getNext ();
00112                                 if ( curUnit->hasDestination () )
00113                                 {
00114                                         // For each waypoint of each unit on each team
00115                                         Unit::WaypointIterator wayIter = curUnit->getWaypointIterator ();
00116                                         while ( wayIter.hasMoreElements () )
00117                                         {
00118                                                 Vector3 dest = wayIter.getNext ();
00119                                                 int x;
00120                                                 int z;
00121 
00122                                                 mLevel->getCellLocation ( dest, &x, &z );
00123                                                 
00124                                                 int index;
00125                                                 index = z * ( mLevel->mCellsWide * 4 );
00126                                                 index += z * rowSkip * 4;
00127                                                 index += x * 4;
00128 
00129                                                 data[ index + 0] = 255;
00130                                                 data[ index + 1] = 255;
00131                                                 data[ index + 2] = 255;
00132                                                 data[ index + 3] = 155;
00133                                         }
00134                                 }
00135                         }
00136                 }
00137         }
00138 
00139 
00140         // ----------------------------------------------------------------------------
00141         void LevelStatusVisualizer::_paintBlockedTiles ( unsigned char* data, unsigned int rowSkip )
00142         {
00143                 for ( unsigned int j = 0; j < mLevel->mCellsHigh; j++ )
00144                 {
00145                         for ( unsigned int i = 0; i < mLevel->mCellsWide; i++ )
00146                         {
00147                                 if ( mLevel->mOccupied[ (j * mLevel->mCellsWide) + i ].getTraversalCost () < 0.0f )
00148                                 {
00149                                         data[i*4] = 0;
00150                                         data[i*4 + 1] = 0;
00151                                         data[i*4 + 2] = 125;
00152                                         data[i*4 + 3] = 80;
00153                                 }
00154                                 else
00155                                 {
00156                                         data[i*4] = 0;
00157                                         data[i*4 + 1] = 0;
00158                                         data[i*4 + 2] = 0;
00159                                         data[i*4 + 3] = 80;
00160                                 }
00161                         }
00162                         data += (mLevel->mCellsWide * 4) + (rowSkip * 4);
00163                 }
00164         }
00165 }

Generated on Sun Jun 25 19:23:43 2006 for Valors End by  doxygen 1.4.7