diff --git a/classes.lua b/classes.lua index 01721ec..c995787 100644 --- a/classes.lua +++ b/classes.lua @@ -22,6 +22,7 @@ local function newImage(path, alphaColor) return image end + --[[ Sprite Class This is essentially just a Wrapper for LĂ–VEs Image @@ -58,16 +59,15 @@ function Spritesheet.new(path, width, height, alphaColor) if (string.find(path, ".json")) then local contents, size = love.filesystem.read(path) - local sliceData = json.decode(contents) + local metadata = json.decode(contents) - print("games/" .. sliceData.image) - self.image = newImage("games/" .. sliceData.image, alphaColor) + self.image = newImage("games/" .. metadata.image, alphaColor) -- Create the slices according to the sliceData local imageWidth, imageHeight = self.image:getWidth(), self.image:getHeight() local counter = 0 - for _, data in pairs(sliceData) do + for _, data in pairs(metadata.tiles) do table.insert(self.sprites, love.graphics.newQuad( data.x, data.y, diff --git a/games/test.lua b/games/test.lua index ea70599..d1f68ee 100644 --- a/games/test.lua +++ b/games/test.lua @@ -11,28 +11,41 @@ end function Update(dt, curTime) end -local sprite = Spritesheet("testSprites.json", 16, 16, Color(48, 104, 80, 255)) +local sprite = Spritesheet("testSprites.png", 16, 16, Color(48, 104, 80, 255)) +local tileset = Spritesheet("tileset.json", 16, 16, Color(48, 104, 80, 255)) local testSound = Sound("testSound.mp3", "static") + local test = 1 +local test2 = 1 function KeyPressed(key) if (key == "f3") then test = test - 1 + test2 = test2 - 1 if (test == 0) then test = sprite.Count end + if (test2 == 0) then + test2 = tileset.Count + end elseif (key == "f4") then test = test + 1 + test2 = test2 + 1 if (test == sprite.Count + 1) then test = 1 end + if (test2 == tileset.Count + 1) then + test2 = 1 + end elseif (key == "f2") then testSound:Play() end end function Draw() - DrawText(test .. "/" .. sprite.Count, 25, 25) - sprite:Draw(test, 50, 0) + DrawText(test .. "/" .. sprite.Count, 50, 25) + + sprite:Draw(test, 5, 25) + tileset:Draw(test2, 25, 0) end diff --git a/games/test/main.lua b/games/test/main.lua new file mode 100644 index 0000000..f48e51a --- /dev/null +++ b/games/test/main.lua @@ -0,0 +1,59 @@ + +NAME = "Test" +AUTHOR = "Tarion" +VERSION = 0 + +local backgroundColor = Color(48, 104, 80, 255) +local colo3r = Color(48, 104, 80, 255) +function Load() + SetBackgroundColor(backgroundColor) + color = Color(48, 104, 80, 255) +end + +function Update(dt, curTime) +end + +local sprite = Spritesheet("testSprites.png", 16, 16, Color(48, 104, 80, 255)) +local tileset = Spritesheet("tileset.json", 16, 16, Color(48, 104, 80, 255)) +local testSound = Sound("testSound.mp3", "static") + +local test = 1 +local test2 = 1 +function KeyPressed(key) + if (key == "f3") then + test = test - 1 + test2 = test2 - 1 + + if (test == 0) then + test = sprite.Count + end + if (test2 == 0) then + test2 = tileset.Count + end + elseif (key == "f4") then + test = test + 1 + test2 = test2 + 1 + + if (test == sprite.Count + 1) then + test = 1 + end + if (test2 == tileset.Count + 1) then + test2 = 1 + end + elseif (key == "f2") then + testSound:Play() + testSound = nil + collectgarbage() + end +end + +function Draw() + DrawText(test .. "/" .. sprite.Count, 50, 25) + + sprite:Draw(test, 5, 25) + tileset:Draw(test2, 30, 0) +end + + +-- TODO: +-- 1. Implement memory limits for code, track how many variables are loaded, based on the size, like int64 and stuff diff --git a/games/test/test.lua b/games/test/test.lua new file mode 100644 index 0000000..89dbfe7 --- /dev/null +++ b/games/test/test.lua @@ -0,0 +1,2 @@ + +return "test" \ No newline at end of file diff --git a/games/test/testSound.mp3 b/games/test/testSound.mp3 new file mode 100644 index 0000000..8e64435 Binary files /dev/null and b/games/test/testSound.mp3 differ diff --git a/games/test/testSprites.png b/games/test/testSprites.png new file mode 100644 index 0000000..c843ce3 Binary files /dev/null and b/games/test/testSprites.png differ diff --git a/games/test/tileset.json b/games/test/tileset.json new file mode 100644 index 0000000..20d6dff --- /dev/null +++ b/games/test/tileset.json @@ -0,0 +1,22 @@ +{ + "image": "tileset.png", + "tiles": [ + {"x":0,"y":0,"width":64,"height":24}, + {"x":64,"y":0,"width":16,"height":24}, + {"x":64,"y":0,"width":16,"height":24}, + {"x":0,"y":24,"width":64,"height":24}, + {"x":80,"y":52,"width":8,"height":12}, + {"x":80,"y":68,"width":8,"height":12}, + {"x":0,"y":72,"width":80,"height":24}, + {"x":90,"y":48,"width":14,"height":48}, + {"x":72,"y":96,"width":32,"height":15}, + {"x":32,"y":96,"width":40,"height":15}, + {"x":24,"y":96,"width":8,"height":8}, + {"x":16,"y":96,"width":8,"height":8}, + {"x":16,"y":104,"width":8,"height":8}, + {"x":0,"y":96,"width":16,"height":16}, + {"x":0,"y":112,"width":32,"height":24}, + {"x":32,"y":111,"width":32,"height":24}, + {"x":72,"y":136,"width":32,"height":24} + ] +} \ No newline at end of file diff --git a/games/test/tileset.png b/games/test/tileset.png new file mode 100644 index 0000000..5b21c11 Binary files /dev/null and b/games/test/tileset.png differ diff --git a/games/testSprites.json b/games/testSprites.json deleted file mode 100644 index e806425..0000000 --- a/games/testSprites.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - "image": "testSprites.png" - }, - {"x":0,"y":0,"width":64,"height":24}, - {"x":64,"y":0,"width":16,"height":24}, - {"x":64,"y":0,"width":16,"height":24}, - {"x":0,"y":24,"width":64,"height":24}, - {"x":80,"y":52,"width":8,"height":12}, - {"x":80,"y":68,"width":8,"height":12}, - {"x":0,"y":72,"width":80,"height":24}, - {"x":90,"y":48,"width":14,"height":48}, - {"x":72,"y":96,"width":32,"height":15}, - {"x":32,"y":96,"width":40,"height":15}, - {"x":24,"y":96,"width":8,"height":8}, - {"x":16,"y":96,"width":8,"height":8}, - {"x":16,"y":104,"width":8,"height":8}, - {"x":0,"y":96,"width":16,"height":16},{"x":0,"y":112,"width":32,"height":24},{"x":32,"y":111,"width":32,"height":24},{"x":72,"y":136,"width":32,"height":24}] \ No newline at end of file diff --git a/games/tileset.json b/games/tileset.json new file mode 100644 index 0000000..20d6dff --- /dev/null +++ b/games/tileset.json @@ -0,0 +1,22 @@ +{ + "image": "tileset.png", + "tiles": [ + {"x":0,"y":0,"width":64,"height":24}, + {"x":64,"y":0,"width":16,"height":24}, + {"x":64,"y":0,"width":16,"height":24}, + {"x":0,"y":24,"width":64,"height":24}, + {"x":80,"y":52,"width":8,"height":12}, + {"x":80,"y":68,"width":8,"height":12}, + {"x":0,"y":72,"width":80,"height":24}, + {"x":90,"y":48,"width":14,"height":48}, + {"x":72,"y":96,"width":32,"height":15}, + {"x":32,"y":96,"width":40,"height":15}, + {"x":24,"y":96,"width":8,"height":8}, + {"x":16,"y":96,"width":8,"height":8}, + {"x":16,"y":104,"width":8,"height":8}, + {"x":0,"y":96,"width":16,"height":16}, + {"x":0,"y":112,"width":32,"height":24}, + {"x":32,"y":111,"width":32,"height":24}, + {"x":72,"y":136,"width":32,"height":24} + ] +} \ No newline at end of file diff --git a/main.lua b/main.lua index 08a522b..c8f8528 100644 --- a/main.lua +++ b/main.lua @@ -95,6 +95,7 @@ local consoleEnv = { NAME = "NULL", AUTHOR = "NULL", VERSION = 0, + path = "", Load, Update, @@ -111,11 +112,17 @@ local consoleEnv = { DrawText = love.graphics.print, print = print, + collectgarbage = collectgarbage, + require = function(name) + require(virtual.path .. name) + end, } consoleEnv._G = consoleEnv function loadGame(gameName) - local fn, err = loadfile("games/" .. gameName .. ".lua", "t", consoleEnv) + local fn, err = loadfile("games/" .. gameName .. "/main.lua", "t", consoleEnv) + virtual.path = "games/" .. gameName .. "/" + if not fn then error("Failed to load sandbox file (1): " .. err) end @@ -145,6 +152,8 @@ function love.load() -- Create Virtual Console virtual = { + path = "", + maxMemory = 16000, currentMemory = 0, @@ -191,9 +200,17 @@ function love.update(dt) end function love.draw() + local count = 0 + for k, v in pairs(consoleEnv) do + if (type(v) ~= "function" and v ~= nil) then + count = count + 1 + end + end + love.graphics.scale(scaling*0.5, scaling*0.5) love.graphics.print(virtual.currentMemory/1000 .. "/" .. virtual.maxMemory/1000 .. " KB", 2, 2) love.graphics.print("Loaded Objects: " .. virtual.loadedCount, 2, 10) + love.graphics.print("Loaded Variables: " .. count, 2, 18) love.graphics.scale(scaling, scaling) love.graphics.setFont(font)