Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreWin32GLSupport.cpp

Go to the documentation of this file.
00001 #include "OgreException.h"
00002 #include "OgreLogManager.h"
00003 #include "OgreStringConverter.h"
00004 
00005 #include <algorithm>
00006 
00007 #include "OgreWin32GLSupport.h"
00008 
00009 #include "OgreWin32Window.h"
00010 
00011 using namespace Ogre;
00012 
00013 namespace Ogre {
00014     Win32GLSupport::Win32GLSupport():
00015         mExternalWindowHandle(0)
00016     {
00017     } 
00018 
00019     template<class C> void remove_duplicates(C& c)
00020     {
00021         std::sort(c.begin(), c.end());
00022         typename C::iterator p = std::unique(c.begin(), c.end());
00023         c.erase(p, c.end());
00024     }
00025 
00026     void Win32GLSupport::addConfig()
00027     {
00028         //TODO: EnumDisplayDevices http://msdn.microsoft.com/library/en-us/gdi/devcons_2303.asp
00029         /*vector<string> DisplayDevices;
00030         DISPLAY_DEVICE DisplayDevice;
00031         DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
00032         DWORD i=0;
00033         while (EnumDisplayDevices(NULL, i++, &DisplayDevice, 0) {
00034             DisplayDevices.push_back(DisplayDevice.DeviceName);
00035         }*/
00036           
00037         ConfigOption optFullScreen;
00038         ConfigOption optVideoMode;
00039         ConfigOption optColourDepth;
00040         ConfigOption optDisplayFrequency;
00041         ConfigOption optVSync;
00042 
00043         // FS setting possiblities
00044         optFullScreen.name = "Full Screen";
00045         optFullScreen.possibleValues.push_back("Yes");
00046         optFullScreen.possibleValues.push_back("No");
00047         optFullScreen.currentValue = "Yes";
00048         optFullScreen.immutable = false;
00049 
00050         // Video mode possiblities
00051         DEVMODE DevMode;
00052         DevMode.dmSize = sizeof(DEVMODE);
00053         optVideoMode.name = "Video Mode";
00054         optVideoMode.immutable = false;
00055         for (DWORD i = 0; EnumDisplaySettings(NULL, i, &DevMode); ++i)
00056         {
00057             if (DevMode.dmBitsPerPel < 16 || DevMode.dmPelsHeight < 480)
00058                 continue;
00059             mDevModes.push_back(DevMode);
00060             char szBuf[16];
00061             snprintf(szBuf, 16, "%d x %d", DevMode.dmPelsWidth, DevMode.dmPelsHeight);
00062             optVideoMode.possibleValues.push_back(szBuf);
00063         }
00064         remove_duplicates(optVideoMode.possibleValues);
00065         optVideoMode.currentValue = optVideoMode.possibleValues.front();
00066 
00067         optColourDepth.name = "Colour Depth";
00068         optColourDepth.immutable = false;
00069         optColourDepth.currentValue = "";
00070 
00071         optDisplayFrequency.name = "Display Frequency";
00072         optDisplayFrequency.immutable = false;
00073         optDisplayFrequency.currentValue = "";
00074 
00075         optVSync.name = "VSync";
00076         optVSync.immutable = false;
00077         optVSync.possibleValues.push_back("No");
00078         optVSync.possibleValues.push_back("Yes");
00079         optVSync.currentValue = "No";
00080 
00081         mOptions[optFullScreen.name] = optFullScreen;
00082         mOptions[optVideoMode.name] = optVideoMode;
00083         mOptions[optColourDepth.name] = optColourDepth;
00084         mOptions[optDisplayFrequency.name] = optDisplayFrequency;
00085         mOptions[optVSync.name] = optVSync;
00086 
00087         refreshConfig();
00088     }
00089 
00090     void Win32GLSupport::refreshConfig()
00091     {
00092         ConfigOptionMap::iterator optVideoMode = mOptions.find("Video Mode");
00093         ConfigOptionMap::iterator moptColourDepth = mOptions.find("Colour Depth");
00094         ConfigOptionMap::iterator moptDisplayFrequency = mOptions.find("Display Frequency");
00095         if(optVideoMode == mOptions.end() || moptColourDepth == mOptions.end() || moptDisplayFrequency == mOptions.end())
00096             Except(999, "Can't find mOptions!", "Win32GLSupport::refreshConfig");
00097         ConfigOption* optColourDepth = &moptColourDepth->second;
00098         ConfigOption* optDisplayFrequency = &moptDisplayFrequency->second;
00099 
00100         String val = optVideoMode->second.currentValue;
00101         String::size_type pos = val.find('x');
00102         if (pos == String::npos)
00103             Except(999, "Invalid Video Mode provided", "Win32GLSupport::refreshConfig");
00104         int width = atoi(val.substr(0, pos).c_str());
00105 
00106         for(std::vector<DEVMODE>::const_iterator i = mDevModes.begin(); i != mDevModes.end(); ++i)
00107         {
00108             if (int(i->dmPelsWidth) != width)
00109                 continue;
00110             char buf[128];
00111             sprintf(buf, "%d", i->dmBitsPerPel);
00112             optColourDepth->possibleValues.push_back(buf);
00113             sprintf(buf, "%d", i->dmDisplayFrequency);
00114             optDisplayFrequency->possibleValues.push_back(buf);
00115         }
00116         remove_duplicates(optColourDepth->possibleValues);
00117         remove_duplicates(optDisplayFrequency->possibleValues);
00118         optColourDepth->currentValue = optColourDepth->possibleValues.back();
00119         optDisplayFrequency->currentValue = optDisplayFrequency->possibleValues.front();
00120     }
00121 
00122     void Win32GLSupport::setConfigOption(const String &name, const String &value)
00123     {
00124         ConfigOptionMap::iterator it = mOptions.find(name);
00125 
00126         // Update
00127         if(it != mOptions.end())
00128             it->second.currentValue = value;
00129         else
00130         {
00131             char msg[128];
00132             sprintf( msg, "Option named '%s' does not exist.", name.c_str() );
00133             Except( Exception::ERR_INVALIDPARAMS, msg, "Win32GLSupport::setConfigOption" );
00134         }
00135 
00136         if( name == "Video Mode" )
00137             refreshConfig();
00138 
00139         if( name == "Full Screen" )
00140         {
00141             it = mOptions.find( "Display Frequency" );
00142             if( value == "No" )
00143             {
00144                 it->second.currentValue = "N/A";
00145                 it->second.immutable = true;
00146             }
00147             else
00148             {
00149                 it->second.currentValue = it->second.possibleValues.front();
00150                 it->second.immutable = false;
00151             }
00152         }
00153     }
00154 
00155     String Win32GLSupport::validateConfig()
00156     {
00157         // TODO, DX9
00158         return String("");
00159     }
00160 
00161     RenderWindow* Win32GLSupport::createWindow(bool autoCreateWindow, GLRenderSystem* renderSystem, const String& windowTitle)
00162     {
00163         if (autoCreateWindow)
00164         {
00165             ConfigOptionMap::iterator opt = mOptions.find("Full Screen");
00166             if (opt == mOptions.end())
00167                 Except(999, "Can't find full screen options!", "Win32GLSupport::createWindow");
00168             bool fullscreen = (opt->second.currentValue == "Yes");
00169 
00170             opt = mOptions.find("Video Mode");
00171             if (opt == mOptions.end())
00172                 Except(999, "Can't find video mode options!", "Win32GLSupport::createWindow");
00173             String val = opt->second.currentValue;
00174             String::size_type pos = val.find('x');
00175             if (pos == String::npos)
00176                 Except(999, "Invalid Video Mode provided", "Win32GLSupport::createWindow");
00177 
00178             unsigned int w = StringConverter::parseUnsignedInt(val.substr(0, pos));
00179             unsigned int h = StringConverter::parseUnsignedInt(val.substr(pos + 1));
00180 
00181             opt = mOptions.find("Colour Depth");
00182             if (opt == mOptions.end())
00183                 Except(999, "Can't find Colour Depth options!", "Win32GLSupport::createWindow");
00184             unsigned int colourDepth = StringConverter::parseUnsignedInt(opt->second.currentValue);
00185 
00186             opt = mOptions.find("VSync");
00187             if (opt == mOptions.end())
00188                 Except(999, "Can't find VSync options!", "Win32GLSupport::createWindow");
00189             bool vsync = (opt->second.currentValue == "Yes");
00190             renderSystem->setWaitForVerticalBlank(vsync);
00191 
00192             return renderSystem->createRenderWindow(windowTitle, w, h, colourDepth, fullscreen);
00193         }
00194         else
00195         {
00196             // XXX What is the else?
00197             return NULL;
00198         }
00199     }
00200 
00201     RenderWindow* Win32GLSupport::newWindow(const String& name, unsigned int width, unsigned int height, unsigned int colourDepth,
00202             bool fullScreen, int left, int top, bool depthBuffer, RenderWindow* parentWindowHandle,
00203             bool vsync)
00204     {
00205         ConfigOptionMap::iterator opt = mOptions.find("Display Frequency");
00206         if (opt == mOptions.end())
00207             Except(999, "Can't find Colour Depth options!", "Win32GLSupport::newWindow");
00208         unsigned int displayFrequency = StringConverter::parseUnsignedInt(opt->second.currentValue);
00209 
00210         Win32Window* window = new Win32Window();
00211         if (!fullScreen && mExternalWindowHandle) // ADD CONTROL IF WE HAVE A WINDOW)
00212         {
00213             Win32Window *pWin32Window = (Win32Window *)window;
00214             pWin32Window->setExternalWindowHandle(mExternalWindowHandle);
00215         }
00216         window->create(name, width, height, colourDepth, fullScreen, left, top, depthBuffer,
00217             parentWindowHandle, vsync, displayFrequency);
00218         return window;
00219     }
00220 
00221     void Win32GLSupport::start()
00222     {
00223         LogManager::getSingleton().logMessage("*** Starting Win32GL Subsystem ***");
00224     }
00225 
00226     void Win32GLSupport::stop()
00227     {
00228         LogManager::getSingleton().logMessage("*** Stopping Win32GL Subsystem ***");
00229     }
00230 
00231     void* Win32GLSupport::getProcAddress(const String& procname)
00232     {
00233         return wglGetProcAddress( procname.c_str() );
00234     }
00235 
00236     void Win32GLSupport::resizeReposition(void* renderTarget)
00237     {
00238         Win32Window  *pWin32Window = (Win32Window *)renderTarget;
00239         if (pWin32Window->getWindowHandle()== m_windowToResize){
00240                     pWin32Window->windowMovedOrResized();
00241         }
00242 
00243     }
00244 }

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:51 2004