[SOLVED][STI]crash when map:resize
Posted: Wed Jan 29, 2020 7:59 pm
I'm using karai17's STI library for my little project. Thanks karai17, it realy helps!
But when I try to implement zoom in or out the tiled map, the program just crashes every runtime without any error infomation to me. I have located the error to map:resize. If I delete this line, everything's ok and stable. I really need this function for zoom in and out the stage map, because the function map:draw with scale will also scale the drawsize of it, so that I need map:resize to change the drawarea back to original. Pls help and suggestion, thanks in advance!
I attach the love file and copy my urgly code as below:
mouse drag to pan the map
mouse wheel up/down to scale the map
But when I try to implement zoom in or out the tiled map, the program just crashes every runtime without any error infomation to me. I have located the error to map:resize. If I delete this line, everything's ok and stable. I really need this function for zoom in and out the stage map, because the function map:draw with scale will also scale the drawsize of it, so that I need map:resize to change the drawarea back to original. Pls help and suggestion, thanks in advance!
I attach the love file and copy my urgly code as below:
mouse drag to pan the map
mouse wheel up/down to scale the map
Code: Select all
sti = require('sti')
map = sti("assets/map01.lua")
main_canvas = love.graphics.newCanvas()
Game = {}
Game.tileWidth = 48
Game.tileHeight = 48
Game.tileRowNum = 30
Game.tileColNum = 30
Game.xScale = 1
Game.yScale = 1
Game.windowWidth = 48*15
Game.windowHeight = 48*10
local cam, map
local flag_XYRecorded
local dx, dy
local x_start, y_start
local scale --scale for sti
local main_canvas
local floor = math.floor
function clamp(value, min, max)
if max then
if value > max then return max end
end
if min then
if value < min then return min end
end
return value
end
function initCamera()
flag_XYRecorded = false
dx, dy = 0, 0
x_start, y_start = 0, 0
scale = 1
end
function drawWorld(dx, dy)
love.graphics.setCanvas(main_canvas)
love.graphics.clear()
width_change = floor(Game.tileRowNum*Game.tileWidth/scale)
height_change = floor(Game.tileColNum*Game.tileHeight/scale)
map:resize(width_change,height_change)
map:draw(dx,dy,scale, scale)
love.graphics.setCanvas()
love.graphics.draw(main_canvas, 0, 0, 0, 1)
end
function love.wheelmoved(x, y)
if y > 0 then
--Mouse wheel moved up
scale = scale + 0.1
elseif y < 0 then
--Mouse wheel moved down
scale = scale - 0.1
end
scale = clamp(scale, 0.5, 1.0)
end
----------------
function love:load()
map = sti("assets/map01.lua")
initCamera()
main_canvas = love.graphics.newCanvas(Game.tileRowNum*Game.tileWidth, Game.tileColNum*Game.tileHeight)
end
function love:update(dt)
-- print('update')
if love.mouse.isDown(1) then -- mouse key1 pressed
if not flag_XYRecorded then
local x, y = love.mouse.getPosition()
x_start = x - dx
y_start = y - dy
flag_XYRecorded = true
else
local x, y = love.mouse.getPosition()
dx = x - x_start
dy = y - y_start
end
else -- mouse key1 release
if flag_XYRecorded then
local x, y = love.mouse.getPosition()
dx = x - x_start
dy = y - y_start
dx = clamp(dx, floor(Game.windowWidth/Game.xScale-Game.tileWidth*Game.tileRowNum), 0)
dy = clamp(dy, floor(Game.windowHeight/Game.yScale-Game.tileHeight*Game.tileColNum), 0)
flag_XYRecorded = false
end
end
map:update(dt)
end
function love:draw()
drawWorld(dx,dy)
--debug info
local x, y = love.mouse.getPosition()
love.graphics.setColor(1, 0, 0)
love.graphics.print('scr: '..x..' '..y, 10, 10)
love.graphics.print('map: '..(x-dx*Game.xScale)..' '..(y-dy*Game.yScale), 10, 40)
love.graphics.print('init: '..dx..' '..dy, 10, 70)
love.graphics.print('start: '..x_start..' '..y_start, 10, 100)
love.graphics.print('scale: '..scale, 10, 130)
love.graphics.setColor(1, 1, 1)
end