Skip to content

Commit

Permalink
Added Time and Clock classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zsbzsb committed Jun 8, 2014
1 parent 65f01ca commit f7aeeb1
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/System/Clock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Security;
using System.Runtime.InteropServices;

namespace SFML.System
{
////////////////////////////////////////////////////////////
/// <summary>
/// Utility class that measures the elapsed time
/// </summary>
////////////////////////////////////////////////////////////
public class Clock : ObjectBase
{
////////////////////////////////////////////////////////////
/// <summary>
/// Default Constructor
/// </summary>
////////////////////////////////////////////////////////////
public Clock()
: base(sfClock_create())
{
}

////////////////////////////////////////////////////////////
/// <summary>
/// Handle the destruction of the object
/// </summary>
/// <param name="disposing">Is the GC disposing the object, or is it an explicit call ?</param>
////////////////////////////////////////////////////////////
protected override void Destroy(bool disposing)
{
sfClock_destroy(CPointer);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Gets the time elapsed since the last call to Restart
/// </summary>
////////////////////////////////////////////////////////////
public Time ElapsedTime
{
get
{
return sfClock_getElapsedTime(CPointer);
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// This function puts the time counter back to zero.
/// </summary>
/// <returns>Time elapsed since the clock was started.</returns>
////////////////////////////////////////////////////////////
public Time Restart()
{
return sfClock_restart(CPointer);
}

#region Imports

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfClock_create();

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void sfClock_destroy(IntPtr CPointer);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Time sfClock_getElapsedTime(IntPtr Clock);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Time sfClock_restart(IntPtr Clock);

#endregion
}
}
305 changes: 305 additions & 0 deletions src/System/Time.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
using System;
using System.Security;
using System.Runtime.InteropServices;

namespace SFML.System
{
////////////////////////////////////////////////////////////
/// <summary>
/// This class represents a time value
/// </summary>
////////////////////////////////////////////////////////////
[StructLayout(LayoutKind.Sequential)]
public struct Time : IEquatable<Time>
{
////////////////////////////////////////////////////////////
/// <summary>
/// Construct a time value from a number of seconds
/// </summary>
/// <param name="seconds">Number of seconds</param>
/// <returns>Time value constructed from the amount of seconds</returns>
////////////////////////////////////////////////////////////
public static Time FromSeconds(float seconds)
{
return sfSeconds(seconds);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct a time value from a number of milliseconds
/// </summary>
/// <param name="milliseconds">Number of milliseconds</param>
/// <returns>Time value constructed from the amount of milliseconds</returns>
////////////////////////////////////////////////////////////
public static Time FromMilliseconds(int milliseconds)
{
return sfMilliseconds(milliseconds);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Construct a time value from a number of microseconds
/// </summary>
/// <param name="microseconds">Number of microseconds</param>
/// <returns>Time value constructed from the amount of microseconds</returns>
////////////////////////////////////////////////////////////
public static Time FromMicroseconds(long microseconds)
{
return sfMicroseconds(microseconds);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the time value as a number of seconds
/// </summary>
////////////////////////////////////////////////////////////
public float AsSeconds()
{
return sfTime_asSeconds(this);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the time value as a number of milliseconds
/// </summary>
////////////////////////////////////////////////////////////
public int AsMilliseconds()
{
return sfTime_asMilliseconds(this);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns the time value as a number of microseconds
/// </summary>
////////////////////////////////////////////////////////////
public long AsMicroseconds()
{
return sfTime_asMicroseconds(this);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Compare two times and checks if they are equal
/// </summary>
/// <returns>Times are equal</returns>
////////////////////////////////////////////////////////////
public static bool operator ==(Time left, Time right)
{
return left.Equals(right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Compare two times and checks if they are not equal
/// </summary>
/// <returns>Times are not equal</returns>
////////////////////////////////////////////////////////////
public static bool operator !=(Time left, Time right)
{
return !left.Equals(right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Compare time and object and checks if they are equal
/// </summary>
/// <param name="obj">Object to check</param>
/// <returns>Object and time are equal</returns>
////////////////////////////////////////////////////////////
public override bool Equals(object obj)
{
return (obj is Time) && obj.Equals(this);
}

///////////////////////////////////////////////////////////
/// <summary>
/// Compare two times and checks if they are equal
/// </summary>
/// <param name="other">Time to check</param>
/// <returns>times are equal</returns>
////////////////////////////////////////////////////////////
public bool Equals(Time other)
{
return microseconds == other.microseconds;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of &lt; operator to compare two time values
/// </summary>
/// <returns>True if left is lesser than right</returns>
////////////////////////////////////////////////////////////
public static bool operator <(Time left, Time right)
{
return left.AsMicroseconds() < right.AsMicroseconds();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of &lt;= operator to compare two time values
/// </summary>
/// <returns>True if left is lesser or equal than right</returns>
////////////////////////////////////////////////////////////
public static bool operator <=(Time left, Time right)
{
return left.AsMicroseconds() <= right.AsMicroseconds();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of &gt; operator to compare two time values
/// </summary>
/// <returns>True if left is greater than right</returns>
////////////////////////////////////////////////////////////
public static bool operator >(Time left, Time right)
{
return left.AsMicroseconds() > right.AsMicroseconds();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of &gt;= operator to compare two time values
/// </summary>
/// <returns>True if left is greater or equal than right</returns>
////////////////////////////////////////////////////////////
public static bool operator >=(Time left, Time right)
{
return left.AsMicroseconds() >= right.AsMicroseconds();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary - operator to subtract two time values
/// </summary>
/// <returns>Difference of the two times values</returns>
////////////////////////////////////////////////////////////
public static Time operator -(Time left, Time right)
{
return FromMicroseconds(left.AsMicroseconds() - right.AsMicroseconds());
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary + operator to add two time values
/// </summary>
/// <returns>Sum of the two times values</returns>
////////////////////////////////////////////////////////////
public static Time operator +(Time left, Time right)
{
return FromMicroseconds(left.AsMicroseconds() + right.AsMicroseconds());
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary * operator to scale a time value
/// </summary>
/// <returns>left multiplied by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator *(Time left, float right)
{
return FromSeconds(left.AsSeconds() * right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary * operator to scale a time value
/// </summary>
/// <returns>left multiplied by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator *(Time left, long right)
{
return FromMicroseconds(left.AsMicroseconds() * right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary * operator to scale a time value
/// </summary>
/// <returns>left multiplied by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator *(float left, Time right)
{
return FromSeconds(left * right.AsSeconds());
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary * operator to scale a time value
/// </summary>
/// <returns>left multiplied by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator *(long left, Time right)
{
return FromMicroseconds(left * right.AsMicroseconds());
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary / operator to scale a time value
/// </summary>
/// <returns>left divided by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator /(Time left, Time right)
{
return FromMicroseconds(left.AsMicroseconds() / right.AsMicroseconds());
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary / operator to scale a time value
/// </summary>
/// <returns>left divided by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator /(Time left, float right)
{
return FromSeconds(left.AsSeconds() / right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary / operator to scale a time value
/// </summary>
/// <returns>left divided by the right</returns>
////////////////////////////////////////////////////////////
public static Time operator /(Time left, long right)
{
return FromMicroseconds(left.AsMicroseconds() / right);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Overload of binary % operator to compute remainder of a time value
/// </summary>
/// <returns>left modulo of right</returns>
////////////////////////////////////////////////////////////
public static Time operator %(Time left, Time right)
{
return FromMicroseconds(left.AsMicroseconds() % right.AsMicroseconds());
}

private long microseconds;

#region Imports

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Time sfSeconds(float Amount);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Time sfMilliseconds(int Amount);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern Time sfMicroseconds(long Amount);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern float sfTime_asSeconds(Time time);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern int sfTime_asMilliseconds(Time time);

[DllImport("csfml-system-2", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern long sfTime_asMicroseconds(Time time);

#endregion
}
}
2 changes: 2 additions & 0 deletions src/System/sfml-system.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@
</Target>
-->
<ItemGroup>
<Compile Include="Clock.cs" />
<Compile Include="ObjectBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Time.cs" />
<Compile Include="Vector2.cs" />
<Compile Include="Vector3.cs" />
</ItemGroup>
Expand Down

0 comments on commit f7aeeb1

Please sign in to comment.