From 4478c07acfdd5502657ff93544ba287a5de7ffb2 Mon Sep 17 00:00:00 2001 From: aur9ra <93367045+aur9ra@users.noreply.github.com> Date: Fri, 14 Jul 2023 17:54:35 -0700 Subject: [PATCH 1/2] First commit, WIP --- load/graphics.lua | 85 +++++++++++++++++++++++++++++++++------ scene/credits.lua | 2 +- scene/game_config.lua | 2 +- scene/input_config.lua | 2 +- scene/key_config.lua | 2 +- scene/mode_select.lua | 2 +- scene/replay_select.lua | 2 +- scene/settings.lua | 2 +- scene/stick_config.lua | 2 +- scene/title.lua | 4 +- scene/tuning.lua | 2 +- tetris/modes/gamemode.lua | 4 +- 12 files changed, 85 insertions(+), 26 deletions(-) diff --git a/load/graphics.lua b/load/graphics.lua index 893bb1b..eb7f747 100644 --- a/load/graphics.lua +++ b/load/graphics.lua @@ -1,17 +1,76 @@ -backgrounds = { - title = love.graphics.newImage("res/backgrounds/title.png"), - title_no_icon = love.graphics.newImage("res/backgrounds/title-no-icon.jpg"), - title_night = love.graphics.newImage("res/backgrounds/title-night.jpg"), - snow = love.graphics.newImage("res/backgrounds/snow.png"), - input_config = love.graphics.newImage("res/backgrounds/options-input.png"), - game_config = love.graphics.newImage("res/backgrounds/options-game.png"), -} +named_backgrounds = {"title", "title_no_icon", "title_night", "snow", "options_input", "options_game"} +backgrounds_played_recently = {} +image_formats = {".png", ".jpg"} +local bgpath = "res/backgrounds/" -local i = 0 -local bgpath = "res/backgrounds/%d.png" -while love.filesystem.getInfo(bgpath:format(i*100)) do - backgrounds[i] = love.graphics.newImage(bgpath:format(i*100)) - i = i + 1 +backgrounds = {} + +--helper method to populate backgrounds +function createBackgroundIfExists(name, file_name) + local format_index = 1 + + --try creating image backgrounds + while format_index <= #image_formats do + if love.filesystem.getInfo(bgpath.. file_name ..image_formats[format_index]) then + local tempBgPath = bgpath .. file_name .. image_formats[format_index] + backgrounds[name] = love.graphics.newImage(tempBgPath) + return true + end + format_index = format_index + 1 + end + + if love.filesystem.getInfo(bgpath .. file_name ..".ogv") then + local tempBgPath = bgpath .. file_name .. ".ogv" + 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 + return true + end + + return false +end + +function fetchBackgroundAndLoop(id) + bg = backgrounds[id] + + if bg:typeOf("Video") and not bg:isPlaying() then + bg:rewind() + 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 + + --if background is not loaded, rewind it and pause it + 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 +end + +--create section backgrounds +local section = 0 +while (createBackgroundIfExists(section, section*100)) do + section = section + 1 +end + +--create named backgrounds +local nbgIndex = 1 +while nbgIndex <= #named_backgrounds do + createBackgroundIfExists(named_backgrounds[nbgIndex], string.gsub(named_backgrounds[nbgIndex], "_", "-")) + nbgIndex = nbgIndex + 1 end -- in order, the colors are: diff --git a/scene/credits.lua b/scene/credits.lua index 63fbc98..f37301d 100644 --- a/scene/credits.lua +++ b/scene/credits.lua @@ -33,7 +33,7 @@ function CreditsScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds[19], + fetchBackgroundAndLoop(id), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/game_config.lua b/scene/game_config.lua index 2276ad6..597a33a 100644 --- a/scene/game_config.lua +++ b/scene/game_config.lua @@ -46,7 +46,7 @@ end function ConfigScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["game_config"], + fetchBackgroundAndLoop("options_game"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/input_config.lua b/scene/input_config.lua index 7b488f8..7909f33 100644 --- a/scene/input_config.lua +++ b/scene/input_config.lua @@ -21,7 +21,7 @@ function ConfigScene:update() end function ConfigScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["input_config"], + fetchBackgroundAndLoop("options_input"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/key_config.lua b/scene/key_config.lua index 419ebb5..f025bd9 100644 --- a/scene/key_config.lua +++ b/scene/key_config.lua @@ -46,7 +46,7 @@ end function KeyConfigScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["input_config"], + fetchBackgroundAndLoop("input_config"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/mode_select.lua b/scene/mode_select.lua index b394896..015cc0f 100755 --- a/scene/mode_select.lua +++ b/scene/mode_select.lua @@ -59,7 +59,7 @@ end function ModeSelectScene:render() love.graphics.draw( - backgrounds[0], + fetchBackgroundAndLoop(0), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/replay_select.lua b/scene/replay_select.lua index 5655df7..a120918 100644 --- a/scene/replay_select.lua +++ b/scene/replay_select.lua @@ -75,7 +75,7 @@ end function ReplaySelectScene:render() love.graphics.draw( - backgrounds[0], + fetchBackgroundAndLoop(0), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/settings.lua b/scene/settings.lua index eaa7361..14d7282 100644 --- a/scene/settings.lua +++ b/scene/settings.lua @@ -30,7 +30,7 @@ function SettingsScene:update() end function SettingsScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["game_config"], + fetchBackgroundAndLoop("options_game"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/stick_config.lua b/scene/stick_config.lua index 3e7be01..7c68d23 100644 --- a/scene/stick_config.lua +++ b/scene/stick_config.lua @@ -47,7 +47,7 @@ end function StickConfigScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["input_config"], + fetchBackgroundAndLoop("options_input"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/title.lua b/scene/title.lua index 35bbc82..72978fa 100644 --- a/scene/title.lua +++ b/scene/title.lua @@ -74,7 +74,7 @@ function TitleScene:render() love.graphics.setFont(font_3x5_4) love.graphics.setColor(1, 1, 1, 1 - self.snow_bg_opacity) love.graphics.draw( - backgrounds["title_no_icon"], -- title, title_night + fetchBackgroundAndLoop("title_no_icon"), -- title, title_night 0, 0, 0, 0.5, 0.5 ) @@ -100,7 +100,7 @@ function TitleScene:render() love.graphics.setFont(font_3x5_2) love.graphics.setColor(1, 1, 1, self.snow_bg_opacity) love.graphics.draw( - backgrounds["snow"], + fetchBackgroundAndLoop("snow"), 0, 0, 0, 0.5, 0.5 ) diff --git a/scene/tuning.lua b/scene/tuning.lua index ef0f4ee..738d22c 100644 --- a/scene/tuning.lua +++ b/scene/tuning.lua @@ -35,7 +35,7 @@ end function TuningScene:render() love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds["game_config"], + fetchBackgroundAndLoop("options_game"), 0, 0, 0, 0.5, 0.5 ) diff --git a/tetris/modes/gamemode.lua b/tetris/modes/gamemode.lua index a24e1a5..1179d20 100644 --- a/tetris/modes/gamemode.lua +++ b/tetris/modes/gamemode.lua @@ -980,10 +980,10 @@ end function GameMode:drawBackground() local id = self:getBackground() - if type(id) == "number" then id = clamp(id, 0, #backgrounds) end + -- if type(id) == "number" then id = clamp(id, 0, #backgrounds) end love.graphics.setColor(1, 1, 1, 1) love.graphics.draw( - backgrounds[id], + fetchBackgroundAndLoop(id), 0, 0, 0, 0.5, 0.5 ) From 2ba3611c564120fc33aeb0bbc53fa630207929cb Mon Sep 17 00:00:00 2001 From: aur9ra <93367045+aur9ra@users.noreply.github.com> Date: Sat, 15 Jul 2023 21:28:03 -0700 Subject: [PATCH 2/2] Added support for video backgrounds --- load/graphics.lua | 84 +++++++++++++++++---------- res/backgrounds/extend_section_bg.lua | 14 +++++ 2 files changed, 66 insertions(+), 32 deletions(-) create mode 100644 res/backgrounds/extend_section_bg.lua diff --git a/load/graphics.lua b/load/graphics.lua index eb7f747..aa075c0 100644 --- a/load/graphics.lua +++ b/load/graphics.lua @@ -1,32 +1,70 @@ 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"} -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 function createBackgroundIfExists(name, file_name) 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 - if love.filesystem.getInfo(bgpath.. file_name ..image_formats[format_index]) then - local tempBgPath = bgpath .. file_name .. image_formats[format_index] - backgrounds[name] = love.graphics.newImage(tempBgPath) - return true + 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] + backgrounds[name] = love.graphics.newImage(tempBgPath) + return true + end end format_index = format_index + 1 end + -- try creating video background if love.filesystem.getInfo(bgpath .. file_name ..".ogv") then - local tempBgPath = bgpath .. file_name .. ".ogv" - 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 - return true + for num, existing_file in pairs(dir) do + if existing_file == (file_name..".ogv") then + local tempBgPath = bgpath .. file_name .. ".ogv" + 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 + return true + end + end + end + return false +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 - return false end function fetchBackgroundAndLoop(id) @@ -35,27 +73,9 @@ function fetchBackgroundAndLoop(id) if bg:typeOf("Video") and not bg:isPlaying() then bg:rewind() 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 - --if background is not loaded, rewind it and pause it - 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 + StopOtherBgs(bg) return bg end diff --git a/res/backgrounds/extend_section_bg.lua b/res/backgrounds/extend_section_bg.lua new file mode 100644 index 0000000..33dbb4c --- /dev/null +++ b/res/backgrounds/extend_section_bg.lua @@ -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 \ No newline at end of file