Tilemap sourcerect

I noticed the tilemap draw method has the option to provide a sourcerect to only draw parts of the tilemap. playdate.graphics.tilemap:draw(x, y, [sourceRect])

I was wondering if anyone knew what this did and had an example of it working. I was unsure if this was a rect relative to the screen or the tilemap itself (and then, in world coordinates or tile coordinates?), but every rect I passed it resulted in the tilemap not drawing at all. I have a large tilemap so I was looking for ways to improve performance.
Thanks!

EDIT: Unrelated, is there an ETA for the Sampler to come to the Windows SDK?

1 Like

Jake, we are hoping to get the Sampler into the Windows SDK in time for our next release (0.10.3, due later this month.)

In regards to Tilemap, I’ll try to have someone qualified answer here soon! (And sorry for the delay in our response!)

2 Likes

When you specify sourceRect all it’s doing under the hood is setting a clip rect on the current graphics context the size of sourceRect then translating the tilemap drawing to line up the sourceRect with the x,y arguments to tilemap:draw(). Coordinates are in pixel coordinates, not tile coordinates.

Here’s a demo!

tilemap-sourcerect.pdx.zip (2.6 KB)

local gfx = playdate.graphics
local t = gfx.imagetable.new("slash")
local map = gfx.tilemap.new()
map:setImageTable(t)
map:setSize(10,6)

for n=0,60 do
	map:setTileAtPosition(1+n%10,1+n/10,math.random(2))
end

local mapx, mapy = 30, 30
local dx,dy = 0, 0

function playdate.update()
	local x = math.random(10)
	local y = math.random(6)
	local t = map:getTileAtPosition(x,y)
	map:setTileAtPosition(x,y,3-t)

	mapx += dx
	mapy += dy

	-- draw the tilemap's subrect (mapx,mapy,160,160) at screen coordinates (120,40)
	map:draw(120,40, mapx,mapy,160,160)
end

function playdate.leftButtonDown() dx -= 1 end
function playdate.leftButtonUp() dx += 1 end
function playdate.rightButtonDown() dx += 1 end
function playdate.rightButtonUp() dx -= 1 end
function playdate.upButtonDown() dy -= 1 end
function playdate.upButtonUp() dy += 1 end
function playdate.downButtonDown() dy += 1 end
function playdate.downButtonUp() dy -= 1 end

slash-table-40-40.png
slash-table-40-40

2 Likes

Thanks for the example, this helps a bunch!