[Post] How to read compressed files in the game

Source: Internet
Author: User
We bought a game on the market, and seldom saw a bunch of BMP and PCX resource images in the game directory. Don't they use files like BMP as image resources? They actually package and compress these resource images.

Here are a few examples of Games. The mix file of the famous mongowood "Red Guard" is a compressed package that contains a pile of game images, sounds, and other resources. There are also the. mpq files of "Starcraft", "Diablo" and "Warcraft series" of blizzard, which contain resources used by various games. Almost all games use compressed packages.

So why is this compressed package used? There are many reasons. The first is compression. The images and other resources used in the game are extremely suitable for compression, which reduces the size of the game. If we unbind all the resources in Red Alert 2, we believe it will exceed 3 GB. Second, the reduction of the file size after compression will significantly increase the speed of reading the disk. Generally, a 2 mb bmp file can be compressed to less than kb, and the speed of reading a 2 MB file and reading a K file and then decompressing the file in the memory is quite different. Finally, packaging a large number of resource files can make the game directory clear, instead of a pile of folders and files, which can reduce disk space waste and disk fragmentation.

After talking about the benefits of many compressed packages, how can we use them in the game?
1. We need to define our own compressed package file storage format.
2. Based on 1, we write a compression tool to compress and store Resources in the game.
3. Based on step 1, we can write an decompressed library for use in the game.
4. In the game code, we call this library to read the files in our package.

It seems that we still need to do a lot of complicated work. Define a file structure, write a compression tool, and write a decompressed library. The actual use is indeed the last step, and it is simply to call a library. It seems a little better. Now, we need to find a shortcut to solve the problem of using compressed packages for our games. For example, for 1 and 2, it is very easy. There are a lot of ready-made compression software that can generate compressed packages, such as winzip and winrar, winace ...... It is a very good compression tool, so we do not need to define the storage format of the compressed package, nor need to write a compression tool. Steps 1 and 2 are completed without any effort. What about Step 1? The file storage format is not designed by us. We cannot know how files are organized and compressed, not to mention how to compress them. However, after searching online, we can still get some winzip and winrar decompressed source code. However, it is a pain to study these codes, not to mention using them in your own games.

Here, we have encountered a severe test. Now I will introduce you to the MDFile file system. So what is MDFile? MDFile is an extensible file operation library. It can be used in the simplest way to read files in any compressed package format. So far, we can implement both 3 and 4. The following uses MDFile as an example to illustrate how to use it to read files in our package.

First, obtain the MDFile library we need, download the MDFile library from the Chinese game development technical consultation network, click the original site, find the MDFile and download it.

Put the library in your project and set it up. Here I will not detail how to set up an external library. If not, you can refer to relevant books. Now let's start with our code. Here we take a test.txt file in test.zipas an example:

We add a LoadFile function in the program to load the file in a package.
Bool LoadFile (char * filename, char * packfile)
{
BYTE * buf = NULL; // used to store the content of the read object
__File into file ("ZipExt. dll"); // you can create an instance.
Unzip file.open(unzip test.zip ", true, true); // open the compressed package test.zip.
Export file.locate(“test.txt "); // locate to the test.txt File
Buf = new BYTE [specify file. GetLength ()]; // specify the size of test.txt to allocate memory.
Writable file. Read (buf); // Read the file to our memory
// At this point, the Buf has stored the content of the test.txt file in test.zip.
// Is it easy?
}

Careful friends may have noticed that pai_file using file ("ZipExt. dll ");

What does initialization mean? The extension library zipext.dllis used to open our test.zip file. Because the MDFile itself does not support the Zip format, it supports an internal epk compressed package format. To support Zip, you must obtain ZipExt. dll extension library, If you need Rar support, you need to obtain RarExt. dll extension library. Fortunately, the MDFile package we just downloaded already contains a ZipExt. dll (in Sample \ Sample4 \ Release), or in @ J--S Studio is constantly developing the MDFile extension library, so that we can use more compressed package formats.

It should be noted that, although it is simple to use these ready-made compression software to create file packages, there are also many problems, such as security. Sometimes we don't want others to see the resources used in the game, at this time, if we use the Zip format, it may be easily unlocked and modified by others. In this case, the internal epk format of MDFile is better. It uses dynamic encryption technology and the epk format is not supported by other compression software. In addition, some of its other features are very good. To support the epk format, he also provides a JSFilePacker program to package the required files in the epk format. All of these can be obtained in the MDFile we downloaded. (JSFilePacker is under tools)

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.