summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthe lemons <citrons@mondecitronne.com>2023-03-25 20:02:27 -0500
committerthe lemons <citrons@mondecitronne.com>2023-03-25 20:04:03 -0500
commitd427eefea1d6ec979a5af7a511516f1f42b5c6f7 (patch)
tree9d9500b1d133631bbf82c60cdca2ecd5f0020423
parent96ae18ce6bd9647052896f71fc3289f1a0b94bed (diff)
viewport
-rw-r--r--evloop.lua2
-rw-r--r--main.lua6
-rw-r--r--viewport.lua41
3 files changed, 45 insertions, 4 deletions
diff --git a/evloop.lua b/evloop.lua
index 57e15c0..6c12c3a 100644
--- a/evloop.lua
+++ b/evloop.lua
@@ -97,8 +97,8 @@ function M.mainloop(start)
if not resume_task(main, {"update", dt}) then return 0 end
if love.graphics and love.graphics.isActive() then
- love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
+ require "viewport".origin()
if not resume_task(main, {"draw", dt}) then return 0 end
love.graphics.present()
end
diff --git a/main.lua b/main.lua
index 73f762b..6a53f05 100644
--- a/main.lua
+++ b/main.lua
@@ -1,4 +1,5 @@
local evloop = require "evloop"
+local viewport = require "viewport"
local playfield = require "game.playfield"
local tetrominoes = require "game.tetrominoes"
@@ -8,8 +9,7 @@ local piece
local function draw()
evloop.poll "draw"
- local _, height = love.graphics.getDimensions()
- local size = height / field.lines
+ local size = viewport.height / field.lines
love.graphics.setColor(0.1, 0.1, 0.1)
love.graphics.rectangle("fill",
@@ -18,7 +18,7 @@ local function draw()
local function draw_block(line, column)
local x = (column - 1) * size
- local y = height - line * size
+ local y = viewport.height - line * size
love.graphics.rectangle("fill", x, y, size, size)
end
diff --git a/viewport.lua b/viewport.lua
new file mode 100644
index 0000000..e54c9d8
--- /dev/null
+++ b/viewport.lua
@@ -0,0 +1,41 @@
+local M = {}
+
+M.width, M.height = 1920, 1080
+
+local dims = {love.graphics.getDimensions()}
+
+function M.scale()
+ local sw, sh = unpack(dims)
+ local vw, vh = unpack(M)
+ local scalex, scaley = sw / vw, sh / vh
+ if scalex * vh < sh then
+ return scalex
+ else
+ return scaley
+ end
+end
+
+function M.offset()
+ local sw, sh = unpack(dims)
+ local vw, vh = unpack(M)
+ local sc = M.scale()
+ local x, y = sw / 2 - vw * sc / 2, sh / 2 - vh * sc / 2
+ return x, y
+end
+
+function M.pos(win_x, win_y)
+ local x, y = M.offset()
+ local sc = M.scale()
+ return (win_x - x) / sc, (win_y - y) / sc
+end
+
+function M.origin()
+ dims = {love.graphics.getDimensions()}
+ M[1], M[2] = M.width, M.height
+
+ love.graphics.origin()
+ love.graphics.translate(M.offset())
+ love.graphics.scale(M.scale())
+end
+
+return M