Index: OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLTexture.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLTexture.h	(revision 153)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLTexture.h	(revision 657)
@@ -46,6 +46,4 @@
         virtual ~GLTexture();      
 
-        /// @copydoc Texture::createInternalResources
-        void createInternalResources(void);
 		void loadImage( const Image& img );
         void createRenderTexture();
@@ -61,8 +59,10 @@
 
     protected:
+		/// @copydoc Texture::createInternalResourcesImpl
+		void createInternalResourcesImpl(void);
         /// @copydoc Resource::loadImpl
         void loadImpl(void);
-        /// @copydoc Resource::unloadImpl
-        void unloadImpl(void);
+        /// @copydoc Resource::freeInternalResourcesImpl
+        void freeInternalResourcesImpl(void);
 
 		/** internal method, create GLHardwarePixelBuffers for every face and
Index: OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLHardwareOcclusionQuery.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLHardwareOcclusionQuery.h	(revision 343)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/include/OgreGLHardwareOcclusionQuery.h	(revision 657)
@@ -53,7 +53,7 @@
 
 // If you use multiple rendering passes you can test only the first pass and all other passes don't have to be rendered 
-// if the first pass resultet has too few pixels visable.
+// if the first pass result has too few pixels visible.
 
-// Be sure to render all occlluder first and whats out so the RenderQue don't switch places on 
+// Be sure to render all occluder first and whats out so the RenderQue don't switch places on 
 // the occluding objects and the tested objects because it thinks it's more effective..
 
@@ -64,4 +64,5 @@
   *
   * @author Lee Sandberg email: lee@abcmedia.se
+  * Updated on 4/8/2005 by Tuan Kuranes email: tuan.kuranes@free.fr
   */
 
@@ -91,20 +92,20 @@
 	unsigned int getLastQuerysPixelcount() { return mPixelCount; }
 
-	// This functions are optional, it's a simple filter that simply skipps some hardware occlusion tests on visable objects only
-	// It's easy to use if you don't have to keep track on which objects are visable (can be skipped) and what objects arn't visable..
-	// (None visable objects and object you introduce for the first time have allways to be tested allthough the cheepest possible 
-	// LOD (Level Of Detail) mesh and material wize). 
+	// This functions are optional, it's a simple filter that simply skips some hardware occlusion tests on visible objects only
+	// It's easy to use if you don't have to keep track on which objects are visible (can be skipped) and what objects arn't visible..
+	// (None visible objects and object you introduce for the first time have always to be tested although the cheapest possible 
+	// LOD (Level Of Detail) mesh and material wise). 
 
 	/**
 	  *   
 	  * Remarks This function allows you to set how often the hardware occlusion really sent to the driver
-	  * if you set it to 0 every hw occlusion test is acctually made. If you set it to 2 only 50% of your queries are sent. 
-	  * for all visable objects. 3 will result in 33% of all queries to acctualy be sent and so on. 
-	  * New and none visable objects will be tested all the time.
-	  * This functionality is here because this class can keep track on visable and none visable objects for you.
+	  * if you set it to 0 every hardware occlusion test is actually made. If you set it to 2 only 50% of your queries are sent. 
+	  * for all visible objects. 3 will result in 33% of all queries to actually be sent and so on. 
+	  * New and none visible objects will be tested all the time.
+	  * This functionality is here because this class can keep track on visible and none visible objects for you.
 	  * Once you you set the SkipRate for any hardware occlusion instance it effects all others.
 	  */
 
-	void setSkipRate( int skip ) { mSkipInterval = skip; }		// Using 2 only 50 % of the tests are actully made and 3 results in only 33% of the tests. So on.
+	void setSkipRate( int skip ) { mSkipInterval = skip; }		// Using 2 only 50 % of the tests are actually made and 3 results in only 33% of the tests. So on.
 	int	 getSkipRate() { return mSkipInterval; } 
 
Index: OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp	(revision 193)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp	(revision 657)
@@ -69,5 +69,12 @@
         // have to call this here reather than in Resource destructor
         // since calling virtual methods in base destructors causes crash
+		if (mIsLoaded)
+		{
         unload(); 
+    }
+		else
+		{
+			freeInternalResources();
+		}
     }
 
@@ -90,5 +97,5 @@
 
 	//* Creation / loading methods ********************************************
-	void GLTexture::createInternalResources(void)
+	void GLTexture::createInternalResourcesImpl(void)
     {
 		// Adjust requested parameters to capabilities
@@ -131,4 +138,5 @@
 		// Zero means create mip levels until 1x1
 		size_t maxMips = GLPixelUtil::getMaxMipmaps(mWidth, mHeight, mDepth, mFormat);
+		mNumMipmaps = mNumRequestedMipmaps;
 		if(mNumMipmaps>maxMips)
 			mNumMipmaps = maxMips;
@@ -144,7 +152,8 @@
 		
 		// If we can do automip generation and the user desires this, do so
+		mMipmapsHardwareGenerated = 
+			Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_AUTOMIPMAP);
 		if((mUsage & TU_AUTOMIPMAP) &&
-		    mNumMipmaps &&
-		 	Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_AUTOMIPMAP))
+		    mNumRequestedMipmaps && mMipmapsHardwareGenerated)
         {
             glTexParameteri( getGLTextureTarget(), GL_GENERATE_MIPMAP, GL_TRUE );
@@ -242,5 +251,4 @@
 		// Get final internal format
 		mFormat = getBuffer(0,0)->getFormat();
-		mIsLoaded = true;
 	}
 	
@@ -326,5 +334,5 @@
 	//*************************************************************************
     
-    void GLTexture::unloadImpl()
+    void GLTexture::freeInternalResourcesImpl()
     {
 		mSurfaceList.clear();
@@ -337,17 +345,11 @@
 	{
 		mSurfaceList.clear();
-		// Make our understanding of the number of mips matches the GL one
-		glBindTexture( getGLTextureTarget(), mTextureID );
-		GLint value;
-		glGetTexParameteriv( getGLTextureTarget(), GL_TEXTURE_MAX_LEVEL, &value );
-		mNumMipmaps = value;
 		
 		// For all faces and mipmaps, store surfaces as HardwarePixelBufferSharedPtr
 		bool wantGeneratedMips = (mUsage & TU_AUTOMIPMAP)!=0;
-		bool canMip = Root::getSingleton().getRenderSystem()->getCapabilities()->hasCapability(RSC_AUTOMIPMAP);
 		
 		// Do mipmapping in software? (uses GLU) For some cards, this is still needed. Of course,
 		// only when mipmap generation is desired.
-		bool doSoftware = wantGeneratedMips && !canMip && getNumMipmaps(); 
+		bool doSoftware = wantGeneratedMips && !mMipmapsHardwareGenerated && getNumMipmaps(); 
 		
 		for(int face=0; face<getNumFaces(); face++)
Index: OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 343)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 657)
@@ -38,4 +38,5 @@
   * Updated on 12/7/2004 by Chris McGuirk
   * - Implemented ARB_occlusion_query
+  * Updated on 4/8/2005 by Tuan Kuranes email: tuan.kuranes@free.fr
   */
 
@@ -154,2 +155,4 @@
 #endif // GTP_VIBILITY_MODIFIED_OGRE
 }
+
+
Index: OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp	(revision 350)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreGLRenderSystem.cpp	(revision 657)
@@ -257,5 +257,4 @@
             delete mTextureManager;
 
-        delete mCapabilities;
         delete mGLSupport;
     }
@@ -894,4 +893,5 @@
             case Light::LT_SPOTLIGHT:
                 glLightf( gl_index, GL_SPOT_CUTOFF, 0.5f * lt->getSpotlightOuterAngle().valueDegrees() );
+                glLightf(gl_index, GL_SPOT_EXPONENT, lt->getSpotlightFalloff());
                 break;
             default:
@@ -1090,5 +1090,6 @@
 			if(!tex.isNull())
 				glBindTexture( mTextureTypes[stage], tex->getGLID() );
-
+			else
+				glBindTexture( mTextureTypes[stage], static_cast<GLTextureManager*>(mTextureManager)->getWarningTextureID() );
         }
         else
@@ -1238,8 +1239,5 @@
 
             // Set scale and translation matrix for projective textures
-            projectionBias = Matrix4::ZERO;
-            projectionBias[0][0] = 0.5; projectionBias[1][1] = -0.5; 
-            projectionBias[2][2] = 1.0; projectionBias[0][3] = 0.5; 
-            projectionBias[1][3] = 0.5; projectionBias[3][3] = 1.0;
+            projectionBias = Matrix4::CLIPSPACE2DTOIMAGESPACE;
 
             projectionBias = projectionBias * frustum->getProjectionMatrix();
@@ -1260,4 +1258,5 @@
         switch(tam)
         {
+        default:
         case TextureUnitState::TAM_WRAP:
             type = GL_REPEAT;
@@ -1295,4 +1294,6 @@
 			mat[12] = mat[8];
 			mat[13] = mat[9];
+            mat[8] = 0;
+            mat[9] = 0;
 		}
 //        mat[14] = mat[10];
@@ -1312,16 +1313,11 @@
         glMatrixMode(GL_TEXTURE);
 
+        // Load this matrix in
+        glLoadMatrixf(mat);
+
         if (mUseAutoTextureMatrix)
         {
-            // Load auto matrix in
-            glLoadMatrixf(mAutoTextureMatrix);
-            // Concat new matrix
-            glMultMatrixf(mat);
-
-        }
-        else
-        {
-            // Just load this matrix
-            glLoadMatrixf(mat);
+            // Concat auto matrix
+            glMultMatrixf(mAutoTextureMatrix);
         }
 
@@ -1403,6 +1399,10 @@
               h = vp->getActualHeight();
               x = vp->getActualLeft();
-              y = target->getHeight() - vp->getActualTop() - h;
-  
+              y = vp->getActualTop();
+              if (!target->requiresTextureFlipping())
+              {
+                  // Convert "upper-left" corner to "lower-left"
+                  y = target->getHeight() - h - y;
+              }
               glViewport(x, y, w, h);
   
@@ -2344,5 +2344,5 @@
             glDisableVertexAttribArrayARB_ptr(1); // disable weights
         }
-
+        glColor4f(1,1,1,1);
         glSecondaryColor3fEXT_ptr(0.0f, 0.0f, 0.0f);
 
@@ -2415,5 +2415,5 @@
             const Plane& plane = clipPlanes[i];
 
-            if (i >= GL_MAX_CLIP_PLANES)
+            if (i >= 6/*GL_MAX_CLIP_PLANES*/)
             {
                 OGRE_EXCEPT(0, "Unable to set clip plane", 
@@ -2424,5 +2424,5 @@
             clipPlane[1] = plane.normal.y;
             clipPlane[2] = plane.normal.z;
-            clipPlane[3] = -plane.d;
+            clipPlane[3] = plane.d;
 
             glClipPlane(clipPlaneId, clipPlane);
@@ -2440,4 +2440,6 @@
         size_t top, size_t right, size_t bottom)
     {
+        // If request texture flipping, use "upper-left", otherwise use "lower-left"
+        bool flipping = mActiveRenderTarget->requiresTextureFlipping();
         //  GL measures from the bottom, not the top
         size_t targetHeight = mActiveRenderTarget->getHeight();
@@ -2450,4 +2452,7 @@
             // NB GL uses width / height rather than right / bottom
             x = left;
+            if (flipping)
+                y = top;
+            else
             y = targetHeight - bottom;
             w = right - left;
@@ -2462,4 +2467,7 @@
             h = mActiveViewport->getActualHeight();
             x = mActiveViewport->getActualLeft();
+            if (flipping)
+                y = mActiveViewport->getActualTop();
+            else
             y = targetHeight - mActiveViewport->getActualTop() - h;
             glScissor(x, y, w, h);
@@ -2575,5 +2583,7 @@
     HardwareOcclusionQuery* GLRenderSystem::createHardwareOcclusionQuery(void)
     {
-        return new GLHardwareOcclusionQuery(); 
+        GLHardwareOcclusionQuery* ret = new GLHardwareOcclusionQuery(); 
+		mHwOcclusionQueries.push_back(ret);
+		return ret;
     }
     //---------------------------------------------------------------------
@@ -2639,4 +2649,40 @@
     }
     //---------------------------------------------------------------------
+    void GLRenderSystem::_switchContext(GLContext *context)
+    {
+        // Unbind GPU programs and rebind to new context later, because
+        // scene manager treat render system as ONE 'context' ONLY, and it
+        // cached the GPU programs using state.
+        if (mCurrentVertexProgram)
+            mCurrentVertexProgram->unbindProgram();
+        if (mCurrentFragmentProgram)
+            mCurrentFragmentProgram->unbindProgram();
+
+        // It's ready to switching
+            mCurrentContext->endCurrent();
+        mCurrentContext = context;
+        mCurrentContext->setCurrent();
+        
+            // Check if the context has already done one-time initialisation
+        if(!mCurrentContext->getInitialized()) 
+        {
+               _oneTimeContextInitialization();
+               mCurrentContext->setInitialized();
+            }
+
+        // Rebind GPU programs to new context
+        if (mCurrentVertexProgram)
+            mCurrentVertexProgram->bindProgram();
+        if (mCurrentFragmentProgram)
+            mCurrentFragmentProgram->bindProgram();
+
+        // Must reset depth/colour write mask to according with user desired, otherwise,
+        // clearFrameBuffer would be wrong because the value we are recorded may be
+        // difference with the really state stored in GL context.
+        glDepthMask(mDepthWrite);
+        glColorMask(mColourWrite[0], mColourWrite[1], mColourWrite[2], mColourWrite[3]);
+
+    }
+    //---------------------------------------------------------------------
     void GLRenderSystem::_setRenderTarget(RenderTarget *target)
     {
@@ -2645,12 +2691,5 @@
         ContextMap::iterator i = mContextMap.find(target);
         if(i != mContextMap.end() && mCurrentContext != i->second) {
-            mCurrentContext->endCurrent();
-            mCurrentContext = i->second;
-            // Check if the context has already done one-time initialisation
-            if(!mCurrentContext->getInitialized()) {
-               _oneTimeContextInitialization();
-               mCurrentContext->setInitialized();
-            }
-            mCurrentContext->setCurrent();
+            _switchContext(i->second);
         }
     }
@@ -2669,7 +2708,5 @@
             // we set the main context to 0.
             if(mCurrentContext != mMainContext) {
-                mCurrentContext->endCurrent();
-                mCurrentContext = mMainContext;
-                mCurrentContext->setCurrent();
+                _switchContext(mMainContext);
             } else {
                 mMainContext = 0;
Index: OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp	(revision 193)
+++ OGRE/trunk/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp	(revision 657)
@@ -226,4 +226,7 @@
 		    }
 
+		HDC old_hdc = wglGetCurrentDC();
+		HGLRC old_context = wglGetCurrentContext();
+
 			RECT rc;
 		// top and left represent outer window position
@@ -255,4 +258,15 @@
 		
 		wglSwapIntervalEXT(vsync? 1 : 0);
+
+        if (old_context)
+        {
+            // Restore old context
+		    if (!wglMakeCurrent(old_hdc, old_context))
+			    OGRE_EXCEPT(0, "wglMakeCurrent() failed", "Win32Window::create");
+
+            // Share lists with old context
+		    if (!wglShareLists(old_context, mGlrc))
+			    OGRE_EXCEPT(0, "wglShareLists() failed", " Win32Window::create");
+        }
 
         // Create RenderSystem context
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7Texture.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7Texture.h	(revision 154)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7Texture.h	(revision 657)
@@ -53,7 +53,4 @@
         LPDIRECTDRAWSURFACE7 getDDSurface(void);
 
-        /// @copydoc Texture::createInternalResources
-        void createInternalResources(void);
-
 		/// @copydoc Texture::getBuffer
 		HardwarePixelBufferSharedPtr getBuffer(size_t face, size_t mipmap);
@@ -63,4 +60,8 @@
 		static PixelFormat closestD3DXFormat( PixelFormat format );
 		static bool OgreFormat_to_DDPixelFormat( PixelFormat format, DDPIXELFORMAT & out );
+
+		/// Restore this texture from a lost device
+		void restoreFromLostDevice(void);
+
     protected:
         IDirect3DDevice7 * mD3DDevice;       ///< A pointer to the Direct3D device.
@@ -71,10 +72,15 @@
 		typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
 		SurfaceList						mSurfaceList;
+		/// Are we restoring from a lost device?
+		bool mRestoring;
+
 	
 
         /// @copydoc Resource::loadImpl
         void loadImpl(void);
-        /// @copydoc Resource::unloadImpl
-        void unloadImpl(void);
+		/// @copydoc Texture::createInternalResourcesImpl
+		void createInternalResourcesImpl(void);
+        /// @copydoc Resource::freeInternalResourcesImpl
+        void freeInternalResourcesImpl(void);
 		
 		void createSurface2D(void);
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7RenderWindow.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7RenderWindow.h	(revision 153)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/include/OgreD3D7RenderWindow.h	(revision 657)
@@ -119,5 +119,9 @@
         */
         void writeContentsToFile(const String& filename);
-
+	// DirectDraw Methods
+		void createDDSurfaces(void);
+		void releaseDDSurfaces(void);
+		void restoreDDSurfaces(void);
+		void createDepthBuffer(void);
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
 		uchar *getBufferContents(int &dimx, int &dimy);
@@ -159,9 +163,4 @@
         LPDIRECT3DDEVICE7 mlpD3DDevice;
 
-        // DirectDraw Methods
-        void createDDSurfaces(void);
-        void releaseDDSurfaces(void);
-        void restoreDDSurfaces(void);
-        void createDepthBuffer(void);
 
         // Method for dealing with resize / move & 3d library
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/src/OgreD3D7RenderWindow.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/src/OgreD3D7RenderWindow.cpp	(revision 153)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D7/src/OgreD3D7RenderWindow.cpp	(revision 657)
@@ -96,11 +96,4 @@
                 break;
 
-            case WM_MOVE:
-                // Move messages need to be tracked to update the screen rects
-                // used for blitting the backbuffer to the primary.
-                if(win->mActive && win->mReady)
-                    win->windowMovedOrResized();
-                break;
-
             case WM_ENTERSIZEMOVE:
                 // Prevent rendering while moving / sizing
@@ -113,4 +106,5 @@
                 break;
 
+            case WM_MOVE:
             case WM_SIZE:
                 // Check to see if we are losing or gaining our window. Set the
@@ -391,4 +385,7 @@
     void D3D7RenderWindow::swapBuffers(bool waitForVSync)
     {
+		if (!mlpDDSFront)
+			return;
+
         HRESULT hr;
         DWORD flags;
@@ -423,5 +420,5 @@
             {
                 // Restore surfaces
-                restoreDDSurfaces();
+                //restoreDDSurfaces();
             }
             else if (FAILED(hr))
@@ -539,4 +536,6 @@
         ddscaps.dwCaps = DDSCAPS_ZBUFFER;
 
+		if (!mlpDDSBack->IsLost())
+		{
         LPDIRECTDRAWSURFACE7 zBufSurface;
 
@@ -546,4 +545,5 @@
         zBufSurface->Release();
         zBufSurface->Release();
+		}
 
         // Release std buffers
@@ -563,9 +563,25 @@
 
             if( FAILED( hr ) )
+			{
+				if (hr == DDERR_WRONGMODE)
+				{
+					// Fullscreen exclusive mode problem
+					// Need to release & recreate
+					releaseDDSurfaces();
+					createDDSurfaces();
+					createDepthBuffer();
+					return;
+				}
+				else
+				{
+					char szBuffer[512];
+					D3DXGetErrorString( hr, 512, szBuffer );
                 OGRE_EXCEPT( 
                     Exception::ERR_INTERNAL_ERROR, 
-                    "Error restoring lost primary surface.", 
+						"Error restoring lost primary surface." + String(szBuffer), 
                     "D3D7RenderWindow - restoreDDSurfaces" );
         }
+			}
+        }
 
         if( mlpDDSBack->IsLost() )
@@ -574,9 +590,13 @@
 
             if( FAILED( hr ) )
+			{
+				char szBuffer[512];
+				D3DXGetErrorString( hr, 512, szBuffer );
                 OGRE_EXCEPT( 
                     Exception::ERR_INTERNAL_ERROR, 
-                    "Error restoring lost back buffer surface.", 
+                    "Error restoring lost back buffer surface." + String(szBuffer), 
                     "D3D7RenderWindow - restoreDDSurfaces" );
         }
+    }
     }
 
@@ -592,4 +612,10 @@
         ClientToScreen( mHWnd, (POINT*)&rcCheck.left );
         ClientToScreen( mHWnd, (POINT*)&rcCheck.right );
+
+		if ((rcCheck.right - rcCheck.left) == 0 ||
+			(rcCheck.bottom - rcCheck.top) == 0)
+		{
+			return;
+		}
 
         // Has the window resized? If so, we need to recreate surfaces
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9Texture.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9Texture.h	(revision 193)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9Texture.h	(revision 657)
@@ -68,6 +68,4 @@
 		/// device capabilities pointer
 		D3DCAPS9						mDevCaps;
-        // Auto-generated mipmaps?
-        bool                            mAutoGenMipmaps;
 		// Dynamic textures?
 		bool                            mDynamicTextures;
@@ -95,6 +93,8 @@
 		D3DFORMAT _chooseD3DFormat();
 
-		/// internal method, free D3D9 resources
-		void _freeResources();
+		/// @copydoc Texture::createInternalResourcesImpl
+		void createInternalResourcesImpl(void);
+		/// free internal resources
+		void freeInternalResourcesImpl(void);
 		/// internal method, set Texture class source image protected attributes
 		void _setSrcAttributes(unsigned long width, unsigned long height, unsigned long depth, PixelFormat format);
@@ -116,10 +116,8 @@
 		/// internal method, create D3D9HardwarePixelBuffers for every face and
 		/// mipmap level. This method must be called after the D3D texture object was created
-		void _createSurfaceList(bool updateOldList=false);
+		void _createSurfaceList(void);
 
         /// overriden from Resource
         void loadImpl();
-        /// overriden from Resource
-        void unloadImpl();
 	public:
 		/// constructor 
@@ -135,6 +133,4 @@
 		void loadImage( const Image &img );
 
-        /// @copydoc Texture::createInternalResources
-        void createInternalResources(void);
 
 		/// @copydoc Texture::getBuffer
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h	(revision 115)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/include/OgreD3D9HardwareOcclusionQuery.h	(revision 657)
@@ -34,7 +34,7 @@
 
 // If you use multiple rendering passes you can test only the first pass and all other passes don't have to be rendered 
-	// if the first pass resultet has too few pixels visible.
+	// if the first pass results has too few pixels visible.
 
-// Be sure to render all occlluder first and whats out so the RenderQue don't switch places on 
+	// Be sure to render all occluder first and whats out so the RenderQue don't switch places on 
 // the occluding objects and the tested objects because it thinks it's more effective..
 
@@ -46,4 +46,5 @@
 	*
 	* Updated on 12/7/2004 by Chris McGuirk
+	* Updated on 4/8/2005 by Tuan Kuranes email: tuan.kuranes@free.fr
   */
 class D3D9HardwareOcclusionQuery : public HardwareOcclusionQuery
@@ -73,4 +74,5 @@
 		bool pullOcclusionQuery( unsigned int* NumOfFragments, const HW_OCCLUSIONQUERY flag = HWOCCLUSIONQUERY_FLUSH);
 		unsigned int getLastQuerysPixelcount() { return mPixelCount; }
+        bool isStillOutstanding(void);
 
 		// These functions are optional, it's a simple filter that simply skips some hardware occlusion tests on visible objects only
@@ -82,11 +84,11 @@
 	  *   
 		* Remarks This function allows you to set how often the hardware occlusion query is sent to the driver
-	  * if you set it to 0 every hw occlusion test is acctually made. If you set it to 1 only the half of your queries are sent 
-		* 2 will result in 25% of all queries to acctualy be sent. 
+		* if you set it to 0 every hardware occlusion test is actually made. If you set it to 1 only the half of your queries are sent 
+		* 2 will result in 25% of all queries to factually be sent. 
 		* This functionality is here because this class can keep track on visible and none visible objects for you.
 	  * Once you you set the SkipRate for any hardware occlusion instance it effects all others.
 	  */
 
-		void setSkipRate( int skip ) { mSkipInterval = skip; }			// Using 2 only 50 % of the tests are actully made and 3 results in only 33% of the tests. So on.
+		void setSkipRate( int skip ) { mSkipInterval = skip; }			// Using 2 only 50 % of the tests are actually made and 3 results in only 33% of the tests. So on.
 		int	 getSkipRate() { return mSkipInterval; } 
 
@@ -105,4 +107,5 @@
 		int					mSkipInterval;
 		bool				mHasOcclusionSupport;
+		bool				mIsQueryResultStillOutstanding;
 };
 
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 193)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9HardwareOcclusionQuery.cpp	(revision 657)
@@ -113,5 +113,5 @@
 	// This version of pullOcclusionQuery causes the DX9 API/Driver to not flush all commands to the 3D card
 // to allow a fast result from the query, but the batching of API calls to the card will be normal. 
-	// But the query wont be processed until the card recives the query in the next batch.
+	// But the query wont be processed until the card receives the query in the next batch.
 // Note: OpenGL dosn't use this flag at all so the application running OpenGL won't display any different behaviour.
 //--
Index: OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9RenderWindow.cpp
===================================================================
--- OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9RenderWindow.cpp	(revision 193)
+++ OGRE/trunk/ogre_changes/RenderSystems/Direct3D9/src/OgreD3D9RenderWindow.cpp	(revision 657)
@@ -110,11 +110,5 @@
 	D3D9RenderWindow::~D3D9RenderWindow()
 	{
-		// access and update device through driver, realse only primary
-		if (!mIsSwapChain) 
-		{
-			LPDIRECT3DDEVICE9 mpD3DDevice = mDriver->getD3DDevice();
-			SAFE_RELEASE( mpD3DDevice );
-			mDriver->setD3DDevice( NULL );
-		}
+		destroyD3DResources();
 	}
 
@@ -342,7 +336,4 @@
 		md3dpp.BackBufferHeight			= mHeight;
 
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	//md3dpp.Flags |= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
-#endif
 		if (mVSync)
 			md3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
@@ -493,7 +484,4 @@
 			// ignore depth buffer, access device through driver
 			mpRenderZBuffer = 0;
-			LPDIRECT3DDEVICE9 mpD3DDevice = mDriver->getD3DDevice();
-			SAFE_RELEASE(mpD3DDevice);
-			mDriver->setD3DDevice(NULL);
 		}
 	}
@@ -727,7 +715,15 @@
 		}
 
-		if (!mIsFullScreen)
-		{
-			POINT pt={0, 0};
+		D3DLOCKED_RECT lockedRect;
+		if(mIsFullScreen)
+		{
+			if (FAILED(hr = pTempSurf->LockRect(&lockedRect, NULL, 
+			D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK)))
+			{
+				OGRE_EXCEPT(hr, "can't lock rect!", "D3D9RenderWindow::writeContentsToFile");
+			} 
+		}
+		else
+		{
 			RECT srcRect;
 			GetWindowRect(mHWnd, &srcRect);
@@ -735,40 +731,12 @@
 			desc.Width = srcRect.right - srcRect.left;
 			desc.Height = srcRect.bottom - srcRect.top;
-			desc.Format = D3DFMT_A8R8G8B8;         // this is what we get from the screen, so stick with it
-
-			// NB we can't lock the back buffer direct because it's no created that way
-			// and to do so hits performance, so copy to another surface
-			// Must be the same format as the source surface
-			if (FAILED(hr = mpD3DDevice->CreateOffscreenPlainSurface(
-							desc.Width, 
-							desc.Height, 
-							desc.Format, 
-							D3DPOOL_DEFAULT, 
-							&pSurf,
-							NULL)))
-			{
-				SAFE_RELEASE(pSurf);
-				OGRE_EXCEPT(hr, "Cannot create offscreen buffer 2!", "D3D9RenderWindow::writeContentsToFile");
-			}
-
-			// Copy
-			if (FAILED(hr = mpD3DDevice->UpdateSurface(pTempSurf, &srcRect, pSurf, &pt)))
-			{
-				SAFE_RELEASE(pTempSurf);
-				SAFE_RELEASE(pSurf);
-				OGRE_EXCEPT(hr, "Cannot update surface!", "D3D9RenderWindow::writeContentsToFile");
-			}
-
-			SAFE_RELEASE(pTempSurf);
-			pTempSurf = pSurf;
-			pSurf = NULL;
-		}
-
-		D3DLOCKED_RECT lockedRect;
-		if (FAILED(hr = pTempSurf->LockRect(&lockedRect, NULL, 
+
+			if (FAILED(hr = pTempSurf->LockRect(&lockedRect, &srcRect, 
+
 			D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK)))
 		{
 			OGRE_EXCEPT(hr, "can't lock rect!", "D3D9RenderWindow::writeContentsToFile");
 		} 
+		}
 
         ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
