Save / Load the ofApp window settings with ofxSerialize

hey @bakercp , thanks for this addon, it’s working fine for what I tested until now.

I am using the addon to store/recall the window settings (pos, size, mode) but I am not sure if there’s a better way… Maybe it’s possible to access to the main window pointer or something.

Here’s my code. I don’t know how to handle the window mode…

void ofApp::save_Window()
{
	ofWindowSettings AppWindow;
    AppWindow.setPosition(glm::vec2(ofGetWindowPositionX(), ofGetWindowPositionY()));
    AppWindow.setSize(ofGetWindowSize().x, ofGetWindowSize().y);
    
    ofJson j;
    to_json(j, AppWindow);
    ofSavePrettyJson ("AppWindow.json", j);
}

void ofApp::load_Window()
{
   	ofWindowSettings AppWindow;
    ofJson j = ofLoadJson("AppWindow.json");
    from_json(j, AppWindow);

    ofSetWindowShape(AppWindow.getWidth(), AppWindow.getHeight());
    ofSetWindowPosition(AppWindow.getPosition().x, AppWindow.getPosition().y);
}

Hey there, currently ofAppBaseWindow doesn’t have an accessor for ofWindowSettings. You can pass it in ofAppBaseWindow::setup(..), but can’t recover it all together. You have to recover it one member variable at a time. Your method works, but you might also do something like this:

ofWindowSettings settings;
settings.windowMode = ofGetCurrentWindow()->getWindowMode();
// etc.

It is a little tricky to serialize ofWindowSettings, because usually you will actually be using one of its subclasses, so I didn’t build in a bunch of special window settings that are available in ofGLFWWindowSettings for instance.

To set the window settings, you can use this helper function:

// Load the json.
ofJson json = ofLoadJson("AppWindow.json");

// This will apply all of the settings to the current window.
ofx::Serializer::ApplyWindowSettings(json);

There are also some helpful app settings you can save / apply, just take a look at the code.

1 Like