[148] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
| 8 | |
---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
---|
| 11 | in the Software without restriction, including without limitation the rights |
---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
---|
| 14 | furnished to do so, subject to the following conditions: |
---|
| 15 | |
---|
| 16 | The above copyright notice and this permission notice shall be included in |
---|
| 17 | all copies or substantial portions of the Software. |
---|
| 18 | |
---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 25 | THE SOFTWARE. |
---|
| 26 | ----------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | #ifndef __RenderSystemCapabilitiesSerializer_H__ |
---|
| 29 | #define __RenderSystemCapabilitiesSerializer_H__ |
---|
| 30 | |
---|
| 31 | #include "OgrePrerequisites.h" |
---|
| 32 | #include "OgreRenderSystemCapabilities.h" |
---|
| 33 | #include "OgreStringVector.h" |
---|
| 34 | #include "OgreDataStream.h" |
---|
| 35 | #include "OgreHeaderPrefix.h" |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | namespace Ogre { |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /** \addtogroup Core |
---|
| 42 | * @{ |
---|
| 43 | */ |
---|
| 44 | /** \addtogroup RenderSystem |
---|
| 45 | * @{ |
---|
| 46 | */ |
---|
| 47 | /** Class for serializing RenderSystemCapabilities to / from a .rendercaps script.*/ |
---|
| 48 | class _OgreExport RenderSystemCapabilitiesSerializer : public RenderSysAlloc |
---|
| 49 | { |
---|
| 50 | |
---|
| 51 | public: |
---|
| 52 | /** default constructor*/ |
---|
| 53 | RenderSystemCapabilitiesSerializer(); |
---|
| 54 | /** default destructor*/ |
---|
| 55 | virtual ~RenderSystemCapabilitiesSerializer() {} |
---|
| 56 | |
---|
| 57 | /** Writes a RenderSystemCapabilities object to a data stream */ |
---|
| 58 | void writeScript(const RenderSystemCapabilities* caps, String name, String filename); |
---|
| 59 | |
---|
| 60 | /** Writes a RenderSystemCapabilities object to a string */ |
---|
| 61 | String writeString(const RenderSystemCapabilities* caps, String name); |
---|
| 62 | |
---|
| 63 | /** Parses a RenderSystemCapabilities script file passed as a stream. |
---|
| 64 | Adds it to RenderSystemCapabilitiesManager::_addRenderSystemCapabilities |
---|
| 65 | */ |
---|
| 66 | void parseScript(DataStreamPtr& stream); |
---|
| 67 | |
---|
| 68 | protected: |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | enum CapabilityKeywordType {UNDEFINED_CAPABILITY_TYPE = 0, SET_STRING_METHOD, SET_INT_METHOD, SET_BOOL_METHOD, SET_REAL_METHOD, |
---|
| 72 | SET_CAPABILITY_ENUM_BOOL, ADD_SHADER_PROFILE_STRING}; |
---|
| 73 | // determines what keyword is what type of capability. For example: |
---|
| 74 | // "automipmap" and "pbuffer" are both activated with setCapability (passing RSC_AUTOMIPMAP and RSC_PBUFFER respectivelly) |
---|
| 75 | // while "max_num_multi_render_targets" is an integer and has it's own method: setMaxMultiNumRenderTargets |
---|
| 76 | // we need to know these types to automatically parse each capability |
---|
| 77 | typedef map<String, CapabilityKeywordType>::type KeywordTypeMap; |
---|
| 78 | KeywordTypeMap mKeywordTypeMap; |
---|
| 79 | |
---|
| 80 | typedef void (RenderSystemCapabilities::*SetStringMethod)(const String&); |
---|
| 81 | // maps capability keywords to setCapability(String& cap) style methods |
---|
| 82 | typedef map<String, SetStringMethod>::type SetStringMethodDispatchTable; |
---|
| 83 | SetStringMethodDispatchTable mSetStringMethodDispatchTable; |
---|
| 84 | |
---|
| 85 | // SET_INT_METHOD parsing tables |
---|
| 86 | typedef void (RenderSystemCapabilities::*SetIntMethod)(ushort); |
---|
| 87 | typedef map<String, SetIntMethod>::type SetIntMethodDispatchTable; |
---|
| 88 | SetIntMethodDispatchTable mSetIntMethodDispatchTable; |
---|
| 89 | |
---|
| 90 | // SET_BOOL_METHOD parsing tables |
---|
| 91 | typedef void (RenderSystemCapabilities::*SetBoolMethod)(bool); |
---|
| 92 | typedef map<String, SetBoolMethod>::type SetBoolMethodDispatchTable; |
---|
| 93 | SetBoolMethodDispatchTable mSetBoolMethodDispatchTable; |
---|
| 94 | |
---|
| 95 | // SET_REAL_METHOD parsing tables |
---|
| 96 | typedef void (RenderSystemCapabilities::*SetRealMethod)(Real); |
---|
| 97 | typedef map<String, SetRealMethod>::type SetRealMethodDispatchTable; |
---|
| 98 | SetRealMethodDispatchTable mSetRealMethodDispatchTable; |
---|
| 99 | |
---|
| 100 | typedef map<String, Capabilities>::type CapabilitiesMap; |
---|
| 101 | CapabilitiesMap mCapabilitiesMap; |
---|
| 102 | |
---|
| 103 | inline void addCapabilitiesMapping(String name, Capabilities cap) |
---|
| 104 | { |
---|
| 105 | mCapabilitiesMap.insert(CapabilitiesMap::value_type(name, cap)); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | // capabilities lines for parsing are collected along with their line numbers for debugging |
---|
| 110 | typedef vector<std::pair<String, int> >::type CapabilitiesLinesList; |
---|
| 111 | // the set of states that the parser can be in |
---|
| 112 | enum ParseAction {PARSE_HEADER, FIND_OPEN_BRACE, COLLECT_LINES}; |
---|
| 113 | |
---|
| 114 | int mCurrentLineNumber; |
---|
| 115 | String* mCurrentLine; |
---|
| 116 | DataStreamPtr mCurrentStream; |
---|
| 117 | |
---|
| 118 | RenderSystemCapabilities* mCurrentCapabilities; |
---|
| 119 | |
---|
| 120 | inline void addKeywordType(String keyword, CapabilityKeywordType type) |
---|
| 121 | { |
---|
| 122 | mKeywordTypeMap.insert(KeywordTypeMap::value_type(keyword, type)); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | inline CapabilityKeywordType getKeywordType(const String& keyword) const |
---|
| 126 | { |
---|
| 127 | KeywordTypeMap::const_iterator it = mKeywordTypeMap.find(keyword); |
---|
| 128 | if(it != mKeywordTypeMap.end()) |
---|
| 129 | return (*it).second; |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | logParseError("Can't find the type for keyword: " + keyword); |
---|
| 133 | return UNDEFINED_CAPABILITY_TYPE; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | inline void addSetStringMethod(String keyword, SetStringMethod method) |
---|
| 138 | { |
---|
| 139 | mSetStringMethodDispatchTable.insert(SetStringMethodDispatchTable::value_type(keyword, method)); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | inline void callSetStringMethod(String& keyword, String& val) |
---|
| 143 | { |
---|
| 144 | SetStringMethodDispatchTable::iterator methodIter = mSetStringMethodDispatchTable.find(keyword); |
---|
| 145 | if (methodIter != mSetStringMethodDispatchTable.end()) |
---|
| 146 | { |
---|
| 147 | SetStringMethod m = (*methodIter).second; |
---|
| 148 | (mCurrentCapabilities->*m)(val); |
---|
| 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | logParseError("undefined keyword: " + keyword); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | inline void addSetIntMethod(String keyword, SetIntMethod method) |
---|
| 158 | { |
---|
| 159 | mSetIntMethodDispatchTable.insert(SetIntMethodDispatchTable::value_type(keyword, method)); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | inline void callSetIntMethod(String& keyword, ushort val) |
---|
| 163 | { |
---|
| 164 | SetIntMethodDispatchTable::iterator methodIter = mSetIntMethodDispatchTable.find(keyword); |
---|
| 165 | if (methodIter != mSetIntMethodDispatchTable.end()) |
---|
| 166 | { |
---|
| 167 | SetIntMethod m = (*methodIter).second; |
---|
| 168 | (mCurrentCapabilities->*m)(val); |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | logParseError("undefined keyword: " + keyword); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | inline void addSetBoolMethod(String keyword, SetBoolMethod method) |
---|
| 178 | { |
---|
| 179 | mSetBoolMethodDispatchTable.insert(SetBoolMethodDispatchTable::value_type(keyword, method)); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | inline void callSetBoolMethod(String& keyword, bool val) |
---|
| 183 | { |
---|
| 184 | SetBoolMethodDispatchTable::iterator methodIter = mSetBoolMethodDispatchTable.find(keyword); |
---|
| 185 | if (methodIter != mSetBoolMethodDispatchTable.end()) |
---|
| 186 | { |
---|
| 187 | SetBoolMethod m = (*methodIter).second; |
---|
| 188 | (mCurrentCapabilities->*m)(val); |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
| 192 | logParseError("undefined keyword: " + keyword); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | inline void addSetRealMethod(String keyword, SetRealMethod method) |
---|
| 198 | { |
---|
| 199 | mSetRealMethodDispatchTable.insert(SetRealMethodDispatchTable::value_type(keyword, method)); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | inline void callSetRealMethod(String& keyword, Real val) |
---|
| 203 | { |
---|
| 204 | SetRealMethodDispatchTable::iterator methodIter = mSetRealMethodDispatchTable.find(keyword); |
---|
| 205 | if (methodIter != mSetRealMethodDispatchTable.end()) |
---|
| 206 | { |
---|
| 207 | SetRealMethod m = (*methodIter).second; |
---|
| 208 | (mCurrentCapabilities->*m)(val); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | logParseError("undefined keyword: " + keyword); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | inline void addShaderProfile(String& val) |
---|
| 217 | { |
---|
| 218 | mCurrentCapabilities->addShaderProfile(val); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | inline void setCapabilityEnumBool(String& name, bool val) |
---|
| 222 | { |
---|
| 223 | // check for errors |
---|
| 224 | if(mCapabilitiesMap.find(name) == mCapabilitiesMap.end()) |
---|
| 225 | { |
---|
| 226 | logParseError("Undefined capability: " + name); |
---|
| 227 | return; |
---|
| 228 | } |
---|
| 229 | // only set true capabilities, we can't unset false |
---|
| 230 | if(val) |
---|
| 231 | { |
---|
| 232 | Capabilities cap = mCapabilitiesMap[name]; |
---|
| 233 | mCurrentCapabilities->setCapability(cap); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | void initialiaseDispatchTables(); |
---|
| 238 | |
---|
| 239 | void parseCapabilitiesLines(CapabilitiesLinesList& linesList); |
---|
| 240 | |
---|
| 241 | void logParseError(const String& error) const; |
---|
| 242 | |
---|
| 243 | }; |
---|
| 244 | /** @} */ |
---|
| 245 | /** @} */ |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | #include "OgreHeaderSuffix.h" |
---|
| 250 | |
---|
| 251 | #endif |
---|