Skip to content

kokinomura/ofxSceneManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ofxSceneManager

A lightweight addon for openFrameworks supporting crossfading, data passing and event handling.

You can write your own scenes just like ofApp!

Dependency

Getting Started

  • Write your own subclasses of ofxScene.
#include "ofxScene.h"

class FirstScene : public ofxScene {
public:
    void setup();
    void update();
    void draw();
    void exit();
}
  • Fill all functions you need.

You have basic functions of ofApp and four additional functions you can override, and none of them are mandatory.

    void setup();
    void update();
    void draw();

    void willFadeIn();
    void willDraw();
    void willFadeOut();
    void willExit();

    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
  • Instantiate ofxSceneManager and add scenes.
class testApp : public ofBaseApp {
public:
    // definitions
    ofxSceneManager sceneManager;
}
void testApp::setup() {
    sceneManager.addScene(ofPtr<ofxScene>(new FirstScene));
    sceneManager.addScene(ofPtr<ofxScene>(new SecondScene));
}
  • Run the ofxSceneManager.
void testApp::setup() {
    sceneManager.addScene(ofPtr<ofxScene>(new FirstScene));
    sceneManager.addScene(ofPtr<ofxScene>(new SecondScene));

    sceneManager.run();
}

void testApp::update() {
    sceneManager.update();
}

void testApp::draw() {
    sceneManager.draw();
}

Functionality

Transition

ofxSceneManager supports two types of transition.

  • Transition when you trigger. (default)
  • Transition after certain period of time.
void testApp::setup() {
    sceneManager.addScene(ofPtr<ofxScene>(new FirstScene));
    sceneManager.addScene(ofPtr<ofxScene>(new SecondScene));
    sceneManager.setExitByTime(false);  // You don't need this line since it's false by default.
    sceneManager.setSceneDuration(0.3, 0.3);  //fade-in, fade-out time.
}

void testApp::keyPressed(int key){
    if (key == ' ') {
        sceneManager.changeScene();
    }
}
void testApp::setup() {
    sceneManager.addScene(ofPtr<ofxScene>(new FirstScene));
    sceneManager.addScene(ofPtr<ofxScene>(new SecondScene));
    sceneManager.setExitByTime(true);
    sceneManager.setSceneDuration(0.3, 15.0, 0.3);  //fade-in, drawing, fade-out time respectively.
}

Call ofxSceneManager::changeScene() to change scenes.

Fading

ofxSceneManager does fading by default, and the background color while transition is a default color of oF. If you want another color or a picture as a background, you can change in testApp::draw();

void testApp::draw() {
	ofBackground(255, 255, 0);
	sceneManager.draw();
}

Data Passing

You have two kinds of setup(). ofxSceneManager calls one you implement.

    void setup()
    void setup(ofPtr<ofxScene> previousScene)

If a scene needs data of a previous scene, implement void setup(ofPtr<ofxScene> previousScene) instead of void setup(). You can get data as public variables of the previous scene.

void SecondScene::setup() {

}
void SecondScene::setup(ofPtr<ofxScene> previousScene) {

}

Note: If you implement both, only setup() will be called and setup(ofPtr<ofxScene> previousScene) will be ignored.

About

A lightweight addon for openFrameworks supporting crossfading, data passing and event handling. You can write your own scenes just like ofApp.

Resources

License

Stars

Watchers

Forks

Packages

No packages published