Joined: 8/23/2011(UTC) Posts: 245
Thanks: 6 times Was thanked: 12 time(s) in 11 post(s)
|
Ok so I updated the sample so holding a cursor key scrolls and also clicking and dragging the mouse pans the tilemap. I've created this as a Visual Test in SpriteTests, but it's getting complicated enough that it'll probably end up as a Sample before too long. (again, quoting it as well as including in code tags as it's not rendering properly for me...) Code:using System;
using DeltaEngine.Content;
using DeltaEngine.Core;
using DeltaEngine.Datatypes;
using DeltaEngine.Graphics;
using DeltaEngine.Input;
using DeltaEngine.Platforms.All;
using DeltaEngine.Platforms.Tests;
using DeltaEngine.Rendering.Fonts;
using DeltaEngine.Rendering.Shapes;
using DeltaEngine.Rendering.Sprites;
using NUnit.Framework;
namespace DeltaEngine.Rendering.Tests.Sprites
{
public class SpriteTests : TestWithAllFrameworks
{
[VisualTest]
public void RenderSmoothlyScrollingTileMap(Type resolver)
{
Start(resolver, (ContentLoader content, InputCommands input) =>
{
new FontText(new Font(content, "Verdana12"),
"Hold down cursor keys or drag mouse to scroll", new Point(0.3f, 0.75f));
logo = content.Load<Image>("DeltaEngineLogo");
CreateWorld();
CreateMap();
CreateBorder();
RespondToInput(input);
}, ScrollMap);
}
private Image logo;
private void CreateWorld()
{
for (int x = 0; x < WorldWidth; x++)
for (int y = 0; y < WorldHeight; y++)
world[x, y] = new Color(Rainbow(x, WorldWidth), Rainbow(y, WorldHeight),
Rainbow(x + y, WorldWidth));
}
private readonly Color[,] world = new Color[WorldWidth,WorldHeight];
private const int WorldWidth = 30;
private const int WorldHeight = 30;
private static float Rainbow(int value, int max)
{
return ((2.0f * value) % max) / max;
}
private void CreateMap()
{
for (int x = 0; x < MapWidth; x++)
for (int y = 0; y < MapHeight; y++)
map[x, y] = new Sprite(logo);
UpdateMapSprites();
}
private readonly Sprite[,] map = new Sprite[MapWidth,MapHeight];
private const int MapWidth = 10;
private const int MapHeight = 10;
private const float MapLeft = 0.1f;
private const float MapTop = 0.3f;
private const float TileWidth = 0.04f;
private const float TileHeight = 0.04f;
private void UpdateMapSprites()
{
var offset = new Point(renderingTopLeft.X % 1.0f, renderingTopLeft.Y % 1.0f);
for (int x = 0; x < MapWidth; x++)
for (int y = 0; y < MapHeight; y++)
UpdateMapSprite(x, y, offset);
}
private Point renderingTopLeft;
private void UpdateMapSprite(int x, int y, Point offset)
{
map[x, y].Color = world[(int)renderingTopLeft.X + x, (int)renderingTopLeft.Y + y];
map[x, y].DrawArea = new Rectangle(MapLeft + (x - offset.X) * TileWidth,
MapTop + (y - offset.Y) * TileHeight, TileWidth, TileHeight);
}
private static void CreateBorder()
{
new Rect(new Rectangle(MapLeft - TileWidth, MapTop - TileHeight, BorderWidth, TileHeight),
Color.Gray);
new Rect(new Rectangle(MapLeft - TileWidth, MapTop - TileHeight, TileWidth, BorderHeight),
Color.Gray);
new Rect(
new Rectangle(MapLeft - TileWidth, MapTop + (MapHeight - 1) * TileHeight, BorderWidth,
TileHeight), Color.Gray);
new Rect(
new Rectangle(MapLeft + (MapWidth - 1) * TileWidth, MapTop - TileHeight, TileWidth,
BorderHeight), Color.Gray);
}
private const float BorderWidth = (MapWidth + 1) * TileWidth;
private const float BorderHeight = (MapHeight + 1) * TileHeight;
private void RespondToInput(InputCommands input)
{
input.Add(MouseButton.Left, State.Pressing, BeginMouseDrag);
input.Add(MouseButton.Left, State.Pressed, ContinueMouseDrag);
input.Add(Key.CursorLeft, State.Pressed, key => CursorLeftPressed());
input.Add(Key.CursorRight, State.Pressed, key => CursorRightPressed());
input.Add(Key.CursorUp, State.Pressed, key => CursorUpPressed());
input.Add(Key.CursorDown, State.Pressed, key => CursorDownPressed());
}
private void BeginMouseDrag(Mouse mouse)
{
lastMousePosition = mouse.Position;
}
private Point lastMousePosition;
private void ContinueMouseDrag(Mouse mouse)
{
desiredTopLeft.X += (lastMousePosition.X - mouse.Position.X) / TileWidth;
desiredTopLeft.Y += (lastMousePosition.Y - mouse.Position.Y) / TileHeight;
lastMousePosition = mouse.Position;
RestrictScrollingToWithinWorld();
}
private Point desiredTopLeft;
private void RestrictScrollingToWithinWorld()
{
if (desiredTopLeft.X < 0)
desiredTopLeft.X = 0;
if (desiredTopLeft.X > WorldWidth - MapWidth)
desiredTopLeft.X = WorldWidth - MapWidth;
if (desiredTopLeft.Y < 0)
desiredTopLeft.Y = 0;
if (desiredTopLeft.Y > WorldHeight - MapHeight)
desiredTopLeft.Y = WorldHeight - MapHeight;
}
private void CursorLeftPressed()
{
desiredTopLeft.X -= Time.Current.Delta * ScrollSpeed;
RestrictScrollingToWithinWorld();
}
private const float ScrollSpeed = 4.0f;
private void CursorRightPressed()
{
desiredTopLeft.X += Time.Current.Delta * ScrollSpeed;
RestrictScrollingToWithinWorld();
}
private void CursorUpPressed()
{
desiredTopLeft.Y -= Time.Current.Delta * ScrollSpeed;
RestrictScrollingToWithinWorld();
}
private void CursorDownPressed()
{
desiredTopLeft.Y += Time.Current.Delta * ScrollSpeed;
RestrictScrollingToWithinWorld();
}
private void ScrollMap()
{
if (renderingTopLeft == desiredTopLeft)
return;
renderingTopLeft.X = MathExtensions.Lerp(renderingTopLeft.X, desiredTopLeft.X,
Time.Current.Delta);
renderingTopLeft.Y = MathExtensions.Lerp(renderingTopLeft.Y, desiredTopLeft.Y,
Time.Current.Delta);
UpdateMapSprites();
}
}
}
Quote:using System; using DeltaEngine.Content; using DeltaEngine.Core; using DeltaEngine.Datatypes; using DeltaEngine.Graphics; using DeltaEngine.Input; using DeltaEngine.Platforms.All; using DeltaEngine.Platforms.Tests; using DeltaEngine.Rendering.Fonts; using DeltaEngine.Rendering.Shapes; using DeltaEngine.Rendering.Sprites; using NUnit.Framework;
namespace DeltaEngine.Rendering.Tests.Sprites { public class SpriteTests : TestWithAllFrameworks { [VisualTest] public void RenderSmoothlyScrollingTileMap(Type resolver) { Start(resolver, (ContentLoader content, InputCommands input) => { new FontText(new Font(content, "Verdana12"), "Hold down cursor keys or drag mouse to scroll", new Point(0.3f, 0.75f)); logo = content.Load<Image>("DeltaEngineLogo"); CreateWorld(); CreateMap(); CreateBorder(); RespondToInput(input); }, ScrollMap); }
private Image logo;
private void CreateWorld() { for (int x = 0; x < WorldWidth; x++) for (int y = 0; y < WorldHeight; y++) world[x, y] = new Color(Rainbow(x, WorldWidth), Rainbow(y, WorldHeight), Rainbow(x + y, WorldWidth)); }
private readonly Color[,] world = new Color[WorldWidth,WorldHeight]; private const int WorldWidth = 30; private const int WorldHeight = 30;
private static float Rainbow(int value, int max) { return ((2.0f * value) % max) / max; }
private void CreateMap() { for (int x = 0; x < MapWidth; x++) for (int y = 0; y < MapHeight; y++) map[x, y] = new Sprite(logo);
UpdateMapSprites(); }
private readonly Sprite[,] map = new Sprite[MapWidth,MapHeight]; private const int MapWidth = 10; private const int MapHeight = 10; private const float MapLeft = 0.1f; private const float MapTop = 0.3f; private const float TileWidth = 0.04f; private const float TileHeight = 0.04f;
private void UpdateMapSprites() { var offset = new Point(renderingTopLeft.X % 1.0f, renderingTopLeft.Y % 1.0f); for (int x = 0; x < MapWidth; x++) for (int y = 0; y < MapHeight; y++) UpdateMapSprite(x, y, offset); }
private Point renderingTopLeft;
private void UpdateMapSprite(int x, int y, Point offset) { map[x, y].Color = world[(int)renderingTopLeft.X + x, (int)renderingTopLeft.Y + y]; map[x, y].DrawArea = new Rectangle(MapLeft + (x - offset.X) * TileWidth, MapTop + (y - offset.Y) * TileHeight, TileWidth, TileHeight); }
private static void CreateBorder() { new Rect(new Rectangle(MapLeft - TileWidth, MapTop - TileHeight, BorderWidth, TileHeight), Color.Gray); new Rect(new Rectangle(MapLeft - TileWidth, MapTop - TileHeight, TileWidth, BorderHeight), Color.Gray); new Rect( new Rectangle(MapLeft - TileWidth, MapTop + (MapHeight - 1) * TileHeight, BorderWidth, TileHeight), Color.Gray); new Rect( new Rectangle(MapLeft + (MapWidth - 1) * TileWidth, MapTop - TileHeight, TileWidth, BorderHeight), Color.Gray); }
private const float BorderWidth = (MapWidth + 1) * TileWidth; private const float BorderHeight = (MapHeight + 1) * TileHeight;
private void RespondToInput(InputCommands input) { input.Add(MouseButton.Left, State.Pressing, BeginMouseDrag); input.Add(MouseButton.Left, State.Pressed, ContinueMouseDrag); input.Add(Key.CursorLeft, State.Pressed, key => CursorLeftPressed()); input.Add(Key.CursorRight, State.Pressed, key => CursorRightPressed()); input.Add(Key.CursorUp, State.Pressed, key => CursorUpPressed()); input.Add(Key.CursorDown, State.Pressed, key => CursorDownPressed()); }
private void BeginMouseDrag(Mouse mouse) { lastMousePosition = mouse.Position; }
private Point lastMousePosition;
private void ContinueMouseDrag(Mouse mouse) { desiredTopLeft.X += (lastMousePosition.X - mouse.Position.X) / TileWidth; desiredTopLeft.Y += (lastMousePosition.Y - mouse.Position.Y) / TileHeight; lastMousePosition = mouse.Position; RestrictScrollingToWithinWorld(); }
private Point desiredTopLeft;
private void RestrictScrollingToWithinWorld() { if (desiredTopLeft.X < 0) desiredTopLeft.X = 0;
if (desiredTopLeft.X > WorldWidth - MapWidth) desiredTopLeft.X = WorldWidth - MapWidth;
if (desiredTopLeft.Y < 0) desiredTopLeft.Y = 0;
if (desiredTopLeft.Y > WorldHeight - MapHeight) desiredTopLeft.Y = WorldHeight - MapHeight; }
private void CursorLeftPressed() { desiredTopLeft.X -= Time.Current.Delta * ScrollSpeed; RestrictScrollingToWithinWorld(); }
private const float ScrollSpeed = 4.0f;
private void CursorRightPressed() { desiredTopLeft.X += Time.Current.Delta * ScrollSpeed; RestrictScrollingToWithinWorld(); }
private void CursorUpPressed() { desiredTopLeft.Y -= Time.Current.Delta * ScrollSpeed; RestrictScrollingToWithinWorld(); }
private void CursorDownPressed() { desiredTopLeft.Y += Time.Current.Delta * ScrollSpeed; RestrictScrollingToWithinWorld(); }
private void ScrollMap() { if (renderingTopLeft == desiredTopLeft) return;
renderingTopLeft.X = MathExtensions.Lerp(renderingTopLeft.X, desiredTopLeft.X, Time.Current.Delta); renderingTopLeft.Y = MathExtensions.Lerp(renderingTopLeft.Y, desiredTopLeft.Y, Time.Current.Delta); UpdateMapSprites(); } } } Edited by user Thursday, May 30, 2013 6:05:54 AM(UTC)
| Reason: Not specified
|