0
\$\begingroup\$

I've set up a basic map with Tiled, imported it with STI library (https://github.com/karai17/Simple-Tiled-Implementation) and everything looks in place.

How can i get a zoom-out feature now? I can kinda zoom in using scalex and scaley and i tried the same procedure to zoom out.

However, if i set i.e. scalex = 0.5, scaley = 0.5 the map actually scales in size but it only fit 1/4 the screen size, and the remaining 3/4 are just black space.

\$\endgroup\$
1
\$\begingroup\$

This code just uses individual tile images that must each be 100x100 px. The zoom still needs a bit of tweaking.

function love.load()

  tileImage = love.graphics.newImage("tileImage.png") --tile size is currently 100x100px
  tileSize = 100

  mapLength,mapHeight = 20,10
  zoom, zoomSpeed = 0.3, 0.05
  cameraX,cameraY = 1,1
  cameraSpeed = 0.1
  zoomOffset = 0.01

  map = {}

  for x = 0,mapLength+1 do
    map[x] = {}

    for y = 0,mapHeight+1 do

      map[x][y] = {tileImage}

    end
  end



end

function love.draw()

  for x=1,mapLength do
    for y=1, mapHeight do
        love.graphics.draw(map[x][y][1],applyScrollX(x),applyScrollY(y),0,zoom,zoom)
    end
  end

end

function love.update()

  print(zoom..", "..cameraX..", "..cameraY)

  if love.keyboard.isDown('w') then cameraY = cameraY + cameraSpeed end
  if love.keyboard.isDown('a') then cameraX = cameraX + cameraSpeed end
  if love.keyboard.isDown('s') then cameraY = cameraY - cameraSpeed end
  if love.keyboard.isDown('d') then cameraX = cameraX - cameraSpeed end

  function love.wheelmoved(x, y)
    if y > 0 then
        zoom = zoom + zoomSpeed 
    cameraX,cameraY = cameraX - zoomOffset, cameraY - zoomOffset
    elseif y < 0 then
        zoom = zoom - zoomSpeed 
    cameraX,cameraY = cameraX + zoomOffset, cameraY + zoomOffset
    end
end

end

function applyScrollX(num) --num is x position in tiles

  return num*zoom*tileSize+(cameraX*tileSize)

end

function applyScrollY(num)

  return num*zoom*tileSize+(cameraY*tileSize)

end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.