Multi Windows, open/close it programatically. Is there a way?


#1

Hi guys,

Been looking to the new multi window feature in OF for a possible project. It’s pretty cool that now you can set multiple windows so easily.

I just wonder one thing, after closing the window is there a way to open it again programatically?

Thanks,
João


#2

Hey, that’s a complex question to answer, it depends on your specific needs.
Raven Kwok has recently done some experiments with windows using Processing.

Looking at the multiWindowOneAppExample, I just tried making them on the fly from the main ofApp and it seems to run stable.

ofApp.h

std::vector<shared_ptr<ofAppBaseWindow> > windows;
void drawRandomInWindow(ofEventArgs & args);

ofApp.cpp


	void ofApp::keyPressed(int key){
		ofGLFWWindowSettings settings;
		settings.width = 300;
		settings.height = 300;
		settings.setPosition(ofVec2f(0,0));
		settings.resizable = false;
		windows.push_back( ofCreateWindow(settings) );
		windows.back()->setVerticalSync(false);

		ofAddListener(windows.back()->events().draw, this,&ofApp::drawRandomInWindow);
	}

	void ofApp::drawRandomInWindow(ofEventArgs &args){
		ofPushStyle();
		ofSetColor(ofRandom(128,255));
		ofFill();
		ofDrawRectangle(ofRandomuf()*20.f, ofRandomuf()*20.f, ofRandomuf()*80.f, ofRandomuf()*80.f);
		ofPopStyle();
	}

	// add to ofApp::setup() to manipulate created windows
	for(auto it=windows.begin(); it!=windows.end(); it++){
		ofDefaultVec2 pos(150+cos(fmod(ofGetElapsedTimef(),TWO_PI)) * 100.f, 150+sin(fmod(ofGetElapsedTimef(),TWO_PI)) * 100.f);
		(*it)->setWindowPosition(pos.x, pos.y);
	}

#3

Hi @Daan,

Awesome, thanks a lot for the insight :slight_smile:


#4

awesome indeed… thanks a lot…

is it also possible to do…
settings.shareContextWith = mainWindow;
with this method?
or how can i access/refer to windows created in main.cpp?

and how is it possible to identify different elements of the vector with the “auto it” method?

greetz

pls


#5

You have either to register the window instances from your ofApp (not in main.cpp), store them, then loop trough them to send commands.

In the example above, when you press a key, windows is populated with a new window instance.
The last piece of code in my example above shows how to loop trough them and call a member function on each (setWindowPosition).

Another way to communicate is to listen to window events.
ofAddListener(windows.back()->events().draw, this,&ofApp::drawRandomInWindow);


#6

thanks a lot… sorry for my late reply… did not login for quite a long time…
so you are not doing any window related stuff in main.cpp?