2D camera that defines what region is shown on screen.
1 auto window = RenderWindow(); 2 auto view = View(); 3 4 // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200 5 view.reset(FloatRect(100, 100, 400, 200)); 6 7 // Rotate it by 45 degrees 8 view.rotate(45); 9 10 // Set its target viewport to be half of the window 11 view.setViewport(FloatRect(0.f, 0.f, 0.5f, 1.f)); 12 13 // Apply it 14 window.view = view; 15 16 // Render stuff 17 window.draw(someSprite); 18 19 // Set the default view back 20 window.view = window.getDefaultView(); 21 22 // Render stuff not affected by the view 23 window.draw(someText);
$(PARA See also the note on coordinates and undistorted rendering in $(TRANSFORMABLE_LINK).)
$(RENDERWINDOW_LINK), $(RENDERTEXTURE_LINK)
$(U View) defines a camera in the 2D scene. This is a very powerful concept: you can scroll, rotate or zoom the entire scene without altering the way that your drawable objects are drawn.
A view is composed of a source rectangle, which defines what part of the 2D scene is shown, and a target viewport, which defines where the contents of the source rectangle will be displayed on the render target (window or texture).
The viewport allows to map the scene to a custom part of the render target, and can be used for split-screen or for displaying a minimap, for example. If the source rectangle has not the same size as the viewport, its contents will be stretched to fit in.
To apply a view, you have to assign it to the render target. Then, every objects drawn in this render target will be affected by the view until you use another view.