Game Loop Tutorial
Game
In this article I talk about the basic high-level structure of a typical Managed Direct3D application. Most Direct3D applications will have code that looks something like this.
Let's face it: most people that are using Direct3D are probably doing it because they want to write a game. So it makes sense that the typical, top-level structure of a Managed Direct3D application would be called the "Game Loop". (I stole the term from the mediocre book The Zen of Direct3D Game Programming.) This code or a variant of it will appear in many Managed Direct3D applications, including the sample code that Microsoft ships with the DirectX SDK . The code looks something like this:
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Craig.Samples.Direct3D
{
public class Game : System.Windows.Forms.Form
{
static void Main ()
{
Game app = new Game();
app.InitializeGraphics();
app.Show();
while (app.Created)
{
app.Render();
Application.DoEvents();
}
app.DisposeGraphics();
}
}
}
Straightaway you can see it's strikingly different from a "normal" WinForms application. It certainly blew me away to see a tight loop - the while (app.Created) bit - in a graphical application. I'm much more used to seeing the a call to Application.Run, which waits for events like mouse or keyboard activity to happen, calls any handlers I might have set up, and exits when my program goes away. Let's walk through the code.
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
This bit is fairly obvious. We're going to be using stuff from these namespaces, so let's save ourselves having to type them all the time. Note that what we don't see is that we have to have the DirectX SDK installed, and we have to have a reference to both Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll.
namespace Craig.Samples.Direct3D
{
public class Game : System.Windows.Forms.Form
{
The preceding code is actually fairly interesting, because it shows that we're actually writing a class that derives from System.Windows.Forms.Form, just like all the other WinForms apps you've ever written. So all the stuff you know about Windows Forms still applies, we just have to learn how to integrate the Direct3D bits.
static void Main ()
{
Game app = new Game();
app.InitializeGraphics();
app.Show();
Here's where we create a new instance of the form (remember,
while (app.Created)
{
app.Render();
Application.DoEvents();
}
This is the meat of the application, where it will spend most of its time. What we're doing here is checking to see if the form has been closed, and if not, calling our Render method. Render is where all the cool 3D drawing takes place, and I'll talk about it in detail in a later article (stay tuned!). The call to Application.DoEvents
is to make sure that any pending Windows events get processed - like I said, we're writing a Windows Forms application, so we can't get away with not servicing the message pump.
The reason that we do it this way, rather than - say - setting a timer and rendering when it goes off - is that our game probably wants as much CPU as it can get. So we do all our rendering in a tight loop rather than take the usual, "Oh, I'll only do something when a windows event happens" approach. But of course we still have to process messages from Windows.
app.DisposeGraphics();
Of course we need to clean up when we're done, so we provide a DisposeGraphics method to get called whenever someone shuts down our application. We'll talk about this more later, too.
You can go ahead and compile this code - it should run just fine if you provide do-nothing InitializeGraphics, Render, and DisposeGraphics methods. Of course, it'll just be a blank form. To do anything interesting, we need a Direct3D Device, which we'll talk about next time.
张志敏所有文章遵循创作共用版权协议,要求署名、非商业 、保持一致。在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处。
本博客已经迁移到 GitHub , 围观地址: https://beginor.github.io/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架