Siemkowski
Siemkowski

Reputation: 1441

Restrict drawing to an area

The problem

I would like to have an area inside love2d in which movable objects are drawn. The movement of the objects is not restricted by the area boundries but drawing is. Think of it as looking outside through a window. For example: a blue rectangle in an area, if it moves to the side its drawing should be truncated to the boundries of the area.

Before moving:

area and object

After moving (wrong):

enter image description here

After moving (right):

enter image description here

Restrictions and assumptions

  1. You can assume the area is rectangular.
  2. The object to draw inside can be anything: polygon, image or text.
  3. The area covers anything behind it (as if it has its own background)
  4. Objects not 'belonging' to the area should be drawn as usual.

Atempted solutions

I know I could stop drawing objects as soon as they 'touch' the boundries of the area, but that would cause them to suddenly disappear and then appear when they are wholly inside the area. I guess it takes some sort of layering system but I have no clue on how to include that in love2d.

Upvotes: 3

Views: 435

Answers (1)

Spencer
Spencer

Reputation: 4158

I think you are looking for love.graphics.setScissor.

The scissor limits the drawing area to a specified rectangle.

Calling the function without any arguments (i.e. love.graphics.setScissor()) disables scissor.

Example:

function love.draw ()
  -- sets the drawing area to the top left quarter of the screen
  local width, height = love.graphics.getDimensions()
  love.graphics.setScissor(0, 0, width / 2, height / 2)
  -- code to draw things
  love.graphics.setScissor()
end

Upvotes: 4

Related Questions