import ft8xx
import gui

def display_img(fn, x, y, ptr):
    with open(fn, 'rb') as f:
        img = f.read()
    # TODO: Use media FIFO for this.
    ft8xx.cpcmd_loadimage(ptr, 0, len(img), ft8xx.addressof(img))
    ft8xx.cp_cmd(gui.DL_BEGIN(gui.PRIM_BITMAPS))
    ft8xx.cp_cmd(gui.DL_VERTEX2F(x, y))
    ft8xx.cp_cmd(gui.DL_END())

def cat_and_doge():
    # Stop GUI loop so we can compose a custom scene
    gui.pause(True)
    # Take low-level mutex
    ft8xx.mutex_take()
    # Compose a scene with the pictures
    ft8xx.cp_start()
    ft8xx.cp_cmd(gui.DL_CLEAR(True, True, True))
    display_img('doge_lg.jpg', 0, 0, 0)
    display_img('catpc.png', 507, 0, 0x80000)
    ft8xx.cp_finish()
    # Release mutex (but GUI is still paused, so we can view the result)
    ft8xx.mutex_give()


cat_and_doge()