Cropping a sprite batch
Posted: Tue Aug 10, 2021 9:09 pm
Currently, I'm drawing a tiled background. I do this by loading an image with all my tiles, creating quads for each tile in the image. To draw my tile map, I create a sprite batch, draw all the tiles into the batch, then draw the batch to the screen. Here is what it looks like:
However, I'd like to center the player and provide a "half tile" around the edges of the map, like in this mock-up below.
My initial thought to do this is to draw all the tiles, then crop half off each of the tiles around the perimeter of the sprite batch, but I can't find a way to do this. Anyone know how to do this, or, can you suggest a better way?
Here is how I'm loading my background tiles:
and here is how I'm creating the sprite batch:
Thanks!
However, I'd like to center the player and provide a "half tile" around the edges of the map, like in this mock-up below.
My initial thought to do this is to draw all the tiles, then crop half off each of the tiles around the perimeter of the sprite batch, but I can't find a way to do this. Anyone know how to do this, or, can you suggest a better way?
Here is how I'm loading my background tiles:
Code: Select all
imgBackgroundTiles = love.graphics.newImage("assets/graphics/Tiles.png")
tileSize = 40
tileQuads = {}
for i = 0, 3 do
tileQuads[i] = love.graphics.newQuad(i * tileSize, 0, tileSize, tileSize, imgBackgroundTiles:getWidth(), imgBackgroundTiles:getHeight())
end
mapWindowTilesWide = 10
mapWindowTilesHigh = 10
tileMapBatch = love.graphics.newSpriteBatch(imgBackgroundTiles, mapWindowTilesWide * mapWindowTilesHigh)
Code: Select all
mapData = love.filesystem.read("assets/maps/map" .. id .. ".map")
offset = 0
for y = 1, 30 do
map[y] = {}
for x = 1, 30 do
offset = offset + 1
cell = string.sub(mapData, offset, offset)
map[y][x] = tonumber(cell)
end
offset = offset + 2
end