XNa 3.0 preliminary -- use gamecomponents

Source: Internet
Author: User
1.7 gamecomponents

You want to applyProgramSeparated into gamecomponent class for Component reuse.

Solution

A specific part of the program can be separated from the program. In an xNa program, most parts of this type need to be updated and drawn, such as particle systems and billboard (usually translated as announcement boards or billboards) system (see tutorials 3-11 and 3-12 ).

The correct method is to create a separate class for these specific parts. In the xNa game class, create an instance of this class and initialize, update, and draw it. So you want your new class to have its own initialize, (UN) loadcontent, update, and draw methods, which makes it easy for you to call them in the game class.

If you want to define these methods in this new class, the best method is to inherit from the gamecomponent class. You can add this class to the components set of the game class, so that after the game class initialization is complete, the initialize method of the gamecomponent class will be called (if you have already defined one ). In addition, the update method of gamecomponent is automatically called after each update method of the game class is called.

If your component has something to draw, you can inherit from the drawablegamecomponent class rather than the gamecomponent class, so that the component class will contain a draw method, it will be called after the draw method of the game class.

Note:At the end of the initialize method of the game class, base. initialize is called.CodeThe initialize method of all the gamecomponent classes is called. In other methods, you can also see base. Update (gametime) and base. Draw (gametime ).

Working Principle

For example, the billboarding code in tutorial 3-11 is encapsulated in a gamecomponent. The drawablegamecomponent class is used because the code still needs to be drawn.

Create a new (drawable) gamecomponent

Right-click the project and select Add → new file to add a new class file. Then, select class and name it billboardgc. Add the xNa using code line to the new file. You only need to copy the game class using to this new class.

The next step is to ensure that the new class inherits from the gamecomponent class or the drawablegamecomponent class, as shown in the following code. The code shows how to create billboarding for tutorial 3-11. Some of the Code, such as createbbvertices, is not written, because the initialize, loadcontent, update, and draw methods are concerned in this tutorial.

Class billboardgc: drawablegamecomponent {private graphicsdevice device; private basiceffect; private texture2d mytexture; private vertexpositiontexture [] billboardvertices; private vertexdeclaration myvertexdeclaration; private list <vector4> billboardlist = new list <vector4> (); Public vector3 camposition; Public vector3 camforward; Public matrix viewmatrix; Public matrix projectionmatrix; Public billboardgc (Game ): base (game) {} public override void initialize () {Device = game. graphicsdevice; base. initialize ();} protected override void loadcontent () {basiceffect = new basiceffect (device, null); mytexture = game. content. load <texture2d> ("billboardtexture"); addbillboards (); myvertexdeclaration = new vertexdeclaration (device, vertexpositiontexture. vertexelements);} public override void Update (gametime) {createbbvertices (); base. update (gametime );}... public override void draw (gametime) {// draw billboards ...}}

Note:In the initialize method, your component can obtain the game class, so that component can obtain the public fields of the game class, such as game. graphicsdevice and game. content.

Use gamecomponent

After defining the gamecomponent class, you can add it to the gamecomponent set of the game class. Once added, the attention method of gamecomponent will be automatically called. The simplest way is to create a gamecomponent instance and add it to the components set immediately. The best position is in the game class constructor.

 
Public game1 () {graphics = new graphicsdevicemanager (this); content. rootdirectory = "content"; components. Add (New billboardgc (this ));}

In this way, the initialize and loadcontent methods can be called at the beginning, and the update and draw methods of the new class can be called after the update and draw methods of the game class are completed. In some cases, you need to update the public variables of some components. For example, in the billboarding component, you need to update the camposition and camforward variables to adjust the view and projection matrices so that they can be correctly drawn to the screen. Therefore, you should add a component variable to the game class.

 
Billboardgc;

Then store the component and add it to the components set:

Public game1 () {graphics = new graphicsdevicemanager (this); content. rootdirectory = "content"; billboardgc = new billboardgc (this); components. Add (billboardgc );}

In the update method of the game class, you can update four variables in the component, call the update method of all components at the end of the update method, and let the billboarding component update:

 
Protected override void Update (gametime ){... billboardgc. camforward = quatcam. forward; billboardgc. camposition = quatcam. position; billboardgc. viewmatrix = quatcam. viewmatrix; billboardgc. projectionmatrix = quatcam. projectionmatrix; base. update (gametime );}

The draw method of the game class is simpler, but the background is cleared before calling the draw method of all components:

 
Protected override void draw (gametime) {Device. Clear (clearoptions. Target | clearoptions. depthbuffer, color. cornflowerblue, 1, 0); base. Draw (gametime );}

The last line of code calls the draw method of the billboarding component to draw billboards to the screen.

Code

Code for the initialize, loadcontent, update, and draw methods of the gamecomponent and game classes has been written before.

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.