0
\$\begingroup\$

I'm trying to put all the assets inside a binary file and then trying to read them from the file itself, without scattering all them around in separate folders to let them exposed. What I'm trying to achieve is something like the way that RPG Maker and Wolf Editor use to join all the images / sounds of the game into a single customized format file (e.g. Data.wolf).

It's a very simple security's method but is enough for what I'm planning.

For now, I have the following code for reading a PNG file inside the binary, that always prints an error's message when it trys to create a new surface from IMG_LoadPNG_RW, saying "Error reading the PNG file."

tString sResult{};
cFile_Manager::tRead_File oRead(sDir, std::ios::binary);

if(oRead.is_open()) {
    tString sLine{};
    while(getline(oRead, sLine)) sResult += sLine + "\n";
    oRead.close();
}else {
    throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
}

SDL_RWops* aRW{};
aRW = SDL_RWFromConstMem(&sResult, sizeof(sResult));

if(!aRW) // Error message

SDL_Surface* aSurface{};
aSurface = IMG_LoadPNG_RW(aRW);

if(!aSurface) // This is where SDL shows "Error reading the PNG file."

EDIT: Omg I made some mistakes, now this is the correctly code. Now it gives a different error: "PNG file corrupted by ASCII conversion"

    char* sBuffer{};
    if(oRead.is_open()) {
        oRead >> std::noskipws;
        oRead.seekg(0, oRead.end);
        int iLength = oRead.tellg();
        sBuffer = new char[iLength];
        oRead.seekg(0, oRead.beg);
        oRead.read(sBuffer, iLength);
        oRead.close();
    }else {
        throw cLog("\n\n[ERROR] Wrong dir passed.\n\nDIR: %s\n\n", sDir);
    }

    SDL_RWops* aRW{};
    aRW = SDL_RWFromConstMem(sBuffer, sizeof(sBuffer));
\$\endgroup\$
  • \$\begingroup\$ Try oRead >> std::noskipws; right after opening the file. \$\endgroup\$ – HolyBlackCat Jan 14 '18 at 2:13
  • \$\begingroup\$ Also, this way of doing things is slightly inefficient. It would be faster to get the file size first, allocate a buffer of needed length, and then do a single read into it. \$\endgroup\$ – HolyBlackCat Jan 14 '18 at 2:14
  • \$\begingroup\$ @HolyBlackCat I'm sorry, I'm very newbie so how can I do that? (The oRead>>std::noskipws didn't worked) \$\endgroup\$ – Dormin Jan 14 '18 at 2:16
  • \$\begingroup\$ Reading binary file line by line can fail in many ways. I assume oRead is a std::ifstream? Don't remove std::noskipws, use .seekg() to move to the end of file, .tellg() to get the size, allocate a buffer of this size, .seekg() back to the beginning, .read() into the buffer. en.cppreference.com/w/cpp/io/basic_istream \$\endgroup\$ – HolyBlackCat Jan 14 '18 at 2:26
  • \$\begingroup\$ @HolyBlackCat I edited the post with the code that you suggested and keeps giving the same error... "libpng error: Not a PNG file", "Error reading the PNG file." \$\endgroup\$ – Dormin Jan 14 '18 at 2:42
1
\$\begingroup\$

I was able to find a solution, which was a result of various information I collected here in Gamedev and StackOverFlow.

To increase the range of the solution, I'm going to pass the link from where I got it: https://stackoverflow.com/questions/4317318/how-do-i-pass-an-stdstring-to-a-function-that-expects-char

It's the first answer in this link

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.