Skip to content

Commit

Permalink
More code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hüseyin Uslu committed Dec 30, 2012
1 parent 0c63174 commit 3d63a73
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 113 deletions.
19 changes: 18 additions & 1 deletion src/Engine/Assets/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,35 @@ public class AssetManager : GameComponent, IAssetManager

public SpriteFont Verdana { get; private set; }

// MonoGame requires specially compiled shaders with mgfxo extension.
#if MONOGAME
private const string EffectShaderExtension = ".mgfxo";
private const string EffectShaderExtension = ".mgfxo";
#else
private const string EffectShaderExtension = "";
#endif

private static readonly Logger Logger = LogManager.CreateLogger(); // the logger.

//Creates a new asset manager instance.
public AssetManager(Game game)
: base(game)
{
this.Game.Services.AddService(typeof(IAssetManager), this); // export service.
_instance = this;
}

/// <summary>
/// Initializes the asset manager.
/// </summary>
public override void Initialize()
{
this.LoadContent();
base.Initialize();
}

/// <summary>
/// Loads required assets.
/// </summary>
public void LoadContent()
{
try
Expand Down Expand Up @@ -123,13 +131,22 @@ public void LoadContent()
}
}

/// <summary>
/// Loads an effect shared file.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private Effect LoadEffectShader(string path)
{
// Note that monogame requires special compiled shaders with mgfxo extension.
return this.Game.Content.Load<Effect>(path + EffectShaderExtension);
}

private static AssetManager _instance; // the instance.

/// <summary>
/// Returns the memory instance of AssetManager.
/// </summary>
public static AssetManager Instance
{
get { return _instance; }
Expand Down
47 changes: 26 additions & 21 deletions src/Engine/Blocks/BlockStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ public static Block BlockAt(int x, int y, int z)
return Blocks[flattenIndex];
}

/// <summary>
/// Gets a block by given world position.
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
/// <remarks>As <see cref="Block"/> is a struct, the returned block will be a copy of original one.</remarks>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
public static Block BlockAt(Vector3 position)
{
return BlockAt((int)position.X, (int)position.Y, (int)position.Z);
}

/// <summary>
/// Gets a block by given world position.
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
/// <remarks>As <see cref="Block"/> is a struct, the returned block will be a copy of original one.</remarks>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
public static Block BlockAt(Vector3Int position)
{
return BlockAt(position.X, position.Y, position.Z);
}

/// <summary>
/// Gets a block by given world position.
/// </summary>
Expand Down Expand Up @@ -123,26 +147,7 @@ public static Block FastBlockAt(int x, int y, int z)
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
public static Block BlockAt(Vector3 position)
{
return BlockAt((int)position.X, (int)position.Y, (int)position.Z);
}

/// <summary>
/// Gets a block by given world position.
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
public static Block BlockAt(Vector3Int position)
{
return BlockAt(position.X, position.Y, position.Z);
}

/// <summary>
/// Gets a block by given world position.
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
/// <remarks>As <see cref="Block"/> is a struct, the returned block will be a copy of original one.</remarks>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
public static Block FastBlockAt(Vector3 position)
{
Expand All @@ -154,7 +159,7 @@ public static Block FastBlockAt(Vector3 position)
/// </summary>
/// <param name="position">Point/block position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
/// <remarks>As <see cref="Block"/> is a struct, the returned block will be a copy of original one.</remarks> /// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
public static Block FastBlockAt(Vector3Int position)
{
return FastBlockAt(position.X, position.Y, position.Z);
Expand Down
29 changes: 27 additions & 2 deletions src/Engine/Blocks/BlockVertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace VoxeliqEngine.Blocks
{
/// Represents block vertex.
[Serializable]
public struct BlockVertex : IVertexType
{
Expand All @@ -20,6 +21,12 @@ public struct BlockVertex : IVertexType
private float _sunLight;
//private Vector3 _localLight;

/// <summary>
/// Creates a new instance of BlockVertex.
/// </summary>
/// <param name="position"></param>
/// <param name="blockTextureCoordinate"></param>
/// <param name="sunLight"></param>
public BlockVertex(Vector3 position, HalfVector2 blockTextureCoordinate, float sunLight) //, Vector3 localLight)
{
_position = position;
Expand All @@ -28,11 +35,17 @@ public BlockVertex(Vector3 position, HalfVector2 blockTextureCoordinate, float s
//_localLight = localLight;
}

/// <summary>
/// Returns the block vertex declaration.
/// </summary>
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}

/// <summary>
/// The actual block vertex declaration.
/// </summary>
private static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(new[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage .Position, 0),
Expand All @@ -41,29 +54,41 @@ VertexDeclaration IVertexType.VertexDeclaration
//new VertexElement(sizeof (float)*5, VertexElementFormat.Vector3, VertexElementUsage.Color, 1)
});

/// <summary>
/// The position.
/// </summary>
public Vector3 Position
{
get { return _position; }
set { _position = value; }
}

/// <summary>
/// Texture coordinates.
/// </summary>
public HalfVector2 BlockTextureCoordinate
{
get { return _blockTextureCoordinate; }
set { _blockTextureCoordinate = value; }
}

//public Vector3 LocalLight { get { return _localLight; } set { _localLight = value; } }

/// <summary>
/// Sunlight.
/// </summary>
public float SunLight
{
get { return _sunLight; }
set { _sunLight = value; }
}

/// <summary>
/// The size of vertex declaration in bytes.
/// </summary>
public static int SizeInBytes
{
get { return sizeof (float)*5; }
}

//public Vector3 LocalLight { get { return _localLight; } set { _localLight = value; } }
}
}
94 changes: 28 additions & 66 deletions src/Engine/Chunks/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,46 +122,6 @@ public sealed class Chunk : IInGameDebuggable
/// </summary>
public bool Disposed = false;

public Chunk North
{
get { return ChunkStorage.Instance[this.RelativePosition.X, this.RelativePosition.Z + 1]; }
}

public Chunk South
{
get { return ChunkStorage.Instance[this.RelativePosition.X, this.RelativePosition.Z - 1]; }
}

public Chunk West
{
get { return ChunkStorage.Instance[this.RelativePosition.X - 1, this.RelativePosition.Z]; }
}

public Chunk East
{
get { return ChunkStorage.Instance[this.RelativePosition.X + 1, this.RelativePosition.Z]; }
}

public Chunk NorthWest
{
get { return ChunkStorage.Instance[this.RelativePosition.X - 1, this.RelativePosition.Z + 1]; }
}

public Chunk NorthEast
{
get { return ChunkStorage.Instance[this.RelativePosition.X + 1, this.RelativePosition.Z + 1]; }
}

public Chunk SouthWest
{
get { return ChunkStorage.Instance[this.RelativePosition.X - 1, this.RelativePosition.Z - 1]; }
}

public Chunk SouthEast
{
get { return ChunkStorage.Instance[this.RelativePosition.X + 1, this.RelativePosition.Z - 1]; }
}

/// <summary>
/// Creates a new chunk instance.
/// </summary>
Expand Down Expand Up @@ -200,37 +160,39 @@ public bool IsInBounds(float x, float z)
}

/// <summary>
/// Returns the block at given wolrd coordinate.
/// Gets a block by given world position.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
/// <param name="x">Block's x world position.</param>
/// <param name="y">Block's y world position.</param>
/// <param name="z">Block's z world position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
public Block BlockAt(int x, int y, int z)
{
return BlockStorage.BlockAt(this.WorldPosition.X + x, y, this.WorldPosition.Z + z);
}

/// <summary>
/// Returns the block at given wolrd coordinate.
/// Gets a block by given world position.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
/// <param name="x">Block's x world position.</param>
/// <param name="y">Block's y world position.</param>
/// <param name="z">Block's z world position.</param>
/// <returns>Copy of <see cref="Block"/></returns>
/// <remarks>As <see cref="Block"/> is a struct, the returned block will be a copy of original one.</remarks>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="BlockAt"/> instead.</remarks>
public Block FastBlockAt(int x, int y, int z)
{
return BlockStorage.FastBlockAt(this.WorldPosition.X + x, y, this.WorldPosition.Z + z);
}


/// <summary>
/// Sets block at given relative position.
/// Sets a block by given relative position.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <param name="block"></param>
/// <param name="x">Block's x world position.</param>
/// <param name="y">Block's y world position.</param>
/// <param name="z">Block's z world position.</param>
/// <param name="block">Block to set.</param>
/// <remarks>This method will not check if given point/block coordinates are in chunk-cache's bounds. If you need a reliable & safe way, use <see cref="SetBlockAt"/> instead.</remarks>
public void FastSetBlockAt(byte x, byte y, byte z, Block block)
{
switch (block.Exists)
Expand All @@ -256,8 +218,14 @@ public override string ToString()

#region ingame debugger

public void DrawInGameDebugVisual(GraphicsDevice graphicsDevice, ICamera camera, SpriteBatch spriteBatch,
SpriteFont spriteFont)
/// <summary>
/// Draws ingame chunk debugger.
/// </summary>
/// <param name="graphicsDevice"></param>
/// <param name="camera"></param>
/// <param name="spriteBatch"></param>
/// <param name="spriteFont"></param>
public void DrawInGameDebugVisual(GraphicsDevice graphicsDevice, ICamera camera, SpriteBatch spriteBatch, SpriteFont spriteFont)
{
var position = RelativePosition + " " + this.ChunkState;
var positionSize = spriteFont.MeasureString(position);
Expand Down Expand Up @@ -287,8 +255,7 @@ private void Dispose(bool disposing)
{
if (this.Disposed) return; // if already disposed, just return

if (disposing)
// only dispose managed resources if we're called from directly or in-directly from user code.
if (disposing) // only dispose managed resources if we're called from directly or in-directly from user code.
{
this.IndexList.Clear();
this.IndexList = null;
Expand All @@ -305,12 +272,7 @@ private void Dispose(bool disposing)
Disposed = true;
}

~Chunk()
{
Dispose(false);
}

// finalizer called by the runtime. we should only dispose unmanaged objects and should NOT reference managed ones.
~Chunk() { Dispose(false); } // finalizer called by the runtime. we should only dispose unmanaged objects and should NOT reference managed ones.

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Chunks/Processors/VertexBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.Xna.Framework.Graphics.PackedVector;
using VoxeliqEngine.Blocks;
using VoxeliqEngine.Common.Vector;
using VoxeliqEngine.Texture;
using VoxeliqEngine.Graphics.Texture;
using VoxeliqEngine.Universe;

// TODO: Document this file!
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Common/Configuration/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

using System;
using Nini.Config;
using VoxeliqEngine.Common.Helpers.IO;
using VoxeliqEngine.Common.Logging;
using VoxeliqEngine.IO;

namespace VoxeliqEngine.Common.Configuration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Linq;
using System.Reflection;

namespace VoxeliqEngine.IO
namespace VoxeliqEngine.Common.Helpers.IO
{
public static class FileHelpers
{
Expand Down
1 change: 1 addition & 0 deletions src/Engine/Graphics/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Microsoft.Xna.Framework;
using VoxeliqEngine.Common.Logging;
using VoxeliqEngine.Universe;

namespace VoxeliqEngine.Graphics
{
Expand Down
Loading

0 comments on commit 3d63a73

Please sign in to comment.