commit f865922080aba3b93e1abaa44999e5592315cf96 Author: Fierelier Date: Fri Nov 25 22:36:18 2022 +0100 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..449cea4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 + +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. diff --git a/element/example.lua b/element/example.lua new file mode 100644 index 0000000..cf03b6a --- /dev/null +++ b/element/example.lua @@ -0,0 +1,28 @@ +local self = ... + +print("[" ..self._type.. "] type loaded") + +function self.create(element) + element.memhiOriginal = element.memhi + element.memhi = {} + local mt = { + __index = function(t,k) + print("[" ..element.memhiOriginal._type.. " " ..tostring(element.memhiOriginal._id).. "] access: " ..tostring(k)) + return element.memhiOriginal[k] + end, + __newindex = function(t,k,v) + print("[" ..element.memhiOriginal._type.. " " ..tostring(element.memhiOriginal._id).. "] set: " ..tostring(k).. " = " ..tostring(v)) + element.memhiOriginal[k] = v + end + } + setmetatable(element.memhi,mt) + print("[" ..element.memhi._type.. " " ..tostring(element.memhi._id).. "] element created") +end + +function self.destroy(element) + print("[" ..element.memhi._type.. " " ..tostring(element.memhi._id).. "] element destroyed") +end + +function self.unload() + print("[" ..self._type.. "] type unloaded") +end \ No newline at end of file diff --git a/example.lua b/example.lua new file mode 100644 index 0000000..b88c565 --- /dev/null +++ b/example.lua @@ -0,0 +1,28 @@ +#!/usr/bin/lua + +-- Bootstrap, this is required for engine +mainScriptPath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) +package.path = mainScriptPath.. "/lib/?.lua;" ..mainScriptPath.. "/lib/?/init.lua;" ..package.path + +function main() + local engine = require("engine") + local el1 = engine.element.create("example") + local el2 = engine.element.create("example") + local el3 = engine.element.create("example") + engine.element.attach(el2,el1) + engine.element.attach(el3,el1) + engine.element.destroy(el1) + local tableHelper = require("tableHelper") + local memDebug = require("memDebug") + print("") + print("-- GLOBALS --") + print(tableHelper.toString(_G)) + print("") + print("-- LOCALS --") + print(tableHelper.toString(memDebug.locals())) + print("") + print("-- UPVALUES --") + print(tableHelper.toString(memDebug.upvalues())) +end + +main() \ No newline at end of file diff --git a/lib/engine/init.lua b/lib/engine/init.lua new file mode 100644 index 0000000..3400181 --- /dev/null +++ b/lib/engine/init.lua @@ -0,0 +1,98 @@ +local engine = {} + +function engine.null() end + +engine.elements = {} +engine.elementTypes = {} +engine.element = {} + +engine.paths = { mainScriptPath } +function engine.path(path) + for i,v in pairs(engine.paths) do + local fpath = v .. "/" .. path + local f = io.open(fpath) + if f ~= nil then + io.close(f) + return fpath + end + end + error("File not found: " .. path) +end + +function engine.element.create(elementType) + if engine.elementTypes[elementType] == nil then + engine.elementTypes[elementType] = { + _type = elementType, + _references = 0 + } + + assert(loadfile(engine.path("element/" ..elementType.. ".lua")))(engine.elementTypes[elementType]) + end + + engine.elementTypes[elementType]._references = engine.elementTypes[elementType]._references + 1 + + local id = 1 + while true do + id = math.random(1,2147483647) + if engine.elements[id] == nil then break end + end + + local element = { + memlo = { + _children = {} + }, + memhi = { + _id = id, + _type = elementType, + _children = {}, + _childrenCount = 0 + } + } + + engine.elementTypes[elementType].create(element) + engine.elements[id] = element + return element +end + +function engine.element.attach(child,parent) + if child.memlo._parent ~= nil then + child.memlo._parent.memlo._children[child.memhi._id] = nil + child.memlo._parent.memhi._children[child.memhi._id] = nil + child.memlo._parent.memhi._childrenCount = child.memlo._parent.memhi._childrenCount - 1 + end + + child.memlo._parent = parent + if parent == nil then + child.memhi._parent = nil + return + end + + child.memhi._parent = parent.memhi._id + parent.memhi._children[child.memhi._id] = true + parent.memlo._children[child.memhi._id] = child + parent.memhi._childrenCount = parent.memhi._childrenCount + 1 +end + +function engine.element.destroy(element,destroyChildren) + if destroyChildren == nil then destroyChildren = true end + engine.element.attach(element) + + if destroyChildren == true then + if element.memhi._childrenCount > 0 then + for i,v in pairs(element.memlo._children) do + engine.element.destroy(v) + end + end + end + + engine.elementTypes[element.memhi._type].destroy(element) + engine.elements[element.memhi._id] = nil + + engine.elementTypes[element.memhi._type]._references = engine.elementTypes[element.memhi._type]._references - 1 + if engine.elementTypes[element.memhi._type]._references == 0 then + engine.elementTypes[element.memhi._type].unload() + engine.elementTypes[element.memhi._type] = nil + end +end + +return engine \ No newline at end of file diff --git a/lib/memDebug/init.lua b/lib/memDebug/init.lua new file mode 100644 index 0000000..200b444 --- /dev/null +++ b/lib/memDebug/init.lua @@ -0,0 +1,30 @@ +-- Much thanks to https://stackoverflow.com/a/2835433 + +local self = {} + +function self.locals() + local variables = {} + local idx = 1 + while true do + local ln, lv = debug.getlocal(2, idx) + if ln == nil then break end + variables[ln] = lv + idx = 1 + idx + end + return variables +end + +function self.upvalues() + local variables = {} + local idx = 1 + local func = debug.getinfo(2, "f").func + while true do + local ln, lv = debug.getupvalue(func, idx) + if ln == nil then break end + variables[ln] = lv + idx = 1 + idx + end + return variables +end + +return self \ No newline at end of file diff --git a/lib/tableHelper/init.lua b/lib/tableHelper/init.lua new file mode 100644 index 0000000..abfd58b --- /dev/null +++ b/lib/tableHelper/init.lua @@ -0,0 +1,36 @@ +local self = {} + +function self.toString(tbl,checkedTbls,depth) + if depth == nil then depth = 0 end + if checkedTbls == nil then checkedTbls = {} end + table.insert(checkedTbls,tbl) + local str = "" + local i = depth + local prefix = ">" + while i > 0 do + prefix = prefix .. ">" + i = i - 1 + end + prefix = prefix .. " " + for i,v in pairs(tbl) do + if type(v) == "table" then + local found = false + for i2,v2 in pairs(checkedTbls) do + if v2 == v then + found = true + break + end + end + + if found == false then + str = str .. prefix.. tostring(i) .. ":\n" ..self.toString(v,checkedTbls,depth + 1) + end + else + str = str .. prefix.. tostring(i) .. ": " ..tostring(v).. "\n" + end + end + + return str +end + +return self \ No newline at end of file diff --git a/lib/utf8Helper/init.lua b/lib/utf8Helper/init.lua new file mode 100644 index 0000000..9cc9542 --- /dev/null +++ b/lib/utf8Helper/init.lua @@ -0,0 +1,57 @@ +local utf8 = require("utf8") +local self = {} + +function self.toTable(str) + local chars = {} + local pos = 1 + for p,c in utf8.codes(str) do + local bytes = 1 + if c > 127 then bytes = 2 end + if c > 255 then bytes = 3 end + if c > 65535 then bytes = 4 end + if c > 16777215 then bytes = 5 end + + chars[pos] = string.sub(str,p,p + bytes - 1) + pos = pos + 1 + end + + return chars +end + +function self.split(text,sep,count) + local output = {} + local outputLen = 0 + local part = {} + local partLen = 0 + + local length = #text + local index = 1 + while index <= length do + local char = text[index] + if char == sep then + outputLen = outputLen + 1 + output[outputLen] = part + part = {} + partLen = 0 + if outputLen == count then + index = index + 1 + break + end + else + partLen = partLen + 1 + part[partLen] = char + end + + index = index + 1 + end + + if index <= length then + part = {unpack(text,index)} + end + + outputLen = outputLen + 1 + output[outputLen] = part + return output +end + +return self \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..f8764d8 --- /dev/null +++ b/readme.txt @@ -0,0 +1 @@ +This is an agnostic engine that implements a simple system for creating abstract objects. You can implement any engine you want in it, and use it with this. By itself, this engine doesn't do much. You can check out my raylib-lua implementation of the engine here: https://git.lumen.sh/Fierelier/engine-raylib -- This has implementations for cameras, models, etc. \ No newline at end of file