Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
Added texture atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
craftersmine committed Mar 4, 2019
1 parent ee74327 commit af1b5c0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
31 changes: 31 additions & 0 deletions craftersmine.EtherEngine.Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ public Texture LoadTexture(string id)
else throw new ContentManagerException("Package does not contains object with this id \"" + id + "\"!");
}

/// <summary>
/// Loads texture as texture atlas from content package with specified ID
/// </summary>
/// <param name="id">Texture ID</param>
/// <returns></returns>
public TextureAtlas LoadTextureAtlas(string id)
{
if (Contains(id))
{
ContentObject cObj = objects[id];
if (cObj.Type == ContentType.TextureAtlas || cObj.Type == ContentType.TextureAtlas)
{
string path = Path.Combine(ContentPath, id + ".etx");
try
{
return new TextureAtlas(Texture.FromFile(path));
}
catch (Exception ex)
{
throw new ContentManagerException("Unable to load object with this id \"" + id + "\" as texture atlas! " + ex.Message);
}
}
else throw new ContentManagerException("Object with this id \"" + id + "\" is not a texture or texture atlas!");
}
else throw new ContentManagerException("Package does not contains object with this id \"" + id + "\"!");
}

/// <summary>
/// Loads animation data and animation texture from content package with specified ID
/// </summary>
Expand Down Expand Up @@ -223,6 +250,10 @@ public enum ContentType
/// </summary>
Texture = 100,
/// <summary>
/// Texture atlas
/// </summary>
TextureAtlas = 110,
/// <summary>
/// Animation
/// </summary>
Animation = 200,
Expand Down
57 changes: 57 additions & 0 deletions craftersmine.EtherEngine.Content/TextureAtlas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace craftersmine.EtherEngine.Content
{
/// <summary>
/// Represents multi-texture atlas. This class cannot be inherited
/// </summary>
public sealed class TextureAtlas
{
/// <summary>
/// Gets currently used texture
/// </summary>
public Texture Texture { get; private set; }

/// <summary>
/// Creates new instance of <see cref="TextureAtlas"/> from specified texture
/// </summary>
/// <param name="textureAtlas"></param>
public TextureAtlas(Texture textureAtlas)
{
Texture = textureAtlas;
}

/// <summary>
/// Cuts and returns single texture with specified rectangle bounds from atlas
/// </summary>
/// <param name="cuttingBounds"><see cref="Rectangle"/> with specified cutting bounds</param>
/// <returns></returns>
public Texture CutFromAtlas(Rectangle cuttingBounds)
{
Bitmap bmp = new Bitmap(cuttingBounds.Width, cuttingBounds.Height);
var image = Graphics.FromImage(bmp);
image.DrawImage(Texture.BaseBitmap, new Rectangle(0, 0, cuttingBounds.Width, cuttingBounds.Height), cuttingBounds, GraphicsUnit.Pixel);
image.Dispose();

return new Texture(bmp);
}

/// <summary>
/// Cuts and returns single texture with specified bounds from atlas
/// </summary>
/// <param name="x">X position of left-upper corner of cutting bounds</param>
/// <param name="y">Y position of left-upper corner of cutting bounds</param>
/// <param name="height">Height of cutting texture</param>
/// <param name="width">Width of cutting texture</param>
/// <returns></returns>
public Texture CutFromAtlas(int x, int y, int width, int height)
{
return CutFromAtlas(new Rectangle(x, y, width, height));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="ContentManagerException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Texture.cs" />
<Compile Include="TextureAtlas.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit af1b5c0

Please sign in to comment.