Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ofScopedMatrix #7946

Merged
merged 3 commits into from
May 14, 2024
Merged

ofScopedMatrix #7946

merged 3 commits into from
May 14, 2024

Conversation

artificiel
Copy link
Contributor

@artificiel artificiel commented May 10, 2024

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

@ofTheo
Copy link
Member

ofTheo commented May 10, 2024

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.

@2bbb
Copy link
Contributor

2bbb commented May 10, 2024

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

@artificiel
Copy link
Contributor Author

@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 #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?

@2bbb
Copy link
Contributor

2bbb commented May 12, 2024

@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.

@artificiel
Copy link
Contributor Author

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.

@2bbb
Copy link
Contributor

2bbb commented May 12, 2024

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

@artificiel
Copy link
Contributor Author

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

@ofTheo ofTheo merged commit d17ba23 into openframeworks:master May 14, 2024
11 checks passed
@artificiel artificiel deleted the scopedmatrix branch May 15, 2024 00:05
danoli3 added a commit to danoli3/openFrameworks that referenced this pull request May 20, 2024
…leedingmacOS

* commit '7f37e70f65e9e022ba8868fb555570ce2c78a6ba': (37 commits)
  Allows Retina hi res enabled via App or Project.xcconfig (openframeworks#7971)
  actions changes (openframeworks#7968)
  Changing exr to hdr files for compatability with windows (openframeworks#7964)
  ofMesh - newfaces push_back to insert a list (openframeworks#7772)
  restore default-copy-constructibility of ofEvent (openframeworks#7969)
  [actions] ccache update (openframeworks#7967)
  Core small changes (openframeworks#7952)
  config.emscripten.default.mk for Emscripten >= 3.1.52 (openframeworks#7909)
  Fix edge case in findDelimiter (openframeworks#7911)
  oscpack / udpSocket: invert the "break_" semaphore (openframeworks#7963)
  ofxOscMessage: extra implicit adds [fixes something noted through openframeworks#7938 debugging] (openframeworks#7953) #changelog #ofxOsc
  ofThreadChannel::clear() to clear the channel (openframeworks#7921) #changelog #threadChannel
  OfxOscReceiver: from detach() to join() (openframeworks#7949)
  Update ofMathConstants.h (openframeworks#7958)
  [actions] update ubuntu 24.04 (openframeworks#7955)
  ofScopedMatrix (openframeworks#7946)
  [actions] - testing one action with multiple jobs for tests (openframeworks#7860)
  adding of.entitlements and vscode files to gitignore (openframeworks#7031)
  Make - use relative paths (openframeworks#7519)
  FPS timing with chrono (openframeworks#7867)
  ...

# Conflicts:
#	libs/openFrameworks/sound/ofAVEngineSoundPlayer.mm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants