Using setPattern() with an offset

I'm trying to offset a pattern so it matches the arbitrarily placed polygons in my game. For some reason, the result of this code:

local pattern = {0x1E, 0x3C, 0x78, 0xF0, 0xE1, 0xC3, 0x87, 0xF}
local offset_x = 0
local offset_y = 0
playdate.graphics.setPattern(pattern, offset_x, offset_y)

Is the same as this:

local pattern = {0x1E, 0x3C, 0x78, 0xF0, 0xE1, 0xC3, 0x87, 0xF}
local offset_x = 4
local offset_y = 4
playdate.graphics.setPattern(pattern, offset_x, offset_y)

How does the offset work? What am I missing?

The offset is only for when you're using an image as the pattern source, so you can locate the position of the 8x8 square inside the image. I'll file an issue to add a warning there if you use a pattern+offset to explain that. Thanks for finding this!

..okay, just to be pedantic, and because I can't not do something once I get the idea in my head, here's a function that shifts an array pattern:

local gfx = playdate.graphics
local pattern = {0xf0, 0xf0, 0xf0, 0xf0, 0x0f, 0x0f, 0x0f, 0x0f}

function shiftPattern(p, x, y)
  out = {}
  for i=1,8 do
    local n = p[1+(i-y-1)%8] -- Lua's % works correctly with negative numbers!
    out[i] = ((n>>(x%8)) | (n<<(8-(x%8)))) & 0xff
  end
  return out
end

x = 20
y = 20

function playdate.update()
  gfx.clear()
  gfx.setPattern(shiftPattern(pattern, x, y))
  gfx.fillEllipseInRect(x,y,40,40)
  x += 1
  y += 1
end
3 Likes

Ah ok, gotcha. And thanks for offering a workaround, that's neat!