Sunday, May 1, 2011

Embedding data inside a c++ application

Is there a platform-independent method to embed file data into a c++ program? For instance, I am making a game, and the levels are stored in text format. But I don't want to distribute those text files with the game itself. What are my options?

From stackoverflow
  • You can always put the text in the code. Say, for example, as an array of Strings or array of pointers to characters.

    String txt[] = {
      "My first line.\n",
      "My second line.\n"
    }
    

    Of course there are better structures in the standard libraries, but in any case you put the text in a source file.

    You could also consider putting the text into the package and encrypting it, if you're worried about people accessing it.

  • This has been asked here before. Basically, you just name the data with an accessible symbol. I like this method best:

    You can always write a small program or script to convert your text file into a header > > file and run it as part of your build process.

  • If the levels are really stored simply as text, you could just declare static char arrays in your source:

    static const char level1[] = "abcdeabcdeabcde....";
    

    You can compile and link this right into your application and reference it just like any other variable.

0 comments:

Post a Comment