Skip to content

Instantly share code, notes, and snippets.

@lg3bass
Forked from jmsaavedra/ofGetAppPtrExample.md
Created June 8, 2017 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lg3bass/a6875af78217024d55b7d5ac16260bbf to your computer and use it in GitHub Desktop.
Save lg3bass/a6875af78217024d55b7d5ac16260bbf to your computer and use it in GitHub Desktop.
example use of ofGetAppPtr()

###ofGetAppPtr() example

from a .cpp file, for example, "particle.cpp" you can do two things:

  • a) include "ofApp.h" (so you know what's inside the ofApp)
  • b) cast the ofGetAppPtr as a ptr

you can't include ofApp.h inside another .h file, as you would recursive includes (ie, ofApp includes particle, particle includes ofApp), but putting it in the .cpp is fine.

example code:

    //particle.cpp particle class
    
    #include "particle.h"
    #include "ofApp.h"
    
    void particle::update(){
    
      ((ofApp*) ofGetAppPtr())->someVariableThatsInOFApp = 100; //update ofApp var
      
      int myParticleInt = ((ofApp*) ofGetAppPtr())->someIntVarInOFApp; //reference ofApp var
      
      if(someLocalVar > 10)
        ((ofApp*) ofGetAppPtr())->somOFAppMethod(); //run ofApp function from here
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment