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));
oRead >> std::noskipws;
right after opening the file. \$\endgroup\$oRead
is astd::ifstream
? Don't removestd::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\$