OpenWalnut  1.4.0
WGEShaderDefineOptions.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <stdarg.h>
00026 
00027 #include <algorithm>
00028 #include <string>
00029 #include <vector>
00030 
00031 #include "../../common/exceptions/WPreconditionNotMet.h"
00032 #include "WGEShaderDefineOptions.h"
00033 
00034 WGEShaderDefineOptions::WGEShaderDefineOptions( std::string first,
00035                             std::string option2, std::string option3, std::string option4, std::string option5,
00036                             std::string option6, std::string option7, std::string option8, std::string option9,
00037                             std::string option10 ):
00038     WGEShaderPreprocessor(),
00039     m_options( 1, first ),
00040     m_idx( 1, 0 )
00041 {
00042     // init
00043     if( !option2.empty() )
00044     {
00045         m_options.push_back( option2 );
00046     }
00047     if( !option3.empty() )
00048     {
00049         m_options.push_back( option3 );
00050     }
00051     if( !option4.empty() )
00052     {
00053         m_options.push_back( option4 );
00054     }
00055     if( !option5.empty() )
00056     {
00057         m_options.push_back( option5 );
00058     }
00059     if( !option6.empty() )
00060     {
00061         m_options.push_back( option6 );
00062     }
00063     if( !option7.empty() )
00064     {
00065         m_options.push_back( option7 );
00066     }
00067     if( !option8.empty() )
00068     {
00069         m_options.push_back( option8 );
00070     }
00071     if( !option9.empty() )
00072     {
00073         m_options.push_back( option9 );
00074     }
00075     if( !option10.empty() )
00076     {
00077         m_options.push_back( option10 );
00078     }
00079 }
00080 
00081 WGEShaderDefineOptions::WGEShaderDefineOptions( std::vector< std::string > options ):
00082     WGEShaderPreprocessor(),
00083     m_options( options ),
00084     m_idx( 1, 0 )
00085 {
00086     WPrecond( options.size() >= 1, "You need to specify at least one option." );
00087 }
00088 
00089 WGEShaderDefineOptions::~WGEShaderDefineOptions()
00090 {
00091     // cleanup
00092 }
00093 
00094 std::string WGEShaderDefineOptions::process( const std::string& /*file*/, const std::string& code ) const
00095 {
00096     if( !getActive() )
00097     {
00098         return code;
00099     }
00100 
00101     // add a define for every active option
00102     std::stringstream ss;
00103     for( IdxList::const_iterator iter = m_idx.begin(); iter != m_idx.end(); ++iter )
00104     {
00105         ss << "#define " + getOptionName( *iter ) << std::endl;
00106     }
00107 
00108     // add the original code again
00109     ss << code;
00110     return ss.str();
00111 }
00112 
00113 const WGEShaderDefineOptions::IdxList& WGEShaderDefineOptions::getActiveOptions() const
00114 {
00115     return m_idx;
00116 }
00117 
00118 std::string WGEShaderDefineOptions::getOptionName( size_t idx ) const
00119 {
00120     WPrecond( idx < m_options.size(), "Index invalid." );
00121     return m_options[ idx ];
00122 }
00123 
00124 void WGEShaderDefineOptions::activateOption( size_t idx, bool exclusive )
00125 {
00126     WPrecond( idx < m_options.size(), "Index invalid." );
00127 
00128     // if this option is already active, avoid an update even if exclusive is true
00129     if( ( m_idx.size() == 1 ) && ( m_idx[ 0 ] == idx ) )
00130     {
00131         return;
00132     }
00133 
00134     if( exclusive )
00135     {
00136         m_idx.clear();
00137     }
00138 
00139     // is the option already active?
00140     if( std::find( m_idx.begin(), m_idx.end(), idx ) == m_idx.end() )
00141     {
00142         m_idx.push_back( idx );
00143         updated();
00144     }
00145 }
00146 
00147 void WGEShaderDefineOptions::deactivateOption( size_t idx )
00148 {
00149     IdxList::iterator iter = std::find( m_idx.begin(), m_idx.end(), idx );
00150     if( iter != m_idx.end() )
00151     {
00152         m_idx.erase( iter );
00153         updated();
00154     }
00155 }
00156 
00157 void WGEShaderDefineOptions::activateAllOptions()
00158 {
00159     // simply add all
00160     for( size_t i = 0; i < m_options.size(); ++i )
00161     {
00162         m_idx.push_back( i );
00163     }
00164 
00165     updated();
00166 }
00167 
00168 void WGEShaderDefineOptions::deactivateAllOptions()
00169 {
00170     // clear active list
00171     m_idx.clear();
00172     updated();
00173 }
00174 
00175 void WGEShaderDefineOptions::addOption( std::string opt )
00176 {
00177     WPrecond( !opt.empty(), "Options need to have a non-empty name." );
00178     if( std::find( m_options.begin(), m_options.end(), opt ) == m_options.end() )
00179     {
00180         m_options.push_back( opt );
00181 
00182         // signal update
00183         updated();
00184     }
00185 }
00186 
00187 void WGEShaderDefineOptions::setActivationList( const IdxList& newList )
00188 {
00189     if( m_idx != newList )
00190     {
00191         m_idx = newList;
00192         updated();
00193     }
00194 }
00195