From 65010fae16a473549458497c16ef0b595b10e68f Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 11 Apr 2019 17:10:28 +0200 Subject: [PATCH] loveutils: better comments and organisation --- gamecore/utils/filesystem.lua | 23 +++++++++++++ gamecore/utils/graphics.lua | 63 ++++++++++++++++++++++++++--------- gamecore/utils/init.lua | 30 +++++++++++++++-- gamecore/utils/math.lua | 35 +++++++++++++++++++ 4 files changed, 133 insertions(+), 18 deletions(-) diff --git a/gamecore/utils/filesystem.lua b/gamecore/utils/filesystem.lua index 3d38e27..ce99fbb 100644 --- a/gamecore/utils/filesystem.lua +++ b/gamecore/utils/filesystem.lua @@ -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 = {} function Filesystem.exists(filepath) diff --git a/gamecore/utils/graphics.lua b/gamecore/utils/graphics.lua index 79ad753..925ddbe 100644 --- a/gamecore/utils/graphics.lua +++ b/gamecore/utils/graphics.lua @@ -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 = {} +-- COLOR FUNCTIONS +-- Handle colors and scene colors + function Graphics.resetColor() love.graphics.setColor(1,1,1,1) end -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 +-- PRINT TEXT +-- Functions to draw text on screen function Graphics.print(text, x, y, align, r, sx, sy, ox, oy, kx, ky) local width @@ -61,4 +75,23 @@ function Graphics.printWithSpacing(text, spacing, align, x, y, r, sx, sy, ox, oy 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 diff --git a/gamecore/utils/init.lua b/gamecore/utils/init.lua index 0f6ea3c..765016a 100644 --- a/gamecore/utils/init.lua +++ b/gamecore/utils/init.lua @@ -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$', '') .. "." +-- load the different elements from loveutils return { - math = require(cwd .. "math"), - graphics = require(cwd .. "graphics"), - filesystem = require(cwd .. "filesystem") + math = require(cwd .. "math"), + graphics = require(cwd .. "graphics"), + filesystem = require(cwd .. "filesystem") } diff --git a/gamecore/utils/math.lua b/gamecore/utils/math.lua index b030035..4168c6b 100644 --- a/gamecore/utils/math.lua +++ b/gamecore/utils/math.lua @@ -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 = {} +-- ALGEBRA FUNCTIONS +-- Simple yet usefull functions not supported by base love2D + function Math.sign(x) if (x < 0) then return -1 @@ -14,6 +40,9 @@ function Math.round(num) return math.floor(num + 0.5) end +-- VECTOR/DIRECTION functions +-- Easy-to-use function to handle point and motion + function Math.vector(x1, y1, x2, y2) local vecx, vecy @@ -54,6 +83,9 @@ function Math.pointDirection(x1,y1,x2,y2) return angle end +-- STRING FUNCTIONS +-- Transform into string numbers + function Math.numberToString(x, length) local length = length or 1 local string = "" @@ -72,6 +104,9 @@ function Math.numberToString(x, length) return string end +-- COORDINATE FUNCTIONS +-- Easy computation on coordinate + function Math.floorCoord(x, y) return math.floor(x), math.floor(y) end