Created
November 6, 2017 17:36
-
-
Save F483/7aaed9279b37eeef7137fc3848e6cb72 to your computer and use it in GitHub Desktop.
Slow rendering of tilemap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tiles_path = "tiles.png" | |
tiles_horizontal = 4 | |
tiles_vertical = 4 | |
tiles_img = nil | |
tiles_quads = {} | |
tiles_size = 8 | |
map_width = 100 | |
map_height = 75 | |
map_tiles = nil | |
function love.load() | |
-- load image | |
tiles_img = love.graphics.newImage(tiles_path) | |
tiles_img:setFilter("nearest") | |
-- generate quads lookup table | |
local iw, ih = tiles_img:getDimensions() | |
for x=1, tiles_horizontal do | |
tiles_quads[x] = {} | |
for y=1, tiles_vertical do | |
local qx = tiles_size * (x - 1) | |
local qy = tiles_size * (y - 1) | |
tiles_quads[x][y] = love.graphics.newQuad(qx, qy, tiles_size, | |
tiles_size, iw, ih) | |
end | |
end | |
-- generate tilemap | |
map_tiles = {} | |
for x=1, map_width do | |
map_tiles[x] = {} | |
for y=1, map_height do | |
local tx = math.ceil(love.math.random() * 4) | |
local ty = math.ceil(love.math.random() * 4) | |
map_tiles[x][y] = {x=tx, y=ty} | |
end | |
end | |
end | |
function love.draw() | |
love.window.setTitle("FPS: "..love.timer.getFPS()) | |
-- render tilemap | |
for mx=1, map_width do | |
for my=1, map_height do | |
local tile = map_tiles[mx][my] | |
local quad = tiles_quads[tile.x][tile.y] | |
local sx = (mx - 1) * tiles_size | |
local sy = (my - 1) * tiles_size | |
love.graphics.draw(tiles_img, quad, sx, sy) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment