-- Set globals variables for easier use of the engine everywhere

function Setglobals()

  Global={["Speedt"]={},["Displayt"]={},["Key"]={},["Touchscreen"]={},["Joystick"]={},["Parameter"]={}}

  Global.maxresx,Global.maxresy=love.window.getDesktopDimensions()
  Global.Speedt.low,Global.Speedt.med,Global.Speedt.high=.1,.4,.8
  Global.x,Global.y=nil,nil
  Global.scale=4.8 -- scale coef for res
  Global.scalex,Global.scaley=0,0
  
  Global.OS=love.system.getOS()
  
  Global.Key.pressed=""
  Global.Key.released=""
  Global.Key.list={}
  
  Global.Touchscreen.position={x=nil,y=nil}
  Global.Touchscreen.pressure=nil
  
  Global.Joystick.isjoy=false
  
  Global.icon=love.image.newImageData("icon.png")

  Global.Parameter.touchscreen=true
  Global.Parameter.mouse=true
  Global.Parameter.keyboard=true
  Global.Parameter.gamepad=true
  Global.Parameter.debug=true
  
  local alldisp={a={2048,1080},b={1920,1200},c={1920,1080},d={1536,864},e={1440,900},f={1366,768},g={1280,720},h={1024,768},i={800,600}}
  
  for i in pairs(alldisp) do
  
    if alldisp[i][1]<=Global.maxresx and alldisp[i][2]<=Global.maxresy then
    
      table.insert(Global.Displayt,alldisp[i]) -- all possible display for screen
    
    end
  
  end

end

-- Load libs and classes

function Setlibs()

  -- Main files

  Engine=require("Source.engine")
  Cache=require("Source.ressource")
  Commands=require("Source.input")
  Settings=require("Source.settings")
  require("Source.sound")
  
  -- Utils
  
  require("Source.Utils.camera")
  File=require("Source.Utils.file")
  Info=require("Source.Utils.debug")
  Physics=require("Source.Utils.physics")
  
  -- Thirdparty libs
  
  Toml=require("Source.Thirdparty.toml")
  Push=require("Source.Thirdparty.push")
  Flux=require("Source.Thirdparty.flux")
  Sti=require("Source.Thirdparty.sti")
  
  -- Game classes
  
  require("Source.Classes.Text")
  require("Source.Classes.Entities")
  require("Source.Classes.Blocks")
  require("Source.Classes.Objects")

end

-- Some gamestate

function Setstate()

  Gamestate={}
  Gamestate.mainmenu=false
  Gamestate.overworld=false
  Gamestate.battle=false
  Gamestate.loading=true
  Gamestate.cutscene=false
  Gamestate.gameover=false
  Gamestate.credits=false
  
  Substate={}
  Substate.static=false -- No movement in game
  Substate.autocam=false -- false for locked in entity
  Substate.focus=false

end

-- Set first need shared assets

function Setassets()

  Sharedfont=Cache.Font.new()
  
  Sharedfont.add("eb","Assets/fonts/EBMain.ttf",50)
  Sharedfont.setmain("eb")

end

-- Most important function !! Use this to launch the game

function Setup()
  
  Setglobals()
  Setlibs()
  Setstate()
  Setassets()
  
  love.filesystem.setIdentity(love.filesystem.getIdentity(),true) -- avoid crash while reading directories
  
  if Global.Parameter.touchscreen then
  
    sensor=Commands.touchscreen.new()
  
  end
  
  keyboard=Commands.keyboard.new()
  joystick=Commands.joystick.new()
  settings=Settings.new()
  
  keyboard.getfromfile("input.toml")
  settings.getfromfile("settings.toml")
  settings.apply() -- do it at least one time or it will crash
  
  if Global.Parameter.debug then
  
    debug=Info.enable()
  
  end
  
  love.graphics.setDefaultFilter("linear","nearest",0) -- to change if graphics go wrong
  love.window.setTitle("The Photon Project")
  love.window.setIcon(Global.icon)

end
