require("api")
strict(true)
global("json")
require("json")
require("gui")

require("engine")
require("menu")
require("preload")

function _null() end

function json_ext_call(action,data)
    if cuz.params("platform") == 'web' then return nil end
    return json.decode(cuz_ext_call(action,json.encode(data)))
end



function game_init()
    math.randomseed( os.time() )


    cuz.sample_play = function(name,vol) 
        if GAME.cfg_sound ~= 1 then return end
        cuz_sample_play(name,mknumber(vol or 1.0)*1.0) 
    end

    cuz.params("filter","linear")
    cuz.params("caption","beastbook")
    cuz.params("appid","beastbook")
    cuz.params("bgcolor",0x446699)
    
    -- cuz.sample_play = _null
    
    global("GAME")
    GAME = GAME or {}
    GAME.release = true
    GAME.debug = false
    GAME.mode = 0

    GAME.SW = 160
    GAME.SH = 240

    GAME.VW = GAME.SW
    GAME.VH = math.floor(GAME.VW * cuz.params("window_h") / cuz.params("window_w"))
    if GAME.VH < GAME.SH then
        GAME.VH = GAME.SH
    end

    GAME.font = GUI.Font("font",10,{0.5,0,0.5,0})
    GAME.bfont = GUI.Font("bfont",10,{0.5,0,0.5,0})

    GAME.button_b1 = 0x446699
    GAME.button_b2 = 0x6699bb


    GAME.button_c1 = 0x0432fc -- 0x0222bc
    GAME.button_c2 = 0x3bbcfc --  0x6888fc


    GAME.button_g1 = 0x7c7c7c
    GAME.button_g2 = 0xbcbcbc


    GAME.button_d1 = 0x333333
    GAME.button_d2 = 0x333333

    play_ready()


    engine_init()
    menu_init()



end

function _prep() 
    cuz.view_set(0,0,GAME.SW,GAME.SH)
end

        
function init() _prep(); GAME.engine:init() end
function event(e) 
    GAME.engine:event(e)
end
function loop(t)
    GAME.engine:loop(t) 
end
function paint() GAME.engine:paint() end
    
game_init() 