Two Windows and ci::app::App::get()->getWindowSize()

Hey Embers,

I couldn’t figure out why, for the life of me, the getWindowSize() call was yielding differing values at seemingly random until I finally realized that the fact that I have two windows could influence this particular call. Two windows - D’uh - I know. I just (mistakenly) assumed it was always the primary window this particular call would refer to. And just to complete the story I’d honestly forgotten all about the secondary window given that I currently rarely use it. But rest assured I do plan to - in the future.

Cinders innards are still a little nebulous to me, so I’m wondering if someone has dealt with this before and knows of a method in cinder to indicate which window to let this (and similar function calls) refer to, if you have more than one window.

I know an option would be to pass around a reference to the window in question. For now, this is an approach I’d like to avoid.

Much obliged,
Gazoo

Cinder has a function that lets you choose which window you’re referring to.

ci::app::WindowRef winRef = getWindowIndex(0);
Which accepts an number between 0 and getNumWindows()

From here you can call normal functions on the window like
winRef->getWidth();
or anything else listed in the docs. (https://libcinder.org/docs/classcinder_1_1app_1_1_window.html)

If you’re checking the window size from the virtual App::draw() method, yes that will get called once per window, but if you instead register your draw method with the window->getSignalDraw(), that will only be called for the respective window and you’ll always get the expected window size.

I believe that in App::update(), calling getWindow() will always return the primary window.

@sharkbox - Fantastic, that’s exactly what I was looking for. If you were standing next to me now, I’d give you a high five.

@rich.e - I’m slightly confused by your statements. Are you saying that if I have 2 window and call getWindowSize() in the App::draw() method it’ll return alternating values? From what you’d wrote I’d almost think you’re saying it would be called twice which I don’t quite understand.

Furthermore, I think I can confirm that calling getWindow() in the App::update() function does not necessarily yield the primary window. The following two calls:

CI_LOG_I( " ci::app::App::get()->getWindowSize() " << ci::app::App::get()->getWindowSize() );
CI_LOG_I( " ci::app::App::get()->getWindow().get()->getSize() " << ci::app::App::get()->getWindow().get()->getSize() );

called inside of an object (called from within App::update() yield a size pertaining to the secondary window.

draw will be called once for each window. So if you have two windows, it will be called twice per frame. You read it right, Rich is explicitly saying that App::draw will be called twice.

If you want to have some other drawing functions called specific to a window, you can connect to that window’s draw signal.

@sansumbrella My confusion stems from the fact that grammatically, @rich.e, seemed to be referring to the function to check the window size being called twice and not App::draw().

I think I’ve got the hang of the situation now thou - thanks for following up.

I’m having trouble connecting my drawing function to a specific window, I noticed on old posts (2-3 years old) that there was a connectDraw() that could link the functions to a window but I can’t find it anywhere in the documentation.

How can I do this? any guidance is greatly appreciated.

Thank you

Take a look at the app::Window's getSignalDraw() method.

The signals for different events emitted by Cinder are now accessed through a more consistent getSignal*() interface. Windows have signals for various user input events as well as draw.

e.g.

getWindow(0)->getSignalDraw().connect([this] { drawFirstWindow(); });

@sansumbrella & @rich.e Thank you very much !

Hey people,

Just a short follow up on this matter. I’ve recently happened upon another issue caused by the getWindowSize() function. In the Cinder’s source the following text can be found:

> //! Returns the size of the App's current window measured in points
> 	ivec2				getWindowSize() const { return ivec2( getWindowWidth(), getWindowHeight() ); }

My current understanding is that outside of the draw calls, you also cannot depend on this function to yield the size of the active window (which is what I interpret from current window). In my application, I have the main window focused, but calling this function like so:

ci::app::App::get()->getWindowSize();

Will yield the size of my other window. The text should be updated in the Cinder source I’d say, no?

Regards,
Gazoo

Hi,

Usually I store references to the windows and call the getWindowSize on the references. It is not prone to the state dependent errors.

Bala.

I am using the approach of the BasicAppMultiWindow sample, saving the index or name of my window with setUserData(). Then I make a simple case distinction in the draw() function, and getWindowWidth() etc always yield the expected results.

1 Like