source: OGRE/trunk/ogrenew/RenderSystems/GL/src/OgreWin32Window.cpp @ 692

Revision 692, 14.1 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25
26#include "OgreWin32Window.h"
27#include "OgreRoot.h"
28#include "OgreLogManager.h"
29#include "OgreRenderSystem.h"
30#include "OgreImageCodec.h"
31#include "OgreStringConverter.h"
32#include "OgreException.h"
33#include "OgreWin32GLSupport.h"
34#include "OgreWin32Context.h"
35
36namespace Ogre {
37
38        Win32Window::Win32Window(Win32GLSupport &glsupport):
39                mGLSupport(glsupport),
40                mContext(0)
41        {
42                mIsFullScreen = false;
43                mHWnd = 0;
44                mIsExternal = false;
45                mSizing = false;
46                mClosed = false;
47                mDisplayFrequency = 0;
48                mActive = false;
49        }
50
51        Win32Window::~Win32Window()
52        {
53                destroy();
54        }
55
56        void Win32Window::create(const String& name, unsigned int width, unsigned int height,
57                                                        bool fullScreen, const NameValuePairList *miscParams)
58        {
59                // destroy current window, if any
60                if (mHWnd)
61                        destroy();
62
63                HINSTANCE hInst = GetModuleHandle("RenderSystem_GL.dll");
64
65                mHWnd = 0;
66                mName = name;
67                mIsFullScreen = fullScreen;
68                mClosed = false;
69
70                // load window defaults
71                mLeft = mTop = -1; // centered
72                mWidth = width;
73                mHeight = height;
74                mDisplayFrequency = 0;
75                mIsDepthBuffered = true;
76                mColourDepth = mIsFullScreen? 32 : GetDeviceCaps(GetDC(0), BITSPIXEL);
77
78                HWND parent = 0;
79                String title = name;
80                bool vsync = false;
81                int fsaa = 0;
82                String border = "";
83                bool outerSize = false;
84
85                if(miscParams)
86                {
87                        // Get variable-length params
88                        NameValuePairList::const_iterator opt;
89                        NameValuePairList::const_iterator end = miscParams->end();
90
91                        if ((opt = miscParams->find("title")) != end)
92                                title = opt->second;
93
94                        if ((opt = miscParams->find("left")) != end)
95                                mLeft = StringConverter::parseInt(opt->second);
96
97                        if ((opt = miscParams->find("top")) != end)
98                                mTop = StringConverter::parseInt(opt->second);
99
100                        if ((opt = miscParams->find("depthBuffer")) != end)
101                                mIsDepthBuffered = StringConverter::parseBool(opt->second);
102
103                        if ((opt = miscParams->find("vsync")) != end)
104                                vsync = StringConverter::parseBool(opt->second);
105
106                        if ((opt = miscParams->find("FSAA")) != end)
107                                fsaa = StringConverter::parseUnsignedInt(opt->second);
108
109                        if ((opt = miscParams->find("externalWindowHandle")) != end)
110                        {
111                                mHWnd = (HWND)StringConverter::parseUnsignedInt(opt->second);
112                                if (mHWnd)
113                                {
114                                        mIsExternal = true;
115                                        mIsFullScreen = false;
116                                }
117                        }
118                        // window border style
119                        opt = miscParams->find("border");
120                        if(opt != miscParams->end())
121                                border = opt->second;
122                        // set outer dimensions?
123                        opt = miscParams->find("outerDimensions");
124                        if(opt != miscParams->end())
125                                outerSize = StringConverter::parseBool(opt->second);
126
127                        if (mIsFullScreen)
128                        {
129                                // only available with fullscreen
130                                if ((opt = miscParams->find("displayFrequency")) != end)
131                                        mDisplayFrequency = StringConverter::parseUnsignedInt(opt->second);
132                                if ((opt = miscParams->find("colourDepth")) != end)
133                                        mColourDepth = StringConverter::parseUnsignedInt(opt->second);
134                        }
135                        else
136                        {
137                                // incompatible with fullscreen
138                                if ((opt = miscParams->find("parentWindowHandle")) != end)
139                                        parent = (HWND)StringConverter::parseUnsignedInt(opt->second);
140                        }
141                }
142
143                if (!mIsExternal)
144                {
145                        DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
146                        DWORD dwStyleEx = 0;
147                        int outerw, outerh;
148
149                        if (mIsFullScreen)
150                        {
151                                dwStyle |= WS_POPUP;
152                                dwStyleEx |= WS_EX_TOPMOST;
153                                outerw = mWidth;
154                                outerh = mHeight;
155                                mLeft = mTop = 0;
156                        }
157                        else
158                        {
159                                if (border == "none")
160                                        dwStyle |= WS_POPUP;
161                                else if (border == "fixed")
162                                        dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
163                                        WS_SYSMENU | WS_MINIMIZEBOX;
164                                else
165                                        dwStyle |= WS_OVERLAPPEDWINDOW;
166
167                                int screenw = GetSystemMetrics(SM_CXSCREEN);
168                                int screenh = GetSystemMetrics(SM_CYSCREEN);
169
170                                if (!outerSize)
171                                {
172                                        // calculate overall dimensions for requested client area
173                                        RECT rc = { 0, 0, mWidth, mHeight };
174                                        AdjustWindowRect(&rc, dwStyle, false);
175
176                                        // clamp window dimensions to screen size
177                                        outerw = (rc.right-rc.left < screenw)? rc.right-rc.left : screenw;
178                                        outerh = (rc.bottom-rc.top < screenh)? rc.bottom-rc.top : screenh;
179                                }
180
181                                // center window if given negative coordinates
182                                if (mLeft < 0)
183                                        mLeft = (screenw - outerw) / 2;
184                                if (mTop < 0)
185                                        mTop = (screenh - outerh) / 2;
186
187                                // keep window contained in visible screen area
188                                if (mLeft > screenw - outerw)
189                                        mLeft = screenw - outerw;
190                                if (mTop > screenh - outerh)
191                                        mTop = screenh - outerh;
192                        }
193
194                        // register class and create window
195                        WNDCLASS wc = { CS_OWNDC, WndProc, 0, 0, hInst,
196                                LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
197                                (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "OgreGLWindow" };
198                        RegisterClass(&wc);
199
200                        // Pass pointer to self as WM_CREATE parameter
201                        mHWnd = CreateWindowEx(dwStyleEx, "OgreGLWindow", title.c_str(),
202                                dwStyle, mLeft, mTop, outerw, outerh, parent, 0, hInst, this);
203
204                        StringUtil::StrStreamType str;
205                        str << "Created Win32Window '"
206                                << mName << "' : " << mWidth << "x" << mHeight
207                                << ", " << mColourDepth << "bpp";
208                        LogManager::getSingleton().logMessage(LML_NORMAL, str.str());
209
210                        if (mIsFullScreen)
211                        {
212                                DEVMODE dm;
213                                dm.dmSize = sizeof(DEVMODE);
214                                dm.dmBitsPerPel = mColourDepth;
215                                dm.dmPelsWidth = mWidth;
216                                dm.dmPelsHeight = mHeight;
217                                dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
218                                if (mDisplayFrequency)
219                                {
220                                        dm.dmDisplayFrequency = mDisplayFrequency;
221                                        dm.dmFields |= DM_DISPLAYFREQUENCY;
222                                }
223                                if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
224                                        LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
225                        }
226                }
227
228                HDC old_hdc = wglGetCurrentDC();
229                HGLRC old_context = wglGetCurrentContext();
230
231                RECT rc;
232                // top and left represent outer window position
233                GetWindowRect(mHWnd, &rc);
234                mTop = rc.top;
235                mLeft = rc.left;
236                // width and height represent drawable area only
237                GetClientRect(mHWnd, &rc);
238                mWidth = rc.right;
239                mHeight = rc.bottom;
240
241                mHDC = GetDC(mHWnd);
242
243                if (!mGLSupport.selectPixelFormat(mHDC, mColourDepth, fsaa))
244                {
245                        if (fsaa == 0)
246                                OGRE_EXCEPT(0, "selectPixelFormat failed", "Win32Window::create");
247
248                        LogManager::getSingleton().logMessage(LML_NORMAL, "FSAA level not supported, falling back");
249                        if (!mGLSupport.selectPixelFormat(mHDC, mColourDepth, 0))
250                                OGRE_EXCEPT(0, "selectPixelFormat failed", "Win32Window::create");
251                }
252
253                mGlrc = wglCreateContext(mHDC);
254                if (!mGlrc)
255                        OGRE_EXCEPT(0, "wglCreateContext", "Win32Window::create");
256                if (!wglMakeCurrent(mHDC, mGlrc))
257                        OGRE_EXCEPT(0, "wglMakeCurrent", "Win32Window::create");
258
259                // Don't use wglew as if this is the first window, we won't have initialised yet
260                PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT =
261                        (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
262                _wglSwapIntervalEXT(vsync? 1 : 0);
263
264        if (old_context)
265        {
266            // Restore old context
267                    if (!wglMakeCurrent(old_hdc, old_context))
268                            OGRE_EXCEPT(0, "wglMakeCurrent() failed", "Win32Window::create");
269
270            // Share lists with old context
271                    if (!wglShareLists(old_context, mGlrc))
272                            OGRE_EXCEPT(0, "wglShareLists() failed", " Win32Window::create");
273        }
274
275                // Create RenderSystem context
276                mContext = new Win32Context(mHDC, mGlrc);
277
278                mActive = true;
279        }
280
281        void Win32Window::destroy(void)
282        {
283                if (!mHWnd)
284                        return;
285
286                // Unregister and destroy OGRE GLContext
287                delete mContext;
288
289                if (mGlrc)
290                {
291                        wglDeleteContext(mGlrc);
292                        mGlrc = 0;
293                }
294                if (!mIsExternal)
295                {
296                        if (mIsFullScreen)
297                                ChangeDisplaySettings(NULL, 0);
298                        DestroyWindow(mHWnd);
299                }
300                mActive = false;
301                mHDC = 0; // no release thanks to CS_OWNDC wndclass style
302                mHWnd = 0;
303        }
304
305        bool Win32Window::isVisible() const
306        {
307                return (mHWnd && !IsIconic(mHWnd));
308        }
309
310        bool Win32Window::isClosed() const
311        {
312                return mClosed;
313        }
314
315        void Win32Window::reposition(int left, int top)
316        {
317                if (mHWnd && !mIsFullScreen)
318                {
319                        SetWindowPos(mHWnd, 0, left, top, 0, 0,
320                                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
321                }
322        }
323
324        void Win32Window::resize(unsigned int width, unsigned int height)
325        {
326                if (mHWnd && !mIsFullScreen)
327                {
328                        RECT rc = { 0, 0, width, height };
329                        AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
330                        width = rc.right - rc.left;
331                        height = rc.bottom - rc.top;
332                        SetWindowPos(mHWnd, 0, 0, 0, width, height,
333                                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
334                }
335        }
336
337        void Win32Window::windowMovedOrResized()
338        {
339                if (!isVisible())
340                        return;
341
342                RECT rc;
343                // top and left represent outer window position
344                GetWindowRect(mHWnd, &rc);
345                mTop = rc.top;
346                mLeft = rc.left;
347                // width and height represent drawable area only
348                GetClientRect(mHWnd, &rc);
349
350                if (mWidth == rc.right && mHeight == rc.bottom)
351                        return;
352
353                mWidth = rc.right;
354                mHeight = rc.bottom;
355
356                // Notify viewports of resize
357                ViewportList::iterator it, itend;
358                itend = mViewportList.end();
359                for( it = mViewportList.begin(); it != itend; ++it )
360                        (*it).second->_updateDimensions();
361        }
362
363        void Win32Window::swapBuffers(bool waitForVSync)
364        {
365                SwapBuffers(mHDC);
366        }
367
368        void Win32Window::writeContentsToFile(const String& filename)
369        {
370                ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
371                imgData->width = mWidth;
372                imgData->height = mHeight;
373                imgData->depth = 1;
374                imgData->format = PF_BYTE_RGB;
375
376                // Allocate buffer
377                uchar* pBuffer = new uchar[mWidth * mHeight * 3];
378
379                // Read pixels
380                // I love GL: it does all the locking & colour conversion for us
381                glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer);
382
383                // Wrap buffer in a memory stream
384                DataStreamPtr stream(new MemoryDataStream(pBuffer, mWidth * mHeight * 3, false));
385
386                // Need to flip the read data over in Y though
387                Image img;
388                img.loadRawData(stream, mWidth, mHeight, imgData->format );
389                img.flipAroundX();
390
391                MemoryDataStreamPtr streamFlipped(new MemoryDataStream(img.getData(), stream->size(), false));
392
393                // Get codec
394                size_t pos = filename.find_last_of(".");
395                String extension;
396                if( pos == String::npos )
397                        OGRE_EXCEPT(
398                        Exception::ERR_INVALIDPARAMS,
399                        "Unable to determine image type for '" + filename + "' - invalid extension.",
400                        "Win32Window::writeContentsToFile" );
401
402                while( pos != filename.length() - 1 )
403                        extension += filename[++pos];
404
405                // Get the codec
406                Codec * pCodec = Codec::getCodec(extension);
407
408                // Write out
409                Codec::CodecDataPtr ptr(imgData);
410                pCodec->codeToFile(streamFlipped, filename, ptr);
411
412                delete [] pBuffer;
413        }
414
415        // Window procedure callback
416        // This is a static member, so applies to all windows but we store the
417        // Win32Window instance in the window data GetWindowLog/SetWindowLog
418        LRESULT Win32Window::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
419        {
420
421                if (uMsg == WM_CREATE)
422                {
423                        // Store pointer to Win32Window in user data area
424                        SetWindowLongPtr(hWnd, GWLP_USERDATA,
425                                (LONG)(((LPCREATESTRUCT)lParam)->lpCreateParams));
426                        return 0;
427                }
428
429                // look up window instance
430                // note: it is possible to get a WM_SIZE before WM_CREATE
431                Win32Window* win = (Win32Window*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
432                if (!win)
433                        return DefWindowProc(hWnd, uMsg, wParam, lParam);
434
435                switch( uMsg )
436                {
437                case WM_ACTIVATE:
438                        if (win->mIsFullScreen)
439                        {
440                                if (LOWORD(wParam) == WA_INACTIVE)
441                                {
442                                        win->mActive = false;
443                                        ChangeDisplaySettings(NULL, 0);
444                                        ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
445                                }
446                                else
447                                {
448                                        win->mActive = true;
449                                        ShowWindow(hWnd, SW_SHOWNORMAL);
450
451                                        DEVMODE dm;
452                                        dm.dmSize = sizeof(DEVMODE);
453                                        dm.dmBitsPerPel = win->mColourDepth;
454                                        dm.dmPelsWidth = win->mWidth;
455                                        dm.dmPelsHeight = win->mHeight;
456                                        dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
457                                        if (win->mDisplayFrequency)
458                                        {
459                                                dm.dmDisplayFrequency = win->mDisplayFrequency;
460                                                dm.dmFields |= DM_DISPLAYFREQUENCY;
461                                        }
462                                        ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
463                                }
464                        }
465                        break;
466
467                case WM_ENTERSIZEMOVE:
468                        win->mSizing = true;
469                        break;
470
471                case WM_EXITSIZEMOVE:
472                        win->windowMovedOrResized();
473                        win->mSizing = false;
474                        break;
475
476                case WM_MOVE:
477                case WM_SIZE:
478                        if (!win->mSizing)
479                                win->windowMovedOrResized();
480                        break;
481
482                case WM_GETMINMAXINFO:
483                        // Prevent the window from going smaller than some minimu size
484                        ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
485                        ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
486                        break;
487
488                case WM_CLOSE:
489                        win->destroy(); // will call DestroyWindow
490                        win->mClosed = true;
491                        return 0;
492                }
493
494                return DefWindowProc( hWnd, uMsg, wParam, lParam );
495        }
496
497        void Win32Window::getCustomAttribute( const String& name, void* pData )
498        {
499                if( name == "GLCONTEXT" ) {
500                        *static_cast<GLContext**>(pData) = mContext;
501                        return;
502                } else if( name == "HWND" )
503                {
504                        HWND *pHwnd = (HWND*)pData;
505                        *pHwnd = getWindowHandle();
506                        return;
507                }
508        }
509
510}
Note: See TracBrowser for help on using the repository browser.