diff options
author | David Walter Seikel | 2013-01-13 17:24:39 +1000 |
---|---|---|
committer | David Walter Seikel | 2013-01-13 17:24:39 +1000 |
commit | 393b5cd1dc438872af89d334ef6e5fcc59f27d47 (patch) | |
tree | 6a14521219942a08a1b95cb2f5a923a9edd60f63 /libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp | |
parent | Add a note about rasters suggested start up code. (diff) | |
download | SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.zip SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.gz SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.bz2 SledjHamr-393b5cd1dc438872af89d334ef6e5fcc59f27d47.tar.xz |
Added Irrlicht 1.8, but without all the Windows binaries.
Diffstat (limited to 'libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp b/libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp new file mode 100644 index 0000000..33d111e --- /dev/null +++ b/libraries/irrlicht-1.8/examples/14.Win32Window/main.cpp | |||
@@ -0,0 +1,251 @@ | |||
1 | /** Example 014 Win32 Window | ||
2 | |||
3 | This example only runs under MS Windows and demonstrates that Irrlicht can | ||
4 | render inside a win32 window. MFC and .NET Windows.Forms windows are possible, | ||
5 | too. | ||
6 | |||
7 | In the beginning, we create a windows window using the windows API. I'm not | ||
8 | going to explain this code, because it is windows specific. See the MSDN or a | ||
9 | windows book for details. | ||
10 | */ | ||
11 | |||
12 | #include <irrlicht.h> | ||
13 | #ifndef _IRR_WINDOWS_ | ||
14 | #error Windows only example | ||
15 | #else | ||
16 | #include <windows.h> // this example only runs with windows | ||
17 | #include <iostream> | ||
18 | #include "driverChoice.h" | ||
19 | |||
20 | using namespace irr; | ||
21 | |||
22 | #pragma comment(lib, "irrlicht.lib") | ||
23 | |||
24 | HWND hOKButton; | ||
25 | HWND hWnd; | ||
26 | |||
27 | static LRESULT CALLBACK CustomWndProc(HWND hWnd, UINT message, | ||
28 | WPARAM wParam, LPARAM lParam) | ||
29 | { | ||
30 | switch (message) | ||
31 | { | ||
32 | case WM_COMMAND: | ||
33 | { | ||
34 | HWND hwndCtl = (HWND)lParam; | ||
35 | int code = HIWORD(wParam); | ||
36 | |||
37 | if (hwndCtl == hOKButton) | ||
38 | { | ||
39 | DestroyWindow(hWnd); | ||
40 | PostQuitMessage(0); | ||
41 | return 0; | ||
42 | } | ||
43 | } | ||
44 | break; | ||
45 | case WM_DESTROY: | ||
46 | PostQuitMessage(0); | ||
47 | return 0; | ||
48 | |||
49 | } | ||
50 | |||
51 | return DefWindowProc(hWnd, message, wParam, lParam); | ||
52 | } | ||
53 | |||
54 | |||
55 | /* | ||
56 | Now ask for the driver and create the Windows specific window. | ||
57 | */ | ||
58 | int main() | ||
59 | { | ||
60 | // ask user for driver | ||
61 | video::E_DRIVER_TYPE driverType=driverChoiceConsole(); | ||
62 | if (driverType==video::EDT_COUNT) | ||
63 | return 1; | ||
64 | |||
65 | printf("Select the render window (some dead window may exist too):\n"\ | ||
66 | " (a) Window with button (via CreationParam)\n"\ | ||
67 | " (b) Window with button (via beginScene)\n"\ | ||
68 | " (c) Own Irrlicht window (default behavior)\n"\ | ||
69 | " (otherKey) exit\n\n"); | ||
70 | |||
71 | char key; | ||
72 | std::cin >> key; | ||
73 | if (key != 'a' && key != 'b' && key != 'c') | ||
74 | return 1; | ||
75 | |||
76 | HINSTANCE hInstance = 0; | ||
77 | // create dialog | ||
78 | |||
79 | const char* Win32ClassName = "CIrrlichtWindowsTestDialog"; | ||
80 | |||
81 | WNDCLASSEX wcex; | ||
82 | wcex.cbSize = sizeof(WNDCLASSEX); | ||
83 | wcex.style = CS_HREDRAW | CS_VREDRAW; | ||
84 | wcex.lpfnWndProc = (WNDPROC)CustomWndProc; | ||
85 | wcex.cbClsExtra = 0; | ||
86 | wcex.cbWndExtra = DLGWINDOWEXTRA; | ||
87 | wcex.hInstance = hInstance; | ||
88 | wcex.hIcon = NULL; | ||
89 | wcex.hCursor = LoadCursor(NULL, IDC_ARROW); | ||
90 | wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW); | ||
91 | wcex.lpszMenuName = 0; | ||
92 | wcex.lpszClassName = Win32ClassName; | ||
93 | wcex.hIconSm = 0; | ||
94 | |||
95 | RegisterClassEx(&wcex); | ||
96 | |||
97 | DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION | | ||
98 | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX; | ||
99 | |||
100 | int windowWidth = 440; | ||
101 | int windowHeight = 380; | ||
102 | |||
103 | hWnd = CreateWindow( Win32ClassName, "Irrlicht Win32 window example", | ||
104 | style, 100, 100, windowWidth, windowHeight, | ||
105 | NULL, NULL, hInstance, NULL); | ||
106 | |||
107 | RECT clientRect; | ||
108 | GetClientRect(hWnd, &clientRect); | ||
109 | windowWidth = clientRect.right; | ||
110 | windowHeight = clientRect.bottom; | ||
111 | |||
112 | // create ok button | ||
113 | |||
114 | hOKButton = CreateWindow("BUTTON", "OK - Close", WS_CHILD | WS_VISIBLE | BS_TEXT, | ||
115 | windowWidth - 160, windowHeight - 40, 150, 30, hWnd, NULL, hInstance, NULL); | ||
116 | |||
117 | // create some text | ||
118 | |||
119 | CreateWindow("STATIC", "This is Irrlicht running inside a standard Win32 window.\n"\ | ||
120 | "Also mixing with MFC and .NET Windows.Forms is possible.", | ||
121 | WS_CHILD | WS_VISIBLE, 20, 20, 400, 40, hWnd, NULL, hInstance, NULL); | ||
122 | |||
123 | // create window to put irrlicht in | ||
124 | |||
125 | HWND hIrrlichtWindow = CreateWindow("BUTTON", "", | ||
126 | WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, | ||
127 | 50, 80, 320, 220, hWnd, NULL, hInstance, NULL); | ||
128 | video::SExposedVideoData videodata((key=='b')?hIrrlichtWindow:0); | ||
129 | |||
130 | /* | ||
131 | So now that we have some window, we can create an Irrlicht device | ||
132 | inside of it. We use Irrlicht createEx() function for this. We only | ||
133 | need the handle (HWND) to that window, set it as windowsID parameter | ||
134 | and start up the engine as usual. That's it. | ||
135 | */ | ||
136 | // create irrlicht device in the button window | ||
137 | |||
138 | irr::SIrrlichtCreationParameters param; | ||
139 | param.DriverType = driverType; | ||
140 | if (key=='a') | ||
141 | param.WindowId = reinterpret_cast<void*>(hIrrlichtWindow); | ||
142 | |||
143 | irr::IrrlichtDevice* device = irr::createDeviceEx(param); | ||
144 | if (!device) | ||
145 | return 1; | ||
146 | |||
147 | // setup a simple 3d scene | ||
148 | |||
149 | irr::scene::ISceneManager* smgr = device->getSceneManager(); | ||
150 | video::IVideoDriver* driver = device->getVideoDriver(); | ||
151 | |||
152 | if (driverType==video::EDT_OPENGL) | ||
153 | { | ||
154 | HDC HDc=GetDC(hIrrlichtWindow); | ||
155 | PIXELFORMATDESCRIPTOR pfd={0}; | ||
156 | pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); | ||
157 | int pf = GetPixelFormat(HDc); | ||
158 | DescribePixelFormat(HDc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd); | ||
159 | pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; | ||
160 | pfd.cDepthBits=16; | ||
161 | pf = ChoosePixelFormat(HDc, &pfd); | ||
162 | SetPixelFormat(HDc, pf, &pfd); | ||
163 | videodata.OpenGLWin32.HDc = HDc; | ||
164 | videodata.OpenGLWin32.HRc=wglCreateContext(HDc); | ||
165 | wglShareLists((HGLRC)driver->getExposedVideoData().OpenGLWin32.HRc, (HGLRC)videodata.OpenGLWin32.HRc); | ||
166 | } | ||
167 | scene::ICameraSceneNode* cam = smgr->addCameraSceneNode(); | ||
168 | cam->setTarget(core::vector3df(0,0,0)); | ||
169 | |||
170 | scene::ISceneNodeAnimator* anim = | ||
171 | smgr->createFlyCircleAnimator(core::vector3df(0,15,0), 30.0f); | ||
172 | cam->addAnimator(anim); | ||
173 | anim->drop(); | ||
174 | |||
175 | scene::ISceneNode* cube = smgr->addCubeSceneNode(20); | ||
176 | |||
177 | cube->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp")); | ||
178 | cube->setMaterialTexture(1, driver->getTexture("../../media/water.jpg")); | ||
179 | cube->setMaterialFlag( video::EMF_LIGHTING, false ); | ||
180 | cube->setMaterialType( video::EMT_REFLECTION_2_LAYER ); | ||
181 | |||
182 | smgr->addSkyBoxSceneNode( | ||
183 | driver->getTexture("../../media/irrlicht2_up.jpg"), | ||
184 | driver->getTexture("../../media/irrlicht2_dn.jpg"), | ||
185 | driver->getTexture("../../media/irrlicht2_lf.jpg"), | ||
186 | driver->getTexture("../../media/irrlicht2_rt.jpg"), | ||
187 | driver->getTexture("../../media/irrlicht2_ft.jpg"), | ||
188 | driver->getTexture("../../media/irrlicht2_bk.jpg")); | ||
189 | |||
190 | // show and execute dialog | ||
191 | |||
192 | ShowWindow(hWnd , SW_SHOW); | ||
193 | UpdateWindow(hWnd); | ||
194 | |||
195 | // do message queue | ||
196 | |||
197 | /* | ||
198 | Now the only thing missing is the drawing loop using | ||
199 | IrrlichtDevice::run(). We do this as usual. But instead of this, there | ||
200 | is another possibility: You can also simply use your own message loop | ||
201 | using GetMessage, DispatchMessage and whatever. Calling | ||
202 | Device->run() will cause Irrlicht to dispatch messages internally too. | ||
203 | You need not call Device->run() if you want to do your own message | ||
204 | dispatching loop, but Irrlicht will not be able to fetch user input | ||
205 | then and you have to do it on your own using the window messages, | ||
206 | DirectInput, or whatever. | ||
207 | */ | ||
208 | |||
209 | while (device->run()) | ||
210 | { | ||
211 | driver->beginScene(true, true, 0, videodata); | ||
212 | smgr->drawAll(); | ||
213 | driver->endScene(); | ||
214 | } | ||
215 | |||
216 | /* | ||
217 | The alternative, own message dispatching loop without Device->run() | ||
218 | would look like this: | ||
219 | */ | ||
220 | |||
221 | /*MSG msg; | ||
222 | while (true) | ||
223 | { | ||
224 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) | ||
225 | { | ||
226 | TranslateMessage(&msg); | ||
227 | DispatchMessage(&msg); | ||
228 | |||
229 | if (msg.message == WM_QUIT) | ||
230 | break; | ||
231 | } | ||
232 | |||
233 | // advance virtual time | ||
234 | device->getTimer()->tick(); | ||
235 | |||
236 | // draw engine picture | ||
237 | driver->beginScene(true, true, 0, (key=='c')?hIrrlichtWindow:0); | ||
238 | smgr->drawAll(); | ||
239 | driver->endScene(); | ||
240 | }*/ | ||
241 | |||
242 | device->closeDevice(); | ||
243 | device->drop(); | ||
244 | |||
245 | return 0; | ||
246 | } | ||
247 | #endif // if windows | ||
248 | |||
249 | /* | ||
250 | That's it, Irrlicht now runs in your own windows window. | ||
251 | **/ | ||