Step 3 Disch's Binary Tutorial, File Format

Hi, I'm sorry to bother y'all but I'm a bit confused by what Disch's Binary Tutorial meant on Step 3 Define your file format.

Would anyone mind showing an example of how you would "Define" your file format, or was it basically like,
 
string ReaderString(istream& file) { ... }

If so then I'd be writing something like..
1
2
char fHeader(char extension[5]) { return *extension; };
char fileHeader = fHeader("MyFi");

But then I don't see the relevance of doing that when I would just write,
 
char fileHeader[5] = "MyFi";


Also note that I made a char array of 5 because the size of "MyFi" is 5 if you include the NULL ending.
Defining the file format isn't really an exercise in programming, it's more of a design step.

The idea is to outline how you want your file to look/act before you actually write any read/write code.

I typically create a text file and jot down all the stuff I want to save in the order in which I want to save it. It might look something like this:


char[4]   header      "MyFi"
s32       somedata    Description of somedata

s32       blockcount  Description of blockcount

{  ... for each 'blockcount' ...

    s16   someval     Description
    s16   anotherval  Description
}



Having this outline is useful because:

-) It gives a clear definition of what the file is supposed to look like
-) It makes it much easier to read/write the file, since all you have to do is follow the outline
-) If you have problems reading/writing the file, it makes them easier to debug because you can look at the file in a hex editor and see exactly how the data is being stored. You can compare that to your outline to make sure it's getting stored correctly.



If you look at documentation for any published binary format, they all do something similar to this. They outline the format in great detail.


For a real-life example... see the zip file format in appnote:
https://www.pkware.com/documents/casestudies/APPNOTE.TXT

(skip to section 4.3.6 where it actually outline the format)


Of course... your outline does not have to be that detailed and verbose. But it does certainly help to lay everything out.
Ah I can visualize it much better now, thank you!

But now the question is how would this all get put together?

Here would be the XML equivalent of what I'm trying to get at...
1
2
3
4
5
6
7
8
9
10
11
12
13
<sprite>
    <animation>
        <type>Walk</type>
        <direction>North</direction>
        <order>0,1,2,3</order>
        <track>
            <frame id="0" x="0" y="0>
            <frame id="1" x="32' y="0>
            <frame id="2" x="64' y="0>
            <frame id="3" x="96' y="0>
         </track>
    </animation>
</sprite> 


So what I do with this is read it and grab the information out of it and insert it into my Spriteset object.

And a "Theory" example of the code is..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
enum eTypes
{
    WALK, RUN, ATTACK
};
enum eDirections
{
    NORTH, EAST, SOUTH, WEST
};
struct sFrame
{
    int ID;
    int PosX;
    int PosY;
};
struct sTrack
{
    int ID;
    int Order[4];
    sFrames* frames[4];
};
struct sAnimations
{
    eTypes type;
    eDirection dir;
    sTrack* track;
};
struct sSpriteset
{
    Texture* img;
    sAnimations* Animations;
};
//============================

class cSpriteset
{
private:
    sSpriteset* set;
public:
    cSpriteset();
    ~cSpriteset();
    cSpriteset getInstance();

    bool readSpriteset(std::ifstream file);    // Open Existing Spriteset file
    bool writeSpriteset(std::ofstream file);  // Write to Existing
    bool writeSpriteset(std::ofstream file, std::string name); // Create New File
}


so basically what I'd want the file output to look like is...

// Texture
"images/sprite01.png" 

//  Direction, Track ID, [Ordering], [Frame Info]
0 0 [0,1,2,3] [0,0,0] 
1 0 [0,1,2,3] [0,32,0]
2 0 [0,1,2,3] [0,64,0]
3 0 [0,1,2,3] [0,96,0]


You know what.. I think I might've just realized the answer here...

Thank you for your help Disch ^^
Last edited on
Topic archived. No new replies allowed.