How to separate windows and use one as renderer


#1

I know this may be a very silly questión.

But, i don’t seem to crack it out

So´ive been checking the multiWindowOneAppExample wich uses a ofxGui to pass the values from one window to the other. The problem is that i don´t want to use GUI EVENTS, i want to use an event to know when the renderer that i´ve been using has change, or just use a window to display an FBO and other to use my own Gui system.

This is my code so far in my main.cpp that is where the different windows are defined:

#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
	

	//GUI WINDOW
	ofGLFWWindowSettings settings;
	settings.setSize(600, 600);
	settings.setPosition(ofVec2f(300, 300));
	//settings.resizable = true;
	shared_ptr<ofAppBaseWindow> renderwindow = ofCreateWindow(settings);

	//RENDER WINDOW
	settings.setSize(800, 1200);
	settings.setPosition(ofVec2f(-1500, 300));

	settings.shareContextWith = renderwindow;
	shared_ptr<ofAppBaseWindow> GuiWindow = ofCreateWindow(settings);

	shared_ptr<ofApp> mainApp(new ofApp);
	shared_ptr<ofApp> mainApp2(new ofApp);
	
	mainApp2->iswindow1 = true;

	//ofAddListener(&ofApp::notifyactiverender, mainApp.get(), &ofApp::drawRender2);

	ofRunApp(renderwindow, mainApp);
	ofRunApp(GuiWindow, mainApp2);
	ofRunMainLoop();
}

Im using the variable iswindow1 to decide wich one is a window and wich one is not. So in one window i get the renderer and in the other my customized gui.

The problem for me comes when i´ve tryed to used the function :

ofAddListener(&ofApp::notifyactiverender, mainApp.get(), &ofApp::drawRender2);

I just want to comunicate to the other window when the FBO that i choose has change, but i don´t seem to be able to link ofApp events or what so ever. Or even, if i could, how could tell the second window to change my fbo ?

i´ve tryed making my own events and then link them to the other window but i failed on doing it, just can´t hit the syntax on how to do it right.

Any help is greatly apreciatted.


#2

Hello Julian. Using “the variable iswindow1 to decide wich one is a window and which one is not” is misleading.
Let’s say that you have two apps, one is called Editor, it contains the GUI and it contains all the resources that you need in your application. The other one is called ofApp, and it simply draw whatever you tell her to draw. The key here is that while creating the window for the App, you pass as argument settings.shareContextWith = editorWindow;. This means that ofApp can read everything from Editor.

This is your main.cpp file.

#include "ofMain.h"
#include "ofApp.h"
#include "Editor.h"

int main( ){    
    ofGLFWWindowSettings settings;
    settings.setGLVersion(3, 2);

    // Editor GUI
    settings.setSize(700, 700);
    settings.setPosition(ofVec2f(0,0));
    settings.resizable = true;
    shared_ptr<ofAppBaseWindow> editorWindow = ofCreateWindow(settings);
    shared_ptr<Editor> editor(new Editor);

    // App. Where you draw whatever you want
     settings.setSize(1400, 700);
     settings.setPosition(ofVec2f(300,0));
    settings.resizable = true;
    settings.shareContextWith = editorWindow;
    shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
    shared_ptr<ofApp> mainApp(new ofApp);

    mainApp->editor = editor;

    ofRunApp(editorWindow, editor);
    ofRunApp(mainWindow, mainApp);
    ofRunMainLoop();
}

This is your ofApp.h file:

#pragma once

#include "ofMain.h"
#include "Editor.h"

class ofApp : public ofBaseApp{

    public:
        void setup();
        void update();
        void draw();
        void drawScene();
        void setupGui();
        void drawGui(ofEventArgs & args);
        void keyPressed(int key);

        ofEasyCam ecam;
        shared_ptr<Editor> editor;
};

And this is your Editor.h file, where you have the resources that you want to draw (ofPrimitives, Fbos, whatever), and the GUI.

#pragma once

#include "ofMain.h"
#include "ofxGui.h"


class Editor: public ofBaseApp {
public:
    Editor(){};
    void setup();
    void update();
    void draw();
    void drawMainStage();
    void keyPressed(int key);
    void mousePressed(int x, int y, int button);

    // GUI
    ofxPanel gui;
    ofParameter<ofColor> bgColor;
    ofSpherePrimitive sphere;

};

As you see, the Editor class has two draw methods, the draw, and the drawMainstage. The draw will be automatically called for you, you can use it to draw the gui in the editor window.

Example of Editor.cpp

Editor::draw(){
    gui.draw();
}

Editor::drawMainstage(){
    sphere.draw();
}

The drawMainstage method is thought to be called in the ofApp.cpp file. For example:

ofApp.cpp

ofApp::draw(){
    editor->drawMainstage();
}

Again, the main concept that will help you working with 2 apps is to keep the resources in one application (in this case the Editor one), and use the ofApp just to draw things, accessing your resources in the ofApp using editor->.

I hope this helps


#3

Thanks very much for your help ! I wouln´t be able to solve it if it wasn´t for you.

I´ve used your code to make it work. and it work marvellous


#4

Happy that it worked :wink: