git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1138 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
109
OpenRa.Game/Clock.cs
Normal file
109
OpenRa.Game/Clock.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides access to the current time, frame time, and frame rate.
|
||||
/// </summary>
|
||||
public static class Clock
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs one-time initialization for the clock service
|
||||
/// </summary>
|
||||
static Clock()
|
||||
{
|
||||
startTime = System.Environment.TickCount;
|
||||
StartFrame();
|
||||
}
|
||||
|
||||
public static void Reset()
|
||||
{
|
||||
frameCount = 0;
|
||||
lastFrameCount = 0;
|
||||
lastFrameRate = 0;
|
||||
startTime = System.Environment.TickCount;
|
||||
StartFrame();
|
||||
}
|
||||
|
||||
#region StartFrame
|
||||
|
||||
/// <summary>
|
||||
/// Indicates to the clock that a new frame has begun. This should be called exactly once per frame.
|
||||
/// </summary>
|
||||
public static void StartFrame()
|
||||
{
|
||||
int ticks = System.Environment.TickCount - startTime;
|
||||
frameStartTime = ticks / 1000.0;
|
||||
|
||||
frameCount++;
|
||||
|
||||
if( Time > nextFrameRateUpdateTime )
|
||||
{
|
||||
// set next update time
|
||||
nextFrameRateUpdateTime += (1.0 / FramerateUpdateFrequency);
|
||||
// average between last and current frame, to make spikes less severe
|
||||
const int OldFramerateWeight = 10;
|
||||
const int NewFramerateWeight = 1;
|
||||
int newFrameRate = (frameCount - lastFrameCount) * FramerateUpdateFrequency;
|
||||
lastFrameRate = (lastFrameRate * OldFramerateWeight + NewFramerateWeight * newFrameRate) / (OldFramerateWeight + NewFramerateWeight );
|
||||
lastFrameCount = frameCount;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Implementation Details
|
||||
|
||||
// number of framerate updates per second
|
||||
private const int FramerateUpdateFrequency = 10;
|
||||
|
||||
// the time at which the application started
|
||||
private static int startTime;
|
||||
// the time at which this frame began
|
||||
private static double frameStartTime;
|
||||
// total number of frames rendered
|
||||
private static int frameCount = 0;
|
||||
// next time to update fps count
|
||||
private static double nextFrameRateUpdateTime = 0;
|
||||
// frame count at most recent fps calculation
|
||||
private static int lastFrameCount = 0;
|
||||
// most recently calculated fps
|
||||
private static int lastFrameRate = 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// The time the current frame started
|
||||
/// </summary>
|
||||
public static double Time
|
||||
{
|
||||
get { return frameStartTime; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The number of frames rendered since the engine started
|
||||
/// </summary>
|
||||
public static int FrameCount
|
||||
{
|
||||
get { return frameCount; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The most recent frame-rate
|
||||
/// </summary>
|
||||
public static int FrameRate
|
||||
{
|
||||
get { return lastFrameRate; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame time corresponding to the most recent frame-rate
|
||||
/// </summary>
|
||||
public static double FrameTime
|
||||
{
|
||||
get { return 1.0 / lastFrameRate; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -89,17 +89,17 @@ namespace OpenRa.Game
|
||||
Dictionary<Sheet, List<ushort>> indexMap = new Dictionary<Sheet, List<ushort>>();
|
||||
Vertex[] vertices = new Vertex[4 * 128 * 128];//map.Width * map.Height];
|
||||
|
||||
for( int i = 0; i < 128; i++ )
|
||||
for (int j = 0; j < 128; j++)
|
||||
for( int i = map.XOffset; i < map.XOffset+ map.Width; i++ )
|
||||
for (int j = map.YOffset; j < map.YOffset + map.Height; j++)
|
||||
{
|
||||
SheetRectangle<Sheet> tile = tileMapping[map.MapTiles[i, j]];
|
||||
|
||||
ushort offset = (ushort)(4 * (i * 128 + j));
|
||||
|
||||
vertices[offset] = new Vertex(24 * i, 24 * j, 0, U(tile,0), V(tile,0));
|
||||
vertices[offset + 1] = new Vertex(24 + 24 * i, 24 * j, 0, U(tile,1), V(tile,0));
|
||||
vertices[offset + 2] = new Vertex(24 * i, 24 + 24 * j, 0, U(tile,0), V(tile,1));
|
||||
vertices[offset + 3] = new Vertex(24 + 24 * i, 24 + 24 * j, 0, U(tile,1), V(tile,1));
|
||||
vertices[offset] = new Vertex(24 * i, 24 * j, 0, U(tile, 0), V(tile, 0));
|
||||
vertices[offset + 1] = new Vertex(24 + 24 * i, 24 * j, 0, U(tile, 1), V(tile, 0));
|
||||
vertices[offset + 2] = new Vertex(24 * i, 24 + 24 * j, 0, U(tile, 0), V(tile, 1));
|
||||
vertices[offset + 3] = new Vertex(24 + 24 * i, 24 + 24 * j, 0, U(tile, 1), V(tile, 1));
|
||||
|
||||
List<ushort> indexList;
|
||||
if (!indexMap.TryGetValue(tile.sheet, out indexList))
|
||||
@@ -149,6 +149,8 @@ namespace OpenRa.Game
|
||||
|
||||
spriteHelper = new SpriteHelper(device);
|
||||
fontHelper = new FontHelper(device, "Tahoma", 10, false);
|
||||
|
||||
Clock.Reset();
|
||||
}
|
||||
|
||||
internal void Run()
|
||||
@@ -182,15 +184,16 @@ namespace OpenRa.Game
|
||||
int dx = x1 - e.X;
|
||||
int dy = y1 - e.Y;
|
||||
scrollPos = oldPos;
|
||||
scrollPos.X += (float)dx / 320.0f;
|
||||
scrollPos.Y += (float)dy / 240.0f;
|
||||
scrollPos.X += (float)dx / (ClientSize.Width / 2);
|
||||
scrollPos.Y += (float)dy / (ClientSize.Height / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void Frame()
|
||||
{
|
||||
Clock.StartFrame();
|
||||
device.Begin();
|
||||
device.Clear( Color.Red.ToArgb(), Surfaces.Color );
|
||||
device.Clear( 0, Surfaces.Color );
|
||||
|
||||
vertexBuffer.Bind(0);
|
||||
|
||||
@@ -215,7 +218,7 @@ namespace OpenRa.Game
|
||||
effect.End();
|
||||
|
||||
spriteHelper.Begin();
|
||||
fontHelper.Draw(spriteHelper, "fps: 1337", 0, 0, Color.White.ToArgb());
|
||||
fontHelper.Draw(spriteHelper, "fps: " + Clock.FrameRate, 0, 0, Color.White.ToArgb());
|
||||
spriteHelper.End();
|
||||
|
||||
device.End();
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Clock.cs" />
|
||||
<Compile Include="MainWindow.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user