[Translation] wp7 game source code plus Analysis

Source: Internet
Author: User

The Windows mobile operating system has two programming models developed on the user interface: Silverlight and XNA.

Silverlight provides a relatively rapid system development model for business application development. The wide variety of user interface controls in Silverlight and the ever-growing third-party control market allow us to quickly design diverse user interfaces that serve our business applications. If you want to create Windows Phone 7.5 For the Silverlight application, download "Windows Phone Silverlight toolkit" http://silverlight.codeplex.com /.

 

Here, I have to thank the author who has always supported me for writing such an article with interest. I would like to thank the author again for a very good wp7 Development Forum, I will also post several high-quality articles to you later. Please contact me on the noodles.

Enter the subject:

 

XNA enables us to use DirectX 9 to speed up graphics output. For example, you can play a video, move a graphic rendering task from the CPU to the GPU, and release CPU resources to handle other things.

 

When we use the originally released Windows Phone development, we cannot select from Silverlight and XNA. In Windows Phone 7.5, the Silverlight application can be embedded into the XNA model, and the XNA application can also be embedded into the Silverlight user interface elements-a huge progress.

 

Analysis game

When we use XNA on Windows Phone, it is very important that we firmly learn the basics.

XNA Game Studio 4.0 adds many templates to the Visual Studio project. We want to use a template called XNA Game Studio 4.0 (4.0, it is located in "C #-> XNA Game Studio 4.0 (4.0 ). I named the project "AnatomyOfAGame" and renamed the generated "Game1" class "GameSkeleton".

We really need to understand the generated code and want to know how the framework works for us.

public class GameSkeleton : Microsoft.Xna.Framework.Game{

The "Game" Class of XNA Framework implements Game loops. It provides a window that shows our Game and also contains a virtual method, enables communication between our games and the XNA framework.

GraphicsDeviceManager graphics;

The "GraphicsDeviceManager" feature enables us to manage the configuration of graphics devices.

SpriteBatch spriteBatch;

  

"SpriteBatch" lets us draw textures in the form of genie. The sprite can directly draw the rendering target through pipeline conversion, lighting or effects ,. It allows us to use the genie alone to create some pretty good games ,.

The generated code structure is quite simple:

public GameSkeleton()  {    graphics = new GraphicsDeviceManager(this);

The first step is to create "GraphicsDeviceManager", and then we need to tell "ContentManager" where to find the content.

Content.RootDirectory = "Content";

  

"Content" attributes allow you to access "ContentManager". This is the binary file that is responsible for loading the content project located in the "Management object AnatomyOfAGameContent.

"Content" refers to the content generated during design. It usually refers to content that removes code, such as bitmaps, models, and sounds. Content is added to the project through Content Pipeline, which is called "Content Pipeline" because it will be applied to game resources during game build, rootDirectory "tells" ContentManager "where to find those resources.

// Windowsphone with a frame rate of 30 fps by default. TargetElapsedTime = TimeSpan. FromTicks (333333 );

TargetElapsedTime refers to the time of a fixed game loop. In a fixed-step game, "update" is called regular execution. The interval between each execution is TargetElapsedTime. before calling the draw function, the game checks whether it is time to call update, when draw is complete, the game will remain in the idle State until the next update needs to be called.

 

InactiveSleepTime = TimeSpan.FromSeconds(1);}

  

 protected override void Initialize()  {    base.Initialize();  }

During initialization, we should all think about any services we want and load non-graphical resources. This is important because we call the Initialize of the base class, because it ensures that Initialize can add all game Components to the Components set. Initialize will be called before the game enters the update/draw Loop

 protected override void LoadContent()  {    // Create a new SpriteBatch, which can be used to draw textures.    spriteBatch = new SpriteBatch(GraphicsDevice);  }

  

"The LoadContent method is called during initialization. When a DeviceReset event occurs, the game content must also be reloaded. This function will also be called.

 protected override void UnloadContent()  {  }

UnloadContent is called when the graphic resources need to be detached, that is, the graphics resources of any specific game should be detached here.

protected override void Update(GameTime gameTime)  {    // Allows the game to exit    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)    {      this.Exit();    }    base.Update(gameTime);  }

  

Update is where all the most interesting things are handled. Moving Objects, checking collisions, and shaking mobile phones... All in all: where we are dealing with game logic

The parameter "gameTime" provides three timing information attributes:

public TimeSpan ElapsedGameTime { get; }

The time of the last updated game.

public bool IsRunningSlowly { get; }

IsRunningSlowly indicates whether the game loop is running for a longer time than TargetElapsedTime. In this case, our game needs to handle related delays, because it can give users a smoother gaming experience.

 public TimeSpan TotalGameTime { get; }

  TotalGameTimeSpecifies the time when the game starts.

 protected override void Draw(GameTime gameTime)  {    GraphicsDevice.Clear(Color.DarkKhaki);    // TODO: Add your drawing code here    base.Draw(gameTime);  }}

When the Framework draws a frame, draw will be called by the XNA framework. by calling the draw method of the base class, we can ensure that the game components are drawn.


Now, we haven't created anything, but at least we now know how the framework works.

XNA WP7 Farseer Engine

Before proceeding, I would like to introduce you to the Farseer physical engine. You can download it from http://farseerphysics.codeplex.com. Farseer physical engine is an open-source NET 2D physical engine that can be used for commercial projects. This is pretty cool.

The solution in this article includes the original, almost unchanged, Windows Phone Farseer physical engine 3.3.1. The only change I made was to update Windows Phone software development kit (SDK) 7.1. To do this, click the "upgraded Windows Phone project" menu on the project menu.

Now we have a physical engine that makes it easier for us to make interesting games.

XNAImage-XNA framework Image Processing Module

When you start to play with the XNA framework and Farseer physics, the wizard created during the runtime is obviously faster. The HarlinnXNA project includes an "XNAImage" class, which will help us create games with high operational efficiency.

This class allows us to draw lines, circles, splines, betiller curves, and render shapes that many of our games need to present.

The XNAImageIntro project demonstrates how XNAImage can be used to draw images that can be rendered at runtime. XNAImage is based on the WriteableBitmapEx project and supports all the WriteableBitmapEx image processing functions. This means that we can use the texture created by the library at runtime, and then use the generated image for texture.

 texture = new Texture2D(GraphicsDevice,                  rect.Width, rect.Height, false, SurfaceFormat.Color);  textureImage = new XNAImage(rect.Width, rect.Height, Color.CornflowerBlue);

  

When we create a Texture2D object, we need to use XNAImage. -We must specify the SurfaceFormat. Color format to match the pixel format of the texture with the XNAImage pixel format.

Now that we have created our XNAImage object, we can start to draw the image directly:

Color color = Color.Green;  textureImage.FillEllipseCentered(480/3, 275, 75, 75, color);  color = Color.Blue;  textureImage.FillEllipseCentered(480*2/3, 275, 75, 75, color);

We can create another image and fill in Perlin Noise:

XNAImage noisyImage = new XNAImage(200, 200); noisyImage.PerlinNoise(1.2, 0.025, 0.9, 3, 5);

Perlin Noise is useful when we want to create effects in our games, such as flames, smog, and clouds ., However, these images can be quickly scaled to such a target image:

 Collapse | Copy Code  textureImage.Blit(new Rectangle(140, 400, 200, 100),             noisyImage, new Rectangle(0, 0, 200, 100),BlendMode.Subtractive);  textureImage.Blit(new Rectangle(140, 500, 200, 100),             noisyImage, new Rectangle(0, 100, 200, 100));

The first sentence is the blit operation, which uses both the source image and the target image to reduce the color, while the second sentence only covers the target rectangle. Once we complete our image, we assign it to the Texture2D object:

 texture.Assign(textureImage);

  

XNAImage is still under development, but it has provided many useful game UI APIs:

Bits
FillRectangle
FillEllipse
FillEllipseCentered
FillPolygon
FillQuad
FillTriangle
FillBeziers
FillCurve
FillCurveClosed
Convolute-apply image filters
PerlinNoise
DrawLineBresenham
DrawLineDDA
DrawLine
DrawLineAa
DrawPolyline
DrawTriangle
DrawQuad
DrawRectangle
DrawEllipse
DrawEllipseCentered
Drawbezr
DrawBeziers
DrawCurveSegment
DrawCurve
DrawCurveClosed
Crop
Resize
Rotate
RotateFree
Flip

 

I hope you like it! If you have something to say, please contact me in the Q & a area of the wp7 Development Forum (codewp7.com). I will be glad to know what you are thinking. At the same time, I can also find my figure in wp7 chat QQ Group 172765887. Thank you!

Click Download all source code

 



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.