博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Game Loop Tutorial
阅读量:5837 次
发布时间:2019-06-18

本文共 3436 字,大约阅读时间需要 11 分钟。

Game Loop Tutorial

In this article I talk about the basic high-level structure of a typical Managed  application. Most  applications will have code that looks something like this.

Let's face it: most people that are using  are probably doing it because they want to write a game. So it makes sense that the typical, top-level structure of a Managed  application would be called the "Game Loop". (I stole the term from the mediocre book The Zen of  Game Programming.) This code or a variant of it will appear in many Managed  applications, including the sample code that Microsoft ships with the . 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  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  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 .

 

所有文章遵循,要求署名、非商业 、保持一致。在满足的基础上可以转载,但请以超链接形式注明出处。

本博客已经迁移到 GitHub , 围观地址: 

本文转自张志敏博客园博客,原文链接:http://www.cnblogs.com/beginor/archive/2006/07/21/456548.html
,如需转载请自行联系原作者
你可能感兴趣的文章
在51CTO三年年+了,你也来晒晒
查看>>
js控制图片等比例缩放
查看>>
Java高级开发工程师面试考纲
查看>>
FreeMarker表达式
查看>>
Debian9.2 下使用vnstat查看服务器带宽流量统计
查看>>
NGINX + PHP-FPM 502
查看>>
mysql数据备份与恢复
查看>>
Openstack API常用命令
查看>>
OpenSSL漏洞凶猛来袭 慧眼恶意代码监测应对有方
查看>>
C语言 喝汽水问题
查看>>
LINUX中搭建DNS服务器,实现正向、反向以及访问不同DNS解析
查看>>
SCCM2012 R2实战系列之十:解决WDS服务无法启动问题(错误1067:进程意外终止)...
查看>>
怎么防止重复发送Ajax
查看>>
ubuntu 下安装 mysql
查看>>
关于k-means聚类算法的matlab实现
查看>>
Git分支2
查看>>
一键安装Gitlab后的备份、迁移与恢复
查看>>
因为本人工作繁忙,精力有限,本博客停止更新。有兴趣的博友可以关注我在CSDN上的主博客...
查看>>
三元表达式,推导式,递归,匿名函数,内置函数
查看>>
SQL server查看触发器是否被禁用
查看>>