init
This commit is contained in:
commit
24e11beccd
3 changed files with 181 additions and 0 deletions
0
games/.gitkeep
Normal file
0
games/.gitkeep
Normal file
149
main.lua
Normal file
149
main.lua
Normal file
|
@ -0,0 +1,149 @@
|
|||
|
||||
local root = 'games/'
|
||||
|
||||
-- games library and selection
|
||||
local gms = {}
|
||||
local sel = 1
|
||||
local sgme
|
||||
|
||||
-- some internals
|
||||
local cx, cy, cw, ch
|
||||
local f, bf
|
||||
|
||||
function love.resize(x, y)
|
||||
W, H = x, y
|
||||
|
||||
if resize then resize(W, H) end
|
||||
|
||||
collectgarbage 'collect' -- because new font isn't clears
|
||||
end
|
||||
|
||||
function resize()
|
||||
cw, ch = W / 1.25, H / 1.25
|
||||
cx, cy = (W - cw) / 2, (H - ch) / 2
|
||||
|
||||
local th = math.min(W, H) / 8
|
||||
|
||||
f = love.graphics.newFont(th / 3)
|
||||
bf = love.graphics.newFont(th / 2)
|
||||
end
|
||||
|
||||
love.resize(love.graphics.getDimensions())
|
||||
|
||||
local function gmeNew(cont, dir, file)
|
||||
local gme = {
|
||||
name = 'No name',
|
||||
desc = 'No description provided.',
|
||||
dir = dir,
|
||||
main = 'main.lua',
|
||||
}
|
||||
local fi = false
|
||||
cont = cont or ''
|
||||
cont = cont:gsub('[^\\]#[^\n]*', '')
|
||||
for v in cont:gmatch '([^\n]+)' do
|
||||
local k, v = v:match '^%s*([%w_]+)%s*=%s*(.*)%s*$'
|
||||
if k == 'name'
|
||||
or k == 'desc'
|
||||
or k == 'main'
|
||||
then gme[k] = v
|
||||
fi = true
|
||||
elseif k
|
||||
then error('unknown field "'.. k ..'" in "'.. file ..'"')
|
||||
end
|
||||
end
|
||||
if not fi then
|
||||
gme.name = dir
|
||||
end
|
||||
table.insert(gms, gme)
|
||||
end
|
||||
|
||||
for _, v in pairs(love.filesystem.getDirectoryItems(root)) do
|
||||
local file = v..'/'.. 'info.ll'
|
||||
gmeNew(love.filesystem.read(root .. file), v, file)
|
||||
end
|
||||
|
||||
local pf, mx, mb, mpb
|
||||
local function update()
|
||||
mx = love.mouse.getX()
|
||||
mpb = mb
|
||||
mb = 0
|
||||
if love.mouse.isDown(1) then mb = 1
|
||||
end
|
||||
|
||||
if mpb == 1 and mb == 0 then
|
||||
if mx <= cx then
|
||||
sel = sel - 1
|
||||
if sel < 1 then sel = #gms end
|
||||
elseif mx >= cx + cw then
|
||||
sel = sel + 1
|
||||
if sel > #gms then sel = 1 end
|
||||
else sgme = gms[sel]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function draw()
|
||||
love.graphics.polygon('fill',
|
||||
8, H / 2,
|
||||
32, H / 2 - 32,
|
||||
32, H / 2 + 32)
|
||||
|
||||
love.graphics.polygon('fill',
|
||||
W - 8, H / 2,
|
||||
W - 32, H / 2 - 32,
|
||||
W - 32, H / 2 + 32)
|
||||
|
||||
local oy, t = 0, ''
|
||||
local gme = gms[sel]
|
||||
if gme then
|
||||
local ph = ch / 1.5
|
||||
love.graphics.rectangle('line', cx, cy, cw, ch)
|
||||
love.graphics.rectangle('fill', cx, cy, cw, ph)
|
||||
|
||||
oy = cy + ph
|
||||
love.graphics.setFont(bf)
|
||||
love.graphics.print(gme.name, cx, oy)
|
||||
oy = oy + bf:getHeight(gme.name)
|
||||
love.graphics.setFont(f)
|
||||
love.graphics.print(gme.desc, cx, oy)
|
||||
|
||||
else oy = H / 2
|
||||
t = 'No games'
|
||||
love.graphics.setFont(bf)
|
||||
love.graphics.print(t, (W - bf:getWidth(t)) / 2, oy)
|
||||
oy = oy + bf:getHeight(t)
|
||||
love.graphics.setFont(f)
|
||||
t = 'There is no projects/games to run'
|
||||
love.graphics.print(t, (W - f:getWidth(t)) / 2, oy)
|
||||
end
|
||||
end
|
||||
|
||||
function love.handlers.quit(c)
|
||||
-- for some cases when event handler (like this one below) can't correctly close
|
||||
os.exit(c or 0)
|
||||
end
|
||||
|
||||
while not sgme do
|
||||
-- event handling
|
||||
love.event.pump()
|
||||
for n, a,b,c,d,e,f in love.event.poll() do
|
||||
love.handlers[n](a,b,c,d,e,f)
|
||||
end
|
||||
|
||||
-- update and drawing
|
||||
update()
|
||||
love.graphics.origin()
|
||||
love.graphics.clear(0, 0, 0)
|
||||
draw()
|
||||
love.graphics.present()
|
||||
love.timer.sleep(0.001)
|
||||
end
|
||||
|
||||
love.graphics.setNewFont()
|
||||
resize = nil
|
||||
require(root .. sgme.dir ..'/'.. sgme.main:sub(1, -5))
|
||||
|
||||
-- things left:
|
||||
-- W and H global variables
|
||||
-- love.resize and optional global resize function
|
||||
-- love.handlers.quit - modified but can be required
|
32
readme.md
Normal file
32
readme.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Love Loader
|
||||
|
||||
Custom menu for selecting game from multiple for Love2D.
|
||||
|
||||
# How it works?
|
||||
|
||||
If simple: just place games in `games` folder with structure like `games/yourgame/main.lua`
|
||||
|
||||
If technically: creates local variables functions and variables.
|
||||
Creates the loop until game wasn't selected with manual event handling and redrawing.
|
||||
|
||||
# Things left or API
|
||||
|
||||
We are not keep environment super clear (except local variables ;))
|
||||
so there are some variables can be used in game:
|
||||
|
||||
`W` and `H` variables: width and height of the screen, controlled by love.resize function.
|
||||
`love.resize` and optional `resize` payload: functions called when screen size when changed and at boot.
|
||||
`love.handlers.quit`: function on exit which can be called until game was selected to keep responsive or for custom love.run function.
|
||||
|
||||
# Fill game information
|
||||
|
||||
To fill game information in game folder need to create `info.ll` file.
|
||||
|
||||
Syntax is `k = v` with `# comments`
|
||||
|
||||
```
|
||||
# are there needed any comments?
|
||||
name = New awesome game using Love Loader
|
||||
desc = Some descripion about the game.
|
||||
# main = optional main file instead of `main.lua`
|
||||
```
|
Loading…
Reference in a new issue