MercuryModel.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "MercuryModel.h"
00003 #include "MercuryDisplay.h"
00004 #include "MercuryLog.h"
00005 #include "MercuryUtil.h"
00006 
00007 MercuryModel::MercuryModel()
00008 {
00009     m_polys = 0;
00010     m_file = NULL;
00011 //  m_curFrame = 0;
00012 }
00013 
00014 MercuryModel::~MercuryModel()
00015 {
00016     //If we're a clone, don't do anything.
00017     if( m_bClone )
00018         return;
00019     unsigned int i;
00020     unsigned int num = m_meshes.size();
00021     for (i = 0; i < num; i++)
00022         MESHMAN.UnregMesh( m_meshes[i] );
00023 }
00024 
00025 void MercuryModel::AddMesh(MercuryMesh* mesh)
00026 {
00027     m_meshes.push_back(mesh);
00028     AddObject(mesh);
00029 }
00030 
00031 void MercuryModel::AddMaterial(MercuryMaterial* material)
00032 {
00033     ASSERT(material);
00034     m_materials.push_back(material);
00035 }
00036 
00037 void MercuryModel::OpenFile(const MString& path)
00038 {
00039     SAFE_DELETE(m_file);
00040     m_file = FILEMAN.Open(path);
00041 
00042     if (m_file==NULL)
00043     {   
00044         LOG.Warn("File not oppened:" + path);
00045         throw;
00046     }
00047 }
00048 
00049 void MercuryModel::Load(MString path)
00050 {
00051     m_path =    path.substr(0, path.find_last_of("/")+1);
00052     m_filename = path.substr(path.find_last_of("/")+1);
00053     OpenFile(path);
00054     LoadModel();
00055     CloseFile();
00056 }
00057 
00058 bool MercuryModel::ReadLine()
00059 {
00060     //This will strip out comments for us.
00061     do
00062     {
00063         if (!m_file->Eof())
00064             m_file->ReadLine(m_line);
00065         else
00066             return false;
00067 
00068         if (!m_line.empty())
00069         {
00070             //Remove leading whitespace
00071             while ((m_line.find(" ") == 0)||(m_line.find("\t") == 0))
00072                 m_line = m_line.substr(1, m_line.length());
00073 
00074             //Remove comments
00075             int comment = m_line.find("//");
00076             if (comment >= 0)
00077                 m_line = m_line.substr(0,comment);
00078 
00079             //Remove trailing whitespace
00080             while ((m_line.rfind(" ") == 0)||(m_line.rfind("\t") == 0))
00081                 m_line = m_line.substr(0, m_line.length()-1);
00082         }
00083     }
00084     while (m_line.length() <= 0);
00085 
00086     return true;
00087 }
00088 
00089 bool MercuryModel::Command( PStack & ret, const char * command, PStack & args )
00090 {
00091     if ( strcmp( command, "LoadModel" ) == 0 )
00092     {
00093         MString Parameter = args.PopItem().GetValueS();
00094         while( args.GetSize() )
00095             Parameter = args.PopItem().GetValueS() + "," + Parameter;
00096         Load( Parameter );
00097         return true;
00098     }
00099     return MercuryObject::Command( ret, command, args );
00100 }
00101 
00102 void MercuryModel::EnumerateCommands( MVector< MString > & toAdd )
00103 {
00104     toAdd.push_back( "LoadModel" );
00105     MercuryObject::EnumerateCommands( toAdd );
00106 }
00107 
00108 /* 
00109  * Copyright (c) 2005-2006 Joshua Allen
00110  * All rights reserved.
00111  *
00112  * Redistribution and use in source and binary forms, with or
00113  * without modification, are permitted provided that the following
00114  * conditions are met:
00115  *  -   Redistributions of source code must retain the above
00116  *      copyright notice, this list of conditions and the following disclaimer.
00117  *  -   Redistributions in binary form must reproduce the above copyright
00118  *      notice, this list of conditions and the following disclaimer in
00119  *      the documentation and/or other materials provided with the distribution.
00120  *  -   Neither the name of the Mercury Engine nor the names of its
00121  *      contributors may be used to endorse or promote products derived from
00122  *      this software without specific prior written permission.
00123  *
00124  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00125  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00126  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00127  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00128  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00129  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00130  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00131  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00132  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00133  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00134  */

Hosted by SourceForge.net Logo