# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

driver.cpp

Go to the documentation of this file.
00001 
00008 /* Copyright, 2000 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details.
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #include "std3d.h"
00028 
00029 #include <string>
00030 
00031 #include "nel/misc/types_nl.h"
00032 #include "3d/driver.h"
00033 #include "3d/shader.h"
00034 #include "3d/vertex_buffer.h"
00035 
00036 //#include <stdio.h>
00037 
00038 using namespace NLMISC;
00039 using namespace std;
00040 
00041 
00042 namespace NL3D
00043 {
00044 
00045 // ***************************************************************************
00046 const uint32 IDriver::InterfaceVersion = 0x46;
00047 
00048 // ***************************************************************************
00049 IDriver::IDriver() : _SyncTexDrvInfos( "IDriver::_SyncTexDrvInfos" )
00050 {
00051         _PolygonMode= Filled;
00052 }
00053 
00054 // ***************************************************************************
00055 IDriver::~IDriver()
00056 {
00057         // Must clean up everything before closing driver.
00058         // Must doing this in release(), so assert here if not done...
00059         {               
00060                 CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
00061                 TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
00062                 nlassert( rTexDrvInfos.size() == 0 );
00063         }
00064 
00065         nlassert(_TexDrvShares.size()==0);
00066         nlassert(_Shaders.size()==0);
00067         nlassert(_VBDrvInfos.size()==0);
00068         nlassert(_VtxPrgDrvInfos.size()==0);
00069 }
00070 
00071 
00072 // ***************************************************************************
00073 bool            IDriver::release(void)
00074 {
00075         // Called by derived classes.
00076 
00077         // DO THIS FIRST => to auto kill real textures (by smartptr).
00078         // First, Because must not kill a pointer owned by a CSmartPtr.
00079         // Release Textures drv.
00080         ItTexDrvSharePtrList            ittex;
00081         while( (ittex = _TexDrvShares.begin()) !=_TexDrvShares.end() )
00082         {
00083                 // NB: at CTextureDrvShare deletion, this->_TexDrvShares is updated (entry deleted);
00084                 delete *ittex;
00085         }
00086 
00087 
00088         // Release refptr of TextureDrvInfos. Should be all null (because of precedent pass).
00089         {
00090                 CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
00091                 TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
00092 
00093                 // must be empty, because precedent pass should have deleted all.
00094                 nlassert(rTexDrvInfos.empty());
00095         }
00096 
00097         // Release Shader drv.
00098         ItShaderPtrList         itshd;
00099         while( (itshd = _Shaders.begin()) != _Shaders.end() )
00100         {
00101                 // NB: at IShader deletion, this->_Shaders is updated (entry deleted);
00102                 delete *itshd;
00103         }
00104 
00105         // Release VBs drv.
00106         ItVBDrvInfoPtrList              itvb;
00107         while( (itvb = _VBDrvInfos.begin()) != _VBDrvInfos.end() )
00108         {
00109                 // NB: at IVBDrvInfo deletion, this->_VBDrvInfos is updated (entry deleted);
00110                 delete *itvb;
00111         }
00112 
00113         // Release VtxPrg drv.
00114         ItVtxPrgDrvInfoPtrList          itVtxPrg;
00115         while( (itVtxPrg = _VtxPrgDrvInfos.begin()) != _VtxPrgDrvInfos.end() )
00116         {
00117                 // NB: at IVertexProgramDrvInfos deletion, this->_VtxPrgDrvInfos is updated (entry deleted);
00118                 delete *itVtxPrg;
00119         }
00120 
00121         return true;
00122 }
00123 
00124 
00125 // ***************************************************************************
00126 GfxMode::GfxMode(uint16 w, uint16 h, uint8 d, bool windowed, bool offscreen)
00127 {
00128         Windowed= windowed;
00129         Width= w;
00130         Height= h;
00131         Depth= d;
00132         OffScreen= offscreen;
00133 }
00134 
00135 // ***************************************************************************
00136 IDriver::TMessageBoxId IDriver::systemMessageBox (const char* message, const char* title, IDriver::TMessageBoxType type, IDriver::TMessageBoxIcon icon)
00137 {
00138         static const char* icons[iconCount]=
00139         {
00140                 "",
00141                 "WAIT:\n",
00142                 "QUESTION:\n",
00143                 "HEY!\n",
00144                 "",
00145                 "WARNING!\n",
00146                 "ERROR!\n",
00147                 "INFORMATION:\n",
00148                 "STOP:\n"
00149         };
00150         static const char* messages[typeCount]=
00151         {
00152                 "Press any key...",
00153                 "(O)k or (C)ancel ?",
00154                 "(Y)es or (N)o ?",
00155                 "(A)bort (R)etry (I)gnore ?",
00156                 "(Y)es (N)o (C)ancel ?",
00157                 "(R)etry (C)ancel ?"
00158         };
00159         printf ("%s%s\n%s", icons[icon], title, message);
00160         while (1)
00161         {
00162                 printf ("\n%s", messages[type]);
00163                 int c=getchar();
00164                 if (type==okType)
00165                         return okId;
00166                 switch (c)
00167                 {
00168                 case 'O':
00169                 case 'o':
00170                         if ((type==okType)||(type==okCancelType))
00171                                 return okId;
00172                         break;
00173                 case 'C':
00174                 case 'c':
00175                         if ((type==yesNoCancelType)||(type==okCancelType)||(type==retryCancelType))
00176                                 return cancelId;
00177                         break;
00178                 case 'Y':
00179                 case 'y':
00180                         if ((type==yesNoCancelType)||(type==yesNoType))
00181                                 return yesId;
00182                         break;
00183                 case 'N':
00184                 case 'n':
00185                         if ((type==yesNoCancelType)||(type==yesNoType))
00186                                 return noId;
00187                         break;
00188                 case 'A':
00189                 case 'a':
00190                         if (type==abortRetryIgnoreType)
00191                                 return abortId;
00192                         break;
00193                 case 'R':
00194                 case 'r':
00195                         if (type==abortRetryIgnoreType)
00196                                 return retryId;
00197                         break;
00198                 case 'I':
00199                 case 'i':
00200                         if (type==abortRetryIgnoreType)
00201                                 return ignoreId;
00202                         break;
00203                 }
00204         }
00205         nlassert (0);           // no!
00206         return okId;
00207 }
00208 
00209 
00210 
00211 
00212 // ***************************************************************************
00213 void                    IDriver::removeVBDrvInfoPtr(ItVBDrvInfoPtrList  vbDrvInfoIt)
00214 {
00215         _VBDrvInfos.erase(vbDrvInfoIt);
00216 }
00217 // ***************************************************************************
00218 void                    IDriver::removeTextureDrvInfoPtr(ItTexDrvInfoPtrMap texDrvInfoIt)
00219 {
00220         CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
00221         TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
00222 
00223         rTexDrvInfos.erase(texDrvInfoIt);
00224 }
00225 // ***************************************************************************
00226 void                    IDriver::removeTextureDrvSharePtr(ItTexDrvSharePtrList texDrvShareIt)
00227 {
00228         _TexDrvShares.erase(texDrvShareIt);
00229 }
00230 // ***************************************************************************
00231 void                    IDriver::removeShaderPtr(ItShaderPtrList shaderIt)
00232 {
00233         _Shaders.erase(shaderIt);
00234 }
00235 // ***************************************************************************
00236 void                    IDriver::removeVtxPrgDrvInfoPtr(ItVtxPrgDrvInfoPtrList vtxPrgDrvInfoIt)
00237 {
00238         _VtxPrgDrvInfos.erase(vtxPrgDrvInfoIt);
00239 }
00240 
00241 // ***************************************************************************
00242 bool                    IDriver::invalidateShareTexture (ITexture &texture)
00243 {
00244         // Create the shared Name.
00245         std::string     name;
00246         getTextureShareName (texture, name);
00247 
00248         // Look for the driver info for this share name
00249         CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
00250         TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
00251         TTexDrvInfoPtrMap::iterator iteDrvInfo = rTexDrvInfos.find (name);
00252         if (iteDrvInfo != rTexDrvInfos.end())
00253         {
00254                 // Now parse all shared info
00255                 TTexDrvSharePtrList::iterator shareIte = _TexDrvShares.begin ();
00256                 while (shareIte != _TexDrvShares.end ())
00257                 {
00258                         // Good one ?
00259                         if ((*shareIte)->DrvTexture == iteDrvInfo->second)
00260                         {
00261                                 // Remove this one
00262                                 TTexDrvSharePtrList::iterator toRemove = shareIte;
00263                                 shareIte++;
00264                                 delete (*toRemove);
00265                         }
00266                         else 
00267                                 shareIte++;
00268                 }
00269 
00270                 // Ok
00271                 return true;
00272         }
00273         return false;
00274 }
00275 // ***************************************************************************
00276 void                    IDriver::getTextureShareName (const ITexture& tex, string &output)
00277 {
00278         // Create the shared Name.
00279         output= tex.getShareName();
00280 
00281         // append format Id of the texture.
00282         static char     fmt[256];
00283         smprintf(fmt, 256, "@Fmt:%d", (uint32)tex.getUploadFormat());
00284         output+= fmt;
00285 
00286         // append mipmap info
00287         if(tex.mipMapOn())
00288                 output+= "@MMp:On";
00289         else
00290                 output+= "@MMp:Off";
00291 }
00292 }