git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1140 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
chrisf
2007-07-07 09:06:40 +00:00
parent 27c74b1ef1
commit 761b01a618
2 changed files with 68 additions and 76 deletions

View File

@@ -1,109 +1,75 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
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>
[DllImport("kernel32.dll")]
static extern bool QueryPerformanceCounter(out long value);
[DllImport("kernel32.dll")]
static extern bool QueryPerformanceFrequency(out long frequency);
static int frameCount = 0;
static long frequency;
static long lastTime;
static double frameTime = 0;
static double totalTime = 0;
const int FramerateUpdateFrequency = 10;
static int lastFrameRate = 0;
static int lastFrameCount = 0;
static double nextFrameRateUpdateTime = 0;
static Clock()
{
startTime = System.Environment.TickCount;
StartFrame();
QueryPerformanceFrequency(out frequency);
QueryPerformanceCounter(out lastTime);
}
public static void Reset()
{
frameCount = 0;
lastFrameCount = 0;
lastFrameRate = 0;
startTime = System.Environment.TickCount;
StartFrame();
totalTime = 0;
}
#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()
public static void StartFrame()
{
int ticks = System.Environment.TickCount - startTime;
frameStartTime = ticks / 1000.0;
long time;
QueryPerformanceCounter(out time);
frameTime = (double)(time - lastTime) / (double)frequency;
totalTime += frameTime;
lastTime = time;
frameCount++;
if( Time > nextFrameRateUpdateTime )
if (totalTime > 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 OldFramerateWeight = 20;
const int NewFramerateWeight = 1;
int newFrameRate = (frameCount - lastFrameCount) * FramerateUpdateFrequency;
lastFrameRate = (lastFrameRate * OldFramerateWeight + NewFramerateWeight * newFrameRate) / (OldFramerateWeight + NewFramerateWeight );
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
public static double Time
{
get { return frameStartTime; }
get { return totalTime; }
}
/// <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; }
get { return frameTime; }
}
#endregion
}
}

View File

@@ -73,7 +73,7 @@ namespace OpenRa.Game
float u0 = (float)s.origin.X / (float)s.sheet.bitmap.Width;
float u1 = (float)(s.origin.X + s.size.Width) / (float)s.sheet.bitmap.Width;
return (1 - u) * u0 + u * u1;
return (u > 0) ? u1 : u0;// (1 - u) * u0 + u * u1;
}
float V(SheetRectangle<Sheet> s, float v)
@@ -81,13 +81,13 @@ namespace OpenRa.Game
float v0 = (float)s.origin.Y / (float)s.sheet.bitmap.Height;
float v1 = (float)(s.origin.Y + s.size.Height) / (float)s.sheet.bitmap.Height;
return (1 - v) * v0 + v * v1;
return (v > 0) ? v1 : v0;// return (1 - v) * v0 + v * v1;
}
void LoadVertexBuffer()
{
Dictionary<Sheet, List<ushort>> indexMap = new Dictionary<Sheet, List<ushort>>();
Vertex[] vertices = new Vertex[4 * 128 * 128];//map.Width * map.Height];
Vertex[] vertices = new Vertex[4 * 128 * 128];
for( int i = map.XOffset; i < map.XOffset+ map.Width; i++ )
for (int j = map.YOffset; j < map.YOffset + map.Height; j++)
@@ -131,7 +131,9 @@ namespace OpenRa.Game
Visible = true;
device = GraphicsDevice.Create(this, ClientSize.Width, ClientSize.Height, true, false);
//device = GraphicsDevice.Create(this, ClientSize.Width, ClientSize.Height, true, false);
device = GraphicsDevice.Create(this, 1280, 800, false, false);
IniFile mapFile = new IniFile(File.OpenRead("../../../" + mapName));
map = new Map(mapFile);
@@ -189,6 +191,22 @@ namespace OpenRa.Game
}
}
static T First<T>(IEnumerable<T> src)
{
IEnumerator<T> enumerator = src.GetEnumerator();
return enumerator.MoveNext() ? enumerator.Current : default(T);
}
static T Nth<T>(IEnumerable<T> src, int n)
{
IEnumerator<T> enumerator = src.GetEnumerator();
bool ok = false;
while (n-- > 0)
ok = enumerator.MoveNext();
return ok ? enumerator.Current : default(T);
}
void Frame()
{
Clock.StartFrame();
@@ -225,6 +243,14 @@ namespace OpenRa.Game
device.Present();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.C)
Clock.Reset();
}
TileSet LoadTileSet(Map currentMap)
{
switch (currentMap.Theater.ToLowerInvariant())