-- luafiler (c) 2006 mallchin, http://www.mallchin.com/luafm/
-- You are free to distribute this file without modifications.

function cycle_colours(a, b, c)
  if a < 255 and b == 0 and c == 0 then
    a = a + 1
  elseif a == 255 and b < 255 and c == 0 then
    b = b + 1
  elseif a == 255 and b == 255 and c < 255 then
    c = c + 1
  elseif a <= 255 and a > 0 and b == 255 and c == 255 then
    a = a - 1
  elseif a == 0 and b <= 255 and b > 0 and c == 255 then
    b = b - 1
  elseif a == 0 and b == 0 and c <= 255 and c > 0 then
    c = c - 1
  end
  return { a1 = a, b1 = b, c1 = c }
end

function display_credits()

  local cola = 0
  local colb = 0
  local colc = 0

  local val = 0
  local runcredits = true

  local logo = Image.createEmpty(64, 64)
  logo:clear(grey)

  local plane = {
    {black, -8, -3,  0},
    {purple, 8,  3,  0},
    {black,  8,  -3,  0},
    {purple, 8, 3,  0},
    {black, -8,  -3,  0},
    {purple,  -8,  3,  0},
  }

  local ypos_default = 0
  local ypos_offset = 0
  local ypos_increment = 0.50

  local creditstext = {}
  table.insert(creditstext, "[Credits]")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Coding: mallchin")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Graphics: mallchin")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Bugs: mallchin")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "[Thanks to]")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Shine/Nevyn for LuaPlayer")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Dark Killer for modified LuaPlayer")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Lumo for ScreenShot function")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "http://www.mallchin.com/luafm/")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")

  while runcredits do

    -- cycle colours
    local newcol = cycle_colours(cola, colb, colc)
    cola = newcol.a1
    colb = newcol.b1
    colc = newcol.c1
    local col1 = Color.new(cola, colb, colc)
    local col2 = Color.new(colc, cola, colb)
    local col3 = Color.new(colb, colc, cola)

    local cube = {
      {0, 0, col1, -1, -1,  1},  -- 0
      {2, 0, col1, -1,  1,  1},  -- 4
      {2, 2, col1,  1,  1,  1},  -- 5

      {0, 0, col1, -1, -1,  1},  -- 0
      {2, 2, col1,  1,  1,  1},  -- 5
      {0, 2, col1,  1, -1,  1},  -- 1

      {0, 0, col1, -1, -1, -1},  -- 3
      {2, 0, col1,  1, -1, -1},  -- 2
      {2, 2, col1,  1,  1, -1},  -- 6

      {0, 0, col1, -1, -1, -1},  -- 3
      {2, 2, col1,  1,  1, -1},  -- 6
      {0, 2, col1, -1,  1, -1},  -- 7

      {0, 0, col2,  1, -1, -1},  -- 0
      {2, 0, col2,  1, -1,  1},  -- 3
      {2, 2, col2,  1,  1,  1},  -- 7

      {0, 0, col2,  1, -1, -1},  -- 0
      {2, 2, col2,  1,  1,  1},  -- 7
      {0, 2, col2,  1,  1, -1},  -- 4

      {0, 0, col2, -1, -1, -1},  -- 0
      {2, 0, col2, -1,  1, -1},  -- 3
      {2, 2, col2, -1,  1,  1},  -- 7

      {0, 0, col2, -1, -1, -1},  -- 0
      {2, 2, col2, -1,  1,  1},  -- 7
      {0, 2, col2, -1, -1,  1},  -- 4

      {0, 0, col3, -1,  1, -1},  -- 0
      {2, 0, col3,  1,  1, -1},  -- 1
      {2, 2, col3,  1,  1,  1},  -- 2

      {0, 0, col3, -1,  1, -1},  -- 0
      {2, 2, col3,  1,  1,  1},  -- 2
      {0, 2, col3, -1,  1,  1},  -- 3

      {0, 0, col3, -1, -1, -1},  -- 4
      {2, 0, col3, -1, -1,  1},  -- 7
      {2, 2, col3,  1, -1,  1},  -- 6

      {0, 0, col3, -1, -1, -1},  -- 4
      {2, 2, col3,  1, -1,  1},  -- 6
      {0, 2, col3,  1, -1, -1},  -- 5
    }

    Gu.start3d()

    -- clear screen
    Gu.clearDepth(0);
    Gu.clear(Gu.COLOR_BUFFER_BIT|Gu.DEPTH_BUFFER_BIT)

    -- setup projection and view matrices
    Gum.matrixMode(Gu.PROJECTION)
    Gum.loadIdentity()
    Gum.perspective(75, 16/9, 0.5, 1000)

    Gum.matrixMode(Gu.VIEW)
    Gum.loadIdentity()

    -- setup matrix for triangle
    Gum.matrixMode(Gu.MODEL)
    Gum.loadIdentity()
    Gum.translate(0, 0, -3)

    -- draw triangle without texture
    Gu.disable(Gu.TEXTURE_2D)
    Gum.drawArray(Gu.TRIANGLES, Gu.COLOR_8888|Gu.VERTEX_32BITF|Gu.TRANSFORM_3D, plane)

    -- setup texture
    Gu.enable(Gu.BLEND)
    Gu.blendFunc(Gu.ADD, Gu.SRC_ALPHA, Gu.ONE_MINUS_SRC_ALPHA, 0, 0)
    Gu.enable(Gu.TEXTURE_2D)
    Gu.texImage(logo)
    Gu.texFunc(Gu.TFX_MODULATE, Gu.TCC_RGBA)
    Gu.texEnvColor(white)
    Gu.texFilter(Gu.LINEAR, Gu.LINEAR)
    Gu.texScale(1, 1)
    Gu.texOffset(0, 0)
    Gu.ambientColor(white)

    -- setup matrix for cube
    Gum.matrixMode(Gu.MODEL)
    Gum.loadIdentity()
    Gum.translate(0, 0, -3.5)
    Gum.rotateXYZ(val * 0.79 * (Gu.PI/180), val * 0.98 * (Gu.PI/180.0), val * 1.32 * (Gu.PI/180.0))

    -- draw cube
    Gum.drawArray(Gu.TRIANGLES, Gu.TEXTURE_32BITF|Gu.COLOR_8888|Gu.VERTEX_32BITF|Gu.TRANSFORM_3D, cube)

    Gu.end3d()

    val = val + 1

    -- render credits text
    ypos = 280
    for k,v in pairs(creditstext) do
      screen:print((((screen_width - (string.len(v) * 8)) / 2) + shadow_xoffset), ((ypos - ypos_offset) + shadow_yoffset), v, black)
      screen:print(((screen_width - (string.len(v) * 8)) / 2), (ypos - ypos_offset), v, white)
      ypos = ypos + 10
    end

    -- loop credits
    ypos_offset = ypos_offset + ypos_increment
    if ypos_offset >= ((table.getn(creditstext) * 8) * 2) then
      ypos_offset = ypos_default
    end

    -- colour 1
--    screen:print(10, 230, cola, grey)
--    screen:print(50, 230, colb, grey)
--    screen:print(90, 230, colc, grey)

    -- colour 2
--    screen:print(10, 240, colc, grey)
--    screen:print(50, 240, cola, grey)
--    screen:print(90, 240, colb, grey)

    -- colour 3
--    screen:print(10, 250, colb, grey)
--    screen:print(50, 250, colc, grey)
--    screen:print(90, 250, cola, grey)

    -- render quit message
    screen:print(10, 260, "Press CIRCLE to quit Credits.", grey)

    -- press circle
    if Controls.read():circle() then

      -- clear screen
      screen:clear()

      -- render file list
      render_filelist()

      -- flip screen
      screen.flip()
      screen.waitVblankStart()

      -- read pad
      padmode = "filelist"
      readpad()

      -- quit credits
      runcredits = false

    end

    screen.waitVblankStart()
    screen.flip()

  end

end
