'/*******************************************************************************
'*   Project: Pong Template
'*
'*   by MadBit   2017-01-16
'*
'*   What is shown in this template?
'*
'*   - create sprites and text objects
'*   - load fonts
'*   - load and play sounds
'*   - gamestate handling
'*   - player control with virtual joysticks
'*   - one and two game modes.
'*   - simple ki for computer opponents.
'*
'*   If you need help to individual commands. You only need to go with the
'*   curser on the command and press F1 then you get the description of the
'*   command on your browser.
'*
'*   Note, it's just a template. You can then do what you want.  I give some
'*   suggestions what you could add.
'*
'*   - the look of the playing field, the puck or the player with smart
'*     graphics exchange.
'*   - the control is too difficult for you? Makes it easier for you.
'*   - builds power ups
'*       > Magnetic fields
'*       > Obstacles
'*       > The center line is temporarily obstructed
'*       > The racket becomes larger or smaller
'*       > Ect.
'*   - a hi-scoreliste
'*   - makes an online version of the game (pre-empties other speaders)
'*
'*   Let the imagination run.
'*
'*   You need help? Do not know how to do this. There is a great forum
'*   (https:'//forum.thegamecreators.com/) register and ask your questions.
'*   It will be helpful to you. ;)
'*
'*/


'// I always recommend using #option_explicit. So you force the compiler to use
'// only previously declared variables. Tip errors of a variable are detected
'// more quickly by error messages.
'#option_explicit

'// All these files should be added to the main file. The difference between
'// #insert and #include is - at #include the file is appended at the end of the
'// skin file. While #insert inserts the file at the location where the line is
'// located.
'#insert        "Constants.agc"
$include "Constants.bas"

$include "PongStart.bas"        '#include   "PongStart.agc"
$include "PongGame.bas"     '#include   "PongGame.agc"

Sub AppBegin()
    '// Here a function is called. So the game is initialized. See the function for
    '// more information.
	SetFolder("/media")
    MainInit()
End sub

Function AppLoop()
    '// This is the main loop in our program. It runs until we finish the program.
    'do
    '// We declare a variable so that we can compare whether a status change took
    '// place.
    Dim Raw iNewState

    '// Save the current frame time. 'g_fFrameTime' has been declared in
    '// 'Constants.agc'.
    g_fFrameTime! = GetFrameTime()

    '// Conditional branches dependent on the current gamestate.
    Select Case g_iState
        '// Handle the gamestate 'GAMESTATE_START'.
        '// This gamestate shows a welcome screen and let you chose a one or two
        '// player game.
        Case GAMESTATE_START
            '// Call 'RunIntro' to display the title screen. Retrievs a new state
            '// to begin a new game
            iNewState = RunIntro()

            '// Will the current state change? So deinitialize Intro (the title
            '// screen). Release of used resources.
            If iNewState <> g_iState Then ReleaseIntro()
            'EndCase

            '// Handle the gamestate 'GAMESTATE_GAME'.
            '// This gamestate handle the whole game logic.
        Case GAMESTATE_GAME
            '// Call 'RunGame' for handle user input, position players, handle
            '// colision, ect.. Seeat file 'PongGame.agc' for more information.
            iNewState = RunGame()

            '// Will the current state change? So deinitialize Game.
            '// Release of used resources.
            If iNewState <> g_iState Then ReleaseGame()
            'EndCase
    End Select

    '// To handle in the next loop the new state, save it to current state.
    g_iState = iNewState

    '// Show the scene as it was built by the game state.
    Sync()
    'loop
    Function = 0
End Function

Sub AppEnd()

End Sub


'/*******************************************************************************
'*   MainInit()
'*
'*   Program initialization. Set the window size, load the font, initialize
'*   global variables.
'*/

Sub MainInit()
    '// Show all errors
    SetErrorMode(2)

    '// Set window properties. Window title and size.
    SetWindowTitle( "PingPONG" )
    SetWindowSize( 1024, 576, 0 )

    '// Det display properties. Here we set the virtual resolution to 1024 x 576.
    '// This makes sure that your objects appear with a different resolution, on
    '// the right place. On mobile devices, we only accept the Landscape format
    '// (SetOrientation)
    SetVirtualResolution( 1024, 576 )
    SetOrientationAllowed( 0, 0, 1, 1 )

    '// Here we load the game font. This is found in the 'media' folder.
    LoadFont(FNTID_GAMEFONT, "setbackt.ttf")

    '// Set the background clear color to grey. Every time you clear the screen
    '// with ClearScreen, the screen will fill with this color.
    SetClearColor(96, 96, 96)

    '// We save the screen width and height in global variables (g_iSWidth,
    '// g_iSHeight) for later usage.
    g_iSWidth = GetVirtualWidth()
    g_iSHeight = GetVirtualHeight()

    '// Check device base. And store it.
    Dim Raw szBase$
    szBase$ = GetDeviceBaseName()
    If szBase$ = "windows" Or szBase$ = "mac" Or szBase$ = "linux" Then g_iDesktop = 1
End Sub
