<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
<p>In this tutorial we'll learn how to use splitscreen (e.g. for racing-games) with Irrlicht. We'll create a viewport divided into 4 parts, with 3 fixed cameras and one user-controlled.</p>
<p>Ok, let's start with the headers (I think there's nothing to say about it) </p><divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <irrlicht.h></span></div><divclass="line"><spanclass="preprocessor">#include "driverChoice.h"</span></div><divclass="line"><spanclass="preprocessor">#include "exampleHelper.h"</span></div><divclass="line"></div><divclass="line"><spanclass="preprocessor">#ifdef _MSC_VER</span></div><divclass="line"><spanclass="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span></div><divclass="line"><spanclass="preprocessor">#endif</span></div><divclass="line"></div><divclass="line"><spanclass="comment">//Namespaces for the engine</span></div><divclass="line"><spanclass="keyword">using namespace </span>irr;</div><divclass="line"><spanclass="keyword">using namespace </span>core;</div><divclass="line"><spanclass="keyword">using namespace </span>video;</div><divclass="line"><spanclass="keyword">using namespace </span>scene;</div></div><!-- fragment --><p> Now we'll define the resolution in a constant for use in initializing the device and setting up the viewport. In addition we set up a global variable saying splitscreen is active or not. </p><divclass="fragment"><divclass="line"><spanclass="comment">//Resolution</span></div><divclass="line"><spanclass="keyword">const</span><spanclass="keywordtype">int</span> ResX=800;</div><divclass="line"><spanclass="keyword">const</span><spanclass="keywordtype">int</span> ResY=600;</div><divclass="line"><spanclass="keyword">const</span><spanclass="keywordtype">bool</span> fullScreen=<spanclass="keyword">false</span>;</div><divclass="line"></div><divclass="line"><spanclass="comment">//Use SplitScreen?</span></div><divclass="line"><spanclass="keywordtype">bool</span> SplitScreen=<spanclass="keyword">true</span>;</div></div><!-- fragment --><p> Now we need four pointers to our cameras which are created later: </p><divclass="fragment"><divclass="line"><spanclass="comment">//cameras</span></div><divclass="line">ICameraSceneNode *camera[4]={0,0,0,0};</div></div><!-- fragment --><p> In our event-receiver we switch the SplitScreen-variable, whenever the user press the S-key. All other events are sent to the FPS camera. </p><divclass="fragment"><divclass="line"><spanclass="keyword">class </span>MyEventReceiver : <spanclass="keyword">public</span> IEventReceiver</div><divclass="line">{</div><divclass="line"><spanclass="keyword">public</span>:</div><divclass="line"><spanclass="keyword">virtual</span><spanclass="keywordtype">bool</span> OnEvent(<spanclass="keyword">const</span> SEvent& event)</div><divclass="line"> {</div><divclass="line"><spanclass="comment">//Key S enables/disables SplitScreen</span></div><divclass="line"><spanclass="keywordflow">if</span> (event.EventType == irr::EET_KEY_INPUT_EVENT &&</div><divclass="line"> event.KeyInput.Key == KEY_KEY_S && event.KeyInput.PressedDown)</div><divclass="line"> {</div><divclass="line"> SplitScreen = !SplitScreen;</div><divclass="line"><spanclass="keywordflow">return</span><spanclass="keyword">true</span>;</div><divclass="line"> }</div><divclass="line"><spanclass="comment">//Send all other events to camera4</span></div><divclass="line"><spanclass="keywordflow">if</span> (camera[3])</div><divclass="line"><spanclass="keywordflow">return</span> camera[3]->OnEvent(event);</div><divclass="line"><spanclass="keywordflow">return</span><spanclass="keyword">false</span>;</div><divclass="line"> }</div><divclass="line">};</div></div><!-- fragment --><p> Ok, now the main-function: First, we initialize the device, get the SourceManager and VideoDriver, load an animated mesh from .md2 and a map from .pk3. Because that's old stuff, I won't explain every step. Just take care of the maps position. </p><divclass="f
<p>Sounds a little complicated, but you'll see it isn't: </p><divclass="fragment"><divclass="line"><spanclass="keywordflow">while</span>(device->run())</div><divclass="line">{</div><divclass="line"><spanclass="comment">//Set the viewpoint to the whole screen and begin scene</span></div><divclass="line"> driver->setViewPort(rect<s32>(0,0,ResX,ResY));</div><divclass="line"> driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, SColor(255,100,100,100));</div><divclass="line"><spanclass="comment">//If SplitScreen is used</span></div><divclass="line"><spanclass="keywordflow">if</span> (SplitScreen)</div><divclass="line"> {</div><divclass="line"><spanclass="comment">//Activate camera1</span></div><divclass="line"> smgr->setActiveCamera(camera[0]);</div><divclass="line"><spanclass="comment">//Set viewpoint to the first quarter (left top)</span></div><divclass="line"> driver->setViewPort(rect<s32>(0,0,ResX/2,ResY/2));</div><divclass="line"><spanclass="comment">//Draw scene</span></div><divclass="line"> smgr->drawAll();</div><divclass="line"><spanclass="comment">//Activate camera2</span></div><divclass="line"> smgr->setActiveCamera(camera[1]);</div><divclass="line"><spanclass="comment">//Set viewpoint to the second quarter (right top)</span></div><divclass="line"> driver->setViewPort(rect<s32>(ResX/2,0,ResX,ResY/2));</div><divclass="line"><spanclass="comment">//Draw scene</span></div><divclass="line"> smgr->drawAll();</div><divclass="line"><spanclass="comment">//Activate camera3</span></div><divclass="line"> smgr->setActiveCamera(camera[2]);</div><divclass="line"><spanclass="comment">//Set viewpoint to the third quarter (left bottom)</span></div><divclass="line"> driver->setViewPort(rect<s32>(0,ResY/2,ResX/2,ResY));</div><divclass="line"><spanclass="comment">//Draw scene</span></div><divclass="line"> smgr->drawAll();</div><divclass="line"><spanclass="comment">//Set viewport the last quarter (right bottom)</span></div><divclass="line"> driver->setViewPort(rect<s32>(ResX/2,ResY/2,ResX,ResY));</div><divclass="line"> }</div><divclass="line"><spanclass="comment">//Activate camera4</span></div><divclass="line"> smgr->setActiveCamera(camera[3]);</div><divclass="line"><spanclass="comment">//Draw scene</span></div><divclass="line"> smgr->drawAll();</div><divclass="line"> driver->endScene();</div></div><!-- fragment --><p> As you can probably see, the image is rendered for every viewport separately. That means, that you'll loose much performance. Ok, if you're asking "How do I have to set the viewport
to get this or that screen?", don't panic. It's really easy: In the rect-function you define 4 coordinates:</p><ul>
<li>X-coordinate of the corner left top</li>
<li>Y-coordinate of the corner left top</li>
<li>X-coordinate of the corner right bottom</li>
<li>Y-coordinate of the corner right bottom</li>
</ul>
<p>That means, if you want to split the screen into 2 viewports you would give the following coordinates:</p><ul>
<li>1st viewport: 0,0,ResX/2,ResY</li>
<li>2nd viewport: ResX/2,0,ResX,ResY</li>
</ul>
<p>If you didn't fully understand, just play around with the example to check out what happens.</p>
<p>Now we just view the current fps and shut down the engine, when the user wants to: </p><divclass="fragment"><divclass="line"><spanclass="comment">//Get and show fps</span></div><divclass="line"><spanclass="keywordflow">if</span> (driver->getFPS() != lastFPS)</div><divclass="line"> {</div><divclass="line"> lastFPS = driver->getFPS();</div><divclass="line"> core::stringw tmp = L<spanclass="stringliteral">"Irrlicht SplitScreen-Example (FPS: "</span>;</div><divclass="line"> tmp += lastFPS;</div><divclass="line"> tmp += <spanclass="stringliteral">")"</span>;</div><divclass="line"> device->setWindowCaption(tmp.c_str());</div><divclass="line"> }</div><divclass="line"> }</div><divclass="line"><spanclass="comment">//Delete device</span></div><divclass="line"> device->drop();</div><divclass="line"><spanclass="keywordflow">return</span> 0;</div><divclass="line">}</div></div><!-- fragment --><p> That's it! Just compile and play around with the program. Note: With the S-Key you can switch between using splitscreen and not. </p>