/*
* VariableContentManager.cs
* Authors: August Zinsser
* Copyright (c) 2007-2009 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Util
{
///
/// This class is a simple extension of the XNA ContentManager class. It examines the available graphics hardware
/// and attempts to load smaller textures (if available) when a Load is called on a texture that is larger than the
/// capability of the hardware.
///
public class VariableContentManager : ContentManager
{
#region Fields
private static int maxTextureWidth;
private static int maxTextureHeight;
private Texture2D pixel;
#endregion
#region Properties
///
/// The maximum texture width allowed
///
public int MaxTextureWidth { get { return maxTextureWidth; } }
///
/// The maximum texture height allowed
///
public int MaxTextureHeight { get { return maxTextureHeight; } }
#endregion
#region Constructor
public VariableContentManager(IServiceProvider serviceProvider, string rootDirectory)
: base(serviceProvider, rootDirectory)
{ }
#endregion
#region Methods
///
/// Sets the maximum width and height based on the GraphicsDevice's capabilities
///
///
public static void SetMaxResolutionToHardwareCapabilities(GraphicsDevice graphicsDevice)
{
maxTextureWidth = graphicsDevice.GraphicsDeviceCapabilities.MaxTextureWidth;
maxTextureHeight = graphicsDevice.GraphicsDeviceCapabilities.MaxTextureHeight;
}
///
/// Try to load a texture, and if an exception is thrown return a basic blank texture
///
public override T Load(string assetName)
{
try
{
return base.Load(assetName);
}
catch (Exception e)
{
MainGame.LogError("Exception thrown from VariableContentManager with maxResolution(" + maxTextureWidth
+ "," + maxTextureWidth + ") when trying to load texture " + assetName + ".\n" + e.ToString());
return base.Load("blank");
}
}
#endregion
}
}