Here's a Frame Rate Limiter I hacked together just now as a Custom Object.
All you need to do is call CustomObjects.FPSLimiter.RegisterLimit with an argument of how many FPS to limit to. This only needs to be called once, in an initialization loop, for example. However, if you do call it every frame, it will have no adverse effects.
Call CustomObjects.FPSLimiter.UnregisterLimit to remove the limit.
To change the FPS limit, you can either re-register with a new FPS (no need to unregister first) or set CustomObjects.FPSLimiter.FPS to whatever value you want to limit to.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace CustomObjects
{
public class FPSLimiter
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool QueryPerformanceFrequency(out long frequency);
private static long freq = -1;
private static long pCount = -1;
private static long delay = -1;
private static uint framerate = 0;
private static GameForm.SimpleNotification waiter = null;
[Description("Call once to begin limiting the framerate to the specified value.")]
public static void RegisterLimit(uint FPS)
{
if (null == waiter)
{
waiter = new GameForm.SimpleNotification(Wait);
Project.GameWindow.OnBeforeBeginScene += waiter;
}
if (framerate != FPS || delay == -1)
FPSLimiter.FPS = FPS;
}
[Description("Call once to remove all framerate limits.")]
public static void UnregisterLimit()
{
Project.GameWindow.OnBeforeBeginScene -= waiter;
waiter = null;
}
public static uint FPS
{
get
{
return (null == waiter ? 0 : framerate);
}
set
{
if (value == 0)
throw new Exception ("Cannot limit framerate to 0 FPS");
if (value != framerate)
{
framerate = value;
init();
}
}
}
private static void init()
{
if (freq == -1)
{
if (!QueryPerformanceFrequency(out freq))
throw new Exception("Couldn't get performance frequency for frame rate");
}
delay = (long)(1f/(float)FPS*(float)freq);
if (pCount == -1)
if (!QueryPerformanceCounter(out pCount))
throw new Exception("Couldn't get performance count for frame rate");
else
return;
}
private static void Wait()
{
Project.GameWindow.debugText.Write(FPS.ToString());
long newCount = 0;
QueryPerformanceCounter(out newCount);
while (newCount < pCount + delay)
{
System.Windows.Forms.Application.DoEvents();
QueryPerformanceCounter(out newCount);
}
pCount = newCount;
}
}
}
--Edit--
I have submitted the file to sgdk2.enigmadream.com for distribution. By the by, BlueMonk, I think there needs to be an area for Code objects specifically, and also an easy way to view details about and download files from that website within the SGDK2 IDE.