openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

ofScopedMatrix

Open artificiel opened this issue 1 year ago • 7 comments

image

makes use of the lifetime of an object to frame push and pop matrix and style.

OLD-SCHOOL FLAT

ofPushMatrix()
ofTranslate()
// (…)
ofPushMatrix()
ofTranslate();
// (…)
ofPopMatrx();
// (…)
ofPopMatrix();
// easy to lose track and get push pop errors

ARTISANAL BRACING — TRYING TO UNDERSTAND WHAT’S GOING ON

ofPushMatrix()
{
	ofTranslate()
	// (…)
	ofPushMatrix()
	{
		ofTranslate();
		// (…)
	}
	ofPopMatrx();
	// (…)
}
ofPopMatrix();
// as good as the artisanal visual tabbing 

INTRODUCING SCOPED MATRIX BRACING-AWARE OBJECT

{
	ofScopedMatrix _ ;
	ofTranslate()
	// (…)
	{
		ofScopedMatrix _ ;
		ofTranslate();
		// (…)
	}
	// (…)
} 
// push:pop balanced garanteed

~~FOR THE HARDCORE: LAMBDA IN ANONYMOUS OBJECT ENFORCES STRUCTURE~~

ofScopedMatrix{[&]{
	ofTranslate();
	// (…)
	ofScopedMatrix{[&]{
		ofTranslate();
		// (…)
	}};
        // (...)
}};
// terse and resilient, at the cost of double-bracing…

A TYPICAL CASE

// MyClass.h
void drawWidget(float x, float y, float w, float h) {
	ofScopedMatrixStyle _ ;
	ofTranslate(x,y); 
	ofScale(w,h);
        ofDrawWidget(etc);
} 
// automatically pops back because _ goes out of scope

artificiel avatar May 10 '24 03:05 artificiel

this pretty cool! the _ is a little weird from a style perspective but I guess it could be anything.

This is fine from my perspective to merge.

ofTheo avatar May 10 '24 15:05 ofTheo

fyr https://github.com/2bbb/ofxScope

2bbb avatar May 10 '24 17:05 2bbb

@ofTheo FYI single _ is C++26 standardized for exactly this kind of use, so you will have to get used to it! https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2169r4.pdf

@2bbb whoa you went all-in! my proposal here was with ~10 lines of code simplify visually the weight of PushMatrix/PopMatrix (and not, for instance nt attempting to "refine" the push/pop behaviour as discussed here https://github.com/openframeworks/openFrameworks/issues/1493). your ofxScope is a whole other level! one question: for the lambda version, I am simply calling the std::function with the captured reference to the object — I take for granted that with optimisations the overhead all but disappears, but do you have considerations on that effect?

artificiel avatar May 10 '24 18:05 artificiel

@artificiel I didn't test about optimization to lambda. I created lamdba style just wanted to compare what the code is better looks like. (like the much talked about "_" variable.) If you need severe tuning of your code, you may not want to use this.

2bbb avatar May 12 '24 12:05 2bbb

Yes my intent is about safety and making it impossible to get lost between braces. However if we're not sure the lambda has no overhead i prefer removing the option -- in any case I personally got very happy with the scoped anon objects (and it's a pattern getting more frequent). The lambda syntax is a bit clunky (above I wrote "for the hardcore" haha), is not an established pattern in OF, and not such a great gain for end users. Maybe keep the lambda for specific advanced cases where it's required and not a form of sugar.

artificiel avatar May 12 '24 17:05 artificiel

@artificiel I agree to your opinion if those feature will be include to oF core.

2bbb avatar May 12 '24 20:05 2bbb

OK based on the discussion above the lambda-versions are removed, and some docs are added

artificiel avatar May 13 '24 03:05 artificiel