loveutils: better comments and organisation

This commit is contained in:
Kazhnuz 2019-04-11 17:10:28 +02:00
parent 33143a0ba3
commit 65010fae16
4 changed files with 133 additions and 18 deletions

View file

@ -1,3 +1,26 @@
-- loveutils.filesystem : functions to handle filesystem.
--[[
Copyright © 2019 Kazhnuz
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.
]]
local Filesystem = {} local Filesystem = {}
function Filesystem.exists(filepath) function Filesystem.exists(filepath)

View file

@ -1,24 +1,38 @@
-- loveutils.graphics : a set of useful functions for love2D. Aim to reduce
-- boilerplate in love2D by creating usefull function to handle these roles.
--[[
Copyright © 2019 Kazhnuz
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.
]]
local Graphics = {} local Graphics = {}
-- COLOR FUNCTIONS
-- Handle colors and scene colors
function Graphics.resetColor() function Graphics.resetColor()
love.graphics.setColor(1,1,1,1) love.graphics.setColor(1,1,1,1)
end end
function Graphics.box(x, y, w, h) -- PRINT TEXT
local x = math.floor(x) -- Functions to draw text on screen
local y = math.floor(y)
local w = math.floor(w)
local h = math.floor(h)
local a = a or 1
local r, g, b, a = love.graphics.getColor( )
love.graphics.setColor(r, g, b, 0.3 * a)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("line", x, y, w, h)
end
function Graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky) function Graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky)
local width local width
@ -61,4 +75,23 @@ function Graphics.printWithSpacing(text, spacing, align, x, y, r, sx, sy, ox, oy
end end
end end
-- PLACEHOLDER GRAPHICS FUNCTIONS
-- Ready-to-use placeolder and stuff
function Graphics.box(x, y, w, h)
local x = math.floor(x)
local y = math.floor(y)
local w = math.floor(w)
local h = math.floor(h)
local a = a or 1
local r, g, b, a = love.graphics.getColor( )
love.graphics.setColor(r, g, b, 0.3 * a)
love.graphics.rectangle("fill", x, y, w, h)
love.graphics.setColor(r, g, b, a)
love.graphics.rectangle("line", x, y, w, h)
end
return Graphics return Graphics

View file

@ -1,7 +1,31 @@
-- loveutils : a set of basic functions and utility for love2D.
--[[
Copyright © 2019 Kazhnuz
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.
]]
local cwd = (...):gsub('%.init$', '') .. "." local cwd = (...):gsub('%.init$', '') .. "."
-- load the different elements from loveutils
return { return {
math = require(cwd .. "math"), math = require(cwd .. "math"),
graphics = require(cwd .. "graphics"), graphics = require(cwd .. "graphics"),
filesystem = require(cwd .. "filesystem") filesystem = require(cwd .. "filesystem")
} }

View file

@ -1,5 +1,31 @@
-- loveutils.math : easy to use functions for mathematics and geometry.
--[[
Copyright © 2019 Kazhnuz
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.
]]
local Math = {} local Math = {}
-- ALGEBRA FUNCTIONS
-- Simple yet usefull functions not supported by base love2D
function Math.sign(x) function Math.sign(x)
if (x < 0) then if (x < 0) then
return -1 return -1
@ -14,6 +40,9 @@ function Math.round(num)
return math.floor(num + 0.5) return math.floor(num + 0.5)
end end
-- VECTOR/DIRECTION functions
-- Easy-to-use function to handle point and motion
function Math.vector(x1, y1, x2, y2) function Math.vector(x1, y1, x2, y2)
local vecx, vecy local vecx, vecy
@ -54,6 +83,9 @@ function Math.pointDirection(x1,y1,x2,y2)
return angle return angle
end end
-- STRING FUNCTIONS
-- Transform into string numbers
function Math.numberToString(x, length) function Math.numberToString(x, length)
local length = length or 1 local length = length or 1
local string = "" local string = ""
@ -72,6 +104,9 @@ function Math.numberToString(x, length)
return string return string
end end
-- COORDINATE FUNCTIONS
-- Easy computation on coordinate
function Math.floorCoord(x, y) function Math.floorCoord(x, y)
return math.floor(x), math.floor(y) return math.floor(x), math.floor(y)
end end