Although it is quite likely that you would be better off serialising the state of the game which produces the window rather than the window itself, there is no problem using putwin
and getwin
to serialise and retrieve an ncurses Window.
putwin
just writes a description of the window to the given FILE
. It doesn't rewind the file stream, nor does it require it to be rewound. It doesn't close the file stream, either. Similarly, getwin
just reads the window description from the given FILE
starting at the current read point.
So you can use putwin
/getwin
as part of a serialisation procedure:
int serialiseWindow(Window* game, FILE* file) {
return putwin(game, file);
}
int serialiseGameState(gameState *state, FILE* file) {
int status;
status = serialiseWindow(state->game, file);
if (status != OK) return status;
status = serialisePositionArray(state->positions,
(sizeof state->positions)/(sizeof *state->positions),
file);
if (status != OK) return status;
status = serialiseInteger(state->level, file);
if (status != OK) return status;
status = serialiseInteger(state->found, file);
if (status != OK) return status;
status = serialiseIntegerArray(state->timeSpent,
(sizeof state->timeSpent)/(sizeof *state->timeSpent),
file);
return status;
}
And, to get it back:
int retrieveWindow(Window** game, FILE* file) {
Window* win = getwin(file);
*game = win;
return win ? OK : ERR;
}
int retrieveGameState(gameState *state, FILE* file) {
int status;
status = retrieveWindow(&state->game, file);
if (status != OK) return status;
status = retrievePositionArray(state->positions,
(sizeof state->positions)/(sizeof *state->positions),
file);
if (status != OK) return status;
status = retrieveInteger(&state->level, file);
if (status != OK) return status;
status = retrieveInteger(&state->found, file);
if (status != OK) return status;
status = retrieveIntegerArray(state->timeSpent,
(sizeof state->timeSpent)/(sizeof *state->timeSpent),
file);
return status;
}