hello. I am new to OF and C++ but not to programming (migrating from Processing). I have provided a MCVE based on the ‘multiWindowExample’ (without the use of ofxGui). I am just trying to pass strings between windows just to get started. I was able to reference a string variable from the second window in the main window but not vice-versa. I know I am missing something fundamental here. I have commented out and noted with an arrow the three lines in the code that are the final failed attempt at solving this problem.
main.cpp:
#include "ofMain.h"
#include "ofApp.h"
#include "Window2.h"
#include "ofAppGLFWWindow.h"
//========================================================================
int main() {
ofGLFWWindowSettings settings;
settings.setSize(300, 300);
settings.setPosition(glm::vec2(0, 0));
settings.resizable = true;
shared_ptr<ofAppBaseWindow> secondWindow = ofCreateWindow(settings);
settings.setSize(1920, 1080);
settings.setPosition(glm::vec2(1920, 0));
settings.resizable = true;
shared_ptr<ofAppBaseWindow> mainWindow = ofCreateWindow(settings);
shared_ptr<Window2> secondApp(new Window2);
shared_ptr<ofApp> mainApp(new ofApp);
mainApp->secWindow = secondApp;
//secondApp->mainWindow = mainApp; <----------
ofRunApp(secondWindow, secondApp);
ofRunApp(mainWindow, mainApp);
ofRunMainLoop();
}
ofApp.h:
#pragma once
#include "ofMain.h"
#include "Window2.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
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 mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
string mainString;
shared_ptr<Window2> secWindow;
};
ofApp.cpp:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
mainString = "main window";
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
stringstream ss;
ss << "framerate: " << ofToString(ofGetFrameRate(), 0) << endl;
ss << "mainString: " << mainString << endl;
ss << "secondString: " << secWindow->secondString << endl; //works
ofDrawBitmapString(ss.str().c_str(), 20, 20);
}
Window2.h:
#pragma once
#include "ofMain.h"
class Window2 : public ofBaseApp
{
public:
void setup();
void update();
void draw();
string secondString;
//shared_ptr<ofApp> mainWindow; <---------
};
Window2.cpp:
#include "Window2.h"
void Window2::setup() {
ofBackground(0);
ofSetVerticalSync(false);
secondString = "second window";
}
void Window2::update() {
}
void Window2::draw() {
ofSetColor(255);
stringstream ss;
ss << "framerate: " << ofToString(ofGetFrameRate(), 0) << endl;
ss << "secondString: " << secondString << endl;
//ss << "mainString: " << mainWindow->mainString << endl; <---------
ofDrawBitmapString(ss.str().c_str(), 20, 20);
}