diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8.1/doc/html/example025.html | 588 |
1 files changed, 588 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8.1/doc/html/example025.html b/libraries/irrlicht-1.8.1/doc/html/example025.html new file mode 100644 index 0000000..a79b0fe --- /dev/null +++ b/libraries/irrlicht-1.8.1/doc/html/example025.html | |||
@@ -0,0 +1,588 @@ | |||
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
2 | <html xmlns="http://www.w3.org/1999/xhtml"> | ||
3 | <head> | ||
4 | <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> | ||
5 | <title>Irrlicht 3D Engine: Tutorial 25: Xml Handling</title> | ||
6 | |||
7 | <link href="tabs.css" rel="stylesheet" type="text/css"/> | ||
8 | <link href="doxygen.css" rel="stylesheet" type="text/css" /> | ||
9 | <link href="navtree.css" rel="stylesheet" type="text/css"/> | ||
10 | <script type="text/javascript" src="jquery.js"></script> | ||
11 | <script type="text/javascript" src="resize.js"></script> | ||
12 | <script type="text/javascript" src="navtree.js"></script> | ||
13 | <script type="text/javascript"> | ||
14 | $(document).ready(initResizable); | ||
15 | </script> | ||
16 | <link href="search/search.css" rel="stylesheet" type="text/css"/> | ||
17 | <script type="text/javascript" src="search/search.js"></script> | ||
18 | <script type="text/javascript"> | ||
19 | $(document).ready(function() { searchBox.OnSelectItem(0); }); | ||
20 | </script> | ||
21 | |||
22 | </head> | ||
23 | <body> | ||
24 | <div id="top"><!-- do not remove this div! --> | ||
25 | |||
26 | |||
27 | <div id="titlearea"> | ||
28 | <table cellspacing="0" cellpadding="0"> | ||
29 | <tbody> | ||
30 | <tr style="height: 56px;"> | ||
31 | |||
32 | <td id="projectlogo"><img alt="Logo" src="irrlichtlogo.png"/></td> | ||
33 | |||
34 | |||
35 | <td style="padding-left: 0.5em;"> | ||
36 | <div id="projectname">Irrlicht 3D Engine | ||
37 | |||
38 | </div> | ||
39 | |||
40 | </td> | ||
41 | |||
42 | |||
43 | |||
44 | |||
45 | <td> <div id="MSearchBox" class="MSearchBoxInactive"> | ||
46 | <span class="left"> | ||
47 | <img id="MSearchSelect" src="search/mag_sel.png" | ||
48 | onmouseover="return searchBox.OnSearchSelectShow()" | ||
49 | onmouseout="return searchBox.OnSearchSelectHide()" | ||
50 | alt=""/> | ||
51 | <input type="text" id="MSearchField" value="Search" accesskey="S" | ||
52 | onfocus="searchBox.OnSearchFieldFocus(true)" | ||
53 | onblur="searchBox.OnSearchFieldFocus(false)" | ||
54 | onkeyup="searchBox.OnSearchFieldChange(event)"/> | ||
55 | </span><span class="right"> | ||
56 | <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> | ||
57 | </span> | ||
58 | </div> | ||
59 | </td> | ||
60 | |||
61 | |||
62 | </tr> | ||
63 | </tbody> | ||
64 | </table> | ||
65 | </div> | ||
66 | |||
67 | <!-- Generated by Doxygen 1.7.5.1 --> | ||
68 | <script type="text/javascript"> | ||
69 | var searchBox = new SearchBox("searchBox", "search",false,'Search'); | ||
70 | </script> | ||
71 | <script type="text/javascript" src="dynsections.js"></script> | ||
72 | </div> | ||
73 | <div id="side-nav" class="ui-resizable side-nav-resizable"> | ||
74 | <div id="nav-tree"> | ||
75 | <div id="nav-tree-contents"> | ||
76 | </div> | ||
77 | </div> | ||
78 | <div id="splitbar" style="-moz-user-select:none;" | ||
79 | class="ui-resizable-handle"> | ||
80 | </div> | ||
81 | </div> | ||
82 | <script type="text/javascript"> | ||
83 | initNavTree('example025.html',''); | ||
84 | </script> | ||
85 | <div id="doc-content"> | ||
86 | <div class="header"> | ||
87 | <div class="headertitle"> | ||
88 | <div class="title">Tutorial 25: Xml Handling </div> </div> | ||
89 | </div> | ||
90 | <div class="contents"> | ||
91 | <div class="textblock"><div class="image"> | ||
92 | <img src="025shot.jpg" alt="025shot.jpg"/> | ||
93 | </div> | ||
94 | <p>Demonstrates loading and saving of configurations via XML</p> | ||
95 | <dl class="author"><dt><b>Author:</b></dt><dd>Y.M. Bosman <<a href="mailto:yoran.bosman@gmail.com">yoran.bosman@gmail.com</a>></dd></dl> | ||
96 | <p>This demo features a fully usable system for configuration handling. The code can easily be integrated into own apps.</p> | ||
97 | <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>></span> | ||
98 | |||
99 | <span class="keyword">using namespace </span>irr; | ||
100 | <span class="keyword">using namespace </span>core; | ||
101 | <span class="keyword">using namespace </span>scene; | ||
102 | <span class="keyword">using namespace </span>video; | ||
103 | <span class="keyword">using namespace </span>io; | ||
104 | <span class="keyword">using namespace </span>gui; | ||
105 | |||
106 | <span class="preprocessor">#ifdef _IRR_WINDOWS_</span> | ||
107 | <span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span> | ||
108 | <span class="preprocessor">#endif</span> | ||
109 | </pre></div><p> SettingManager class.</p> | ||
110 | <p>This class loads and writes the settings and manages the options.</p> | ||
111 | <p>The class makes use of irrMap which is a an associative arrays using a red-black tree it allows easy mapping of a key to a value, along the way there is some information on how to use it. </p> | ||
112 | <div class="fragment"><pre class="fragment"><span class="keyword">class </span>SettingManager | ||
113 | { | ||
114 | <span class="keyword">public</span>: | ||
115 | |||
116 | <span class="comment">// Construct setting managers and set default settings</span> | ||
117 | SettingManager(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& settings_file): SettingsFile(settings_file), NullDevice(0) | ||
118 | { | ||
119 | <span class="comment">// Irrlicht null device, we want to load settings before we actually created our device, therefore, nulldevice</span> | ||
120 | NullDevice = <a class="code" href="namespaceirr.html#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.">irr::createDevice</a>(<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0acfdbd476cbfd4d05e72f9adffcc42210" title="Null driver, useful for applications to run the engine without visualisation.">irr::video::EDT_NULL</a>); | ||
121 | |||
122 | <span class="comment">//DriverOptions is an irrlicht map,</span> | ||
123 | <span class="comment">//we can insert values in the map in two ways by calling insert(key,value) or by using the [key] operator</span> | ||
124 | <span class="comment">//the [] operator overrides values if they already exist</span> | ||
125 | DriverOptions.insert(L<span class="stringliteral">"Software"</span>, <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a1598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">EDT_SOFTWARE</a>); | ||
126 | DriverOptions.insert(L<span class="stringliteral">"OpenGL"</span>, <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a2715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">EDT_OPENGL</a>); | ||
127 | DriverOptions.insert(L<span class="stringliteral">"Direct3D9"</span>, <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a4691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">EDT_DIRECT3D9</a>); | ||
128 | |||
129 | <span class="comment">//some resolution options</span> | ||
130 | ResolutionOptions.insert(L<span class="stringliteral">"640x480"</span>, <a class="code" href="namespaceirr_1_1core.html#ad2e562e3219072e2f7fc7c2bba0ef0cb" title="Typedef for an unsigned integer dimension.">dimension2du</a>(640,480)); | ||
131 | ResolutionOptions.insert(L<span class="stringliteral">"800x600"</span>, <a class="code" href="namespaceirr_1_1core.html#ad2e562e3219072e2f7fc7c2bba0ef0cb" title="Typedef for an unsigned integer dimension.">dimension2du</a>(800,600)); | ||
132 | ResolutionOptions.insert(L<span class="stringliteral">"1024x768"</span>, <a class="code" href="namespaceirr_1_1core.html#ad2e562e3219072e2f7fc7c2bba0ef0cb" title="Typedef for an unsigned integer dimension.">dimension2du</a>(1024,768)); | ||
133 | |||
134 | <span class="comment">//our preferred defaults</span> | ||
135 | SettingMap.insert(L<span class="stringliteral">"driver"</span>, L<span class="stringliteral">"Direct3D9"</span>); | ||
136 | SettingMap.insert(L<span class="stringliteral">"resolution"</span>, L<span class="stringliteral">"640x480"</span>); | ||
137 | SettingMap.insert(L<span class="stringliteral">"fullscreen"</span>, L<span class="stringliteral">"0"</span>); <span class="comment">//0 is false</span> | ||
138 | } | ||
139 | |||
140 | <span class="comment">// Destructor, you could store settings automatically on exit of your</span> | ||
141 | <span class="comment">// application if you wanted to in our case we simply drop the</span> | ||
142 | <span class="comment">// nulldevice</span> | ||
143 | ~SettingManager() | ||
144 | { | ||
145 | <span class="keywordflow">if</span> (NullDevice) | ||
146 | { | ||
147 | NullDevice->closeDevice(); | ||
148 | NullDevice->drop(); | ||
149 | } | ||
150 | }; | ||
151 | </pre></div><p>Load xml from disk, overwrite default settings The xml we are trying to load has the following structure settings nested in sections nested in the root node, like so </p> | ||
152 | <pre> | ||
153 | <?xml version="1.0"?> | ||
154 | <mygame> | ||
155 | <video> | ||
156 | <setting name="driver" value="Direct3D9"> | ||
157 | <setting name="fullscreen" value="0"> | ||
158 | <setting name="resolution" value="1024x768"> | ||
159 | </video> | ||
160 | </mygame> | ||
161 | </pre> <div class="fragment"><pre class="fragment"> <span class="keywordtype">bool</span> load() | ||
162 | { | ||
163 | <span class="comment">//if not able to create device don't attempt to load</span> | ||
164 | <span class="keywordflow">if</span> (!NullDevice) | ||
165 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
166 | |||
167 | <a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html" title="Interface providing easy read access to a XML file.">irr::io::IXMLReader</a>* xml = NullDevice->getFileSystem()->createXMLReader(SettingsFile); <span class="comment">//create xml reader</span> | ||
168 | <span class="keywordflow">if</span> (!xml) | ||
169 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
170 | |||
171 | <span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> settingTag(L<span class="stringliteral">"setting"</span>); <span class="comment">//we'll be looking for this tag in the xml</span> | ||
172 | <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> currentSection; <span class="comment">//keep track of our current section</span> | ||
173 | <span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> videoTag(L<span class="stringliteral">"video"</span>); <span class="comment">//constant for videotag</span> | ||
174 | |||
175 | <span class="comment">//while there is more to read</span> | ||
176 | <span class="keywordflow">while</span> (xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a157f458f7dabeeff173f72a0fb443a8e" title="Reads forward to the next xml node.">read</a>()) | ||
177 | { | ||
178 | <span class="comment">//check the node type</span> | ||
179 | <span class="keywordflow">switch</span> (xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a3482e8e6bdc15965fc6a0bcef6e9a8e0" title="Returns the type of the current XML node.">getNodeType</a>()) | ||
180 | { | ||
181 | <span class="comment">//we found a new element</span> | ||
182 | <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1io.html#a86a02676c9cbb822e04d60c81b4f33eda9df4f5baccc23a0ad1f6fa64d8de2fc0" title="An xml element such as <foo>.">irr::io::EXN_ELEMENT</a>: | ||
183 | { | ||
184 | <span class="comment">//we currently are in the empty or mygame section and find the video tag so we set our current section to video</span> | ||
185 | <span class="keywordflow">if</span> (currentSection.empty() && videoTag.equals_ignore_case(xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a7d745b130c895d0f910f191d04e20e87" title="Returns the name of the current node.">getNodeName</a>())) | ||
186 | { | ||
187 | currentSection = videoTag; | ||
188 | } | ||
189 | <span class="comment">//we are in the video section and we find a setting to parse</span> | ||
190 | <span class="keywordflow">else</span> <span class="keywordflow">if</span> (currentSection.equals_ignore_case(videoTag) && settingTag.equals_ignore_case(xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a7d745b130c895d0f910f191d04e20e87" title="Returns the name of the current node.">getNodeName</a>() )) | ||
191 | { | ||
192 | <span class="comment">//read in the key</span> | ||
193 | <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> key = xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a7674852b2e24b2710b90aab10ef1fc22" title="Returns the value of an attribute in a safe way.">getAttributeValueSafe</a>(L<span class="stringliteral">"name"</span>); | ||
194 | <span class="comment">//if there actually is a key to set</span> | ||
195 | <span class="keywordflow">if</span> (!key.empty()) | ||
196 | { | ||
197 | <span class="comment">//set the setting in the map to the value,</span> | ||
198 | <span class="comment">//the [] operator overrides values if they already exist or inserts a new key value</span> | ||
199 | <span class="comment">//pair into the settings map if it was not defined yet</span> | ||
200 | SettingMap[key] = xml-><a class="code" href="classirr_1_1io_1_1_i_irr_x_m_l_reader.html#a7674852b2e24b2710b90aab10ef1fc22" title="Returns the value of an attribute in a safe way.">getAttributeValueSafe</a>(L<span class="stringliteral">"value"</span>); | ||
201 | } | ||
202 | } | ||
203 | |||
204 | <span class="comment">//..</span> | ||
205 | <span class="comment">// You can add your own sections and tags to read in here</span> | ||
206 | <span class="comment">//..</span> | ||
207 | } | ||
208 | <span class="keywordflow">break</span>; | ||
209 | |||
210 | <span class="comment">//we found the end of an element</span> | ||
211 | <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1io.html#a86a02676c9cbb822e04d60c81b4f33eda54ef1997279f08180634f4a897f771b8" title="End of an xml element such as </foo>.">irr::io::EXN_ELEMENT_END</a>: | ||
212 | <span class="comment">//we were at the end of the video section so we reset our tag</span> | ||
213 | currentSection=L<span class="stringliteral">""</span>; | ||
214 | <span class="keywordflow">break</span>; | ||
215 | } | ||
216 | } | ||
217 | |||
218 | <span class="comment">// don't forget to delete the xml reader</span> | ||
219 | xml->drop(); | ||
220 | |||
221 | <span class="keywordflow">return</span> <span class="keyword">true</span>; | ||
222 | } | ||
223 | |||
224 | <span class="comment">// Save the xml to disk. We use the nulldevice.</span> | ||
225 | <span class="keywordtype">bool</span> save() | ||
226 | { | ||
227 | |||
228 | <span class="comment">//if not able to create device don't attempt to save</span> | ||
229 | <span class="keywordflow">if</span> (!NullDevice) | ||
230 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
231 | |||
232 | <span class="comment">//create xml writer</span> | ||
233 | <a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html" title="Interface providing methods for making it easier to write XML files.">irr::io::IXMLWriter</a>* xwriter = NullDevice->getFileSystem()->createXMLWriter( SettingsFile ); | ||
234 | <span class="keywordflow">if</span> (!xwriter) | ||
235 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
236 | |||
237 | <span class="comment">//write out the obligatory xml header. Each xml-file needs to have exactly one of those.</span> | ||
238 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a66fd00f6528fc967e53ea2a83f4fbf09" title="Writes an xml 1.0 header.">writeXMLHeader</a>(); | ||
239 | |||
240 | <span class="comment">//start element mygame, you replace the label "mygame" with anything you want</span> | ||
241 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a09ffde58db20f23b7eba1bf08e1daf42">writeElement</a>(L<span class="stringliteral">"mygame"</span>); | ||
242 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a98d9b558d991211f77f6d3f2f68d30d2" title="Writes a line break.">writeLineBreak</a>(); <span class="comment">//new line</span> | ||
243 | |||
244 | <span class="comment">//start section with video settings</span> | ||
245 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a09ffde58db20f23b7eba1bf08e1daf42">writeElement</a>(L<span class="stringliteral">"video"</span>); | ||
246 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a98d9b558d991211f77f6d3f2f68d30d2" title="Writes a line break.">writeLineBreak</a>(); <span class="comment">//new line</span> | ||
247 | |||
248 | <span class="comment">// getIterator gets us a pointer to the first node of the settings map</span> | ||
249 | <span class="comment">// every iteration we increase the iterator which gives us the next map node</span> | ||
250 | <span class="comment">// until we reach the end we write settings one by one by using the nodes key and value functions</span> | ||
251 | map<stringw, stringw>::Iterator i = SettingMap.getIterator(); | ||
252 | <span class="keywordflow">for</span>(; !i.atEnd(); i++) | ||
253 | { | ||
254 | <span class="comment">//write element as <setting name="key" value="x" /></span> | ||
255 | <span class="comment">//the second parameter indicates this is an empty element with no children, just attributes</span> | ||
256 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a09ffde58db20f23b7eba1bf08e1daf42">writeElement</a>(L<span class="stringliteral">"setting"</span>,<span class="keyword">true</span>, L<span class="stringliteral">"name"</span>, i->getKey().c_str(), L<span class="stringliteral">"value"</span>,i->getValue().c_str() ); | ||
257 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a98d9b558d991211f77f6d3f2f68d30d2" title="Writes a line break.">writeLineBreak</a>(); | ||
258 | } | ||
259 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a98d9b558d991211f77f6d3f2f68d30d2" title="Writes a line break.">writeLineBreak</a>(); | ||
260 | |||
261 | <span class="comment">//close video section</span> | ||
262 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a904c931fe03455eee04fcf41ef519715" title="Writes the closing tag for an element. Like "</foo>".">writeClosingTag</a>(L<span class="stringliteral">"video"</span>); | ||
263 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a98d9b558d991211f77f6d3f2f68d30d2" title="Writes a line break.">writeLineBreak</a>(); | ||
264 | |||
265 | <span class="comment">//..</span> | ||
266 | <span class="comment">// You can add writing sound settings, savegame information etc</span> | ||
267 | <span class="comment">//..</span> | ||
268 | |||
269 | <span class="comment">//close mygame section</span> | ||
270 | xwriter-><a class="code" href="classirr_1_1io_1_1_i_x_m_l_writer.html#a904c931fe03455eee04fcf41ef519715" title="Writes the closing tag for an element. Like "</foo>".">writeClosingTag</a>(L<span class="stringliteral">"mygame"</span>); | ||
271 | |||
272 | <span class="comment">//delete xml writer</span> | ||
273 | xwriter-><a class="code" href="classirr_1_1_i_reference_counted.html#afb169a857e0d2cdb96b8821cb9bff17a" title="Drops the object. Decrements the reference counter by one.">drop</a>(); | ||
274 | |||
275 | <span class="keywordflow">return</span> <span class="keyword">true</span>; | ||
276 | } | ||
277 | |||
278 | <span class="comment">// Set setting in our manager</span> | ||
279 | <span class="keywordtype">void</span> setSetting(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& name, <span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& value) | ||
280 | { | ||
281 | SettingMap[name]=value; | ||
282 | } | ||
283 | |||
284 | <span class="comment">// set setting overload to quickly assign integers to our setting map</span> | ||
285 | <span class="keywordtype">void</span> setSetting(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& name, <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> value) | ||
286 | { | ||
287 | SettingMap[name]=<a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>(value); | ||
288 | } | ||
289 | |||
290 | <span class="comment">// Get setting as string</span> | ||
291 | <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> getSetting(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& key)<span class="keyword"> const</span> | ||
292 | <span class="keyword"> </span>{ | ||
293 | <span class="comment">//the find function or irrmap returns a pointer to a map Node</span> | ||
294 | <span class="comment">//if the key can be found, otherwise it returns null</span> | ||
295 | <span class="comment">//the map node has the function getValue and getKey, as we already know the key, we return node->getValue()</span> | ||
296 | map<stringw, stringw>::Node* n = SettingMap.find(key); | ||
297 | <span class="keywordflow">if</span> (n) | ||
298 | <span class="keywordflow">return</span> n->getValue(); | ||
299 | <span class="keywordflow">else</span> | ||
300 | <span class="keywordflow">return</span> L<span class="stringliteral">""</span>; | ||
301 | } | ||
302 | |||
303 | <span class="comment">//</span> | ||
304 | <span class="keywordtype">bool</span> getSettingAsBoolean(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& key )<span class="keyword"> const</span> | ||
305 | <span class="keyword"> </span>{ | ||
306 | <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> s = getSetting(key); | ||
307 | <span class="keywordflow">if</span> (s.empty()) | ||
308 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
309 | <span class="keywordflow">return</span> s.equals_ignore_case(L<span class="stringliteral">"1"</span>); | ||
310 | } | ||
311 | |||
312 | <span class="comment">//</span> | ||
313 | <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> getSettingAsInteger(<span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a>& key)<span class="keyword"> const</span> | ||
314 | <span class="keyword"> </span>{ | ||
315 | <span class="comment">//we implicitly cast to string instead of stringw because strtol10 does not accept wide strings</span> | ||
316 | <span class="keyword">const</span> <a class="code" href="namespaceirr_1_1core.html#ade1071a878633f2f6d8a75c5d11fec19" title="Typedef for character strings.">stringc</a> s = getSetting(key); | ||
317 | <span class="keywordflow">if</span> (s.empty()) | ||
318 | <span class="keywordflow">return</span> 0; | ||
319 | |||
320 | <span class="keywordflow">return</span> <a class="code" href="namespaceirr_1_1core.html#a6ff97e442233218d18acd56add48766f" title="Convert a simple string of base 10 digits into a signed 32 bit integer.">strtol10</a>(s.c_str()); | ||
321 | } | ||
322 | |||
323 | <span class="keyword">public</span>: | ||
324 | map<stringw, s32> DriverOptions; <span class="comment">//available options for driver config</span> | ||
325 | map<stringw, dimension2du> ResolutionOptions; <span class="comment">//available options for resolution config</span> | ||
326 | <span class="keyword">private</span>: | ||
327 | SettingManager(<span class="keyword">const</span> SettingManager& other); <span class="comment">// defined but not implemented</span> | ||
328 | SettingManager& operator=(<span class="keyword">const</span> SettingManager& other); <span class="comment">// defined but not implemented</span> | ||
329 | |||
330 | map<stringw, stringw> SettingMap; <span class="comment">//current config</span> | ||
331 | |||
332 | <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">stringw</a> SettingsFile; <span class="comment">// location of the xml, usually the</span> | ||
333 | <a class="code" href="classirr_1_1_irrlicht_device.html" title="The Irrlicht device. You can create it with createDevice() or createDeviceEx().">irr::IrrlichtDevice</a>* NullDevice; | ||
334 | }; | ||
335 | </pre></div><p>Application context for global variables </p> | ||
336 | <div class="fragment"><pre class="fragment"><span class="keyword">struct </span>SAppContext | ||
337 | { | ||
338 | SAppContext() | ||
339 | : Device(0),Gui(0), Driver(0), Settings(0), ShouldQuit(false), | ||
340 | ButtonSave(0), ButtonExit(0), ListboxDriver(0), | ||
341 | ListboxResolution(0), CheckboxFullscreen(0) | ||
342 | { | ||
343 | } | ||
344 | |||
345 | ~SAppContext() | ||
346 | { | ||
347 | <span class="keywordflow">if</span> (Settings) | ||
348 | <span class="keyword">delete</span> Settings; | ||
349 | |||
350 | <span class="keywordflow">if</span> (Device) | ||
351 | { | ||
352 | Device->closeDevice(); | ||
353 | Device->drop(); | ||
354 | } | ||
355 | } | ||
356 | |||
357 | IrrlichtDevice* Device; | ||
358 | IGUIEnvironment* Gui; | ||
359 | IVideoDriver* Driver; | ||
360 | SettingManager* Settings; | ||
361 | <span class="keywordtype">bool</span> ShouldQuit; | ||
362 | |||
363 | <span class="comment">//settings dialog</span> | ||
364 | IGUIButton* ButtonSave; | ||
365 | IGUIButton* ButtonExit; | ||
366 | IGUIListBox* ListboxDriver; | ||
367 | IGUIListBox* ListboxResolution; | ||
368 | IGUICheckBox* CheckboxFullscreen; | ||
369 | }; | ||
370 | </pre></div><p>A typical event receiver. </p> | ||
371 | <div class="fragment"><pre class="fragment"><span class="keyword">class </span>MyEventReceiver : <span class="keyword">public</span> IEventReceiver | ||
372 | { | ||
373 | <span class="keyword">public</span>: | ||
374 | MyEventReceiver(SAppContext & a) : App(a) { } | ||
375 | |||
376 | <span class="keyword">virtual</span> <span class="keywordtype">bool</span> OnEvent(<span class="keyword">const</span> SEvent& event) | ||
377 | { | ||
378 | <span class="keywordflow">if</span> (event.EventType == <a class="code" href="namespaceirr.html#ac9eed96e06e85ce3c86fcbbbe9e48a0cae85bb44dd09a29c879d64a05047fc1d2" title="An event of the graphical user interface.">EET_GUI_EVENT</a> ) | ||
379 | { | ||
380 | <span class="keywordflow">switch</span> ( event.GUIEvent.EventType ) | ||
381 | { | ||
382 | <span class="comment">//handle button click events</span> | ||
383 | <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1gui.html#aeac71ad17341a4b6e9026ae11d576808a308ee345c92444931f83e48354072d98" title="A button was clicked.">EGET_BUTTON_CLICKED</a>: | ||
384 | { | ||
385 | <span class="comment">//Our save button was called so we obtain the settings from our dialog and save them</span> | ||
386 | <span class="keywordflow">if</span> ( event.GUIEvent.Caller == App.ButtonSave ) | ||
387 | { | ||
388 | <span class="comment">//if there is a selection write it</span> | ||
389 | <span class="keywordflow">if</span> ( App.ListboxDriver->getSelected() != -1) | ||
390 | App.Settings->setSetting(L<span class="stringliteral">"driver"</span>, App.ListboxDriver->getListItem(App.ListboxDriver->getSelected())); | ||
391 | |||
392 | <span class="comment">//if there is a selection write it</span> | ||
393 | <span class="keywordflow">if</span> ( App.ListboxResolution->getSelected() != -1) | ||
394 | App.Settings->setSetting(L<span class="stringliteral">"resolution"</span>, App.ListboxResolution->getListItem(App.ListboxResolution->getSelected())); | ||
395 | |||
396 | App.Settings->setSetting(L<span class="stringliteral">"fullscreen"</span>, App.CheckboxFullscreen->isChecked()); | ||
397 | |||
398 | |||
399 | <span class="keywordflow">if</span> (App.Settings->save()) | ||
400 | { | ||
401 | App.Gui->addMessageBox(L<span class="stringliteral">"settings save"</span>,L<span class="stringliteral">"settings saved, please restart for settings to change effect"</span>,<span class="stringliteral">""</span>,<span class="keyword">true</span>); | ||
402 | } | ||
403 | } | ||
404 | <span class="comment">// cancel/exit button clicked, tell the application to exit</span> | ||
405 | <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == App.ButtonExit) | ||
406 | { | ||
407 | App.ShouldQuit = <span class="keyword">true</span>; | ||
408 | } | ||
409 | } | ||
410 | <span class="keywordflow">break</span>; | ||
411 | } | ||
412 | } | ||
413 | |||
414 | <span class="keywordflow">return</span> <span class="keyword">false</span>; | ||
415 | } | ||
416 | |||
417 | <span class="keyword">private</span>: | ||
418 | SAppContext & App; | ||
419 | }; | ||
420 | </pre></div><p>Function to create a video settings dialog This dialog shows the current settings from the configuration xml and allows them to be changed </p> | ||
421 | <div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> createSettingsDialog(SAppContext& app) | ||
422 | { | ||
423 | <span class="comment">// first get rid of alpha in gui</span> | ||
424 | <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">irr::s32</a> i=0; i<<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467aaf340f49e2e0827c0f06fdf65098554af">irr::gui::EGDC_COUNT</a> ; ++i) | ||
425 | { | ||
426 | <a class="code" href="classirr_1_1video_1_1_s_color.html" title="Class representing a 32 bit ARGB color.">irr::video::SColor</a> col = app.Gui->getSkin()->getColor((<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">irr::gui::EGUI_DEFAULT_COLOR</a>)i); | ||
427 | col.<a class="code" href="classirr_1_1video_1_1_s_color.html#a7bfe4abc30d563668b947c8bdb055bab" title="Sets the alpha component of the Color.">setAlpha</a>(255); | ||
428 | app.Gui->getSkin()->setColor((<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">irr::gui::EGUI_DEFAULT_COLOR</a>)i, col); | ||
429 | } | ||
430 | |||
431 | <span class="comment">//create video settings windows</span> | ||
432 | gui::IGUIWindow* windowSettings = app.Gui->addWindow(rect<s32>(10,10,400,400),<span class="keyword">true</span>,L<span class="stringliteral">"Videosettings"</span>); | ||
433 | app.Gui->addStaticText (L<span class="stringliteral">"Select your desired video settings"</span>, rect< s32 >(10,20, 200, 40), <span class="keyword">false</span>, <span class="keyword">true</span>, windowSettings); | ||
434 | |||
435 | <span class="comment">// add listbox for driver choice</span> | ||
436 | app.Gui->addStaticText (L<span class="stringliteral">"Driver"</span>, rect< s32 >(10,50, 200, 60), <span class="keyword">false</span>, <span class="keyword">true</span>, windowSettings); | ||
437 | app.ListboxDriver = app.Gui->addListBox(rect<s32>(10,60,220,120), windowSettings, 1,<span class="keyword">true</span>); | ||
438 | |||
439 | <span class="comment">//add all available options to the driver choice listbox</span> | ||
440 | map<stringw, s32>::Iterator i = app.Settings->DriverOptions.getIterator(); | ||
441 | <span class="keywordflow">for</span>(; !i.atEnd(); i++) | ||
442 | app.ListboxDriver->addItem(i->getKey().c_str()); | ||
443 | |||
444 | <span class="comment">//set currently selected driver</span> | ||
445 | app.ListboxDriver->setSelected(app.Settings->getSetting(<span class="stringliteral">"driver"</span>).c_str()); | ||
446 | |||
447 | <span class="comment">// add listbox for resolution choice</span> | ||
448 | app.Gui->addStaticText (L<span class="stringliteral">"Resolution"</span>, rect< s32 >(10,130, 200, 140), <span class="keyword">false</span>, <span class="keyword">true</span>, windowSettings); | ||
449 | app.ListboxResolution = app.Gui->addListBox(rect<s32>(10,140,220,200), windowSettings, 1,<span class="keyword">true</span>); | ||
450 | |||
451 | <span class="comment">//add all available options to the resolution listbox</span> | ||
452 | map<stringw, dimension2du>::Iterator ri = app.Settings->ResolutionOptions.getIterator(); | ||
453 | <span class="keywordflow">for</span>(; !ri.atEnd(); ri++) | ||
454 | app.ListboxResolution->addItem(ri->getKey().c_str()); | ||
455 | |||
456 | <span class="comment">//set currently selected resolution</span> | ||
457 | app.ListboxResolution->setSelected(app.Settings->getSetting(<span class="stringliteral">"resolution"</span>).c_str()); | ||
458 | |||
459 | <span class="comment">//add checkbox to toggle fullscreen, initially set to loaded setting</span> | ||
460 | app.CheckboxFullscreen = app.Gui->addCheckBox( | ||
461 | app.Settings->getSettingAsBoolean(<span class="stringliteral">"fullscreen"</span>), | ||
462 | rect<s32>(10,220,220,240), windowSettings, -1, | ||
463 | L<span class="stringliteral">"Fullscreen"</span>); | ||
464 | |||
465 | <span class="comment">//last but not least add save button</span> | ||
466 | app.ButtonSave = app.Gui->addButton( | ||
467 | rect<s32>(80,250,150,270), windowSettings, 2, | ||
468 | L<span class="stringliteral">"Save video settings"</span>); | ||
469 | |||
470 | <span class="comment">//exit/cancel button</span> | ||
471 | app.ButtonExit = app.Gui->addButton( | ||
472 | rect<s32>(160,250,240,270), windowSettings, 2, | ||
473 | L<span class="stringliteral">"Cancel and exit"</span>); | ||
474 | } | ||
475 | </pre></div><p>The main function. Creates all objects and does the XML handling. </p> | ||
476 | <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main() | ||
477 | { | ||
478 | <span class="comment">//create new application context</span> | ||
479 | SAppContext app; | ||
480 | |||
481 | <span class="comment">//create device creation parameters that can get overwritten by our settings file</span> | ||
482 | SIrrlichtCreationParameters param; | ||
483 | param.<a class="code" href="structirr_1_1_s_irrlicht_creation_parameters.html#a1ea2f50c3b3a8eed6602a1a86e1cdf82" title="Type of video driver used to render graphics.">DriverType</a> = <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a1598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">EDT_SOFTWARE</a>; | ||
484 | param.WindowSize.set(640,480); | ||
485 | |||
486 | <span class="comment">// Try to load config.</span> | ||
487 | <span class="comment">// I leave it as an exercise of the reader to store the configuration in the local application data folder,</span> | ||
488 | <span class="comment">// the only logical place to store config data for games. For all other operating systems I redirect to your manuals</span> | ||
489 | app.Settings = <span class="keyword">new</span> SettingManager(<span class="stringliteral">"../../media/settings.xml"</span>); | ||
490 | <span class="keywordflow">if</span> ( !app.Settings->load() ) | ||
491 | { | ||
492 | <span class="comment">// ...</span> | ||
493 | <span class="comment">// Here add your own exception handling, for now we continue because there are defaults set in SettingManager constructor</span> | ||
494 | <span class="comment">// ...</span> | ||
495 | } | ||
496 | <span class="keywordflow">else</span> | ||
497 | { | ||
498 | <span class="comment">//settings xml loaded from disk,</span> | ||
499 | |||
500 | <span class="comment">//map driversetting to driver type and test if the setting is valid</span> | ||
501 | <span class="comment">//the DriverOptions map contains string representations mapped to to irrlicht E_DRIVER_TYPE enum</span> | ||
502 | <span class="comment">//e.g "direct3d9" will become 4</span> | ||
503 | <span class="comment">//see DriverOptions in the settingmanager class for details</span> | ||
504 | map<stringw, s32>::Node* driver = app.Settings->DriverOptions.find( app.Settings->getSetting(<span class="stringliteral">"driver"</span>) ); | ||
505 | |||
506 | <span class="keywordflow">if</span> (driver) | ||
507 | { | ||
508 | <span class="keywordflow">if</span> ( <a class="code" href="classirr_1_1_irrlicht_device.html#a5a74995aec731b26c6a9cb5bea6842fe" title="Check if a driver type is supported by the engine.">irr::IrrlichtDevice::isDriverSupported</a>( static_cast<E_DRIVER_TYPE>( driver->getValue() ))) | ||
509 | { | ||
510 | <span class="comment">// selected driver is supported, so we use it.</span> | ||
511 | param.DriverType = <span class="keyword">static_cast<</span><a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">E_DRIVER_TYPE</a><span class="keyword">></span>( driver->getValue()); | ||
512 | } | ||
513 | } | ||
514 | |||
515 | <span class="comment">//map resolution setting to dimension in a similar way as demonstrated above</span> | ||
516 | map<stringw, dimension2du>::Node* res = app.Settings->ResolutionOptions.find( app.Settings->getSetting(<span class="stringliteral">"resolution"</span>) ); | ||
517 | <span class="keywordflow">if</span> (res) | ||
518 | { | ||
519 | param.WindowSize = res->getValue(); | ||
520 | } | ||
521 | |||
522 | <span class="comment">//get fullscreen setting from config</span> | ||
523 | param.Fullscreen = app.Settings->getSettingAsBoolean(<span class="stringliteral">"fullscreen"</span>); | ||
524 | } | ||
525 | |||
526 | <span class="comment">//create the irrlicht device using the settings</span> | ||
527 | app.Device = <a class="code" href="namespaceirr.html#ac83a30d674204dcb94d70f849e9b4a62" title="Creates an Irrlicht device with the option to specify advanced parameters.">createDeviceEx</a>(param); | ||
528 | <span class="keywordflow">if</span> (app.Device == 0) | ||
529 | { | ||
530 | <span class="comment">// You can add your own exception handling on driver failure</span> | ||
531 | exit(0); | ||
532 | } | ||
533 | |||
534 | app.Device->setWindowCaption(L<span class="stringliteral">"Xmlhandling - Irrlicht engine tutorial"</span>); | ||
535 | app.Driver = app.Device->getVideoDriver(); | ||
536 | app.Gui = app.Device->getGUIEnvironment(); | ||
537 | |||
538 | createSettingsDialog(app); | ||
539 | |||
540 | <span class="comment">//set event receiver so we can respond to gui events</span> | ||
541 | MyEventReceiver receiver(app); | ||
542 | app.Device->setEventReceiver(&receiver); | ||
543 | |||
544 | <span class="comment">//enter main loop</span> | ||
545 | <span class="keywordflow">while</span> (!app.ShouldQuit && app.Device->run()) | ||
546 | { | ||
547 | <span class="keywordflow">if</span> (app.Device->isWindowActive()) | ||
548 | { | ||
549 | app.Driver->beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, SColor(0,200,200,200)); | ||
550 | app.Gui->drawAll(); | ||
551 | app.Driver->endScene(); | ||
552 | } | ||
553 | app.Device->sleep(10); | ||
554 | } | ||
555 | |||
556 | <span class="comment">//app destroys device in destructor</span> | ||
557 | |||
558 | <span class="keywordflow">return</span> 0; | ||
559 | } | ||
560 | </pre></div> </div></div> | ||
561 | </div> | ||
562 | <div id="nav-path" class="navpath"> | ||
563 | <ul> | ||
564 | <!-- window showing the filter options --> | ||
565 | <div id="MSearchSelectWindow" | ||
566 | onmouseover="return searchBox.OnSearchSelectShow()" | ||
567 | onmouseout="return searchBox.OnSearchSelectHide()" | ||
568 | onkeydown="return searchBox.OnSearchSelectKey(event)"> | ||
569 | <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Defines</a></div> | ||
570 | |||
571 | <!-- iframe showing the search results (closed by default) --> | ||
572 | <div id="MSearchResultsWindow"> | ||
573 | <iframe src="javascript:void(0)" frameborder="0" | ||
574 | name="MSearchResults" id="MSearchResults"> | ||
575 | </iframe> | ||
576 | </div> | ||
577 | |||
578 | |||
579 | <li class="footer"> | ||
580 | <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht | ||
581 | Engine</a> Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on Sun Nov 17 2013 20:18:42 for Irrlicht 3D Engine by | ||
582 | <a href="http://www.doxygen.org/index.html" target="_blank">Doxygen</a> 1.7.5.1 </li> | ||
583 | </ul> | ||
584 | </div> | ||
585 | |||
586 | |||
587 | </body> | ||
588 | </html> | ||