(3)-DebugDraw (XNA rendering)

Source: Internet
Author: User

-DebugDraw is a feature provided by the Farseer physical engine. This allows us to draw object data on the screen during the development phase. This will help you visualize the body, fixtures, and other objects. I was wondering why we cannot use the same method in actual applications. In addition, I have not found any reason to prevent myself from doing so. So I got a project. The name is "RenderXNA"
-This project contains three categories: RenderMaterial, Materials, and RenderXNAHelper. The RenderMaterial class is the data structure we need when rendering objects. The Materials class is a helper class that manages loaded textures, so you don't have to worry about repeatedly loading textures. Finally, the RenderXNAHelper class is derived from the FarseerPhysiscs. DebugView class and the IDisposable interface. We use this class to draw, rather than writing our own code. If you read the code carefully, you will find many methods, such as DrawJoint, DrawShape, DrawPolygon, DrawCircle, and DrawTexturedLine. These methods can actually draw our objects. RenderXNAHelper uses World. BodyList to obtain all the bodies that can be drawn.

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:

-RenderXNAHelper is derived from the DebugView of the DebugViewXNA. cs file in the Farseer physical engine code. I changed it to use resources other than projects. This can save us a lot of time.
-OK. Now it is time for the RenderXNA project to display an example. In the FarseerXNADemo1 project, you can find RenderXNA. dll; I provide all the source code that will be downloaded in the last article.
-We also need the Camera2D class. I have found its code in an example. It really looks like a real camera. We don't have to carefully analyze its implementation, and we can use it directly (even I don't know how to implement it ). In the Camera2D class, several very useful methods are ConvertScreenToWorld and ConvertWorldToScreen. These methods convert the screen point (actual point) to the point on the World (system), followed by a positive and negative. We also have projection and view attributes of the matrix type, which are used to represent the projection and view of the current camera respectively. This class also contains the Update function called by the Update method of the xna Game class. We need to remember the knowledge about the Camera2D class above.

-Now let's start with some simple implementation code. Add a new Game2 class that inherits from the Microsoft. Xna. Framework. Game class. Its construction is similar to the previous Game1 class of our project. Variables of the same class are also declared in the Game2 class (the same as in the Game1 class ). In addition, we need more variables of the Camera2D and RenderXNAHelper types. We need three functions: LoadContent, Update, and Draw. Therefore, we need to reload them first. Initialize the Camera2D and RenderXNAHelper objects in LoadContent. First, copy the code in all LoadContent methods in the Game1 class, and add the following code after LoadContent creates the object world.

//Create DebugView and switch on the Flags to render shapes.RenderHelper = new RenderXNAHelper(MyWorld);RenderHelper.AppendFlags(DebugViewFlags.TexturedShape);RenderHelper.RemoveFlags(DebugViewFlags.Shape);RenderHelper.DefaultShapeColor = Color.White;RenderHelper.SleepingShapeColor = Color.LightGray;RenderHelper.LoadContent(GraphicsDevice, Content);Camera = new Camera2D(GraphicsDevice);

-We also need to add texture details to the physical object itself, because now they will be rendered by RenderXNAHelper. Fortunately, we have an object-type parameter in the CreateRectangle method of FixtureFactory (almost all factory methods use this method and name the parameter UserData ). Just like the Tag attribute of the. NET control. Therefore, when we use the RenderMaterial object, we can specify its texture, color, and other details. The sample code is as follows.

new RenderMaterial(MyTexture, "Blank") { Color = Color.White }

  

-The code above will mask our texture with white (where white is, the texture will be drawn as the original image, not the white part, the texture will be masked and merged by the color of the current part and the color of the image)
-Pass in the new RenderMaterial as the final parameter of FixtureFactory. CreateRectangle In the fixture of the floor and box.

protected override void Update(GameTime gameTime){    // Allows the game to exit    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)        this.Exit();    if (MyWorld != null)    {        // Update the camera        Camera.Update();        // variable time step but never less then 30 Hz        MyWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));        RenderHelper.Update(gameTime);    }    base.Update(gameTime);}protected override void Draw(GameTime gameTime){    GraphicsDevice.Clear(Color.CornflowerBlue);    if (MyWorld != null)    {    //Render the Data        RenderHelper.RenderDebugData(ref Camera2D.Projection, ref Camera2D.View);    }    base.Draw(gameTime);}

-To run this new game class, we need to set the project property to the Startup type.

-In addition, we need to add default resources for fonts and rows.

-You should note that we didn't process all the drawing tasks in the Draw method. On the contrary, we transferred part of the painting work to the RenderXNAHelper class. This will solve most of the problems of rendering objects on the screen.
This section summarizes how to create physical objects and set textures, colors, and other details. The complete sample code for FarseerXNADemo1 will be provided in the following sections.

 

Please pay attention to the next section:

Learn more about the underlying principles of windowsphone7 games

I will release all source code and instances in the last section without reservation.

I hope you like my article! If you have more ideas, 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!


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.