Skip to content

Commit

Permalink
Using BMessage and BFile instead of ofstream and ifstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ondralukes committed Dec 23, 2019
1 parent 2cef9db commit faa9630
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
77 changes: 43 additions & 34 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,52 +456,61 @@ Game::undoMove() {

void
Game::save(BRect frame){
std::ofstream saveFile(fSaveFile_path, std::ios_base::trunc| std::ios_base::binary);
if(saveFile.good()){
saveFile.write((char*)fBoard,sizeof(uint32_t)*fSizeX*fSizeY);
saveFile.write((char*)fPreviousBoard,sizeof(uint32_t)*fSizeX*fSizeY);
BFile saveFile(fSaveFile_path,B_WRITE_ONLY | B_CREATE_FILE);

saveFile.write((char*)&fScore,sizeof(uint32_t));
saveFile.write((char*)&fPreviousScore,sizeof(uint32_t));
BMessage saveMessage(H2048_SAVE_MESSAGE);

saveFile.write((char*)&fCanUndo,sizeof(bool));
saveMessage.AddData("board",B_INT32_TYPE,fBoard,sizeof(uint32_t)*fSizeX*fSizeY,false);

This comment has been minimized.

Copy link
@CodeforEvolution

CodeforEvolution Dec 23, 2019

You could use the AddInt32 function here ^^^^

saveMessage.AddData("previousBoard",B_INT32_TYPE,fPreviousBoard,sizeof(uint32_t)*fSizeX*fSizeY);

This comment has been minimized.

Copy link
@CodeforEvolution

CodeforEvolution Dec 23, 2019

And Here ^^^^


saveFile.write((char*)&frame,sizeof(BRect));
saveFile.close();
}
saveMessage.AddUInt32("score",fScore);
saveMessage.AddUInt32("previousScore",fPreviousScore);

saveMessage.AddBool("canUndo",fCanUndo);
saveMessage.AddRect("windowFrame",frame);

status_t result = saveMessage.Flatten(&saveFile);

This comment has been minimized.

Copy link
@CodeforEvolution

CodeforEvolution Dec 23, 2019

You’ll want to return the “result” variable ^^^^

}
bool
Game::load(){
std::ifstream saveFile(fSaveFile_path, std::ios::binary);
if(saveFile.good()){
saveFile.read((char*)fBoard,sizeof(uint32_t)*fSizeX*fSizeY);
saveFile.read((char*)fPreviousBoard,sizeof(uint32_t)*fSizeX*fSizeY);
BFile saveFile(fSaveFile_path,B_READ_ONLY);

BMessage saveMessage(H2048_SAVE_MESSAGE);
status_t result = saveMessage.Unflatten(&saveFile);
if(result!= B_OK){
return false;
}

saveFile.read((char*)&fScore,sizeof(uint32_t));
saveFile.read((char*)&fPreviousScore,sizeof(uint32_t));
bool loadOK = true;
const void * tempPtr;

saveFile.read((char*)&fCanUndo,sizeof(bool));
loadOK = loadOK && saveMessage.FindData("board",B_ANY_TYPE,0,&tempPtr,NULL) == B_OK;

This comment has been minimized.

Copy link
@CodeforEvolution

CodeforEvolution Dec 23, 2019

You could use the FindInt32 function ^^^^ and avoid the memcpy.

memcpy(fBoard,tempPtr,sizeof(uint32_t)*fSizeX*fSizeY);

BRect frame;
saveFile.read((char*)&frame,sizeof(BRect));
loadOK = loadOK && saveMessage.FindData("previousBoard",B_ANY_TYPE,0,&tempPtr,NULL) == B_OK;

This comment has been minimized.

Copy link
@CodeforEvolution

CodeforEvolution Dec 23, 2019

Same thing here ^^^^

memcpy(fPreviousBoard,tempPtr,sizeof(uint32_t)*fSizeX*fSizeY);

saveFile.close();
loadOK = loadOK && saveMessage.FindUInt32("score",&fScore) == B_OK;
loadOK = loadOK && saveMessage.FindUInt32("previousScore",&fPreviousScore) == B_OK;
loadOK = loadOK && saveMessage.FindBool("canUndo",&fCanUndo) == B_OK;

BMessage setFrameMessage(H2048_SET_FRAME);
setFrameMessage.AddRect("frame",frame);
broadcastMessage(setFrameMessage);
BRect frame;
loadOK = loadOK && saveMessage.FindRect("windowFrame",&frame) == B_OK;

BMessage startNotification(H2048_GAME_STARTED);
broadcastMessage(startNotification);
if(!loadOK) return false;

//Notify the boards about fCanUndo state
BMessage changed(H2048_BOARD_CHANGED);
changed.AddBool("canUndo", fCanUndo);
broadcastMessage(changed);
BMessage setFrameMessage(H2048_SET_FRAME);
setFrameMessage.AddRect("frame",frame);
broadcastMessage(setFrameMessage);

fInGame = true;
return true;
} else {
return false;
}
BMessage startNotification(H2048_GAME_STARTED);
broadcastMessage(startNotification);

//Notify the boards about fCanUndo state
BMessage changed(H2048_BOARD_CHANGED);
changed.AddBool("canUndo", fCanUndo);
broadcastMessage(changed);

fInGame = true;
return true;
}
3 changes: 2 additions & 1 deletion Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ enum
H2048_UNDO_MOVE = '48UM',
H2048_MAKE_MOVE = '48MM',
H2048_LOAD_GAME = '48LG',
H2048_SAVE_GAME = '48SG'
H2048_SAVE_GAME = '48SG',
H2048_SAVE_MESSAGE = '48SM'
};

class Game : public BLooper
Expand Down

0 comments on commit faa9630

Please sign in to comment.