arcade_config = require("arcade_config")
coords = require "coords"
require "helpers"
require "controller"
require "states"

require "map"
require "mapa"
require "warp"
require "fade"
require "audio"
require "logo"
require "intro"
require "scenes"
require "title"
require "game"
require "opcions"
require "opcions_input"
require "point"
require "pause"

require "fps"
--require "menu"


coords.set_config({
    tiles_width  = arcade_config.tiles_width,
    tiles_height = arcade_config.tiles_height,
    room_cols    = mapa_room_cols,
    room_rows    = mapa_room_rows,
    rooms_per_floor = mapa_rooms_per_piso,
})

font_default = font.current()

function images_init()
    tiles=surf.load("tiles.gif")
    surf.source(tiles)
    local paleta=pal.load("tiles.gif")
    pal.set(paleta)
    tiles2=surf.load("tiles2.gif")
    -- Crear el warp
    warp.init(tiles)
end

function image_close()
    surf.free(tiles)
    warp.close()
end

function fonts_init()
    font_sf=font.load("X2_font.fnt")
end

function mapa_init()
    sf_mapa=surf.new(mapa_room_cols*mapa_rooms_per_piso,mapa_room_rows*mapa_pisos)
    load_tilemap( sf_mapa )
end

function audio_init()
    -- Càrrega dels audios
    audio_text_abad     = sound.load(audio_text_abad)
    audio_text_premiere = sound.load(audio_text_premiere)
    audio_text_elalien  = sound.load(audio_text_elalien)
    audio_text_batman   = sound.load(audio_text_batman)
    audio_text_imp      = sound.load(audio_text_imp)
    audio_abad_jump     = sound.load(audio_abad_jump)
    audio_abad_fall     = sound.load(audio_abad_fall)
    audio_abad_hit      = sound.load(audio_abad_hit)
    audio_abad_shot     = sound.load(audio_abad_shot)
    audio_abad_step[1]  = sound.load(audio_abad_step[1])
    audio_abad_step[2]  = sound.load(audio_abad_step[2])
    audio_abad_step[3]  = sound.load(audio_abad_step[3])
    audio_abad_step[4]  = audio_abad_step[2]
    audio_switch        = sound.load(audio_switch)
    audio_hit           = sound.load(audio_hit)
    audio_low           = sound.load(audio_low)
end

function mini.init()
    -- back_sf=surf.new(256,192)
    math.randomseed(os.time())
    fonts_init()
    images_init()
    mapa_init()
    fade.init()
    audio_init()
    controller:init()
    logo_config(font_sf)
    surf.target(0)
    surf.cls(16)
    states:executar("logo")
end

function mini.update()
    if key.press(key.F1) then
        win.zoom(win.zoom()-1)
    elseif key.press(key.F2) then
        win.zoom(win.zoom()+1)
    elseif key.press(key.F3) then
        local fs = win.fullscreen()
        win.fullscreen(not fs)
        win.cursor(fs)
    end

    if (game_update) then game_update() end
    
    -- Pausa per a que vaja be en mini 1.5.1
    -- local t0 = os.clock()
    -- while os.clock()-t0<0.01 do end
	-- draw.text(sys.fps(),0,15,2)
end

function mini.close()
    surf.free(font_sf)
end