Cropping a sprite batch

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
TheAlmightyGuru
Prole
Posts: 2
Joined: Tue Aug 10, 2021 8:56 pm

Cropping a sprite batch

Post 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!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Cropping a sprite batch

Post 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.
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Cropping a sprite batch

Post 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.
Last edited by togFox on Fri Aug 13, 2021 5:50 am, edited 1 time in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
TheAlmightyGuru
Prole
Posts: 2
Joined: Tue Aug 10, 2021 8:56 pm

Re: Cropping a sprite batch

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests