Audio and Sprite Wrappers

Functions to Create and use said wrappers
This commit is contained in:
Patrick Schwarzer 2025-04-13 05:07:15 +02:00
parent d220cfb9da
commit 24b94f4a59
3 changed files with 122 additions and 19 deletions

61
classes.lua Normal file
View file

@ -0,0 +1,61 @@
--[[
Sprite Class
This is essentially just a Wrapper for LÖVEs Image
]]--
Sprite = {}
Sprite.__index = Sprite
function Sprite.new(path)
local self = setmetatable({}, Sprite)
self.image = love.graphics.newImage(path)
return self
end
function Sprite:Draw(x, y)
love.graphics.draw(self.image, x or 0, y or 0)
end
--[[
Audio Class
This is essentially just a Wrapper for LÖVEs Source
]]--
Audio = {}
Audio.__index = Audio
function Audio.new(path, soundType)
local self = setmetatable({}, Audio)
self.source = love.audio.newSource(path, soundType or "static")
return self
end
function Audio:Play()
self.source:play()
end
function Audio:Stop()
self.source:stop()
end
function Audio:Pause()
self.source:pause()
end
function Audio:Resume()
self.source:play()
end
function Audio:SetLooping(bool)
self.source:setLooping(bool)
end
function Audio:SetVolume(volume)
self.source:setVolume(volume)
end

View file

@ -6,19 +6,23 @@ VERSION = 0
local test = 0 local test = 0
local delay = 0 local delay = 0
local last = ""
function Load() function Load()
end end
function Update(dt, curTime) function Update(dt, curTime)
end end
local sprite = Sprite("testSprites.png")
function KeyPressed(key) function KeyPressed(key)
if (key == "f5") then if (key == "f5") then
test = test + 1 test = test + 1
Sprite("testSprites.png") sprite = Sprite("testSprites.png")
end end
end end
function Draw() function Draw()
print("Loaded Sprites: " .. test, 10, 10) print("Loaded Sprites: " .. test, 10, 10)
sprite:Draw(100, 0)
end end

View file

@ -1,20 +1,57 @@
local function CalculateMemoryUsage() require("classes")
end
local function LoadSprite(name) local LoadObject = {
["sprite"] = function(name)
name = "games/" .. name name = "games/" .. name
local info = love.filesystem.getInfo(name) local info = love.filesystem.getInfo(name)
if (not info) then if (not info) then
error("Failed to load Sprite! ("..name..")") error("Failed to load Object (FILE NOT FOUND) ("..name..")")
return
end
if ((info.size + virtual.currentMemory) > virtual.maxMemory) then
error("Failed to load Object (MAX MEMORY) ("..name..")")
return return
end end
virtual.currentMemory = virtual.currentMemory + info.size local newObject = Sprite.new(name)
local newSprite = love.graphics.newImage(name) virtual.loadedObjects[newObject] = {
end object = newSprite,
size = info.size,
}
virtual.currentMemory = virtual.currentMemory + info.size
virtual.loadedCount = virtual.loadedCount + 1
return newObject
end,
["audio"] = function(name, soundType)
name = "games/" .. name
soundType = soundType or "static"
local info = love.filesystem.getInfo(name)
if (not info) then
error("Failed to load Object (FILE NOT FOUND) ("..name..")")
return
end
if ((info.size + virtual.currentMemory) > virtual.maxMemory) then
error("Failed to load Object (MAX MEMORY) ("..name..")")
return
end
local newObject = Audio.new(name, soundType)
virtual.loadedObjects[newObject] = {
object = newSprite,
size = info.size,
}
virtual.currentMemory = virtual.currentMemory + info.size
virtual.loadedCount = virtual.loadedCount + 1
return newObject
end,
}
-- Establish Sandbox -- Establish Sandbox
-- Everything in the table is what the Sandbox has access to -- Everything in the table is what the Sandbox has access to
@ -28,7 +65,8 @@ local consoleEnv = {
Draw, Draw,
KeyPressed, KeyPressed,
Sprite = LoadSprite, Sprite2 = LoadSprite,
Sprite = LoadObject["sprite"],
print = love.graphics.print, print = love.graphics.print,
} }
@ -74,9 +112,8 @@ function love.load()
maxMemory = 16000, maxMemory = 16000,
currentMemory = 0, currentMemory = 0,
loadedSprites = {}, loadedObjects = {},
loadedSounds = {}, loadedCount = 0,
loadedFonts = {},
} }
loadGame("test") loadGame("test")
@ -128,12 +165,13 @@ function love.update(dt)
end end
function love.draw() function love.draw()
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.scale(scaling, scaling) love.graphics.scale(scaling, scaling)
love.graphics.setFont(font) love.graphics.setFont(font)
love.graphics.print(virtual.currentMemory/1000 .. "/" .. virtual.maxMemory/1000 .. " KB", 2, 2)
if (consoleEnv.Draw) then if (consoleEnv.Draw) then
consoleEnv.Draw() consoleEnv.Draw()
end end