Added support for video backgrounds

This commit is contained in:
aur9ra 2023-07-15 21:28:03 -07:00
parent 602e7105ab
commit 2ba3611c56
2 changed files with 66 additions and 32 deletions

View File

@ -1,61 +1,81 @@
named_backgrounds = {"title", "title_no_icon", "title_night", "snow", "options_input", "options_game"} named_backgrounds = {"title", "title_no_icon", "title_night", "snow", "options_input", "options_game"}
backgrounds_played_recently = {} current_playing_bgs = {}
extended_bgs = {}
image_formats = {".png", ".jpg"} image_formats = {".png", ".jpg"}
local bgpath = "res/backgrounds/" bgpath = "res/backgrounds/"
dir = love.filesystem.getDirectoryItems(bgpath)
backgrounds = {} local backgrounds = {}
function loadExtendedBgs()
extended_bgs = require("res.backgrounds.extend_section_bg")
end
--error handling for if there is no extend_section_bg
if pcall(loadExtendedBgs) then end
--helper method to populate backgrounds --helper method to populate backgrounds
function createBackgroundIfExists(name, file_name) function createBackgroundIfExists(name, file_name)
local format_index = 1 local format_index = 1
--try creating image backgrounds -- see if background is an extension of another background
if extended_bgs[file_name] ~= null then
copy_bg = extended_bgs[file_name]
copy_bg = copy_bg/100
backgrounds[name] = backgrounds[copy_bg]
return true
end
-- try creating image backgrounds
while format_index <= #image_formats do while format_index <= #image_formats do
if love.filesystem.getInfo(bgpath.. file_name ..image_formats[format_index]) then for num, existing_file in pairs(dir) do
if existing_file == (file_name..image_formats[format_index]) then
local tempBgPath = bgpath .. file_name .. image_formats[format_index] local tempBgPath = bgpath .. file_name .. image_formats[format_index]
backgrounds[name] = love.graphics.newImage(tempBgPath) backgrounds[name] = love.graphics.newImage(tempBgPath)
return true return true
end end
end
format_index = format_index + 1 format_index = format_index + 1
end end
-- try creating video background
if love.filesystem.getInfo(bgpath .. file_name ..".ogv") then if love.filesystem.getInfo(bgpath .. file_name ..".ogv") then
for num, existing_file in pairs(dir) do
if existing_file == (file_name..".ogv") then
local tempBgPath = bgpath .. file_name .. ".ogv" local tempBgPath = bgpath .. file_name .. ".ogv"
backgrounds[name] = love.graphics.newVideo(tempBgPath, {["audio"] = false}) backgrounds[name] = love.graphics.newVideo(tempBgPath, {["audio"] = false})
-- you can set audio to true, but the video will not loop properly if audio extends beyond video frames -- you can set audio to true, but the video will not loop properly if audio extends beyond video frames
return true return true
end end
end
end
return false return false
end end
function StopOtherBgs(bg)
if #current_playing_bgs == 0 and bg:typeOf("Video") then
current_playing_bgs[#current_playing_bgs+1] = bg
end
if #current_playing_bgs >= 1 then
while current_playing_bgs[1] ~= bg and #current_playing_bgs >= 1 do
current_playing_bgs[1]:pause()
current_playing_bgs[1]:rewind()
table.remove(current_playing_bgs, 1)
end
end
end
function fetchBackgroundAndLoop(id) function fetchBackgroundAndLoop(id)
bg = backgrounds[id] bg = backgrounds[id]
if bg:typeOf("Video") and not bg:isPlaying() then if bg:typeOf("Video") and not bg:isPlaying() then
bg:rewind() bg:rewind()
bg:play() bg:play()
if (not backgrounds_played_recently[1] == bg) or backgrounds_played_recently[1] == nil then
table.insert(backgrounds_played_recently, 1, bg)
print(id)
end
end end
--if background is not loaded, rewind it and pause it StopOtherBgs(bg)
if #backgrounds_played_recently >= 1 then
if backgrounds_played_recently[1] == bg and #backgrounds_played_recently >= 2 then
print("!")
backgrounds_played_recently[2]:pause()
backgrounds_played_recently[2]:rewind()
table.remove(backgrounds_played_recently, 2)
print("Unloaded video #2")
elseif not backgrounds_played_recently[1] == bg then
backgrounds_played_recently[1]:pause()
backgrounds_played_recently[1]:rewind()
table.remove(backgrounds_played_recently, 1)
print("Unloaded most recently played")
end
end
return bg return bg
end end

View File

@ -0,0 +1,14 @@
-- ex: extend_section_bg[100] = 0
-- extend_section_bg[200] = 0
-- the video background associated with section 0 will continue playing into 100 and 200 without restarting.
-- will also cause any existing level 100, 200 backgrounds specified to NOT render.
-- please also note that you cannot currently extend any "named" backgrounds, such as "title" and "options-input"
extend_section_bg = {}
-- extend_section_bg[100] = 0
-- extend_section_bg[200] = 0
-- remove the dashes
return extend_section_bg