mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 17:19:31 +00:00
ubiquitousse.asset
This commit is contained in:
parent
bdb9e710d9
commit
ed683c6a70
5 changed files with 85 additions and 8 deletions
72
asset.lua
Normal file
72
asset.lua
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
-- ubiquitousse.asset
|
||||||
|
|
||||||
|
-- The asset cache. Each cached asset is indexed with a string key "type.assetName".
|
||||||
|
local cache = setmetatable({}, { __mode = "v" }) -- weak values
|
||||||
|
|
||||||
|
--- Asset manager. Loads asset and cache them.
|
||||||
|
-- This file has no dependicy to either ubiquitousse or a ubiquitousse backend.
|
||||||
|
-- This only provides a streamlined way to handle asset, and doesn't handle the actual file loading/object creation itself; you are expected to provide your own asset loaders.
|
||||||
|
-- See asset.load for more details. Hopefully this will allow you to use asset which are more game-specific than "image" or "audio".
|
||||||
|
local asset
|
||||||
|
asset = setmetatable({
|
||||||
|
--- A prefix for asset names
|
||||||
|
-- @impl ubiquitousse
|
||||||
|
prefix = "",
|
||||||
|
|
||||||
|
--- Load (and cache) an asset.
|
||||||
|
-- Asset name are similar to Lua module names (directory separator is the dot . and no extention should be specified).
|
||||||
|
-- To load an asset, ubiquitousse will, in this order:
|
||||||
|
-- * try to load the directory loader: a file named loader.lua in the same directory as the asset we are trying to load
|
||||||
|
-- * try to load the asset-specific loader: a file in the same directory and with the same name (except with the .lua extension) as the asset we are trying to load
|
||||||
|
-- Loaders are expected to return the new asset.
|
||||||
|
-- These loaders have acces to the following variables:
|
||||||
|
-- * directory: the asset directory (including prefix)
|
||||||
|
-- * name: the asset name (directory information removed)
|
||||||
|
-- * asset: the asset data. May be nil if this is the first loader to run.
|
||||||
|
-- @tparam assetName string the asset's full name
|
||||||
|
-- @return the asset
|
||||||
|
-- @impl ubiquitousse
|
||||||
|
load = function(assetName)
|
||||||
|
if not cache[assetName] then
|
||||||
|
-- Get directory and name
|
||||||
|
local path, name = assetName:match("^([^.]+)%.(.+)$")
|
||||||
|
if not path then
|
||||||
|
path, name = "", assetName
|
||||||
|
end
|
||||||
|
local dir = (asset.prefix..path):gsub("%.", "/")
|
||||||
|
|
||||||
|
-- Setup env
|
||||||
|
local oName, oAsset, oDirectory = name, asset, directory
|
||||||
|
name, asset, directory = name, nil, dir
|
||||||
|
|
||||||
|
-- Asset directory loader
|
||||||
|
local f = io.open(dir.."/loader.lua")
|
||||||
|
if f then
|
||||||
|
f:close()
|
||||||
|
asset = dofile(dir.."/loader.lua")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Asset specific loader
|
||||||
|
local f = io.open(dir.."/"..name..".lua")
|
||||||
|
if f then
|
||||||
|
f:close()
|
||||||
|
asset = dofile(dir.."/"..name..".lua")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Done
|
||||||
|
cache[assetName] = asset
|
||||||
|
|
||||||
|
-- Restore env
|
||||||
|
name, asset, directory = oName, oAsset, oDirectory
|
||||||
|
end
|
||||||
|
|
||||||
|
return cache[assetName]
|
||||||
|
end,
|
||||||
|
}, {
|
||||||
|
--- asset(...) is a shortcut for asset.load(...)
|
||||||
|
__call = function(self, ...)
|
||||||
|
return asset.load(...)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
return asset
|
||||||
8
draw.lua
8
draw.lua
|
|
@ -53,10 +53,10 @@ draw = {
|
||||||
fps = function() end,
|
fps = function() end,
|
||||||
|
|
||||||
--- Sets the drawing color
|
--- Sets the drawing color
|
||||||
-- @tparam number r the red component (0-255)
|
-- @tparam number r the red component (0-1)
|
||||||
-- @tparam number g the green component (0-255)
|
-- @tparam number g the green component (0-1)
|
||||||
-- @tparam number b the blue component (0-255)
|
-- @tparam number b the blue component (0-1)
|
||||||
-- @tparam[opt=255] number a the alpha (opacity) component (0-255)
|
-- @tparam[opt=1] number a the alpha (opacity) component (0-1)
|
||||||
-- @impl backend
|
-- @impl backend
|
||||||
color = function(r, g, b, a) end,
|
color = function(r, g, b, a) end,
|
||||||
|
|
||||||
|
|
|
||||||
3
init.lua
3
init.lua
|
|
@ -84,7 +84,8 @@ ubiquitousse = {
|
||||||
audio = false,
|
audio = false,
|
||||||
input = false,
|
input = false,
|
||||||
scene = false,
|
scene = false,
|
||||||
event = false
|
event = false,
|
||||||
|
asset = false
|
||||||
},
|
},
|
||||||
|
|
||||||
--- Backend name.
|
--- Backend name.
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ local m = uqt.module
|
||||||
-- * all scene change callbacks are called after setting scene.current to the new scene but before changing scene.stack
|
-- * all scene change callbacks are called after setting scene.current to the new scene but before changing scene.stack
|
||||||
-- * all scene exit/suspend callbacks are called before scene enter/resume callbacks
|
-- * all scene exit/suspend callbacks are called before scene enter/resume callbacks
|
||||||
local scene
|
local scene
|
||||||
scene = {
|
scene = setmetatable({
|
||||||
--- The current scene table.
|
--- The current scene table.
|
||||||
-- @impl ubiquitousse
|
-- @impl ubiquitousse
|
||||||
current = nil,
|
current = nil,
|
||||||
|
|
@ -131,6 +131,11 @@ scene = {
|
||||||
draw = function(...)
|
draw = function(...)
|
||||||
if scene.current then scene.current:draw(...) end
|
if scene.current then scene.current:draw(...) end
|
||||||
end
|
end
|
||||||
}
|
}, {
|
||||||
|
--- scene(...) is a shortcut for scene.new(...)
|
||||||
|
__call = function(self, ...)
|
||||||
|
return scene.new(...)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
return scene
|
return scene
|
||||||
|
|
|
||||||
1
todo.txt
1
todo.txt
|
|
@ -2,7 +2,6 @@ Ubiquitousse, also known as "The World's Best Video Game Engine Of All Time", de
|
||||||
More specifically, what is lacking to officially turn Ubiquitousse into a sacred text, is:
|
More specifically, what is lacking to officially turn Ubiquitousse into a sacred text, is:
|
||||||
- An i18n API. While some languages are clearly superior to others, the general consensus seems to think otherwise. Ubiquitousse
|
- An i18n API. While some languages are clearly superior to others, the general consensus seems to think otherwise. Ubiquitousse
|
||||||
should be able to get an ordered list of prefered languages and provide translation helpers. See The Pong.
|
should be able to get an ordered list of prefered languages and provide translation helpers. See The Pong.
|
||||||
- Asset management. See The Pong.
|
|
||||||
- Some API are still lacking an API and/or implementation. Search "TODO" for more information.
|
- Some API are still lacking an API and/or implementation. Search "TODO" for more information.
|
||||||
- A filesystem API, to access the game's filesystem. May also rewrite Lua's io functions, see the next item.
|
- A filesystem API, to access the game's filesystem. May also rewrite Lua's io functions, see the next item.
|
||||||
- A sandboxing system. Ubiquitousse should be able to run in a Ubiquitousse game safely. Since Ubiquitousse can run itself, it will
|
- A sandboxing system. Ubiquitousse should be able to run in a Ubiquitousse game safely. Since Ubiquitousse can run itself, it will
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue