set_project("Frostbite2D")
set_version("1.0.0")
set_license("MIT")

set_languages("c++17")
set_encodings("utf-8")

add_rules("mode.debug", "mode.release")

if not is_plat("switch") then
    add_requires("libsdl2", {configs = {shared = true}})
    add_requires("libsdl2_image", {configs = {shared = true}})
    add_requires("libsdl2_mixer", {configs = {shared = true}})
    add_requires("libsdl2_ttf", {configs = {shared = true}})
    add_requires("glm")
    add_requires("zlib")
end

includes("engine.lua")

if is_plat("windows") then
    target("Frostbite2DWindowsSmoke")
        set_kind("binary")
        add_files("examples/windows_smoke/main.cpp")
        add_deps("Frostbite2D")
        add_packages("libsdl2")
        add_packages("libsdl2_image")
        add_packages("libsdl2_mixer")
        add_packages("libsdl2_ttf")
        add_packages("glm")
        add_packages("zlib")
        add_syslinks("imm32")
    target_end()

    target("Frostbite2DGame")
        set_kind("binary")
        add_files("game/src/**.cpp")
        add_deps("Frostbite2D")
        add_packages("libsdl2")
        add_packages("libsdl2_image")
        add_packages("libsdl2_mixer")
        add_packages("libsdl2_ttf")
        add_packages("glm")
        add_packages("zlib")
        add_syslinks("imm32")
        after_build(function (target)
            os.cp(path.join(os.projectdir(), "game/assets"),
                  path.join(target:targetdir(), "assets"))
        end)
    target_end()
end

if is_plat("switch") then
    target("Frostbite2DGameSwitch")
        set_kind("binary")
        set_filename("Frostbite2DGame.elf")
        add_files("game/src/**.cpp")
        add_deps("Frostbite2D")
        add_cxflags("-D__SWITCH__", "-fPIE", {public = true})
        add_ldflags("-specs=" .. path.join(os.getenv("DEVKITPRO") or "C:/devkitPro", "libnx/switch.specs"), "-fPIE", {force = true})
        after_build(function (target)
            local outdir = path.join(target:targetdir(), "switch_game")
            os.mkdir(outdir)
            os.cp(path.join(os.projectdir(), "game/assets"),
                  path.join(outdir, "assets"))

            local devkitpro = os.getenv("DEVKITPRO") or "C:/devkitPro"
            local tools = path.join(devkitpro, "tools/bin")
            local libnx = path.join(devkitpro, "libnx")
            local nacp = path.join(outdir, "control.nacp")
            local nro = path.join(outdir, "NSUnknownGame.nro")
            local romfsdir = path.join(outdir, "romfs")
            os.rm(romfsdir)
            os.mkdir(romfsdir)
            os.cp(path.join(os.projectdir(), "game/assets"),
                  path.join(romfsdir, "assets"))
            os.execv(path.join(tools, "nacptool.exe"), {
                "--create", "NS Unknown Game", "Frostbite2D", "1.0.0", nacp
            })
            os.execv(path.join(tools, "elf2nro.exe"), {
                target:targetfile(), nro,
                "--nacp=" .. nacp,
                "--icon=" .. path.join(libnx, "default_icon.jpg"),
                "--romfsdir=" .. romfsdir
            })
        end)
    target_end()
end
