Page 1 of 1

Cropping a sprite batch

Posted: Tue Aug 10, 2021 9:09 pm
by TheAlmightyGuru
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:

Image

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.

Image

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)
and here is how I'm creating the sprite batch:

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
Thanks!

Re: Cropping a sprite batch

Posted: Wed Aug 11, 2021 8:28 pm
by grump
Looks more like scrolling than cropping? Translate the map by -halfTileSize on each axis, e. g. with love.graphics.translate. Clipping/cropping can be achieved by drawing the map to a Canvas, or with love.graphics.setScissors if you don't wanna use a Canvas.

Re: Cropping a sprite batch

Posted: Thu Aug 12, 2021 3:39 am
by togFox
I just draw tiles "off" the screen, in whatever -ve x/y value is necessary. They will be drawn as per the x/y values and if that means half a tile is drawn on the screens edge then love/graphics/GPU/magic simply draws half the tile at the edge of the screen.

Perhaps you're not drawing right to the screen edge? If not then my technique won't help.

Re: Cropping a sprite batch

Posted: Thu Aug 12, 2021 2:23 pm
by TheAlmightyGuru
grump: setScissors is what I was looking for! Scrolling might come later, for now, I just needed to know how to crop.

togFox: I'm not drawing along the edge of the window, but thanks for the advice still.