Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-release > by-pkgid > 58828b263d8f56d90ac336dea07a4586 > files > 745

irrlicht-doc-1.6.1-1mdv2010.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Irrlicht Engine: Tutorial 14: Win32 Window</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<table class="irrlicht" >
  <tr valign="middle"> 
    <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> 
      | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> 
      | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> 
      | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical 
      List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> 
      Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> 
      | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> 
      Namespace&nbsp;Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class 
      members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File 
      members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td>
  </tr>
</table>
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="example014">Tutorial 14: Win32 Window </a></h1><div align="center">
<img src="014shot.jpg" alt="014shot.jpg">
</div>
 <p>
This example only runs under MS Windows and demonstrates that Irrlicht can render inside a win32 window. MFC and .NET Windows.Forms windows are possible, too.<p>
In the begining, we create a windows window using the windows API. I'm not going to explain this code, because it is windows specific. See the MSDN or a windows book for details. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
<span class="preprocessor">#ifndef _IRR_WINDOWS_</span>
<span class="preprocessor"></span><span class="preprocessor">#error Windows only example</span>
<span class="preprocessor"></span><span class="preprocessor">#else</span>
<span class="preprocessor"></span><span class="preprocessor">#include &lt;windows.h&gt;</span> <span class="comment">// this example only runs with windows</span>

<span class="keyword">using namespace </span>irr;

<span class="preprocessor">#pragma comment(lib, "irrlicht.lib")</span>
<span class="preprocessor"></span>
HWND hOKButton;
HWND hWnd;

<span class="keyword">static</span> LRESULT CALLBACK CustomWndProc(HWND hWnd, UINT message,
                WPARAM wParam, LPARAM lParam)
{
        <span class="keywordflow">switch</span> (message)
        {
        <span class="keywordflow">case</span> WM_COMMAND:
                {
                        HWND hwndCtl = (HWND)lParam;
                        <span class="keywordtype">int</span> code = HIWORD(wParam);

                        <span class="keywordflow">if</span> (hwndCtl == hOKButton)
                        {
                                DestroyWindow(hWnd);
                                PostQuitMessage(0);
                                <span class="keywordflow">return</span> 0;
                        }
                }
                <span class="keywordflow">break</span>;
        <span class="keywordflow">case</span> WM_DESTROY:
                PostQuitMessage(0);
                <span class="keywordflow">return</span> 0;

        }

        <span class="keywordflow">return</span> DefWindowProc(hWnd, message, wParam, lParam);
}


<span class="keywordtype">int</span> main()
<span class="comment">//int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpre, LPSTR cmd, int cc)</span>
{
        HINSTANCE hInstance = 0;
        <span class="comment">// create dialog</span>

        <span class="keyword">const</span> <span class="keywordtype">char</span>* Win32ClassName = <span class="stringliteral">"CIrrlichtWindowsTestDialog"</span>;

        WNDCLASSEX wcex;
        wcex.cbSize                     = <span class="keyword">sizeof</span>(WNDCLASSEX);
        wcex.style                      = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc        = (WNDPROC)CustomWndProc;
        wcex.cbClsExtra         = 0;
        wcex.cbWndExtra         = DLGWINDOWEXTRA;
        wcex.hInstance          = hInstance;
        wcex.hIcon                      = NULL;
        wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW);
        wcex.lpszMenuName       = 0;
        wcex.lpszClassName      = Win32ClassName;
        wcex.hIconSm            = 0;

        RegisterClassEx(&amp;wcex);

        DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION |
                WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX;

        <span class="keywordtype">int</span> windowWidth = 440;
        <span class="keywordtype">int</span> windowHeight = 380;

        hWnd = CreateWindow( Win32ClassName, <span class="stringliteral">"Irrlicht Win32 window example"</span>,
                style, 100, 100, windowWidth, windowHeight,
                NULL, NULL, hInstance, NULL);

        RECT clientRect;
        GetClientRect(hWnd, &amp;clientRect);
        windowWidth = clientRect.right;
        windowHeight = clientRect.bottom;

        <span class="comment">// create ok button</span>

        hOKButton = CreateWindow(<span class="stringliteral">"BUTTON"</span>, <span class="stringliteral">"OK - Close"</span>, WS_CHILD | WS_VISIBLE | BS_TEXT,
                windowWidth - 160, windowHeight - 40, 150, 30, hWnd, NULL, hInstance, NULL);

        <span class="comment">// create some text</span>

        CreateWindow(<span class="stringliteral">"STATIC"</span>, <span class="stringliteral">"This is Irrlicht running inside a standard Win32 window.\n"</span>\
                <span class="stringliteral">"Also mixing with MFC and .NET Windows.Forms is possible."</span>,
                WS_CHILD | WS_VISIBLE, 20, 20, 400, 40, hWnd, NULL, hInstance, NULL);

        <span class="comment">// create window to put irrlicht in</span>

        HWND hIrrlichtWindow = CreateWindow(<span class="stringliteral">"BUTTON"</span>, <span class="stringliteral">""</span>,
                        WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
                        50, 80, 320, 220, hWnd, NULL, hInstance, NULL);
</pre></div><p>
So now that we have some window, we can create an Irrlicht device inside of it. We use Irrlicht createEx() function for this. We only need the handle (HWND) to that window, set it as windowsID parameter and start up the engine as usual. That's it. <div class="fragment"><pre class="fragment">        <span class="comment">// create irrlicht device in the button window</span>

        <a class="code" href="structirr_1_1_s_irrlicht_creation_parameters.html" title="Structure for holding Irrlicht Device creation parameters.">irr::SIrrlichtCreationParameters</a> param;
        param.<a class="code" href="structirr_1_1_s_irrlicht_creation_parameters.html#f287810d910a23f8f7db98cef87b6eae" title="Window Id.">WindowId</a> = <span class="keyword">reinterpret_cast&lt;</span><span class="keywordtype">void</span>*<span class="keyword">&gt;</span>(hIrrlichtWindow); <span class="comment">// hColorButton</span>
        param.<a class="code" href="structirr_1_1_s_irrlicht_creation_parameters.html#1ea2f50c3b3a8eed6602a1a86e1cdf82" title="Type of video driver used to render graphics.">DriverType</a> = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d02715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>;

        <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>* device = <a class="code" href="namespaceirr.html#c83a30d674204dcb94d70f849e9b4a62" title="Creates an Irrlicht device with the option to specify advanced parameters.">irr::createDeviceEx</a>(param);

        <span class="comment">// setup a simple 3d scene</span>

        <a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html" title="The Scene Manager manages scene nodes, mesh recources, cameras and all the other...">irr::scene::ISceneManager</a>* smgr = device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#891b503ff4d5041296d88f23f97d7b3d" title="Provides access to the scene manager.">getSceneManager</a>();
        video::IVideoDriver* driver = device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#da90707ba5c645d47e000e4e0f87c4c4" title="Provides access to the video driver for drawing 3d and 2d geometry.">getVideoDriver</a>();

        scene::ICameraSceneNode* cam = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#3e9a5e3fc10372f126443d346270bc79" title="Adds a camera scene node to the scene graph and sets it as active camera.">addCameraSceneNode</a>();
        cam-&gt;setTarget(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,0));

        scene::ISceneNodeAnimator* anim =
                smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#2e49ff49bc9e88e8ecf3d681354e1ab6" title="Creates a fly circle animator, which lets the attached scene node fly around a center...">createFlyCircleAnimator</a>(<a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,15,0), 30.0f);
        cam-&gt;addAnimator(anim);
        anim-&gt;drop();

        scene::ISceneNode* cube = smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#23d1328c68b1585f613108f386fabc1c" title="Adds a cube scene node.">addCubeSceneNode</a>(20);

        cube-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">"../../media/wall.bmp"</span>));
        cube-&gt;setMaterialTexture(1, driver-&gt;getTexture(<span class="stringliteral">"../../media/water.jpg"</span>));
        cube-&gt;setMaterialFlag( <a class="code" href="namespaceirr_1_1video.html#8a3bc00ae8137535b9fbc5f40add70d3cea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span> );
        cube-&gt;setMaterialType( <a class="code" href="namespaceirr_1_1video.html#c8e9b6c66f7cebabd1a6d30cbc5430f1d8574343353ed8ade6e78bc04d64b6ae" title="A reflecting material with an optional non reflecting texture layer.">video::EMT_REFLECTION_2_LAYER</a> );

        smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#2b08b9f20ec62faeffc02b9fed9fd683" title="Adds a skybox scene node to the scene graph.">addSkyBoxSceneNode</a>(
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_up.jpg"</span>),
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_dn.jpg"</span>),
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_lf.jpg"</span>),
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_rt.jpg"</span>),
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_ft.jpg"</span>),
        driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlicht2_bk.jpg"</span>));

        <span class="comment">// show and execute dialog</span>

        ShowWindow(hWnd , SW_SHOW);
        UpdateWindow(hWnd);

        <span class="comment">// do message queue</span>
</pre></div><p>
Now the only thing missing is the drawing loop using IrrlichtDevice::run(). We do this as usual. But instead of this, there is another possibility: You can also simply use your own message loop using GetMessage, DispatchMessage and whatever. Calling Device-&gt;run() will cause Irrlicht to dispatch messages internally too. You need not call Device-&gt;run() if you want to do your own message dispatching loop, but Irrlicht will not be able to fetch user input then and you have to do it on your own using the window messages, DirectInput, or whatever. <div class="fragment"><pre class="fragment">        <span class="keywordflow">while</span> (device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#0489f8151dc43f6f41503ffb5a160b35" title="Runs the device.">run</a>())
        {
                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, 0);
                smgr-&gt;<a class="code" href="classirr_1_1scene_1_1_i_scene_manager.html#04240262904667c821bd9de5e5fd9b02" title="Draws all the scene nodes.">drawAll</a>();
                driver-&gt;endScene();
        }
</pre></div><p>
The alternative, own message dispatching loop without Device-&gt;run() would look like this: <div class="fragment"><pre class="fragment"></pre></div> MSG msg; while (true) { if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&amp;msg); DispatchMessage(&amp;msg);<p>
if (msg.message == WM_QUIT) break; }<p>
advance virtual time device-&gt;getTimer()-&gt;tick();<p>
draw engine picture driver-&gt;beginScene(true, true, 0); smgr-&gt;drawAll(); driver-&gt;endScene(); }<div class="fragment"><pre class="fragment">        device-&gt;<a class="code" href="classirr_1_1_irrlicht_device.html#08c97937e0f60f98d443b397a7c60e18" title="Notifies the device that it should close itself.">closeDevice</a>();
        device-&gt;<a class="code" href="classirr_1_1_i_reference_counted.html#fb169a857e0d2cdb96b8821cb9bff17a" title="Drops the object. Decrements the reference counter by one.">drop</a>();

        <span class="keywordflow">return</span> 0;
}
<span class="preprocessor">#endif // if windows</span>
</pre></div><p>
That's it, Irrlicht now runs in your own windows window. </div>
<hr size="1">
<address style="align: right;">
<small> </small>
</address>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr> 
    <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td>
    <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
        Engine</a> Documentation &copy; 2003-2009 by Nikolaus Gebhardt. Generated 
        on Sun Jan 10 09:24:07 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.5.6)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>