XNa BASICS (01)-game loop

Source: Internet
Author: User

After vs 2008 and xNa gamestudio 3.0 are installed, we can start to learn xNa.

First, create an xNa gamestudio 2008 project in vs 3.0 (select the Windows game type) to generate the simplest and runnable game template.

Next we will focus on the game1 class inherited from Microsoft. xNa. Framework. game. The Code is as follows:

Public class game1: Microsoft. xNa. Framework. Game
{
Graphicsdevicemanager graphics;
Spritebatch;

Public game1 ()
{
Graphics = new graphicsdevicemanager (this );
Content. rootdirectory = "content ";
}

Protected override void initialize ()
{
Base. initialize ();
}

Protected override void loadcontent ()
{
Spritebatch = new spritebatch (graphicsdevice );
}

Protected override void unloadcontent ()
{
}

Protected override void Update (gametime)
{
If (gamepad. getstate (playerindex. One). Buttons. Back = buttonstate. Pressed)
This. Exit ();

Base. Update (gametime );
}

Protected override void draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );

Base. Draw (gametime );
}
}

 

Let's briefly explain the important types used in this class.

Graphicsdevicemanager graphical Device Manager, used to access the channels of graphical devices.

Graphicsdevice.

Sprite genie: 2D or 3D images drawn on the screen. For example, a monster in a game scenario is a Sprite.

Spritebatch it uses the same method for renderingGroupSprite object.

 

Microsoft. xNa. Framework. game is like a basic framework that Concatenates the entire game process, that is, the entire game will run as follows.

The five methods in this figure correspond to the five methods in the game1 class. Their functions are described as follows.

The initialize method is used to initialize game-related objects, such as initializing graphics devices and game environment settings.

The loadcontent method is called after the initialize method. It is used to load the graphics or other materials required by the game, such as models, images, and sounds.

The update and draw methods constituteGame loop.

The update method is used to change and control the game status, leading the game logic.

The draw method is used to draw our scenario and Sprite on the screen. It should be noted that the game logic should be processed as little as possible in the draw method-they should be processed in the update method. The draw method is only used for plotting.

The update and draw methods both accept a gametime parameter. What is the role of gametime? This parameter helps us determine the time when an animation or other event occurs based on the actual game time rather than the processor speed.

In this simplest example, the game will run at the default 60fps (frame/second.

The unloadcontent method is called at the end of the game. It is used to unload the materials loaded in the loadcontent method and the aftermath that require special processing.

 

Status polling and event listening

Anyone who has written a Windows application knows that clicking a button on the form triggers a click event, our applications process events by listening to events.

In game development, we need to switch our "Event" thinking to "polling" thinking. That is to say, in the game, users' mouse and keyboard operations do not trigger related events. If so, how do we know if the user has clicked the mouse? The answer is that we need to constantly check the status of input devices (such as the mouse) in the game loop (specifically in the update method) for judgment.

This is the difference between the polling mechanism and the event mechanism. It is also the place where game development and general windows application development need to change their thinking.

In the final analysis, the Windows event mechanism is also an encapsulation of the polling mechanism (that is, Windows message loop.

Today's introduction is here. The next section describes basic rendering-related knowledge.

Finally, the http://creators.xna.com/en-us/xnags_islive of xNa gamestudio 3.0 is attached.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.