Skip to content

Commit

Permalink
[172] HUD und Optimierung der Chunk-Prio
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwendel_cp committed Jun 17, 2015
1 parent 91f9996 commit bbf6dce
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 237 deletions.
81 changes: 57 additions & 24 deletions OctoAwesome/OctoAwesome.Client/Components/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@ internal sealed class ChunkRenderer : IDisposable

private Texture2D textures;

/// <summary>
/// Gibt an ob der aktuelle Chunk geladen wurde
/// </summary>
private bool chunkLoaded;

/// <summary>
/// Referenz auf den aktuellen Chunk (falls vorhanden)
/// </summary>
private IChunk chunk;

private VertexBuffer vb;
private IndexBuffer ib;
private int vertexCount;
private int indexCount;
private int lastReset;

public IChunk Chunk { get; private set; }

public Index3 ChunkIndex { get; private set; }

public Index3 RelativeIndex { get; set; }
/// <summary>
/// Referenz auf den aktuell gerenderten Chunk
/// </summary>


public bool InUse { get; set; }
/// <summary>
/// Adresse des aktuellen Chunks
/// </summary>
public PlanetIndex3? ChunkPosition { get; private set; }

public ChunkRenderer(GraphicsDevice graphicsDevice, Matrix projection, Texture2D textures)
{
Expand All @@ -51,28 +63,36 @@ public ChunkRenderer(GraphicsDevice graphicsDevice, Matrix projection, Texture2D
effect.FogColor = new Color(181, 224, 255).ToVector3();
effect.FogStart = SceneComponent.VIEWRANGE * OctoAwesome.Chunk.CHUNKSIZE_X * 0.5f;
effect.FogEnd = SceneComponent.VIEWRANGE * OctoAwesome.Chunk.CHUNKSIZE_X * 0.9f;

InUse = false;
}

public void SetChunk(IChunk chunk)
public void SetChunk(PlanetIndex3? index)
{
ChunkIndex = chunk != null ? chunk.Index : new Index3(0, 0, 0);
Chunk = chunk;
RegenerateVertexBuffer();
ChunkPosition = index;
chunkLoaded = false;
chunk = null;
}

public bool NeedUpdate()
{
if (!InUse || Chunk == null)
// Kein Chunk selektiert -> kein Update notwendig
if (!ChunkPosition.HasValue)
return false;

return Chunk.ChangeCounter > lastReset;
// Selektierter Chunk noch nicht geladen -> Update
if (!chunkLoaded)
return true;

// Chunk vollständig geladen aber nicht vorahden -> kein Update
if (chunk == null)
return false;

// Chunk geladen und existient -> nur Update, wenn sich seit dem letzten Reset was verändert hat.
return chunk.ChangeCounter > lastReset;
}

public void Draw(CameraComponent camera, Index3 shift)
{
if (!InUse || Chunk == null)
if (chunk == null)
return;

effect.World = Matrix.CreateTranslation(
Expand Down Expand Up @@ -100,7 +120,20 @@ public void Draw(CameraComponent camera, Index3 shift)

public void RegenerateVertexBuffer()
{
if (Chunk == null)
if (!ChunkPosition.HasValue)
return;

// Chunk nachladen
if (!chunkLoaded)
{
chunk = ResourceManager.Instance.GetChunk(
ChunkPosition.Value.Planet,
ChunkPosition.Value.ChunkIndex);
chunkLoaded = true;
}

// Ignorieren, falls
if (chunk == null)
return;

List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
Expand All @@ -127,7 +160,7 @@ public void RegenerateVertexBuffer()
{
for (int x = 0; x < OctoAwesome.Chunk.CHUNKSIZE_X; x++)
{
IBlock block = Chunk.GetBlock(x, y, z);
IBlock block = chunk.GetBlock(x, y, z);
if (block == null)
continue;

Expand All @@ -145,7 +178,7 @@ public void RegenerateVertexBuffer()
Vector2 textureSize = new Vector2(textureWidth - 0.005f, textureWidth - 0.005f);

// Top
if (z == OctoAwesome.Chunk.CHUNKSIZE_Z - 1 || Chunk.GetBlock(new Index3(x, y, z + 1)) == null)
if (z == OctoAwesome.Chunk.CHUNKSIZE_Z - 1 || chunk.GetBlock(new Index3(x, y, z + 1)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetTopTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -173,7 +206,7 @@ public void RegenerateVertexBuffer()
}

// Unten
if (z == 0 || Chunk.GetBlock(new Index3(x, y, z - 1)) == null)
if (z == 0 || chunk.GetBlock(new Index3(x, y, z - 1)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetBottomTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -201,7 +234,7 @@ public void RegenerateVertexBuffer()
}

// South
if (y == OctoAwesome.Chunk.CHUNKSIZE_Y - 1 || Chunk.GetBlock(new Index3(x, y + 1, z)) == null)
if (y == OctoAwesome.Chunk.CHUNKSIZE_Y - 1 || chunk.GetBlock(new Index3(x, y + 1, z)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetSouthTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -230,7 +263,7 @@ public void RegenerateVertexBuffer()
}

// North
if (y == 0 || Chunk.GetBlock(new Index3(x, y - 1, z)) == null)
if (y == 0 || chunk.GetBlock(new Index3(x, y - 1, z)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetNorthTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -258,7 +291,7 @@ public void RegenerateVertexBuffer()
}

// West
if (x == 0 || Chunk.GetBlock(new Index3(x - 1, y, z)) == null)
if (x == 0 || chunk.GetBlock(new Index3(x - 1, y, z)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetWestTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -286,7 +319,7 @@ public void RegenerateVertexBuffer()
}

// Ost
if (x == OctoAwesome.Chunk.CHUNKSIZE_X - 1 || Chunk.GetBlock(new Index3(x + 1, y, z)) == null)
if (x == OctoAwesome.Chunk.CHUNKSIZE_X - 1 || chunk.GetBlock(new Index3(x + 1, y, z)) == null)
{
textureOffset = new Vector2(
(((textureIndex + definition.GetEastTextureIndex(block)) % textureColumns) * textureWidth) + 0.002f,
Expand Down Expand Up @@ -350,7 +383,7 @@ public void RegenerateVertexBuffer()
if (ibOld != null)
ibOld.Dispose();

lastReset = Chunk.ChangeCounter;
lastReset = chunk.ChangeCounter;
}

public void Dispose()
Expand Down
28 changes: 5 additions & 23 deletions OctoAwesome/OctoAwesome.Client/Components/GamePadInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void Update()
InteractTrigger = gamePadState.Buttons.X == ButtonState.Pressed;
ApplyTrigger = gamePadState.Buttons.A == ButtonState.Pressed;
JumpTrigger = gamePadState.Buttons.Y == ButtonState.Pressed;
SlotLeftTrigger = gamePadState.Buttons.LeftShoulder == ButtonState.Pressed;
SlotRightTrigger = gamePadState.Buttons.RightShoulder == ButtonState.Pressed;
MoveX = gamePadState.ThumbSticks.Left.X;
MoveY = gamePadState.ThumbSticks.Left.Y;
HeadX = gamePadState.ThumbSticks.Right.X;
Expand All @@ -67,30 +69,10 @@ public void Update()
catch (Exception) { }
}

public bool[] SlotTrigger { get { return null; } }

public bool Slot1Trigger
{
get { return false; }
}
public bool SlotLeftTrigger { get; private set; }

public bool Slot2Trigger
{
get { return false; }
}

public bool Slot3Trigger
{
get { return false; }
}

public bool Slot4Trigger
{
get { return false; }
}

public bool Slot5Trigger
{
get { return false; }
}
public bool SlotRightTrigger { get; private set; }
}
}
39 changes: 34 additions & 5 deletions OctoAwesome/OctoAwesome.Client/Components/HudComponent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OctoAwesome.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

Expand All @@ -23,6 +25,8 @@ internal sealed class HudComponent : DrawableGameComponent
private double seconds = 0;
private double lastfps = 0f;

private Texture2D[] toolTextures;

public HudComponent(Game game, PlayerComponent player)
: base(game)
{
Expand All @@ -41,6 +45,21 @@ protected override void LoadContent()
{
font = Game.Content.Load<SpriteFont>("Hud");
pix = Game.Content.Load<Texture2D>("Textures/pix");

toolTextures = new Texture2D[player.Tools.Length];
int index = 0;
foreach (var tool in player.Tools)
{
using (MemoryStream stream = new MemoryStream())
{
System.Drawing.Bitmap bitmap = tool.Textures.First();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);

toolTextures[index++] = Texture2D.FromStream(GraphicsDevice, stream);
}
}

base.LoadContent();
}

Expand Down Expand Up @@ -85,14 +104,24 @@ public override void Draw(GameTime gameTime)
batch.DrawString(font, selection, new Vector2(5, GraphicsDevice.Viewport.Height - size.Y - 5), Color.White);
}

if (player.Player.BlockTool != null)
if (player.Tools != null && player.Tools.Length > 0)
{
string tool = "Tool: " + player.Player.BlockTool.Name;
size = font.MeasureString(tool);
batch.DrawString(font, tool, new Vector2(GraphicsDevice.Viewport.Width - size.X - 5, GraphicsDevice.Viewport.Height - size.Y - 5), Color.White);
int width = player.Tools.Length * 32 + (player.Tools.Length - 1) * 10;
int offset = (GraphicsDevice.Viewport.Width - width) / 2;
int index = 0;
foreach (var definition in BlockDefinitionManager.GetBlockDefinitions())
{
if (player.Player.ActiveTool == definition)
{
batch.Draw(pix, new Rectangle(offset + (index * 42) - 2, GraphicsDevice.Viewport.Height - 60 - 2, 36, 36), Color.White);
}

batch.Draw(toolTextures[index], new Rectangle(offset + (index * 42), GraphicsDevice.Viewport.Height - 60, 32, 32), Color.White);

index++;
}
}


int centerX = GraphicsDevice.Viewport.Width / 2;
int centerY = GraphicsDevice.Viewport.Height / 2;

Expand Down
Loading

0 comments on commit bbf6dce

Please sign in to comment.