#region LGPL License
/*************************************************************************
Crazy Eddie's GUI System (http://crayzedsgui.sourceforge.net)
Copyright (C)2004 Paul D Turner (crayzed@users.sourceforge.net)
C# Port developed by Chris McGuirk (leedgitar@latenitegames.com)
Compatible with the Axiom 3D Engine (http://axiomengine.sf.net)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*************************************************************************/
#endregion LGPL License
#region Using directives
using System;
using System.IO;
using System.Text;
using Axiom.Core;
using Axiom.Graphics;
using CrayzEdsGui.Base;
#endregion
namespace CrayzEdsGui.Renderers.AxiomEngine{
/// <summary>
/// Texture class created and used by the AxiomRenderer objects.
/// </summary>
/// Last synced on: 30th Oct 2004 by Paul Turner
/// C++ Master Sync Info:
/// ogretexture.cpp v1.8
/// ogretexture.h v1.4
public class AxiomTexture : CrayzEdsGui.Base.Texture
{
#region Fields
/// <summary>
/// Reference to a texture created by Axiom.
/// </summary>
protected Axiom.Core.Texture texture;
/// <summary>
/// Specified whether this texture was linked to an existing Axiom texture,
/// or whether the texture was created by CEGui.
/// </summary>
protected bool isLinked;
#endregion Fields
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="owner">The renderer who created this texture.</param>
public AxiomTexture(Renderer owner) : base(owner) {
isLinked = false;
}
#endregion Constructor
#region Texture Implementation
#region Methods
public override void LoadFromFile(string fileName) {
// free up an existing texture
FreeAxiomTexture();
try {
texture = TextureManager.Instance.Load(fileName, TextureType.TwoD, 1, 1.0f, 1);
}
catch (Exception ex) {
// TODO: Throw a RendererException
throw new Exception("");
}
if (texture != null) {
// cache the texture size info
width = texture.Width;
height = texture.Height;
}
else {
// TODO: Throw new RendererException
}
}
/// <summary>
/// Loads a texture from an existing source in memory.
/// </summary>
/// <param name="buffer"></param>
/// <param name="bufferWidth"></param>
/// <param name="bufferHeight"></param>
public override void LoadFromMemory(Stream buffer, int bufferWidth, int bufferHeight) {
Axiom.Media.Image image = Axiom.Media.Image.FromRawStream(buffer, bufferWidth, bufferHeight, Axiom.Media.PixelFormat.A8R8G8B8);
string uniqueName = string.Format("CEGUI_texture_{0}", Guid.NewGuid());
texture = TextureManager.Instance.LoadImage(uniqueName, image);
this.width = bufferWidth;
this.height = bufferHeight;
}
#endregion Methods
#endregion Texture Implementation
#region AxiomTexture Specific Methods
public void SetAxiomTextureSize(int size)
{
// detach from current texture
FreeAxiomTexture();
string uniqueName = string.Format("CEGUI_texture_{0}", Guid.NewGuid());
// Attempt to create empty texture with given size
texture = TextureManager.Instance.CreateManual(
uniqueName, TextureType.TwoD, size, size, 0, Axiom.Media.PixelFormat.A8R8G8B8,
TextureUsage.Default);
// if texture is valid, cache some details
if(texture != null) {
width = texture.Width;
height = texture.Height;
}
else {
// TODO: Should really be a RendererException
throw new Exception("Failed to create texture of specified size: Axiom Texture creation failed.");
}
}
#endregion
#region Internal Methods
/// <summary>
/// Destroys the Axiom texture if there is one.
/// </summary>
protected void FreeAxiomTexture() {
if ((texture != null) && !isLinked) {
// unload and nullify the reference
TextureManager.Instance.Unload(texture);
texture = null;
}
}
#endregion Internal Methods
#region Properties
/// <summary>
/// Get/Set the Axiom texture that this Gui texture is based upon.
/// </summary>
public Axiom.Core.Texture Texture {
get {
return texture;
}
set {
if (texture != value) {
FreeAxiomTexture();
texture = value;
isLinked = true;
}
}
}
#endregion Properties
}
}
|