Re: Custom map loading technique
Posted: Tue Jul 09, 2019 1:43 pm
I haven't tested the code, but that sounds like the usual problem of gaps between squares. Just be careful to make your quads always be drawn at integer coordinates.
Code: Select all
function love.load()
-- our tiles
tile = {}
for i=0,3 do -- change 3 to the number of tile images minus 1.
tile[i] = love.graphics.newImage( "tile"..i..".png" )
end
-- the map (random junk + copy and paste)
map={
{ 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0},
{ 3, 1, 0, 0, 2, 2, 2, 0, 3, 0, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0, 0},
{ 3, 1, 0, 0, 2, 0, 2, 0, 3, 0, 3, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0},
{ 3, 1, 1, 0, 2, 2, 2, 0, 0, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0},
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2},
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 2, 2, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0},
{ 0, 2, 0, 0, 0, 3, 0, 3, 0, 1, 0, 1, 0, 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, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}
-- map variables
map_w = #map[1] -- Obtains the width of the first row of the map
map_h = #map -- Obtains the height of the map
map_x = 0
map_y = 0
map_display_buffer = 2 -- We have to buffer one tile before and behind our viewpoint.
-- Otherwise, the tiles will just pop into view, and we don't want that.
map_display_w = 20
map_display_h = 15
tile_w = 16
tile_h = 16
end
function draw_map()
offset_x = map_x % tile_w
offset_y = map_y % tile_h
firstTile_x = math.floor(map_x / tile_w)
firstTile_y = math.floor(map_y / tile_h)
for y=1, (map_display_h + map_display_buffer) do
for x=1, (map_display_w + map_display_buffer) do
-- Note that this condition block allows us to go beyond the edge of the map.
if y+firstTile_y >= 1 and y+firstTile_y <= map_h
and x+firstTile_x >= 1 and x+firstTile_x <= map_w
then
love.graphics.draw(
tile[map[y+firstTile_y][x+firstTile_x]],
((x-1)*tile_w) - offset_x - tile_w/2,
((y-1)*tile_h) - offset_y - tile_h/2)
end
end
end
end
function love.update( dt )
local speed = 300 * dt
-- get input
if love.keyboard.isDown( "up" ) then
map_y = map_y - speed
end
if love.keyboard.isDown( "down" ) then
map_y = map_y + speed
end
if love.keyboard.isDown( "left" ) then
map_x = map_x - speed
end
if love.keyboard.isDown( "right" ) then
map_x = map_x + speed
end
if love.keyboard.isDown( "escape" ) then
love.event.quit()
end
-- check boundaries. remove this section if you don't wish to be constrained to the map.
--if map_x < 0 then
-- map_x = 0
--end
--if map_y < 0 then
--map_y = 0
--end
--if map_x > map_w * tile_w - map_display_w * tile_w - 1 then
--map_x = map_w * tile_w - map_display_w * tile_w - 1
--end
--if map_y > map_h * tile_h - map_display_h * tile_h - 1 then
-- map_y = map_h * tile_h - map_display_h * tile_h - 1
--end
end
function love.draw()
draw_map()
end
You can both use spritebatch and round the coordinates. Just don't set the quads in the spritebatch to be at non-integer coordinates, nor draw the spritebatch to the screen at non-integer coordinates.sphyrth wrote: ↑Wed Jul 10, 2019 11:31 am So I got a clue from pgimeno's comment. The cause of the black lines are y coordinates being set to decimals. Rounding them up or down with math.ceil() and math.floor() fixed the issue.
Here's the love file for the people who want to check it out.
loopingmap.love
Although using Spritebatch is still recommended, I'll try to port that concept in bobbymcbobface's code.
Code: Select all
tilesetBatch:add(tileQuads[map[x][y].tile], math.floor(map[x][y].x), math.
floor(map[x][y].y))
sorry to be a pain but I'm having trouble with this
Code: Select all
function tilequad(x, y, count)
local start = #tileQuads + 1 -- The #<tablename> (in this case, the tablename is tileQuads) counts the number of max tiles.
-- the +1 means we'll start at the number NEXT to the last tile.
-- This means we won't override anything before the table.
for i = start, start + count do
tileQuads[i] = love.graphics.newQuad(x * tileSize, y * tileSize, tileSize, tileSize, tilesetImage:getWidth(), tilesetImage:getHeight())
end
end
--Now try this
tilequad(0, 0, 2000)
tilequad(0, 3, 500)