Reduce Lag during screen capture

Hi.
I’m coding an app that should capture a series (almost 100 pictures) of screenshots with alpha that have to move to a certain position.

Everything works but during the capture of the screen I have some lags that I want to reduce.

The program is “a little” complicated so I can’t publish everything but this is the variables and the function that I use for collect the images. ( I use a timer for

in ofApp.h

void makeShapeSnapshot();
int shapeScreenshot_counter;
vector<ofImage> shapeScreenshots_capture;
int shapeScreenshots_counter; // utile
ofFbo fbo_shapeScreenshots;
vector<float> shapeScreenshots_alphas;
int shapeScreenshots_initial_alpha;
bool rafficaStartedShapeScreenshot;
bool last_rafficaStartedShapeScreenshot;
bool pointVisible;
float shapeRotationAngle;
float shapeEnlargeOffset;
bool isShapeMoving;
int shapePoints_num;
vector<ofVec2f>shapePoints_pos;
vector<ofVec2f>old_shapePoints_pos;
vector<ofVec2f>shapeScreenshots_pos;
vector<ofVec2f>shapeScreenshots_dest;
vector<float> shapeScreenshots_scaleFactors;
vector<ofVec2f>shapeScreenshot_diffs;
vector<float>shapeScreenshot_magsAB;
vector<float>shapeScreenshots_posMags;
vector<ofVec2f>shapeScreenshots_start;

in ofApp.cpp

//--------------------------------------------------------------
void ofApp::makeShapeSnapshot() {
	if (shapeScreenshot_counter >= shapePoints_pos.size()) {
		shapeScreenshot_counter = 0;
		cout << " = 0" << endl;
	}
	ofPixels pixels;
	fboLumaKey.readToPixels(pixels);
	ofImage temp;
	temp.setFromPixels(pixels);
	shapeScreenshots_capture.push_back(temp);
	shapeScreenshots_alphas.push_back(shapeScreenshots_initial_alpha);
	shapeScreenshots_start.push_back(ofVec2f(0, 0));
	shapeScreenshots_pos.push_back(ofVec2f(0, 0)); // initial position
	shapeScreenshots_dest.push_back(ofVec2f(shapePoints_pos[shapeScreenshot_counter].x, shapePoints_pos[shapeScreenshot_counter].y));
	int last_n = shapeScreenshots_capture.size() - 1;
	shapeScreenshots_scaleFactors.push_back(0.0);
	shapeScreenshot_diffs.push_back(sub_vector(shapeScreenshots_dest[last_n], ofVec2f(0, 0)));
	shapeScreenshot_magsAB.push_back(mag_vector(shapeScreenshot_diffs[last_n]));
	shapeScreenshots_posMags.push_back(mag_vector(shapeScreenshots_pos[last_n]));
	shapeScreenshot_counter++;
}

I know that in that function I create a lot of stuff… How can I do it better?

You could pre-allocate memory to store the data, rather than adding new objects when you make the snapshot.

You can put the capture part inside a thread so you do not get any “lag” on main one. Look at the threadchannel example

You can also try ofxFastFboReader

I think that this should be the better solutions but is complicated, but I will try!
I’m using threaded timer for trigger the snapshots. Maybe I can integrate the capture in that process.

Maybe I understood how to do it… but… How can I access to an fbo of ofApp from threaded class?

I would like to resolve looking at this solution: https://openframeworks.cc/documentation/utils/ofThread/

In the meanwhile I will try to study the threadchannel and this page of the ofBook example :slight_smile:

If you feel is overcomplicated you can use an addon like this one, i do not know if works on current oF.

My advice is that try to do it yourself, is not hard and understand how thread works and how can you communicate between is usefull