Book Image

OpenFrameworks Essentials

Book Image

OpenFrameworks Essentials

Overview of this book

Table of Contents (19 chapters)
openFrameworks Essentials
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Declare the new exit() function in the ofApp class:

    void exit();
  2. 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 the gui elements to the settings.xml file, which is located in the bin/data folder of the project.

  3. To load the gui state at startup, add the following line to the end of the setup() 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 capabilities a little bit further by...