
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, "FLASHX 0.5XP FILE MANAGER")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "FOR PSP")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "CODED BY: CLES LLC (qj.net- sAnDiX dcemu- murgero1")
  table.insert(creditstext, "Main menu by: xpack")
  table.insert(creditstext, "")
  table.insert(creditstext, "Hope it works")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "Known bugs:")
  table.insert(creditstext, "")
  table.insert(creditstext, "***************************")
  table.insert(creditstext, "Menu is too fast sometimes.")
  table.insert(creditstext, "***************************")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "^^All files are for the psp^^")
  table.insert(creditstext, "**flash1 is BSOD <-- ok just hit O if you get it**")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "***flash0 is RSOD <-- GET PANDORA OR RECOVERY MENU***")
  table.insert(creditstext, "")
  table.insert(creditstext, " http://codelockdown.com/flashx")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "THANKS FOR DOWNLOADING")
  table.insert(creditstext, "")
  table.insert(creditstext, "")
  table.insert(creditstext, "FLASHX is still buggy, but good....")
  table.insert(creditstext, "Just to view your flash0 & flash1 files")
  table.insert(creditstext, "Please do not modify this code. (You can if you know what you are")
  table.insert(creditstext, "doing.)")
  table.insert(creditstext, "As always this program does in fact access psp system files...")
  table.insert(creditstext, "bricks arew still possible, even if you are just viewing!")
  table.insert(creditstext, "USE AT YOUR OWN RISK!")

  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
