Medals:  Joined: 8/20/2011(UTC) Posts: 1,421 Location: Hannover
Thanks: 18 times Was thanked: 97 time(s) in 92 post(s)
|
Here is the full sample (it is a little bit more than you need and not the prettiest code ever, but it is short and does pretty much what you want). You can also just download v0.9.1 (available in a few hours) and check out the SimpleGameExample project yourself: I don't fully understand your question. If you still have gaps between points just increase the NumberOfInterpolations (e.g. to 10 or more for testing). You could also optimize it a bit if you know the distance to the last point and the size of each point, then you would only need extra interpolations if the distance is bigger than the size of each point (e.g. moved 0.8 over screen, point size is 0.1, then you need at least 8 interpolations to connect them, maybe 16 to make it look more like a line). Code:
/// <summary>
/// Game class, which is the entry point for this game. Manages all the game
/// logic and displays everything. More complex games do this differently.
/// </summary>
class Game : DynamicModule
{
#region Constants
/// <summary>
/// Update the trail 60 times per second.
/// </summary>
private const int NumberOfUpdatesPerSecond = 60;
/// <summary>
/// Keep the trail for 2 seconds.
/// </summary>
private const int TrailLengthInSeconds = 2;
/// <summary>
/// Interpolate the trail to make it look more smooth than the input
/// allowes (which might jump great distances).
/// </summary>
private const int NumberOfInterpolations = 3;
#endregion
#region Variables
/// <summary>
/// Just load the DeltaEngine image or if that is not available the
/// DeltaEngineLogo image, which is always available.
/// </summary>
private readonly Material2DColored gameObject =
new Material2DColored("DeltaEngineLogo");
/// <summary>
/// Welcome text, which can be changed once a UIClick happened.
/// </summary>
private string welcomeText =
"Press Space, GamePad, Click or Touch to see the hidden message";
/// <summary>
/// Start drawing in the middle, but allow moving around with cursors or
/// any input device returning position change (mouse, touch, gamepad).
/// </summary>
private Point drawLocation = Point.Half;
/// <summary>
/// Last positions for the trail that is slowly fading out.
/// </summary>
readonly List<Point> lastPositions = new List<Point>();
/// <summary>
/// Last times (in milliseconds) for the trail to reconstruct the size
/// and rotation values.
/// </summary>
readonly List<long> lastTimeMs = new List<long>();
/// <summary>
/// Last colors for the trail to make everything a bit more colorful.
/// </summary>
readonly List<Color> lastColors = new List<Color>();
/// <summary>
/// Current color used for drawing the trail.
/// </summary>
private Color currentColor = Color.Random;
/// <summary>
/// Next color we are fading to for the trail.
/// </summary>
private Color nextColor = Color.Random;
#endregion
#region Constructor
/// <summary>
/// Constructor, do additional initialization code here if needed.
/// </summary>
public Game()
: base("Simple Game", typeof(Application))
{
// Note: Normally in a game you would define those commands via the
// InputSettings.xml in the ContentManager, don't use the commands here!
Input.Commands[Command.UIClick].Add(delegate
{
Application.BackgroundColor = Color.DarkBlue;
welcomeText = "You made it! Yay. Now write some game code :)";
});
CommandDelegate moveDrawLocation = delegate(CommandTrigger command)
{
//Log.Info("Position=" + command.Position + ", from " + command.Button);
drawLocation = command.Position;//.Movement / 10.0f;
};
Input.Commands[Command.CameraRotateLeft].Add(moveDrawLocation);
Input.Commands[Command.CameraRotateRight].Add(moveDrawLocation);
Input.Commands[Command.CameraRotateUp].Add(moveDrawLocation);
Input.Commands[Command.CameraRotateDown].Add(moveDrawLocation);
Input.Commands[Command.QuitTest].Add(delegate
{
Application.Quit();
});
}
#endregion
#region Run
/// <summary>
/// Run game loop, this is called every frame to do all game logic updating.
/// </summary>
public override void Run()
{
// Show FPS
Font.Default.DrawTopLeft("FPS: " + Time.Fps);
// Draw the trail and then the current object
for (int num = 0; num < lastPositions.Count; num++)
{
DrawGameObject(lastPositions[num], lastTimeMs[num], lastColors[num],
num / (float)lastPositions.Count);
}
DrawGameObject(drawLocation, Time.Milliseconds, Color.White, 1.0f);
// Change color every second
if (Time.EverySecond)
{
currentColor = nextColor;
nextColor = Color.Random;
}
// Remember position for the trail 30 times per second.
if (Time.CheckEvery(1.0f / NumberOfUpdatesPerSecond))
{
while (lastPositions.Count >
NumberOfUpdatesPerSecond * TrailLengthInSeconds)
{
lastPositions.RemoveAt(0);
lastTimeMs.RemoveAt(0);
lastColors.RemoveAt(0);
}
float colorPerecentage = (Time.Milliseconds % 1000) / 1000.0f;
Color newColor = Color.Lerp(currentColor, nextColor,
colorPerecentage);
// Add few interpolated entries between this one and the last, this
// will make the output look much more smooth :)
if (lastPositions.Count >= NumberOfInterpolations)
{
// This way the output looks more smooth :)
Point p1 = drawLocation;
Point p2 = lastPositions[
lastPositions.Count - NumberOfInterpolations];
long t1 = Time.Milliseconds;
long t2 = lastTimeMs[
lastTimeMs.Count - NumberOfInterpolations];
for (int num = 1; num < NumberOfInterpolations; num++)
{
float interpolation = num / (float)NumberOfInterpolations;
lastPositions.Add(Point.Lerp(p1, p2, interpolation));
lastTimeMs.Add(MathHelper.Lerp(t1, t2, interpolation));
lastColors.Add(newColor);
}
}
lastPositions.Add(drawLocation);
lastTimeMs.Add(Time.Milliseconds);
lastColors.Add(newColor);
}
// And a simple text message
Font.Default.DrawCentered(welcomeText, new Point(0.5f,
ScreenSpace.DrawArea.Bottom - Font.Default.LineHeight / 2));
// Note: put your game logic here, it will be executed each frame.
// There is no difference between update code and render code as all
// rendering will happen optimized at the end of the frame anyway!
}
/// <summary>
/// Draw game object, also used for the trail.
/// </summary>
/// <param name="position">Current position</param>
/// <param name="milliseconds">Time for this object or trail</param>
/// <param name="color">Color for the trail</param>
/// <param name="alpha">Alpha for drawing</param>
private void DrawGameObject(Point position, long milliseconds, Color color,
float alpha)
{
gameObject.BlendColor = new Color(color, alpha);
gameObject.Draw(Rectangle.FromCenter(position, gameObject.Size + 0.5f *
gameObject.Size * MathHelper.Sin(milliseconds / 20.0f)),
milliseconds / 10.0f);
}
#endregion
}
Edited by user Thursday, November 17, 2011 1:17:43 PM(UTC)
| Reason: Not specified
|