Hey everyone -
I’ve been working on adding save functionality to Blockitto and I think I might’ve stumbled upon a bug. When I try to save and load save data nothing seems to be saved. Either there’s a bug in the library or a bug in EmBitz, as I’m fairly certain my code isn’t faulty seeing as it works on PC.
Here’s my code for saving and loading in case it’s of any use, ignore the strange indentation as that’s just because the original code is indented in a function (POK_HW is defined in the build options for my EmBitz because I thought it would fix the bug, will remove it later):
Save
std::cout << "Saved chunk at x=" << x << ", y=" << y << std::endl;
#ifdef POK_SIM
std::string sdpath = "C:/Users/kaden/Documents/Pokitto/My Projects/Blockitto/PokittoLib/Games/Blockitto/sd/";
#elif defined POK_HW
std::string sdpath = "/sd/";
#endif // POK_SIM
std::string cdir = sdpath + "blockitto";
if (opendir(cdir.c_str())==0) {
#ifdef POK_SIM
mkdir(cdir.c_str());
#elif defined POK_HW
mkdir(cdir.c_str(),0777);
#endif // POK_SIM
};
std::string filepath = sdpath + "blockitto/chunk" + patch::to_string(x) + "x" + patch::to_string(y) + ".data";
FILE *fout = fopen(filepath.c_str(), "wb");
if (fout == NULL) {// error
} else {
// success, write!
int8_t surfaceBuffer[30];
for (int p1=0; p1<5; p1++) {
for (int p2=0; p2<6; p2++) {
surfaceBuffer[p1*6+p2] = ground[p1][p2];
}
}
fwrite (surfaceBuffer,sizeof(int8_t),30,fout);
int32_t TESize[1] = {tients.size()};
fwrite (TESize,sizeof(int32_t),1,fout);
int32_t theTETrinity[3];
for (int i=0; i<tients.size(); i++) {
theTETrinity[0] = tients[i].getX();
theTETrinity[1] = tients[i].getY();
theTETrinity[2] = tients[i].getType();
fwrite (theTETrinity,sizeof(int32_t),3,fout);
}
fclose(fout);
}
Load
bool loadThis = false;
#ifdef POK_SIM
std::string sdpath = "C:/Users/kaden/Documents/Pokitto/My Projects/Blockitto/PokittoLib/Games/Blockitto/sd/";
#elif defined POK_HW
std::string sdpath = "/sd/";
#endif // POK_SIM
std::string cdir = sdpath + "blockitto";
if (opendir(cdir.c_str())==0) {
loadThis = false;
} else {
std::string filepath = sdpath + "blockitto/chunk" + patch::to_string(x) + "x" + patch::to_string(y) + ".data";
FILE *fload = fopen(filepath.c_str(), "rb");
if (fload == NULL) {
loadThis = false;
} else {
loadThis = true;
int8_t initBlocks[30];
fread(initBlocks,sizeof(int8_t),30,fload);
for (int p1=0; p1<5; p1++) {
for (int p2=0; p2<6; p2++) {
ground[p1][p2] = initBlocks[p1*6+p2]; // load surface blocks
}
}
int32_t numTEs[1];
fread(numTEs,sizeof(int32_t),1,fload); // store number of tile entities
int32_t inputTETrinity[3];
for (int i=0; i<numTEs[0]; i++) {
fread(inputTETrinity,sizeof(int32_t),3,fload);
tients.emplace_back(inputTETrinity[0],inputTETrinity[1],inputTETrinity[2]);
}
fclose(fload);
}
}
if (!loadThis) {
// chunk not saved yet (new chunk), use procgen to make it
int ax, ay;
int tempTE;
for (int i=0; i<5; i++) {
for (int j=0; j<6; j++) {
ax = x*6+j;
ay = y*5+i;
ground[i][j] = TerrainGen::getGroundAt(ax,ay); // same generation system as default constructor
tempTE = TerrainGen::getTEAt(ax,ay);
if (!(tempTE == -1)) {
tients.emplace_back(ax,ay,tempTE);
}
}
}
}
Unless the file read/write functions aren’t going to the correct path I have no idea why it isn’t working. Tested on both the current text loader and the WIP graphical loader.