//-----------------------------------------------------------------------
	void ResourceManager::remove(const String& name)
	{
		ResourcePtr res = getResourceByName(name);

		if (!res.isNull())
		{
			removeImpl(res);
		}
	}
	//-----------------------------------------------------------------------
	void ResourceManager::remove(ResourceHandle handle)
	{
		ResourcePtr res = getByHandle(handle);

		if (!res.isNull())
		{
			removeImpl(res);
		}
	}
//-----------------------------------------------------------------------------
void ProgramManager::destroyGpuProgram(GpuProgramPtr& gpuProgram)
{		
	const String& programName = gpuProgram->getName();
	ResourcePtr res			  = HighLevelGpuProgramManager::getSingleton().getByName(programName);	

	if (res.isNull() == false)
	{		
		HighLevelGpuProgramManager::getSingleton().remove(programName);
	}
}
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::getResourceByName(const String& name, const String& groupName /* = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME */)
    {		
		ResourcePtr res;

		// if not in the global pool - get it from the grouped pool 
		if(!ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(groupName))
		{
                    OGRE_LOCK_AUTO_MUTEX;
			ResourceWithGroupMap::iterator itGroup = mResourcesWithGroup.find(groupName);

			if( itGroup != mResourcesWithGroup.end())
			{
				ResourceMap::iterator it = itGroup->second.find(name);

				if( it != itGroup->second.end())
				{
					res = it->second;
				}
			}
		}

		// if didn't find it the grouped pool - get it from the global pool 
		if (res.isNull())
		{
                    OGRE_LOCK_AUTO_MUTEX;

			ResourceMap::iterator it = mResources.find(name);

			if( it != mResources.end())
			{
				res = it->second;
			}
			else
			{
				// this is the case when we need to search also in the grouped hash
				if (groupName == ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME)
				{
					ResourceWithGroupMap::iterator iter = mResourcesWithGroup.begin();
					ResourceWithGroupMap::iterator iterE = mResourcesWithGroup.end();
					for ( ; iter != iterE ; ++iter )
					{
						ResourceMap::iterator resMapIt = iter->second.find(name);

						if( resMapIt != iter->second.end())
						{
							res = resMapIt->second;
							break;
						}
					}
				}
			}
		}
	
		return res;
    }
 //---------------------------------------------------------------------------
 ResourcePtr GpuProgramManager::getByName(const String& name, bool preferHighLevelPrograms)
 {
     ResourcePtr ret;
     if (preferHighLevelPrograms)
     {
         ret = HighLevelGpuProgramManager::getSingleton().getByName(name);
         if (!ret.isNull())
             return ret;
     }
     return ResourceManager::getByName(name);
 }
	//-----------------------------------------------------------------------
	void ResourceManager::unload(ResourceHandle handle)
	{
		ResourcePtr res = getByHandle(handle);

		if (!res.isNull())
		{
			// Unload resource
			res->unload();

		}
	}
	//-----------------------------------------------------------------------
	void ResourceManager::unload(const String& name)
	{
		ResourcePtr res = getResourceByName(name);

		if (!res.isNull())
		{
			// Unload resource
			res->unload();

		}
	}
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::load(const String& name, 
        const String& group, bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* loadParams)
    {
        ResourcePtr ret = getByName(name);
        if (ret.isNull())
        {
            ret = create(name, group, isManual, loader, loadParams);
        }
		// ensure loaded
        ret->load();
        return ret;
    }
    void ContentModule::precreateMeshes() const
    {
        StringVectorPtr meshes = ResourceGroupManager::getSingleton()
            .findResourceNames(getId(), "*.mesh");

        for (size_t i = 0; i < meshes->size(); ++i)
        {
            ResourcePtr res = MeshManager::getSingleton().getByName((*meshes)[i]);
            if (res.isNull())
            {
                MeshPtr mesh = MeshManager::getSingleton().create((*meshes)[i],
                    getId());
            }
        }
    }
ResourcePtr DialogSubsystem::getXmlResource(const Ogre::String& filename)
{
    ResourcePtr res = XmlResourceManager::getSingleton().getByName(filename);

    if (res.isNull())
    {
        Ogre::String group = ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
        if (ResourceGroupManager::getSingleton().resourceExists(
			CoreSubsystem::getSingleton().getActiveAdventureModule()->getId(), filename))
        {
            group = CoreSubsystem::getSingleton().getActiveAdventureModule()->getId();
        }
        res = XmlResourceManager::getSingleton().create(filename, group);

    }
    return res;
}
    //-----------------------------------------------------------------------
    ResourceManager::ResourceCreateOrRetrieveResult 
	ResourceManager::createOrRetrieve(
		const String& name, const String& group, 
		bool isManual, ManualResourceLoader* loader, 
		const NameValuePairList* params)
	{
		// Lock for the whole get / insert
            OGRE_LOCK_AUTO_MUTEX;

		ResourcePtr res = getResourceByName(name, group);
		bool created = false;
		if (res.isNull())
		{
			created = true;
			res = createResource(name, group, isManual, loader, params);
		}

		return ResourceCreateOrRetrieveResult(res, created);
	}
//  Setup render 2 texture (road maps)
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
void App::Rnd2TexSetup()
{
	///  RT:  0 road minimap,  1 road for grass,  2 terrain minimap,  3 track preview full
	const uint32 visMask[RTs] =
		{ RV_Road, RV_Road+RV_Objects, RV_Terrain+RV_Objects, RV_MaskAll-RV_Hud };
	const int dim[RTs] =  //1025: sc->td.iVertsX
		{ 1024, 1025, 1024, 1024 };
		
	asp = float(mWindow->getWidth())/float(mWindow->getHeight());
	Real sz = pSet->size_minimap;
	xm1 = 1-sz/asp, ym1 = -1+sz, xm2 = 1.0, ym2 = -1.0;
	AxisAlignedBox aab;  aab.setInfinite();
	
	for (int i=0; i < RTs+RTsAdd; ++i)
	{
		SRndTrg& r = rt[i];  bool full = i==3;
		String si = toStr(i), sMtr = /*i==3 ? "road_mini_add" :*/ "road_mini_"+si;
		if (i < RTs)
		{
			String sTex = "RttTex"+si, sCam = "RttCam"+si;

			TextureManager::getSingleton().remove(sTex);
			mSceneMgr->destroyCamera(sCam);  // dont destroy old - const tex sizes opt..
			
			///  rnd to tex - same dim as Hmap	// after track load
			Real fDim = scn->sc->td.fTerWorldSize;  // world dim  ..vdr
			TexturePtr texture = TextureManager::getSingleton().createManual(
				sTex, rgDef, TEX_TYPE_2D,
				dim[i], dim[i], 0, PF_R8G8B8A8, TU_RENDERTARGET);
				  
			r.cam = mSceneMgr->createCamera(sCam);  // up
			r.cam->setPosition(Vector3(0,1000,0/*-300..*/));  r.cam->setOrientation(Quaternion(0.5,-0.5,0.5,0.5));
			r.cam->setNearClipDistance(0.5);	r.cam->setFarClipDistance(50000);
			r.cam->setAspectRatio(1.0);			if (!full)  r.cam->setProjectionType(PT_ORTHOGRAPHIC);
			r.cam->setOrthoWindow(fDim,fDim);	//rt[i].rndCam->setPolygonMode(PM_WIREFRAME);

			r.tex = texture->getBuffer()->getRenderTarget();
			r.tex->setAutoUpdated(false);	r.tex->addListener(this);
			Viewport* rvp = r.tex->addViewport(r.cam);
			rvp->setClearEveryFrame(true);   rvp->setBackgroundColour(ColourValue(0,0,0,0));
			rvp->setOverlaysEnabled(false);  rvp->setSkiesEnabled(full);
			rvp->setVisibilityMask(visMask[i]);
			rvp->setShadowsEnabled(false);

			if (i != 3)  rvp->setMaterialScheme("reflection");
		}
		///  minimap  . . . . . . . . . . . . . . . . . . . . . . . . . . . 
		if (r.ndMini)  mSceneMgr->destroySceneNode(r.ndMini);
		ResourcePtr mt = MaterialManager::getSingleton().getByName(sMtr);
		if (!mt.isNull())  mt->reload();

		r.mini = new Rectangle2D(true);  // screen rect preview
		if (i == RTs)  r.mini->setCorners(-1/asp, 1, 1/asp, -1);  // fullscr,square
		else  r.mini->setCorners(xm1, ym1, xm2, ym2);  //+i*sz*all

		r.mini->setBoundingBox(aab);
		r.ndMini = mSceneMgr->getRootSceneNode()->createChildSceneNode("Minimap"+si);
		r.ndMini->attachObject(r.mini);	r.mini->setCastShadows(false);
		r.mini->setMaterial(i == RTs+1 ? "BrushPrvMtr" : sMtr);
		r.mini->setRenderQueueGroup(RQG_Hud2);
		r.mini->setVisibilityFlags(i == RTs ? RV_MaskPrvCam : RV_Hud);
	}

	//  pos dot on minimap  . . . . . . . .
	if (!ndPos)
	{	mpos = Create2D("hud/CarPos", 0.2f, true);  // dot size
		mpos->setVisibilityFlags(RV_Hud);
		mpos->setRenderQueueGroup(RQG_Hud3 /*RENDER_QUEUE_OVERLAY+1*/);
		ndPos = mSceneMgr->getRootSceneNode()->createChildSceneNode(
			Vector3(xm1+(xm2-xm1)/2, ym1+(ym2-ym1)/2, 0));
		float fHudSize = 0.04f;
		ndPos->scale(fHudSize, fHudSize, 1);
		ndPos->attachObject(mpos);
	}
	if (ndPos)   ndPos->setVisible(pSet->trackmap);
	UpdMiniVis();
}