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

OgreDDVideoModeList.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreDDVideoModeList.h"
00026 #include "OgreDDVideoMode.h"
00027 #include "OgreException.h"
00028 
00029 namespace Ogre {
00030 
00031     // Non-member callback functions
00032     static HRESULT CALLBACK DDEnumModesCallback(
00033                                     LPDDSURFACEDESC2 lpDDSD2,
00034                                     LPVOID lpContext)
00035     {
00036         DDVideoModeList* modeList;
00037 
00038         // Omit any palettised modes
00039         if (lpDDSD2->ddpfPixelFormat.dwRGBBitCount >= 16)
00040         {
00041 
00042             modeList = (DDVideoModeList*) lpContext;
00043 
00044             modeList->AddMode(lpDDSD2);
00045         }
00046 
00047         // Continue enumeration
00048         return DDENUMRET_OK;
00049     }
00050 
00051 
00052     DDVideoModeList::DDVideoModeList(LPDIRECTDRAW7 directDraw)
00053     {
00054         // Will create a  new driver list and enumerate it
00055         if (directDraw == NULL)
00056             throw Exception(0, "NULL supplied as "
00057                 "DD interface pointer.", "DDVideoModeList Contructor");
00058 
00059         lpDD7 = directDraw;
00060         // Enumerate the list
00061         enumerate();
00062 
00063     }
00064 
00065     DDVideoModeList::~DDVideoModeList()
00066     {
00067         // Release each video mode object
00068         std::vector<DDVideoMode>::iterator p = mModeList.begin();
00069 
00070         mModeList.erase(p, p+mModeList.size()-1);
00071     }
00072 
00073 
00074 
00075 
00076     BOOL DDVideoModeList::enumerate()
00077     {
00078         HRESULT hr;
00079 
00080         // Enumerate display modes
00081         // Different refresh rates are NOT enumerated (dwFlags = 0)
00082         hr = lpDD7->EnumDisplayModes(0, NULL, this, DDEnumModesCallback);
00083         if (FAILED(hr))
00084             throw Exception(0, "Error enumerating display modes", "DDVideoModeList - enumerate");
00085 
00086 
00087         return TRUE;
00088     }
00089 
00090     void DDVideoModeList::AddMode(LPDDSURFACEDESC2 lpDDSD2)
00091     {
00092         //DDVideoMode *newMode;
00093 
00094 
00095         // Create new mode
00096         //newMode = new DDVideoMode(lpDDSD2);
00097 
00098         // Add it to my list
00099         mModeList.push_back(DDVideoMode(lpDDSD2));
00100 
00101 
00102     }
00103 
00104     unsigned int DDVideoModeList::count(void)
00105     {
00106         return static_cast< unsigned int >( mModeList.size() );
00107     }
00108 
00109     DDVideoMode* DDVideoModeList::item(int index)
00110     {
00111         // Get an iterator for the vector
00112         std::vector<DDVideoMode>::iterator p = mModeList.begin();
00113 
00114         // Return the indexed driver
00115         return &p[index];
00116 
00117 
00118     }
00119 
00120     DDVideoMode* DDVideoModeList::getClosestMatch(int width, int height, int colourDepth)
00121     {
00122         // Search through looking for closest match
00123         int bestDiff, currentDiff, bestIndex;
00124         DDVideoMode *vm;
00125 
00126         std::vector<DDVideoMode>::iterator p = mModeList.begin();
00127 
00128 
00129         bestDiff = 9999;
00130         bestIndex = -1;
00131         for( unsigned j = 0; j < count(); j++ )
00132         {
00133             currentDiff = 0;
00134             vm = &p[j];
00135             currentDiff += abs(vm->mWidth - width);
00136             currentDiff += abs(vm->mHeight - height);
00137             currentDiff += abs(vm->mColourDepth - colourDepth);
00138 
00139             if (currentDiff < bestDiff)
00140             {
00141                 bestDiff = currentDiff;
00142                 bestIndex = j;
00143             }
00144 
00145             /* We Love Intel's Compilers :) */
00146             if (currentDiff == 0)
00147                 break; // No point continuing, direct match
00148         }
00149 
00150         return &p[bestIndex];
00151     }
00152 
00153 }
00154 
00155 

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