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

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:55 2004