SetCanvas() breaks drawing when using Tiled/STI
Posted: Thu Jul 07, 2016 8:21 pm
Hi, I've been working on a math demo using Tiled and the Simple Tiled Implementation library. I've got an issue where when I use setCanvas() to return to drawing the main screen, no graphics calls work until the end of the function setCanvas() is in. I'm trying to draw a 16th size canvas onto the main screen, but nothing appears except for the background. Commenting out the setCanvas calls makes the character draw normally, with the points I want to draw to the "visibleImage" canvas appearing at the top left of the world. Excuse how slow the program is, it iterates over a lot of lists and I'm working on optimizing it.
Here's the code causing the issue, and after that is the .love file, which has a bunch of stuff in it including STI, bump, anim8, an implementation of A* i'm not currently using, etc. Just head to main.lua in the main folder.
EDIT: I've gotten my drawing stuff working! ...mostly. I managed to get things working first with the method that cvai posted. Then I moved stuff around till the drawing order was correct. Now i'm dealing with translation funny business. Right now everything draws, but the points that are supposed to match the tiles are misaligned. It looks like the screenshot below. The "halo" should be centered around the player, but it's off somehow. If anyone wants to take a shot at fixing it, it'd help a lot. I've updated the .love file.
Here's the code causing the issue, and after that is the .love file, which has a bunch of stuff in it including STI, bump, anim8, an implementation of A* i'm not currently using, etc. Just head to main.lua in the main folder.
Code: Select all
function slayer:draw()
local dx,dy = 0,0
local qx,qy = math.floor((self.player.x - (love.graphics.getWidth() / 2))/16),math.floor((self.player.y - (love.graphics.getHeight() / 2))/16)
local hx, hy = math.floor((self.player.x + (love.graphics.getWidth() / 2)) / 16), math.floor((self.player.y + (love.graphics.getHeight() / 2)) / 16)
love.graphics.setCanvas(self.visibleImage)
repeat
if has_val(self.player.visible,{['x'] = qx, ['y'] = qy}) then
love.graphics.setColor(255,255,255,0)
love.graphics.points(dx,dy)
else
love.graphics.setColor(0,0,0,128)
love.graphics.points(dx,dy)
end
qy = qy + 1
dy = dy + 1
if qy > hy then
qy = 1
dy = 1
qx = qx + 1
dx = dx + 1
end
until qx == hx and qy == hy
love.graphics.setColor(255,255,255,255)
love.graphics.setCanvas()
love.graphics.draw(self.visibleImage,(self.player.x - (love.graphics.getWidth() / 2)),(self.player.y - (love.graphics.getHeight() / 2)),0,16,16)
self.player.anims[self.player.currentAnim]:draw(
plsprite,
math.floor(self.player.x),
math.floor(self.player.y),
0,2,2,
self.player.ox,
self.player.oy)
end