Attribute VB_Name = "MainLoop"
'// Programmed by Ervin Louie B. Pe Benito
'// Mainloop.bas
'// The program's tight loop is coded here
Option Explicit
Public bRunning As Boolean
'//Optional, this show only the framerate

Dim LastTimeCheckFPS As Long '//When did we last check the frame rate?
Dim lasttimecheckfps2 As Long '//used in animation timing
Dim FramesDrawn As Long '//How many frames have been drawn
Public FrameRate As Long '//What the current frame rate is.....

Public Function MainLoop()
Dim LoopER As Integer
    OpenHelpFile
    Sfxpath = App.Path & "\Sounds\"
    'initialize the directx objects
    bRunning = DXInit 'DX the parent and the device object being initialized
    
If bRunning Then
    InitializeDI True 'DI object, keyboard and mouse input device has been initialized
    InitializeMusic 'DSound and music device has been initialized
    'music initialization
    getMusic "customphilant", 1
    getMusic "credits", 2, False 'optional false if not midi file
    getMusic "tinik2", 3
    getMusic "battle1", 4
    getMusic "binasuan", 5
    getMusic "pndanggo", 6
    getMusic "carinosa", 7
    getMusic "bayanko", 8
    getMusic "philnatathem", 9
    Music_Start 1
    
    'texture initialization
    AssignMouseTexture
    AssignMainScreenTexture
    AssignStageTexture
    AssignTilesTexture
    AssignCharTexture
    AssignSceneTexture
    AssignFightTexture
    InitializeItems
    'main character initialization
    Userchar.AnimMax = 2
    Userchar.AnimCount = 1
    UserFightChar.Status = 0
    'Turn the mainscreen on
    DrawScreen = 1
    'Drawstage win initialization
    SetGameMapScreen 32, 64, GameWidth - 64, GameHeight - 64
    'Sprite on screen draw delay
    framewait = 5
    'initializes the keyboard arrowkey controls
    bEnablecontrol = True
    'map loading routine
    LoadMap "map1.map", 1
    LoadMap "map2.map", 2
    LoadMap "map3.map", 3
    LoadMap "map4.map", 4
    LoadMap "map5.map", 5
    LoadMap "map6.map", 6
    'Initializes enemy stats
    InitializeEnemy
    'Get the level up screen msg
    GetWordsfrmFile
    'get the introduction msg
    GetIntroFrmFile
    Do While bRunning
        DoEvents '//Allow windows time to think; otherwise you'll get into a really tight (and bad) loop...
        CheckMouse
        ClearScreen
        If bDevMode Then
            If CheckAllKeys Then
                If GetKeyState(DIK_ESCAPE) Then bRunning = False
            End If
        End If
        'gets the Frame Rate Info according to gettickcount
        If GetTickCount() - LastTimeCheckFPS > 1000 Then
            LastTimeCheckFPS = GetTickCount
            FrameRate = FramesDrawn '//Store the frame count
            FramesDrawn = 0 '//Reset the counter
        End If
        If GetTickCount() - lasttimecheckfps2 > 300 Then
            lasttimecheckfps2 = GetTickCount
            Autoframecount = Autoframecount + 1
            If Autoframecount > Userchar.AnimMax Then Autoframecount = 1
        End If
        FramesDrawn = FramesDrawn + 1
        
        If DrawScreen = 1 Then DrawMainMenu
        If DrawScreen = 2 Then DrawCharacterWin
        If DrawScreen = 3 Then DrawLoadWin
        If DrawScreen = 4 Then DrawCreditsWin
        If DrawScreen = 5 Then DrawStageWin 1 'map(1)
        If DrawScreen = 6 Then FightScreen
        If DrawScreen = 7 Then BuyScreen 2
        If DrawScreen = 8 Then DrawStageWin 2 'map(3)
        If DrawScreen = 9 Then DrawStageWin 3 'map(2)
        If DrawScreen = 10 Then DrawStageWin 4 ' map(4)
        If DrawScreen = 11 Then DrawStageWin 5 'map(5)
        If DrawScreen = 12 Then DrawStageWin 6 'map(6)
        If DrawScreen = 13 Then SaveWin
        If DrawScreen = 14 Then DrawPostFightWin
        If DrawScreen = 15 Then DrawHelpWin
        If DrawScreen = 16 Then GameLoadedWin
        If DrawScreen = 17 Then ChurchScreen
        If DrawScreen = 18 Then EnemyHideOut
        If DrawScreen = 19 Then GameEnd
    Loop
    
    For LoopER = 1 To 9
        Music_Stop LoopER
    Next

    TerminateDI
    TerminateSounds
    DestroyChar
    DestroyItems
    DXdestroy
Else
    MsgBox "Cannot initialize directX"
End If
End Function

