-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
382 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 < 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 <= 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 > 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 >= 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters