これは私のprogram.csファイルです(これはすべてC#のVS 2010で行われます)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace GameLoop
{
static class Program
{
static FastLoop _fastLoop = new FastLoop(GameLoop);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void GameLoop(double elapsedTime)
{
//GameCode goes here
//Get Input
//Process
//Render
System.Console.WriteLine("loop");
}
}
}
これは私のFastLoop.csファイルです(これはすべてVS 2010のC#で行われます)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace GameLoop
{
[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public Int32 msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
public class FastLoop
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(
out Message msg,
IntPtr hWnd,
uint messageFilterMin,
uint messageFilterMax,
uint flags);
PreciseTimer _timer = new PreciseTimer();
public delegate void LoopCallback(double elapsedTime);
LoopCallback _callback;
public FastLoop(LoopCallback callback)
{
_callback = callback;
Application.Idle += new EventHandler(OnApplicationEnterIdle);
}
void OnApplicationEnterIdle(object sender, EventArgs e)
{
while (IsAppStillIdle())
{
_callback(_timer.GetElapsedTime());
}
}
private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
}
これは私のPerciseTimer.csファイルです(これはすべてVS 2010のC#で行われます)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace GameLoop
{
public class PreciseTimer
{
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernal32")]
private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("kernal32")]
private static extern bool QueryPerformanceCounter(ref long PerformanceCount);
long _ticksPerSecond = 0;
long _previousElapsedTime = 0;
public PreciseTimer()
{
QueryPerformanceFrequency(ref _ticksPerSecond);
GetElapsedTime();//get rid if first rubbish result
}
public double GetElapsedTime()
{
long time = 0;
QueryPerformanceCounter (ref time);
double elapsedTime = (double)(time -_previousElapsedTime)/(double)_ticksPerSecond;
_previousElapsedTime = time;
return elapsedTime;
}
}
}
これは私のform1.csファイルです(これはすべてVS 2010のC#で行われます)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GameLoop
{
public partial class Form1 : Form
{
bool _fullscreen = true;
public Form1()
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
if (_fullscreen)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void simpleOpenGlControl1_Load(object sender, EventArgs e)
{
}
}
}