OpenWalnut 1.2.5
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 
00029 #include "../../common/exceptions/WPreconditionNotMet.h"
00030 
00031 #include "WGEShaderDefineOptions.h"
00032 
00033 WGEShaderDefineOptions::WGEShaderDefineOptions( std::string first,
00034                             std::string option2, std::string option3, std::string option4, std::string option5,
00035                             std::string option6, std::string option7, std::string option8, std::string option9,
00036                             std::string option10 ):
00037     WGEShaderPreprocessor(),
00038     m_options( 1, first ),
00039     m_idx( 1, 0 )
00040 {
00041     // init
00042     if( !option2.empty() )
00043     {
00044         m_options.push_back( option2 );
00045     }
00046     if( !option3.empty() )
00047     {
00048         m_options.push_back( option3 );
00049     }
00050     if( !option4.empty() )
00051     {
00052         m_options.push_back( option4 );
00053     }
00054     if( !option5.empty() )
00055     {
00056         m_options.push_back( option5 );
00057     }
00058     if( !option6.empty() )
00059     {
00060         m_options.push_back( option6 );
00061     }
00062     if( !option7.empty() )
00063     {
00064         m_options.push_back( option7 );
00065     }
00066     if( !option8.empty() )
00067     {
00068         m_options.push_back( option8 );
00069     }
00070     if( !option9.empty() )
00071     {
00072         m_options.push_back( option9 );
00073     }
00074     if( !option10.empty() )
00075     {
00076         m_options.push_back( option10 );
00077     }
00078 }
00079 
00080 WGEShaderDefineOptions::WGEShaderDefineOptions( std::vector< std::string > options ):
00081     WGEShaderPreprocessor(),
00082     m_options( options ),
00083     m_idx( 1, 0 )
00084 {
00085     WPrecond( options.size() >= 1, "You need to specify at least one option." );
00086 }
00087 
00088 WGEShaderDefineOptions::~WGEShaderDefineOptions()
00089 {
00090     // cleanup
00091 }
00092 
00093 std::string WGEShaderDefineOptions::process( const std::string& /*file*/, const std::string& code ) const
00094 {
00095     if( !getActive() )
00096     {
00097         return code;
00098     }
00099 
00100     // add a define for every active option
00101     std::stringstream ss;
00102     for( IdxList::const_iterator iter = m_idx.begin(); iter != m_idx.end(); ++iter )
00103     {
00104         ss << "#define " + getOptionName( *iter ) << std::endl;
00105     }
00106 
00107     // add the original code again
00108     ss << code;
00109     return ss.str();
00110 }
00111 
00112 const WGEShaderDefineOptions::IdxList& WGEShaderDefineOptions::getActiveOptions() const
00113 {
00114     return m_idx;
00115 }
00116 
00117 std::string WGEShaderDefineOptions::getOptionName( size_t idx ) const
00118 {
00119     WPrecond( idx < m_options.size(), "Index invalid." );
00120     return m_options[ idx ];
00121 }
00122 
00123 void WGEShaderDefineOptions::activateOption( size_t idx, bool exclusive )
00124 {
00125     WPrecond( idx < m_options.size(), "Index invalid." );
00126 
00127     if( exclusive )
00128     {
00129         m_idx.clear();
00130     }
00131 
00132     // is the option already active?
00133     if( std::find( m_idx.begin(), m_idx.end(), idx ) == m_idx.end() )
00134     {
00135         m_idx.push_back( idx );
00136         updated();
00137     }
00138 }
00139 
00140 void WGEShaderDefineOptions::dactivateOption( size_t idx )
00141 {
00142     IdxList::iterator iter = std::find( m_idx.begin(), m_idx.end(), idx );
00143     if( iter != m_idx.end() )
00144     {
00145         m_idx.erase( iter );
00146         updated();
00147     }
00148 }
00149 
00150 void WGEShaderDefineOptions::activateAllOptions()
00151 {
00152     // simply add all
00153     for( size_t i = 0; i < m_options.size(); ++i )
00154     {
00155         m_idx.push_back( i );
00156     }
00157 
00158     updated();
00159 }
00160 
00161 void WGEShaderDefineOptions::deactivateAllOptions()
00162 {
00163     // clear active list
00164     m_idx.clear();
00165     updated();
00166 }
00167 
00168 void WGEShaderDefineOptions::addOption( std::string opt )
00169 {
00170     WPrecond( !opt.empty(), "Options need to have a non-empty name." );
00171     if( std::find( m_options.begin(), m_options.end(), opt ) == m_options.end() )
00172     {
00173         m_options.push_back( opt );
00174 
00175         // signal update
00176         updated();
00177     }
00178 }
00179 
00180 void WGEShaderDefineOptions::setActivationList( const IdxList& newList )
00181 {
00182     if( m_idx != newList )
00183     {
00184         m_idx = newList;
00185         updated();
00186     }
00187 }
00188 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends