top of page
Writer's picturewu iris

Load Binary Files

Downloadable

Controls:

Exit: ESC or click X

Pause/Resume: Space (hold)

Hide Mesh: Shift (hold)

Change Effect: Ctrl (hold)

Change Mesh: Enter (hold)

Move Object: Up/Down/Left/Right (hold)

Move Camera: W/A/S/D (hold)

Rotate Camera: Q/E (hold)


Key Points

Build Binary Files

In the last assignment, we copied human-readable files and loaded them in mesh representation. But this time, we need to load these files in MeshBuilder and write binary files into the destination folder instead. Binary files definitely save time and space at run-time. They are much smaller than human-readable files, so they occupy smaller space. It takes a lot less time to load them, and we can also skip the analyzing part and use the loaded file directly at run-time.

Take my cat.mesh and cat.bmesh as an example. The following table shows how long it takes to load each version:

Version Size Load Extract

Human Readable 7.01MB 0.181477s 0.064189s

Binary 587KB 0.000507s 0.000473s


The human-readable version of is 7.01MB, which is almost 12 times bigger than the binary file. When we are using binary files, it not only means the file size becomes smaller but also means it speeds up loading and extracting.

However, human-readable files are still easier to read, understand, maintain, and debug, so we still want to keep them.

I just copied functions I wrote in Mesh class last time and tweaked a few parameters for the loading part. Writing wasn't very hard. It just wrote what the data looked like in the memory into files. Because I also added some code to support the 32-bit index, the first item was a uint8_t to indicate which kind of index the file used. Here's an example of the binary file of a triangle:

As you can see, the first byte is mode 0, which means the file uses a 16-bit index so that the loader knows that index count, vertex count, and indices are all uint16_t numbers. The next two items are index count and vertex count, they should be written before real arrays, or we won't know how long these arrays are and where they start or end. The rest of the file is the vertex array (each position is followed by a color).

(Updated) These binary files are different for D3D and GL, for transformation is done during build-time. This saves more time during run-time. Since data needed for initializing is loaded by reading files instead of creating and passing in arrays, the interface is the same for both platforms anyway, so we no longer need to take that into consideration and can process that part during build-time.

Since we are using binary files, we don’t need to process them anymore when loading.

Here is the code that extracts data from binary files: (16-bit mode)

Here is a screenshot of the game:


22 views0 comments

Recent Posts

See All

Audio Project v1.0

Downloadable See How to use it? for more information. What does the project do? This audio project is based on Windows XAudio2 library....

Σχόλια


bottom of page