Cinder applications can respond to resizing the window by implementing the resize event. This method takes a ci::app::ResizeEvent
parameter with information about the event.
If your application doesn't have a resize
method, implement one. In the application's class declaration, add the following line of code:
void resize( ResizeEvent event );
In the method's implementation, you can use the ResizeEvent
parameter to find information about the window's new size and format.
We will learn how to work with the ci::app::ResizeEvent
parameter to respond to window resize events. Perform the following steps to do so:
getSize
method which returns a ci::Vec2iwith
object, the window's width as the x component, and the height as the y component.Vec2i windowSize = event.getSize();
The getWidth and getHeight
methods both return int
values with the window's width and height respectively, for example:
int width = event.getWidth(); int height = event.getHeight();
getAspectRatio
method returns a float
value with the aspect ratio of the window, which is the ratio between its width and height:float ratio = event.getAspectRatio();
ci::Rectf
object in the class declaration:Rect frect;
In the setup we set its properties so that it has a 20 pixel margin on all sides from the window:
rect.x1 = 20.0f; rect.y1 = 20.0f; rect.x2 = getWindowWidth() – 20.0f; rect.y2 = getWindowHeight() – 20.0f;
draw
method:gl::color( Color( 1.0f, 0.0f, 0.0f ) ); gl::drawSolidRect( rect );
resize
method, we must recalculate the rectangle properties so that it resizes itself to maintain the 20 pixel margin on all sides of the window:rect.x1 = 20.0f; rect.y1 = 20.0f; rect.x2 = event.getWidth() – 20.0f; rect.y2 = event.getHeight() – 20.0f;
18.119.103.204