--[[
  Copyright Tom Lukeywood 2018
    This file is part of Forest2D

    Forest2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Forest2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Forest2D.  If not, see <http://www.gnu.org/licenses/>.
]]--

Forest2D = {}

function Inc(Number, Value)
    if not Value then return Number + 1 else return Number + Value end
end
function Dec(Number, Value)
    if not Value then return Number - 1 else return Number - Value end
end
function Round(x)
  return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end

function TrySDL(func, ...)
local t = { func(...) }
if not t[1] then
error(t[#t])
end
return table.unpack(t)
end

function Forest2D:Init()

    SDL   = require "SDL"
    Image = require "SDL.image"
    Mixer = require "SDL.mixer"
    TTF   = require "SDL.ttf"

    TrySDL(SDL.init, { SDL.flags.Video })

    print(string.format("SDL %d.%d.%d",
    SDL.VERSION_MAJOR,
    SDL.VERSION_MINOR,
    SDL.VERSION_PATCH
    ))
    if not SDL or not Image or not Mixer or not TTF then return nil end
end

function Forest2D:CreateSDLWindow(Title, Icon)
    local window, error = SDL.createWindow
    {
    title  = Title,
    width  = Forest2D.WINDOW_WIDTH,
    height = Forest2D.WINDOW_HEIGHT,
    flags  = {SDL.window.Resizable, SDL.rendererFlags.Accelerated}
    }
    if not window then error(error) end
    window:setIcon(TrySDL(Image.load, Icon))
    return window

end

function Forest2D:CreateSDLRenderer(window)
  local Renderer, error = TrySDL(SDL.createRenderer, window, -1)
  if not Renderer then error(error) end
  Renderer:setLogicalSize(Forest2D.WINDOW_WIDTH, Forest2D.WINDOW_HEIGHT)
  return Renderer
end

function Forest2D:Quit()
    SDL.quit()
    Mixer.quit()
    Image.quit()
    TTF.quit()
    return
end

Forest2D.Audio = require "Audio"
Forest2D.TileAnimation = require "TileAnimation"
Forest2D.World = require "World"
Forest2D.EventHandler = require "EventHandler"
Forest2D.GUI = require "GUI"
Forest2D.Database = require "Data"
Forest2D.Text = require "Text"
Forest2D.Sprite = require "Sprite"

--default
Forest2D.WINDOW_WIDTH = 640
Forest2D.WINDOW_HEIGHT = 480

Forest2D.TextureList = {}

function Forest2D.TextureList:Load(Renderer, Directory)
  require "paths"

  Files = {}

  for f in paths.files(Directory) do
      Files[#Files+1] = f
  end

  for _, v in pairs(Files) do
    if v:match(".bmp") or v:match(".gif")
    or v:match(".png") or v:match(".jpg")
    or v:match(".jpeg") or v:match(".pcx")
    or v:match(".tiff") or v:match(".lbm")
    or v:match(".pnm") or v:match(".svg")
    or v:match(".tga") or v:match(".webp")
    or v:match(".xcf") or v:match(".xpm")
    or v:match(".xv") then
      Forest2D.TextureList:Push(Renderer:createTextureFromSurface(Image.load(Directory .. "/" .. v)))
    end
  end

end

function Forest2D.TextureList:Push(Texture)
  Forest2D.TextureList[#Forest2D.TextureList+1] = Texture
end

function Forest2D.TextureList:Pop()
  Forest2D.TextureList[#Forest2D.TextureList] = nil
end

return Forest2D
