|
Binary |
|
A crucial element in storing data efficiently is a good knowledge of binary files. It also greatly enhances your understanding of the circuitry behind computer science in general. In this article I will walk you through the basic concepts to the point where you could write a bitmap without using any simplified image writing functions.
When you set variables, they are stored in the system's temporary memory. When a program is stopped, the variables are normally deleted to free memory. If you want to permanently store data, the easiest way is to write binary files to the hard drive. To support this, GM offers the following set of functions:
A binary file can be thought of as a list structure. There are a series of bytes, like positions in a list, 0 being the first and (size)-1 being the last. Each byte has a value, normally ranging between 0 and 255. (Will be explained later.)
Say I want to store variables for the sound and music volume. You will first need to create a blank file in the game's directory, because the file_bin_open function won't create non-existing files for you. A blank text file will work fine. Name it "config" (no extension). Now you would use a code like this to write the sound and music options to the file:
You may have noticed that I didn't specify the bytes to write to, I just wrote. This is because GM auto-magically starts writing at the beginning and when you write a byte it jumps to the next byte. The same goes for reading. If you want to write to a specific byte, you can use the file_bin_seek function.
When you want to load the data from the file, you could use a code like this:
Many people when first introduced to the concept of binary files wonder why they are referred to as binary if values other than 0s and 1s are used. A byte of data stores a value between 0 and 255. The 0/1 data type is a bit. Any value between 0 and 255 has a corresponding sequence of eight bits. If we wanted to convert the value 117 to bits, we would do so as follows. First, you set up the list of comparing values to use. Since we want eight 0s and 1s, we will use the following values:
1: 1 2: 2 3: 4 4: 8 5: 16 6: 32 7: 64 8: 128
Now we move backwards through the list, and compare our number (117) to each value. If it is greater than or equal to the comparing value, we subtract that comparing value and add a 1 to the list. Otherwise, we add a 0 to the list
8. 128>117 so we add a 0 to the binary list and the number remains 117. 7. 64<117 so we add a 1 to the binary list and subtract 64 and the number becomes 53. 6. 32<53 so we add a 1 to the binary list and subtract 32 and the number becomes 21. 5. 16<21 so we add a 1 to the binary list and subtract 16 and the number becomes 5. 4. 8>5 so we add a 0 to the binary list and the number remains 5. 3. 4<5 so we add a 1 to the binary list and subtract 4 and the number becomes 1. 2. 2>1 so we add a 0 to the binary list and the number remains 1. 1. 1=1 so we add a 1 to the binary list and subtract 1 and the number becomes 0. (Indicating that we're done.)
Our binary list is 01110101. Since we went backwards, we now need to reverse the sequence of binary values, making it 10101110. To convert it, we simply reverse the process. We start with 0, and for each 1 we add its corresponding value.
1: 1*(1)=1; new value is 1. 2: 0*(2)=0; new value is 1. 3: 1*(4)=4; new value is 5. 4: 0*(8)=0; new value is 5. 5: 1*(16)=16; new value is 21. 6: 1*(32)=32; new value is 53. 7: 1*(64)=64; new value is 117. 8: 0*(128)=0; new value is 117.
And so, we end up with our original byte value, indicating that we've done it right. With this knowledge at hand, you can store data in different ways. For example, say you had a variable for sound that was either 0 for on or 1 for off, and another variable for music. You could combine the value strings, and convert the binary to a single value using the method above. So you could store the two values in the same variable. This would only save a few bytes of memory, but is a neat thing to know.
Diveloperz - Revision #1 |