Game Loop Tutorial

简介:

Game Loop Tutorial

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 theDirectX SDK . The code looks something like this:

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.

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.

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.

Here's where we create a new instance of the form (remember, Main is static, so there's no instance yet), and then call InitializeGraphics, which is a method that we write. We'll look at it in a future article. The call to app.Show() just makes the form visible.

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 toApplication.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.

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 , 围观地址: http://beginor.github.io/

本文转自张志敏博客园博客,原文链接:http://www.cnblogs.com/beginor/archive/2006/07/21/456548.html ,如需转载请自行联系原作者
相关文章
|
3月前
|
Python
[HDCTF 2023]fake_game
[HDCTF 2023]fake_game
31 0
|
算法 索引
LeetCode 45. Jump Game II
给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。
76 0
LeetCode 45. Jump Game II
|
算法 索引
LeetCode 55. Jump Game
给定一个非负整数数组,您最初定位在数组的第一个索引处。 数组中的每个元素表示该位置的最大跳转长度。 确定您是否能够到达最后一个索引。
88 0
LeetCode 55. Jump Game
|
存储 算法
LeetCode 289. Game of Life
如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡; 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活; 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡; 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
73 0
LeetCode 289. Game of Life
|
人工智能
jump game
最后一步:如果青蛙能跳到最后一块石头n-1,我们考虑他跳的最后一步,这一步是从石头i跳过来,i<n-1 这需要两个条件同时满足: 1.青蛙可以调到石头i; 2.最后一步不超过跳跃的最大距离:n-1-i <= ai
114 0
jump game
|
人工智能
|
人工智能 算法 大数据