Implementing the autosave feature
Autosaving can be implemented by saving the GUI state to a file when the project ends, and loading the GUI state from this file on project startup. To accomplish this, perform the following steps:
- Declare the new
exit()
function in theofApp
class:void exit();
- Then, add its definition to the
ofApp.cpp
file, as follows:void ofApp::exit() { gui.saveToFile( "settings.xml" ); }
openFrameworks calls the
exit()
function right before finishing the project. So, this function saves the state of thegui
elements to thesettings.xml
file, which is located in thebin/data
folder of the project. - To load the
gui
state at startup, add the following line to the end of thesetup()
function:gui.loadFromFile( "settings.xml" );
Autosave is ready; let's check it! Run the project, move the sliders, and note their values. Now, close the project and run it again. You will see that sliders' values were restored properly.
Now let's extend our GUI...