-
-
Save crumblingstatue/923088e65a7bc458add7 to your computer and use it in GitHub Desktop.
Fixes for grid-based map
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
environment:draw(): Use _environment table instead of environment | |
_environment contains the actual objects, environment contains the module API | |
diff --git a/environment.lua b/environment.lua | |
index 3843489..948501e 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -24,7 +24,8 @@ end | |
function environment:draw() | |
for i = math.floor(camera.x / 32), math.floor(camera.x / 32) + 24 do | |
for j = math.floor(camera.y / 32), math.floor(camera.x / 32) + 17 do | |
- love.graphics.draw(spritesheet, props[environment[i][j].prop], environment[i][j].x, environment[i][j].y, 0, scale, scale) | |
+ local obj = _environment[i][j] | |
+ love.graphics.draw(spritesheet, props[obj.prop], obj.x, obj.y, 0, scale, scale) | |
end | |
end | |
end |
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
Print info about FPS and how many objects are drawn | |
This is useful for measuring how well the clipping works | |
diff --git a/environment.lua b/environment.lua | |
index 948501e..8943523 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -22,10 +22,12 @@ function environment:add(prop, x, y, width, height) | |
end | |
function environment:draw() | |
+ environment.objectsDrawn = 0 | |
for i = math.floor(camera.x / 32), math.floor(camera.x / 32) + 24 do | |
for j = math.floor(camera.y / 32), math.floor(camera.x / 32) + 17 do | |
local obj = _environment[i][j] | |
love.graphics.draw(spritesheet, props[obj.prop], obj.x, obj.y, 0, scale, scale) | |
+ environment.objectsDrawn = environment.objectsDrawn + 1 | |
end | |
end | |
end | |
diff --git a/main.lua b/main.lua | |
index 5cd97ff..0d157f5 100644 | |
--- a/main.lua | |
+++ b/main.lua | |
@@ -39,4 +39,6 @@ function love.draw() | |
player:draw() | |
camera:unset() | |
+ love.graphics.print("Objects drawn: " .. environment.objectsDrawn, 0, 0) | |
+ love.graphics.print("FPS: " .. love.timer.getFPS(), 0, 16) | |
end |
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
environment:draw(): Multiply tile positions by 32 | |
The tile positions are not multiplies of 32, so we must multiply | |
them by 32 to get the correct drawing offset. | |
diff --git a/environment.lua b/environment.lua | |
index 8943523..57b0f94 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -26,7 +26,7 @@ function environment:draw() | |
for i = math.floor(camera.x / 32), math.floor(camera.x / 32) + 24 do | |
for j = math.floor(camera.y / 32), math.floor(camera.x / 32) + 17 do | |
local obj = _environment[i][j] | |
- love.graphics.draw(spritesheet, props[obj.prop], obj.x, obj.y, 0, scale, scale) | |
+ love.graphics.draw(spritesheet, props[obj.prop], obj.x * 32, obj.y * 32, 0, scale, scale) | |
environment.objectsDrawn = environment.objectsDrawn + 1 | |
end | |
end |
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
environment:draw(): Fix clipping algorithm | |
diff --git a/environment.lua b/environment.lua | |
index 57b0f94..7a3ad31 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -23,8 +23,10 @@ end | |
function environment:draw() | |
environment.objectsDrawn = 0 | |
- for i = math.floor(camera.x / 32), math.floor(camera.x / 32) + 24 do | |
- for j = math.floor(camera.y / 32), math.floor(camera.x / 32) + 17 do | |
+ local xoff = -math.floor(camera.x / 32) - 1 | |
+ local yoff = -math.floor(camera.y / 32) - 1 | |
+ for i = xoff, xoff + 25 do | |
+ for j = yoff, yoff + 19 do | |
local obj = _environment[i][j] | |
love.graphics.draw(spritesheet, props[obj.prop], obj.x * 32, obj.y * 32, 0, scale, scale) | |
environment.objectsDrawn = environment.objectsDrawn + 1 |
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
environment:draw(): Use a sprite batch for drawing | |
This makes drawing a lot faster | |
diff --git a/environment.lua b/environment.lua | |
index 7a3ad31..797bf78 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -21,17 +21,21 @@ function environment:add(prop, x, y, width, height) | |
table.insert(_environment[x][y], {prop = prop, x = x, y = y, width = width * scale, height = height * scale}) | |
end | |
+local batch = love.graphics.newSpriteBatch(spritesheet, 600, "stream") | |
+ | |
function environment:draw() | |
+ batch:clear() | |
environment.objectsDrawn = 0 | |
local xoff = -math.floor(camera.x / 32) - 1 | |
local yoff = -math.floor(camera.y / 32) - 1 | |
for i = xoff, xoff + 25 do | |
for j = yoff, yoff + 19 do | |
local obj = _environment[i][j] | |
- love.graphics.draw(spritesheet, props[obj.prop], obj.x * 32, obj.y * 32, 0, scale, scale) | |
+ batch:add(props[obj.prop], obj.x * 32, obj.y * 32) | |
environment.objectsDrawn = environment.objectsDrawn + 1 | |
end | |
end | |
+ love.graphics.draw(batch) | |
end | |
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
environment:draw(): Don't try to draw objects that don't exist | |
This avoids errors when the camera goes out of map bounds. | |
diff --git a/environment.lua b/environment.lua | |
index 797bf78..4f40fa6 100644 | |
--- a/environment.lua | |
+++ b/environment.lua | |
@@ -30,9 +30,15 @@ function environment:draw() | |
local yoff = -math.floor(camera.y / 32) - 1 | |
for i = xoff, xoff + 25 do | |
for j = yoff, yoff + 19 do | |
- local obj = _environment[i][j] | |
- batch:add(props[obj.prop], obj.x * 32, obj.y * 32) | |
- environment.objectsDrawn = environment.objectsDrawn + 1 | |
+ local obj | |
+ local row = _environment[i] | |
+ if row then | |
+ obj = _environment[i][j] | |
+ end | |
+ if obj then | |
+ batch:add(props[obj.prop], obj.x * 32, obj.y * 32) | |
+ environment.objectsDrawn = environment.objectsDrawn + 1 | |
+ end | |
end | |
end | |
love.graphics.draw(batch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment