Skip to content

Instantly share code, notes, and snippets.

@thacuber2a03
Last active November 10, 2024 18:14
Show Gist options
  • Save thacuber2a03/f116686326570b6d1f6316922f45cf72 to your computer and use it in GitHub Desktop.
Save thacuber2a03/f116686326570b6d1f6316922f45cf72 to your computer and use it in GitHub Desktop.
a Love2D 11.x simple wrapper library for rendering games with a fixed update rate and chunky pixels
local floor = math.floor
local canvas = {}
canvas._version = "0.1.0"
canvas.frame, canvas.time = 0, 0
canvas.width = 240
canvas.height = floor(canvas.width / (16 / 9))
canvas.pixelPerfect = true
canvas.backgroundColor = { 0, 0, 0, 1 }
canvas.xOffset, canvas.yOffset = 0, 0
local lag, msPerUpdate = 0, 0
local width, height, xScale, yScale, topLeftX, topLeftY
function canvas.framerate(n)
if n then
canvas._framerate = n
msPerUpdate = 1 / n
end
return canvas._framerate
end
canvas.framerate(60)
local function scaleX(x) return (x-topLeftX)/xScale end
local function scaleY(y) return (y-topLeftY)/yScale end
function canvas.getMouseX() return scaleX(love.mouse.getX()) end
function canvas.getMouseY() return scaleY(love.mouse.getY()) end
function canvas.getMousePosition() return canvas.getMouseX(), canvas.getMouseY() end
local function applyIf(f, ...) if f then return f(...) end return end
function love.mousepressed(x, y, ...) applyIf(canvas.mousepressed, scaleX(x), scaleY(y), ...) end
function love.mousemoved(x, y, dx, dy, ...) applyIf(canvas.mousemoved, scaleX(x), scaleY(y), scaleX(dx), scaleY(dy), ...) end
function love.mousereleased(x, y, ...) applyIf(canvas.mousereleased, scaleX(x), scaleY(y), ...) end
local function resize(w, h)
width, height = w, h
xScale, yScale = w / canvas.width, h / canvas.height
if xScale < yScale then yScale = xScale else xScale = yScale end
if canvas.pixelPerfect then
xScale, yScale = floor(xScale), floor(yScale)
end
topLeftX, topLeftY = (width - canvas.width * xScale) / 2, (height - canvas.height * yScale) / 2
end
function love.load(...)
canvas._canvas = love.graphics.newCanvas(canvas.width, canvas.height)
---@diagnostic disable-next-line: missing-parameter
canvas._canvas:setFilter "nearest"
resize(love.graphics.getDimensions())
love.window.updateMode(width, height, {
minwidth = canvas.width, minheight = canvas.height,
})
applyIf(canvas.load, ...)
end
love.resize = resize
local delta = 0
function love.update(dt)
delta = dt
lag = lag + dt
while lag >= msPerUpdate do
applyIf(canvas.update)
lag = lag - msPerUpdate
end
end
function love.draw()
if canvas.draw then
love.graphics.push "all"
love.graphics.setCanvas(canvas._canvas)
love.graphics.clear(love.graphics.getBackgroundColor())
canvas.draw(lag / msPerUpdate)
love.graphics.setCanvas()
love.graphics.pop()
end
love.graphics.clear(canvas.backgroundColor)
love.graphics.translate(canvas.xOffset, canvas.yOffset)
love.graphics.draw(canvas._canvas, topLeftX, topLeftY, 0, xScale, yScale)
canvas.time = canvas.time + delta
canvas.frame = canvas.frame + 1
end
return canvas
-- Copyright (c) 2024 @thacuber2a03
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
@thacuber2a03
Copy link
Author

thacuber2a03 commented Nov 8, 2024

changelog

format: dd/mm/yyyy

10/11/2024:

  • override love.mouse* callbacks

08/11/2024:

  • initial release
  • limit the maximum size of the window to the maximum size of the virtual canvas
  • add getMouseX(), getMouseY(), getMousePosition(), canvas.xOffset and canvas.yOffset
  • optimize getMouse*() and virtual canvas drawing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment