Manage resources in xNa

Source: Internet
Author: User

In xNa, The resourcepool Enumeration type is changed from four in mdx to two, namely, automatic and manual, and the enumeration name is changed to resourcemanagmentmode. In this example, automatic corresponds to managed in mdx and manual corresponds to default.

Resources created in manual mode are stored in video memory or AGP memory. Resources of the automatic type are stored in the system memory and automatically copied to the video memory when needed. By default, all resources in xNa are stored in the automatic mode. The difference between the two is that because manual resources are allocated in the video memory, the data in the video memory is lost every time the device is reset, so you need to manually reload the data; because resetting does not change the properties of the device, the resources stored in the system memory are still available and xNa will automatically manage these resources. Here, operations that will cause device resetting usually include changing the window size, minimizing the window, and directly switching between the window and full screen mode. Resources created in manual mode usually have better performance. xNa puts these resources in the most favorable location for device access. However, if you need to reset the device frequently, you must load these resources repeatedly. Of course, for Xbox, device resetting rarely happens. As recommended by xNa team, use the automatic mode to create resources as much as possible.
When writing a program, you should separate the code for creating the automation and manual resources:

Loadautomaticresource ();
Loadmanualresource ();

When the devicecreate event is triggered, the two methods should be called at the same time:

Private void graphics_devicecreated (Object sender, eventargs E)
{
Loadautomaticresource ();
Loadmanualresource ();
}
Graphicsdevice. Reset + = new system. eventhandler (ondevicereset)

When a reset event is triggered, you only need to call the loadmanualresource () method.

In the xNa game framework, you do not need to directly connect to the event processing code. The loadgraphicscontent method of the game class simplifies this process.

When you create and reset a device, the loadgraphicscontent method is automatically called and the code to be created is selected based on the loadallcontent flag. Therefore, you only need to place the code for loading resources in the appropriate position:

Protected override void loadgraphicscontent (bool loadallcontent)
{
If (loadallcontent)
{
// Todo: load any resourcemanagementmode. Automatic Content loadautomaticresource ();

}
// Todo: load any resourcemanagementmode. Manual content loadmanualresource ();

}

In addition, for resources loaded with content pipeline, you do not need to write the dispose method for these resources. Content pipeline manages these resources for us. To release these resources, you only need to call the unload () method.

Content. Load <model> ("terrain"); // load the terrain model and texture if the model have a texture
Content. Load <model> ("House ");
// Do something with terrain and house
Content. Unload ();

Content pipelineduring the compilation process, all the resource files (such as .jpg,. fbx,. X,. TGA) are repackaged as. xNb binary files with the extension name. For model loading, content pipeline automatically loads the required textures without displaying the required textures. However, ensure that these textures are in the same directory as the model files, or other content pipelines. When a model is loaded, if different models use the same texture, the texture is loaded only once. Assume that we have compiled the dispose method for resources created using content pipeline. For different models that use the same texture, one of the models of dispose will cause some problems.

Note: a program can create multiple Content pipeline objects. For example, we can create one for each level so that we can load all resources when creating a level, when leaving, ensure that all resources are cleared.
In general, xNa greatly simplifies the difficulty of loading and managing resources, allowing us to focus more on gameplay ^

 

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.