Skip to content

Commit

Permalink
Add a single axis version of draw_tiled
Browse files Browse the repository at this point in the history
draw_tiled -> draw_tiled_xy.

draw_tiled_single_axis a lot like draw_tiled_xy, but it operates on the
input axis instead of both.

These seems like a better compromise than trying to support axis
selection in draw_tiled_xy.
  • Loading branch information
idbrii committed Jan 11, 2022
1 parent 84c56a9 commit d391d12
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ how the layer is displayed.
- `dx, dy`: _numbers_. Increment the offset by these values.


### layer:draw_tiled(x, y, image)
### layer:draw_tiled_xy(x, y, image)

Draw an image (or Canvas or Video) tiled using a minimal number of draws. Uses
inverseTransformPoint so transformations from your camera should ensure only
Expand All @@ -133,6 +133,18 @@ visible tiles are drawn.
- returns: _number_. The number of times the image was drawn.


### layer:draw_tiled_single_axis(x, y, image, axis)

Draw an image (or Canvas or Video) tiled using a minimal number of draws and
only along a single axis. See also draw_tiled_xy.

- `x, y`: _numbers_. Tune the positioning of the image.
- `image`: _Drawable_. A drawable (Image, Canvas) that provides getDimensions to draw tiled.
- `axis`: _string_. Either `x` to tile horizontally or `y` to tile vertically.

- returns: _number_. The number of times the image was drawn.


## Credits

* kikito for [gamera](https://github.com/kikito/gamera) which I built this library to work with.
Expand Down
43 changes: 42 additions & 1 deletion parallax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
-- Call from within your function passed to parallax:draw to infinitely tile a
-- background image. If you've implemented rotation or resolution independence,
-- you may want to copy this into your own project to modify as needed.
function parallax:draw_tiled(x, y, image)
function parallax:draw_tiled_xy(x, y, image)
local art_pixels_x,art_pixels_y = image:getDimensions()
local min_x,min_y = love.graphics.inverseTransformPoint(0,0)
local max_x,max_y = love.graphics.inverseTransformPoint(love.window.getMode())
Expand Down Expand Up @@ -70,6 +70,47 @@ function parallax:draw_tiled(x, y, image)
return count
end

-- Call from within your function passed to parallax:draw to infinitely tile a
-- background image on a single axis. If you've implemented rotation or
-- resolution independence, you may want to copy this into your own project to
-- modify as needed.
local cache = { -- cache to avoid per-frame table creation
pos = {}, min = {}, max = {}, size = {}
}
function parallax:draw_tiled_single_axis(x, y, image, axis)
local pos, min, max, art_pixels = cache.pos, cache.min, cache.max, cache.size
pos.x,pos.y = x,y
art_pixels.x,art_pixels.y = image:getDimensions()
min.x,min.y = love.graphics.inverseTransformPoint(0,0)
max.x,max.y = love.graphics.inverseTransformPoint(love.window.getMode())

assert(pos[axis], "axis must be x or y")

-- Offset in steps of image width to first visible.
local num_before_pos = math.floor((min[axis] - pos[axis]) / art_pixels[axis])
pos[axis] = pos[axis] + (art_pixels[axis] * num_before_pos)

local count = 0
repeat
love.graphics.draw(image, pos.x, pos.y)
count = count + 1
-- DEBUG: Indicate where image starts to help debug gaps/mismatch.
--~ love.graphics.rectangle('fill', pos.x, pos.y, 50, max.y)
--~ love.graphics.rectangle('fill', pos.x, pos.y, max.x, 50)
pos[axis] = pos[axis] + art_pixels[axis]
until pos[axis] > max[axis]

--~ -- DEBUG: Show screen left/right to show we can draw without moving.
--~ local w,h = 100,2000
--~ love.graphics.setColor(0.3,0,1,1) -- dark purple
--~ love.graphics.rectangle('fill', min.x, min.y, w, h)
--~ love.graphics.setColor(0.7,0,1,1) -- light purple
--~ love.graphics.rectangle('fill', max.x-w, min.y, w, h)
--~ love.graphics.setColor(1,1,1,1)

return count
end

-- To adjust a layer's position relative to the target.
function parallax:setOffset(x,y)
self.offsetX = x or 0
Expand Down

0 comments on commit d391d12

Please sign in to comment.