openFrameworks
openFrameworks copied to clipboard
ofScopedMatrix
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
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.
fyr https://github.com/2bbb/ofxScope
@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 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.
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 I agree to your opinion if those feature will be include to oF core.
OK based on the discussion above the lambda-versions are removed, and some docs are added