Expanded TextureMapper
This commit is contained in:
parent
7b5b8ee6e4
commit
f66ab8a2be
@ -34,7 +34,7 @@ namespace TrueCraft.Client.Rendering
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public TexturePack TexturePack { get; private set; }
|
||||
private GraphicsDevice Device { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@ -50,27 +50,42 @@ namespace TrueCraft.Client.Rendering
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice"></param>
|
||||
/// <param name="texturePack"></param>
|
||||
public TextureMapper(GraphicsDevice graphicsDevice, TexturePack texturePack = null)
|
||||
public TextureMapper(GraphicsDevice graphicsDevice)
|
||||
{
|
||||
if (graphicsDevice == null)
|
||||
throw new ArgumentException();
|
||||
|
||||
TexturePack = texturePack;
|
||||
Device = graphicsDevice;
|
||||
Customs = new Dictionary<string, Texture2D>();
|
||||
IsDisposed = false;
|
||||
|
||||
if (TexturePack != null)
|
||||
LoadTextures(graphicsDevice);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice"></param>
|
||||
private void LoadTextures(GraphicsDevice graphicsDevice)
|
||||
/// <param name="key"></param>
|
||||
/// <param name="texture"></param>
|
||||
public void AddTexture(string key, Texture2D texture)
|
||||
{
|
||||
foreach (var entry in TexturePack.Archive.Entries)
|
||||
if (string.IsNullOrEmpty(key) || (texture == null))
|
||||
throw new ArgumentException();
|
||||
|
||||
if (Customs.ContainsKey(key))
|
||||
Customs[key] = texture;
|
||||
else
|
||||
Customs.Add(key, texture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="texturePack"></param>
|
||||
public void AddTexturePack(TexturePack texturePack)
|
||||
{
|
||||
if (texturePack == null)
|
||||
throw new ArgumentException();
|
||||
|
||||
foreach (var entry in texturePack.Archive.Entries)
|
||||
{
|
||||
// Make sure to 'silence' errors loading custom texture packs;
|
||||
// they're unimportant as we can just use default textures.
|
||||
@ -78,7 +93,7 @@ namespace TrueCraft.Client.Rendering
|
||||
{
|
||||
var key = entry.FileName;
|
||||
using (var stream = entry.OpenReader())
|
||||
Customs.Add(key, Texture2D.FromStream(graphicsDevice, stream));
|
||||
AddTexture(key, Texture2D.FromStream(Device, stream));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
@ -93,6 +108,9 @@ namespace TrueCraft.Client.Rendering
|
||||
{
|
||||
Texture2D result = null;
|
||||
TryGetTexture(key, out result);
|
||||
if (result == null)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -104,23 +122,19 @@ namespace TrueCraft.Client.Rendering
|
||||
/// <returns></returns>
|
||||
public bool TryGetTexture(string key, out Texture2D texture)
|
||||
{
|
||||
// -> Try to load from external texture pack
|
||||
// -> Try to load from default texture pack
|
||||
// -> Fail gracefully
|
||||
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentException();
|
||||
|
||||
bool hasTexture = false;
|
||||
texture = null;
|
||||
if (TexturePack != null)
|
||||
{
|
||||
Texture2D customTexture = null;
|
||||
var inCustom = Customs.TryGetValue(key, out customTexture);
|
||||
texture = (inCustom) ? customTexture : null;
|
||||
hasTexture = inCustom;
|
||||
}
|
||||
|
||||
// -> Try to load from custom textures
|
||||
Texture2D customTexture = null;
|
||||
var inCustom = Customs.TryGetValue(key, out customTexture);
|
||||
texture = (inCustom) ? customTexture : null;
|
||||
hasTexture = inCustom;
|
||||
|
||||
// -> Try to load from default textures
|
||||
if (!hasTexture)
|
||||
{
|
||||
Texture2D defaultTexture = null;
|
||||
@ -129,6 +143,7 @@ namespace TrueCraft.Client.Rendering
|
||||
hasTexture = inDefault;
|
||||
}
|
||||
|
||||
// -> Fail gracefully
|
||||
return hasTexture;
|
||||
}
|
||||
|
||||
@ -145,7 +160,7 @@ namespace TrueCraft.Client.Rendering
|
||||
|
||||
Customs.Clear();
|
||||
Customs = null;
|
||||
TexturePack = null;
|
||||
Device = null;
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user