'************************************************
'Error.bi handles an error and outputs to Error.txt
'Pretty simple setup
'************************************************
Sub ErrorHandler
    _Dest DisplayScreen&
    Print "Error Dumped to Error.txt"
    Open "Error.txt" For Output As #9
    Print #9, "Error Timestamp "; Date$; " : "; Time$
    Print #9, ""
    If _InclErrorFile$ = "" Then
        Print #9, "Error Code:"; Err; "in source file: Stereospace3.bas on line:"; _ErrorLine
    Else
        Print #9, "Error Code:"; Err; "in source file: "; _InclErrorFile$; " on line:"; _InclErrorLine
    End If
    Print #9, ErrorDescription$(Err)
    Print #9, ""
    'Print #9, "Resolution:"; Settings.RezX; "x"; Settings.RezY, "Collision Detector:"; Collision, "Scroll Speed:"; Settings.SpeedAt, "Speed Requested:"; Settings.SpeedRequested
    'Print #9, "Spawn Data: "; Player1.SpawnData, "Player1.Wait:"; Player1.Wait, "Stage:"; Player1.Stage, "Health:"; Player1.Health
    'Print #9, ""
    'Print #9, "Sub Data: "; Settings.ErrorSub
    'Print #9, ""
    Print #9, "Active Sprite Dump:"
    'Print #9, "Player Posx:"; Player1.PosX; "PosY:"; Player1.PosY; "Frame:"; Player1.Frame
    'Print #9, ""

    For x = 1 To Engine.EnemyMatrixSize
        'PRINT #9, "Sprite:"; x, "Name: "; EnemyMatrix(x).SpriteName, "PosX:"; INT(Sprites(x).PosX), "PosY:"; INT(Sprites(x).PosY), "Active: "; Sprites(x).Active, "Frame:"; Sprites(x).Frame
        Print #9, "Sprite:"; x, "Active: "; EnemyMatrix(x).Active, "PosX:"; Int(EnemyMatrix(x).PosX), "PosY:"; Int(EnemyMatrix(x).PosY), "Image Pointer:"; EnemyMatrix(x).ImageLocation, "Rotate Deg:"; EnemyMatrix(x).RotateAngle, "Rotate Speed:"; EnemyMatrix(x).RotateSpeed
    Next x

    Print #9, ""
    For x = 1 To 5
        Print #9, "Sprite:"; x, "Active:"; Midground(x).Active, "PosX:"; Midground(x).PosX, "PosY:"; Midground(x).PosY, "MoveX:"; Midground(x).MoveX, "Image Pointer:"; Midground(x).ImageLocation
    Next x


    'Print #9, ""
    'Print #9, "Ground Sprite Dump:"
    'For x = 1 To Settings.GroundSprites
    '    PRINT #9, "Sprite:"; x, "PosX:"; Ground(x).PosX, "PosY:"; Ground(x).PosY, "Active:"; Ground(x).Active, "Frame:"; Ground(x).Frame, "Health:"; Ground(x).Health
    'NEXT x

    'PRINT #9, ""
    'PRINT #9, ""
    'PRINT #9, "Powerup Sprite Dump:"
    'FOR x = 1 TO Settings.PowerUpSprites
    '    PRINT #9, "Sprite:"; x, "Name: "; PowerUp(x).SpriteName,"PosX:"; PowerUp(x).PosX, "PosY:"; PowerUp(x).PosY, "Active:"; PowerUp(x).Active, "Frame:"; PowerUp(x).Frame
    'NEXT x

    'PRINT #9, ""
    'PRINT #9, ""
    'PRINT #9, "Projectile Sprite Dump:"
    'FOR x = 1 TO Settings.ProjectileSprites
    '    PRINT #9, "Sprite:"; x, "Name: "; ProjectileSprites(x).SpriteName,"PosX:"; ProjectileSprites(x).PosX, "PosY:"; ProjectileSprites(x).PosY, "Active:"; ProjectileSprites(x).Active, "Frame:"; ProjectileSprites(x).Frame
    'NEXT x


    Close #9
    End
End Sub


Function ErrorDescription$ (Code)
    If Code = 1 Then ErrorDescription$ = "NEXT without FOR: Missing loop end or look for a missing END IF or END SELECT statement."
    If Code = 2 Then ErrorDescription$ = "Syntax error: Mistyped keyword statements or puctuation errors can create syntax errors."
    If Code = 3 Then ErrorDescription$ = "RETURN without GOSUB: Place sub-procedure line label after program END or EXIT in a SUB-procedure."
    If Code = 4 Then ErrorDescription$ = "Out of Data: A READ has read past the end of DATA. Use RESTORE to reset to data start. ."
    If Code = 5 Then ErrorDescription$ = "Illegal function call: A parameter passed does not match the function type or exceeds certain function limitations. See Illegal Function."
    If Code = 6 Then ErrorDescription$ = "Overflow: A numerical value has exceeded the limitations of a variable type."
    If Code = 7 Then ErrorDescription$ = "Out of memory: A module has exceeded the 64K memory limitation of QB. Try breaking the code up to smaller modules."
    If Code = 8 Then ErrorDescription$ = "Label not defined: GOTO or GOSUB tries to branch to a label that doesn't exist."
    If Code = 9 Then ErrorDescription$ = "Subscript out of range: An array's upper or lower dimensioned boundary has been exceeded."
    If Code = 52 Then ErrorDescription$ = "Bad file name or number: The filename must follow the rules for filenames in the OS and use filenumbers from 1 and 255. Use FREEFILE to avoid duplicate OPEN file numbers."
    If Code = 55 Then ErrorDescription$ = "File already open: CLOSE a file to open it in a different mode."
    If Code = 59 Then ErrorDescription$ = "The record length used for a RANDOM file was insufficient to perform the operation."
    If Code = 62 Then ErrorDescription$ = "Input past end of file: Check for the end of file with EOF when reading from a file"
    If Code = 97 Then ErrorDescription$ = "Intentional error to cause a dump"
    If Code = 98 Then ErrorDescription$ = "Ground Matrix not large enough"
    If Code = 99 Then ErrorDescription$ = "Loop did not exit in a timely mannor."
    If Code = 258 Then ErrorDescription$ = "Zero or bad handle values cannot be used by the QB64 procedure creating the error."
End Function
