Clipping
Clipping is the term used within computer graphics for not displaying graphics which are outside of the users viewport. The viewport most commonly recognized is that of the screen. To reduce the expense of calculating the position and appearance of graphics, the graphics which are outside of the viewport are clipped and therefore not drawn.
Whilst the screen is the most common viewport, there are many occasions when you would want to use a clipping area which is say smaller than the screen size. Every window which is opened in OS X or Windows has a clipping area which is smaller than the main screen, so that anything in those windows which falls outside of the windows bounds is not drawn.
Clipping within Slick[edit]
Within slick it is extremely easy to create and use clipping regions within your games. The graphics context (org.newdawn.slick.Graphics) provides a number of methods which allow you to set or get the world clipping.
There are a couple of different types of clipping supported:
World Clipping - This clips the graphics context and is also effected by any changes made to the graphics context such as rotation
Clipping - This clips the viewport and is not effected by any changes to the graphics context. So creates a window through which you can see what is rendered to the graphics context and which is visible through the bounds defined by the clipping Clipping Examples
Example Code[edit]
The following example shows you how you can create a new world clipping region. You will see there are a couple of ways of creating the clipping regions. The first uses an x, y, width, height. The second takes a Rectangle as input to define the region.
g.setWorldClip((int) x, (int) y, (int) width, (int) height);
or
Rectangle rect = new Rectangle((int) x, (int) y, (int) width, (int) height); g.setWorldClip(rect);
This next example does the same but using clipping not world clipping.
g.setClip((int) x, (int) y, (int) width, (int) height);
or
Rectangle rect = new Rectangle((int) x, (int) y, (int) width, (int) height); g.setClip(rect);
To clear any clipping or world clipping which has been defined you use
g.clearClip(); g.clearWorldClip();
It is also possible to get either the clipping or world clipping which has been defined. For both cases NULL is returned if clipping has not been defined.
Rectangle clippingRect = g.getClipping(); Rectangle clippingWorldRect = g.getWorldClipping();