source: trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp @ 193

Revision 193, 14.4 KB checked in by mattausch, 19 years ago (diff)

changed to ogre 103
added readme

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                        RECT rc;
229                // top and left represent outer window position
230                GetWindowRect(mHWnd, &rc);
231                mTop = rc.top;
232                mLeft = rc.left;
233                // width and height represent drawable area only
234                        GetClientRect(mHWnd, &rc);
235                        mWidth = rc.right;
236                        mHeight = rc.bottom;
237               
238                mHDC = GetDC(mHWnd);
239
240                if (!mGLSupport.selectPixelFormat(mHDC, mColourDepth, fsaa))
241                {
242                        if (fsaa == 0)
243                                OGRE_EXCEPT(0, "selectPixelFormat failed", "Win32Window::create");
244
245                        LogManager::getSingleton().logMessage(LML_NORMAL, "FSAA level not supported, falling back");
246                        if (!mGLSupport.selectPixelFormat(mHDC, mColourDepth, 0))
247                                OGRE_EXCEPT(0, "selectPixelFormat failed", "Win32Window::create");
248                }
249
250                mGlrc = wglCreateContext(mHDC);
251                if (!mGlrc)
252                        OGRE_EXCEPT(0, "wglCreateContext", "Win32Window::create");
253                if (!wglMakeCurrent(mHDC, mGlrc))
254                        OGRE_EXCEPT(0, "wglMakeCurrent", "Win32Window::create");
255               
256                wglSwapIntervalEXT(vsync? 1 : 0);
257
258        // Create RenderSystem context
259        mContext = new Win32Context(mHDC, mGlrc);
260        // Register the context with the rendersystem and associate it with this window
261        GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());
262        rs->_registerContext(this, mContext);
263
264                mActive = true;
265    }
266
267    void Win32Window::destroy(void)
268    {
269                if (!mHWnd)
270                        return;
271
272        // Unregister and destroy OGRE GLContext
273        if (mContext)
274        {
275            GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());
276            rs->_unregisterContext(this);
277            delete mContext;
278                        mContext = 0;
279        }
280                if (mGlrc)
281                {
282                        wglDeleteContext(mGlrc);
283                        mGlrc = 0;
284                }
285                if (!mIsExternal)
286                {
287                if (mIsFullScreen)
288                        ChangeDisplaySettings(NULL, 0);
289                DestroyWindow(mHWnd);
290        }
291        mActive = false;
292                mHDC = 0; // no release thanks to CS_OWNDC wndclass style
293                mHWnd = 0;
294    }
295
296        bool Win32Window::isVisible() const
297    {
298                return (mHWnd && !IsIconic(mHWnd));
299    }
300
301    bool Win32Window::isClosed() const
302    {
303        return mClosed;
304    }
305
306    void Win32Window::reposition(int left, int top)
307    {
308                if (mHWnd && !mIsFullScreen)
309                {
310                        SetWindowPos(mHWnd, 0, left, top, 0, 0,
311                                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
312                }
313    }
314
315    void Win32Window::resize(unsigned int width, unsigned int height)
316    {
317                if (mHWnd && !mIsFullScreen)
318                {
319                        RECT rc = { 0, 0, width, height };
320                        AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
321                        width = rc.right - rc.left;
322                        height = rc.bottom - rc.top;
323                        SetWindowPos(mHWnd, 0, 0, 0, width, height,
324                                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
325                }
326        }
327
328        void Win32Window::windowMovedOrResized()
329        {
330                if (!isVisible())
331                        return;
332
333                RECT rc;
334                // top and left represent outer window position
335                GetWindowRect(mHWnd, &rc);
336                mTop = rc.top;
337                mLeft = rc.left;
338                // width and height represent drawable area only
339                GetClientRect(mHWnd, &rc);
340
341                if (mWidth == rc.right && mHeight == rc.bottom)
342                        return;
343
344                mWidth = rc.right;
345                mHeight = rc.bottom;
346
347                // Notify viewports of resize
348                ViewportList::iterator it, itend;
349        itend = mViewportList.end();
350                for( it = mViewportList.begin(); it != itend; ++it )
351                        (*it).second->_updateDimensions();
352        }
353
354    void Win32Window::swapBuffers(bool waitForVSync)
355    {
356                SwapBuffers(mHDC);
357    }
358
359        void Win32Window::writeContentsToFile(const String& filename)
360        {
361                ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
362                imgData->width = mWidth;
363                imgData->height = mHeight;
364                imgData->depth = 1;
365                imgData->format = PF_BYTE_RGB;
366
367                // Allocate buffer
368                uchar* pBuffer = new uchar[mWidth * mHeight * 3];
369
370                // Read pixels
371                // I love GL: it does all the locking & colour conversion for us
372                glReadPixels(0,0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer);
373
374                // Wrap buffer in a memory stream
375                DataStreamPtr stream(new MemoryDataStream(pBuffer, mWidth * mHeight * 3, false));
376
377                // Need to flip the read data over in Y though
378                Image img;
379                img.loadRawData(stream, mWidth, mHeight, imgData->format );
380                img.flipAroundX();
381
382                MemoryDataStreamPtr streamFlipped(new MemoryDataStream(img.getData(), stream->size(), false));
383
384                // Get codec
385                size_t pos = filename.find_last_of(".");
386                String extension;
387                if( pos == String::npos )
388                        OGRE_EXCEPT(
389                        Exception::ERR_INVALIDPARAMS,
390                        "Unable to determine image type for '" + filename + "' - invalid extension.",
391                        "Win32Window::writeContentsToFile" );
392
393                while( pos != filename.length() - 1 )
394                        extension += filename[++pos];
395
396                // Get the codec
397                Codec * pCodec = Codec::getCodec(extension);
398
399                // Write out
400                Codec::CodecDataPtr ptr(imgData);
401        pCodec->codeToFile(streamFlipped, filename, ptr);
402
403                delete [] pBuffer;
404        }
405
406#ifdef GTP_VISIBILITY_MODIFIED_OGRE
407        uchar *Win32Window::getBufferContents(int &dimx, int &dimy)
408        {
409                dimx = mWidth;
410                dimy = mHeight;
411               
412                // Allocate buffer
413                uchar* pBuffer = new uchar[mWidth * mHeight * 3];
414
415                // Read pixels
416                // I love GL: it does all the locking & colour conversion for us
417                glReadPixels(0, 0, mWidth-1, mHeight-1, GL_RGB, GL_UNSIGNED_BYTE, pBuffer);
418
419                return pBuffer;
420        }
421#endif // GTP_VISIBILITY_MODIFIED_OGRE
422
423        // Window procedure callback
424        // This is a static member, so applies to all windows but we store the
425        // Win32Window instance in the window data GetWindowLog/SetWindowLog
426        LRESULT Win32Window::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
427        {
428
429                if (uMsg == WM_CREATE)
430                {
431                        // Store pointer to Win32Window in user data area
432                        SetWindowLong(hWnd, GWL_USERDATA,
433                                (LONG)(((LPCREATESTRUCT)lParam)->lpCreateParams));
434                        return 0;
435                }
436
437                // look up window instance
438                // note: it is possible to get a WM_SIZE before WM_CREATE
439                Win32Window* win = (Win32Window*)GetWindowLong(hWnd, GWL_USERDATA);
440                if (!win)
441                        return DefWindowProc(hWnd, uMsg, wParam, lParam);
442
443                switch( uMsg )
444                {
445                case WM_ACTIVATE:
446                        if (win->mIsFullScreen)
447                        {
448                                if (LOWORD(wParam) == WA_INACTIVE)
449                                {
450                                win->mActive = false;
451                                        ChangeDisplaySettings(NULL, 0);
452                                        ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
453                                }
454                        else
455                                {
456                                win->mActive = true;
457                                        ShowWindow(hWnd, SW_SHOWNORMAL);
458
459                                        DEVMODE dm;
460                                        dm.dmSize = sizeof(DEVMODE);
461                                        dm.dmBitsPerPel = win->mColourDepth;
462                                        dm.dmPelsWidth = win->mWidth;
463                                        dm.dmPelsHeight = win->mHeight;
464                                        dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
465                                        if (win->mDisplayFrequency)
466                                        {
467                                                dm.dmDisplayFrequency = win->mDisplayFrequency;
468                                                dm.dmFields |= DM_DISPLAYFREQUENCY;
469                                        }
470                                        ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
471                                }
472                        }
473                        break;
474
475                case WM_ENTERSIZEMOVE:
476                        win->mSizing = true;
477                        break;
478
479                case WM_EXITSIZEMOVE:
480                        win->windowMovedOrResized();
481                        win->mSizing = false;
482                        break;
483
484                case WM_MOVE:
485                case WM_SIZE:
486                        if (!win->mSizing)
487                                        win->windowMovedOrResized();
488                        break;
489
490                case WM_GETMINMAXINFO:
491                        // Prevent the window from going smaller than some minimu size
492                        ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
493                        ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
494                        break;
495
496                case WM_CLOSE:
497                        win->destroy(); // will call DestroyWindow
498                        win->mClosed = true;
499                        return 0;
500                }
501
502                return DefWindowProc( hWnd, uMsg, wParam, lParam );
503        }
504       
505        void Win32Window::getCustomAttribute( const String& name, void* pData )
506        {
507                if( name == "HWND" )
508                {
509                        HWND *pHwnd = (HWND*)pData;
510                        *pHwnd = getWindowHandle();
511                        return;
512                }
513        }
514
515}
Note: See TracBrowser for help on using the repository browser.