现在的位置: 首页 > 综合 > 正文

Application Model Overview

2013年09月16日 ⁄ 综合 ⁄ 共 6313字 ⁄ 字号 评论关闭
Application Model Overview

The XNA Framework Game class provides a framework for processing game simulation based on a fixed or variable time interval.

This overview covers the following topics.

Making a New Game

The first step in creating a new game is to make a class that derives from Game. The new class needs to override Update, Draw, and Initialize. The Update method is responsible for handling game logic, and the Draw method is responsible for drawing each frame. The Initialize method is responsible for game setup before the first frame of the game.

很自然的逻辑,从Game派生,然后初始化,绘制,游戏逻辑。

Game Loop Timing

A Game is either fixed step or variable step, defaulting to fixed step. The type of step determines how often Update will be called and affects how you need to represent time-based procedures such as movement and animation.

game loop timing决定了游戏的update频率。分为固定的循环计时和变化循环计时。

Fixed-Step Game Loops

A fixed-step Game tries to call its Update method on the fixed interval specified in TargetElapsedTime. Setting Game.IsFixedTimeStep to true causes a Game to use a fixed-step game loop. A new XNA project uses a fixed-step game loop with a default TargetElapsedTime of 1/60th of a second.

可以通过设置game对象的TargetElapsedTimeIsFixedTimeStep来确定循环周期。默认的是1/60秒.

 

In a fixed-step game loop, Game calls Update once the TargetElapsedTime has elapsed. After Update is called, if it is not time to call Update again, Game calls Draw. After Draw is called, if it is not time to call Update again, Game idles until it is time to call Update.

每隔game loop timing之后(默认的就是1/60秒),game会调用Update方法,之后如果下一个Update方法还未被调用,则调用Draw方法。如果Draw调完了,Update方法还未被调用,则游戏进入idle状态,直到下个游戏时间周期调Update。

 

If Update takes too long to process, Game sets IsRunningSlowly to true and calls Update again, without calling Draw in between. When an update runs longer than the TargetElapsedTime, Game responds by calling Update extra times and dropping the frames associated with those updates to catch up.

如果Update方法的执行时间比游戏时间间隔还要长,则game对象会自动设置IsRunningSlowly为True,并且再次调用Update方法,而不去调用Draw。然后,game对象会分配给Update额外的时间去执行,直到Update方法执行完毕。当然,这样就会发生丢帧现象。

 

This ensures that Update will have been called the expected number of times when the game loop catches up from a slowdown. You can check the value of IsRunningSlowly in your Update if you want to detect dropped frames and shorten your Update processing to compensate. You can reset the elapsed times by calling ResetElapsedTime.

我们可以检查IsRunningSlowly来确定是否会发生Update超时和丢帧的现象。我们可以采取的措施就是缩减Update的处理和重置ResetElapsedTime属性。

 

When your game pauses in the debugger, Game will not make extra calls to Update when the game resumes.

在Debug下,暂停一个游戏,不会再调用Update。

Variable-Step Game Loops

A variable-step game calls its Update and Draw methods in a continuous loop without regard to the TargetElapsedTime. Setting Game.IsFixedTimeStep to false causes a Game to use a variable-step game loop.

设置不定时长的game loop.

Animation and Timing

For operations that require precise timing, such as animation, the type of game loop your game uses (fixed-step or variable-step) is important.

Using a fixed step allows game logic to use the TargetElapsedTime as its basic unit of time and assume that Update will be called at that interval.

使用fixed-step的情形。

 

Using a variable step requires the game logic and animation code to be based on ElapsedGameTime to ensure smooth gameplay. Because the Update method is called immediately after the previous frame is drawn, the time between calls to Update can vary. Without taking the time between calls into account, the game would seem to speed up and slow down.

使用变化的timing的情形。这样使得游戏比较流畅,但是容易出现游戏突然被加速和减速的情况。

 

The time elapsed between calls to the Update method is available in the Update method's gameTime parameter. You can reset the elapsed times by calling ResetElapsedTime.

在Update方法有一个参数表示timing.

 

When using a variable-step game loop, you should express rates—such as the distance a sprite moves—in game units per millisecond (ms). The amount a sprite moves in any given update can then be calculated as the rate of the sprite times the elapsed time. Using this approach to calculate the distance the sprite moved ensures that the sprite will move consistently if the speed of the game or computer varies.

我们使用变化的timing时,需要计算物体的移动比率,例如,每毫秒物体运动了百分之多少,这样可以避免针对不同的PC,各自的处理速度不一样造成游戏快慢不一样。

Starting the Game

Calling the Game.Run method starts a game. This method starts a loop that will call Update and Draw multiple times a second until Exit is called.

启动游戏。

Game Components

Game components provide a modular way of adding functionality to a game. You create a game component by deriving the new component either from the GameComponent class, or, if the component loads and draws graphics content, from the DrawableGameComponent class. You then add game logic and rendering code to the game component by overriding GameComponent.Update,DrawableGameComponent.Draw and GameComponent.Initialize. A game component is registered with a game by passing the component to Game.Components.Add. A registered component will have its draw, update, and initialize methods called from the Game.Initialize, Game.Update, and Game.Draw methods.

这是一种组件思想。就是我们可以构建自己的game component。每个组件负责自己的Initialize,Update,Draw。然后将game component添加到组件库中。由Game对象负责调用各个组件的Initialize,Update,Draw方法。

Game Services

Game services are a mechanism for maintaining loose coupling between objects that need to interact with each other. Services work through a mediator—in this case, Game.Services. Service providers register with Game.Services, and service consumers request services from Game.Services. This arrangement allows an object that requires a service to request the service without knowing the name of the service provider.

通过game service进行解耦。相当于一个服务中心,需要服务的对象直接来获取服务而不需要知道提供服务的对象信息。

 

Game services are defined by an interface. A class specifies the services it provides by implementing interfaces and registering the services with Game.Services. A service is registered by calling Game.Services.AddService specifying the type of service being implemented and a reference to the object providing the service. For example, to register an object that provides a service represented by the interface IMyService, you would use the following code.

Services.AddService( typeof( IMyService ), myobject );    

Once a service is registered, the object providing the service can be retrieved by Game.Services.GetService and specifying the desired service. For example, to retrieve IGraphicsDeviceService, you would use the following code.

IGraphicsDeviceService graphicsservice = (IGraphicsDeviceService)Services.GetService( typeof(IGraphicsDeviceService) );    
如何创建自己的game service.

Game Components Consuming Game Services

The GameComponent class provides the Game property so a GameComponent can determine what Game it is attached to. With the Game property, a GameComponent can call Game.Services.GetService to find a provider of a particular service. For example, a GameComponent would find the IGraphicsDeviceService provider by using the following code.

IGraphicsDeviceService graphicsservice = (IGraphicsDeviceService)Game.Services.GetService( typeof( IGraphicsDeviceService ) );      
game component使用game service。

抱歉!评论已关闭.