OgreRenderSystemCapabilitiesSerializer.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef __RenderSystemCapabilitiesSerializer_H__
00029 #define __RenderSystemCapabilitiesSerializer_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 #include "OgreRenderSystemCapabilities.h"
00033 #include "OgreStringVector.h"
00034 #include "OgreDataStream.h"
00035 
00036 
00037 
00038 namespace Ogre {
00039 
00040 
00048     class _OgreExport RenderSystemCapabilitiesSerializer : public RenderSysAlloc
00049     {
00050 
00051     public:
00053         RenderSystemCapabilitiesSerializer();
00055         virtual ~RenderSystemCapabilitiesSerializer() {}
00056 
00058         void writeScript(const RenderSystemCapabilities* caps, String name, String filename);
00059 
00063         void parseScript(DataStreamPtr& stream);
00064 
00065     protected:
00066 
00067 
00068         enum CapabilityKeywordType {UNDEFINED_CAPABILITY_TYPE = 0, SET_STRING_METHOD, SET_INT_METHOD, SET_BOOL_METHOD, SET_REAL_METHOD,
00069                                 SET_CAPABILITY_ENUM_BOOL, ADD_SHADER_PROFILE_STRING};
00070         // determines what keyword is what type of capability. For example:
00071         // "automipmap" and "pbuffer" are both activated with setCapability (passing RSC_AUTOMIPMAP and RSC_PBUFFER respectivelly)
00072         // while "max_num_multi_render_targets" is an integer and has it's own method: setMaxMultiNumRenderTargets
00073         // we need to know these types to automatically parse each capability
00074         typedef map<String, CapabilityKeywordType>::type KeywordTypeMap;
00075         KeywordTypeMap mKeywordTypeMap;
00076 
00077         typedef void (RenderSystemCapabilities::*SetStringMethod)(const String&);
00078         // maps capability keywords to setCapability(String& cap) style methods
00079         typedef map<String, SetStringMethod>::type SetStringMethodDispatchTable;
00080         SetStringMethodDispatchTable mSetStringMethodDispatchTable;
00081 
00082         // SET_INT_METHOD parsing tables
00083         typedef void (RenderSystemCapabilities::*SetIntMethod)(ushort);
00084         typedef map<String, SetIntMethod>::type SetIntMethodDispatchTable;
00085         SetIntMethodDispatchTable mSetIntMethodDispatchTable;
00086 
00087         // SET_BOOL_METHOD parsing tables
00088         typedef void (RenderSystemCapabilities::*SetBoolMethod)(bool);
00089         typedef map<String, SetBoolMethod>::type SetBoolMethodDispatchTable;
00090         SetBoolMethodDispatchTable mSetBoolMethodDispatchTable;
00091 
00092         // SET_REAL_METHOD parsing tables
00093         typedef void (RenderSystemCapabilities::*SetRealMethod)(Real);
00094         typedef map<String, SetRealMethod>::type SetRealMethodDispatchTable;
00095         SetRealMethodDispatchTable mSetRealMethodDispatchTable;
00096 
00097         typedef map<String, Capabilities>::type CapabilitiesMap;
00098         CapabilitiesMap mCapabilitiesMap;
00099 
00100         inline void addCapabilitiesMapping(String name, Capabilities cap)
00101         {
00102             mCapabilitiesMap.insert(CapabilitiesMap::value_type(name, cap));
00103         }
00104 
00105 
00106         // capabilities lines for parsing are collected along with their line numbers for debugging
00107         typedef vector<std::pair<String, int> >::type CapabilitiesLinesList;
00108         // the set of states that the parser can be in
00109         enum ParseAction {PARSE_HEADER, FIND_OPEN_BRACE, COLLECT_LINES};
00110 
00111         int mCurrentLineNumber;
00112         String* mCurrentLine;
00113         DataStreamPtr mCurrentStream;
00114 
00115         RenderSystemCapabilities* mCurrentCapabilities;
00116 
00117         inline void addKeywordType(String keyword, CapabilityKeywordType type)
00118         {
00119             mKeywordTypeMap.insert(KeywordTypeMap::value_type(keyword, type));
00120         }
00121 
00122         inline CapabilityKeywordType getKeywordType(const String& keyword) const
00123         {
00124                         KeywordTypeMap::const_iterator it = mKeywordTypeMap.find(keyword);
00125             if(it != mKeywordTypeMap.end())
00126                              return (*it).second;
00127                         else
00128                         {
00129                              logParseError("Can't find the type for keyword: " + keyword);
00130                              return UNDEFINED_CAPABILITY_TYPE;
00131                         }
00132         }
00133 
00134         inline void addSetStringMethod(String keyword, SetStringMethod method)
00135         {
00136             mSetStringMethodDispatchTable.insert(SetStringMethodDispatchTable::value_type(keyword, method));
00137         }
00138 
00139         inline void callSetStringMethod(String& keyword, String& val)
00140         {
00141             SetStringMethodDispatchTable::iterator methodIter = mSetStringMethodDispatchTable.find(keyword);
00142             if (methodIter != mSetStringMethodDispatchTable.end())
00143             {
00144                             SetStringMethod m = (*methodIter).second;
00145                 (mCurrentCapabilities->*m)(val);
00146             }
00147             else
00148             {
00149                 logParseError("undefined keyword: " + keyword);
00150             }
00151         }
00152 
00153 
00154         inline void addSetIntMethod(String keyword, SetIntMethod method)
00155         {
00156             mSetIntMethodDispatchTable.insert(SetIntMethodDispatchTable::value_type(keyword, method));
00157         }
00158 
00159         inline void callSetIntMethod(String& keyword, ushort val)
00160         {
00161             SetIntMethodDispatchTable::iterator methodIter = mSetIntMethodDispatchTable.find(keyword);
00162             if (methodIter != mSetIntMethodDispatchTable.end())
00163             {
00164                             SetIntMethod m = (*methodIter).second;
00165                 (mCurrentCapabilities->*m)(val);
00166             }
00167             else
00168             {
00169                 logParseError("undefined keyword: " + keyword);
00170             }  
00171         }
00172 
00173 
00174         inline void addSetBoolMethod(String keyword, SetBoolMethod method)
00175         {
00176             mSetBoolMethodDispatchTable.insert(SetBoolMethodDispatchTable::value_type(keyword, method));
00177         }
00178 
00179         inline void callSetBoolMethod(String& keyword, bool val)
00180         {
00181             SetBoolMethodDispatchTable::iterator methodIter = mSetBoolMethodDispatchTable.find(keyword);
00182             if (methodIter != mSetBoolMethodDispatchTable.end())
00183             {
00184                             SetBoolMethod m = (*methodIter).second;
00185                 (mCurrentCapabilities->*m)(val);
00186             }
00187             else
00188             {
00189                 logParseError("undefined keyword: " + keyword);
00190                         }
00191         }
00192 
00193 
00194         inline void addSetRealMethod(String keyword, SetRealMethod method)
00195         {
00196             mSetRealMethodDispatchTable.insert(SetRealMethodDispatchTable::value_type(keyword, method));
00197         }
00198 
00199         inline void callSetRealMethod(String& keyword, Real val)
00200         {
00201             SetRealMethodDispatchTable::iterator methodIter = mSetRealMethodDispatchTable.find(keyword);
00202             if (methodIter != mSetRealMethodDispatchTable.end())
00203             {
00204                             SetRealMethod m = (*methodIter).second;
00205                 (mCurrentCapabilities->*m)(val);
00206             }
00207             else
00208             {
00209                 logParseError("undefined keyword: " + keyword);
00210                         }
00211         }
00212 
00213         inline void addShaderProfile(String& val)
00214         {
00215             mCurrentCapabilities->addShaderProfile(val);
00216         }
00217 
00218         inline void setCapabilityEnumBool(String& name, bool val)
00219         {
00220             // check for errors
00221             if(mCapabilitiesMap.find(name) == mCapabilitiesMap.end())
00222             {
00223                 logParseError("Undefined capability: " + name);
00224                 return;
00225             }
00226             // only set true capabilities, we can't unset false
00227             if(val)
00228             {
00229                 Capabilities cap = mCapabilitiesMap[name];
00230                 mCurrentCapabilities->setCapability(cap);
00231             }
00232         }
00233 
00234         void initialiaseDispatchTables();
00235 
00236         void parseCapabilitiesLines(CapabilitiesLinesList& linesList);
00237 
00238         void logParseError(const String& error) const;
00239 
00240     };
00244 }
00245 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52