'******************************************************************************
'* Here I declare some constants and global variables. That only impotant
'* for this module (gamestate).
'*
'* Everyone has somewhere its own name conventions as specifying its variable.
'* You will surely find your own. But here I describe mine.
'*
'* For constants, I am writing them in upper case. For AGK-Basic totally
'* unimportant, because the compiler works not case sensitive. But it looks
'* better (for my eyes).
'*
'* For global variables, I set a 'g_' at the front followed by another prefix
'* that describes the variable PPType. 'i' for integer, 'f' for float, 'sz' for
'* string and 't' for PPType variable. Finally, then comes the Simple tile name of
'* the variable. The first letter then big again.
'*
'* For global variables that are only important for the module, are similarly
'* written. Only the front is set to an 'm_' followed by the modulame (end with
'* underscore). The rest just like the global variables. >Good to know: You can
'* use all these variables in all modules. It's just a spelling like I prefer it.
'* They behave like all other global variables.
'*
'* AGK works with Id's for its objects/entities. You can receive an free id for
'* your object or you can foce an id. Every time you want to use an entity you
'* have to specify this id.
'* Also here I declare after a certain convention my entities id's.
'* SPRID_ for sprites, SFXID_ for sounds TXTID_ for text and FNTID_ for fonts
'* entities. Again followed by the module name. The last one is the real name.
'*


Const TXTID_INTRO_OPTION1           = 1
Const TXTID_INTRO_OPTION2           = 2
Const TXTID_INTRO_TEXT1                 = 3
Const TXTID_INTRO_TEXT2                 = 4
Const TXTID_INTRO_TEXT3                 = 5
Const TXTID_INTRO_SELECTION             = 6

'// We need a variable to check if this module initialized or not. Starting value
'// is 0 (not initialized).
Raw As integer m_intro_iInit = 0



'******************************************************************************
'*   RunIntro()
'*   RETURN VALUE    - new game state (handle this in the main loop.)
'*
'*   Main function of intro status. Processing at each loop until the status
'*   is active.
'*

Function RunIntro()
    '// Declare a variable and store the current game state.
    Dim iState
    iState = g_iState

    '// Clear the screen with background color. Color was set in
    '// (file: 'main.agc'' -> function: 'MainInit')
    ClearScreen()

    '// Check if Intro is initialized! If not than do it.
    '// (Call function: 'InitIntro')
    If m_intro_iInit = 0 Then InitIntro()

    '// Check if pointer is pressed or the screen is taped if this a touch screen
    '// device.
    If GetPointerPressed() Then
        '// Yes the pointer was pressed. Declare variable to store the position
        '// where it was pressed on the screen.
        Dim iPointerX
        Dim iPointerY

        '// Get the position of the pointer.
        iPointerX = GetPointerX()
        iPointerY = GetPointerY()

        '// Check if the position on the text for 'ONE PLAYER'.
        If GetTextHitTest(TXTID_INTRO_OPTION1, iPointerX, iPointerY) = 1 Then
            '// Yes. Store in the global variable the number of players (1)
            '// (declared in file: 'Constants.agc') and set the return gamestate
            '// to a new state.
            g_iPlayers = 1
            iState = GAMESTATE_GAME
        ElseIf GetTextHitTest(TXTID_INTRO_OPTION2, iPointerX, iPointerY) = 1 Then   '// No. Check if pressed on 'TWO PLAYER'.
            '// Yes. Store in the global variable the number of players (2)
            '// (declared in file: 'Constants.agc') and set the return gamestate
            '// to a new state.
            g_iPlayers = 2
            iState = GAMESTATE_GAME
        EndIf
    EndIf
    Function = iState '// Here we return the new gamestate.
End Function


'******************************************************************************
'*   InitIntro()
'*
'*   To initialize all entities for the introscreen. Loading and / or creating
'*   content for this module.
'*

Sub InitIntro()
    '// Declare variables for store the half screen width and half height.
    Dim fHalfScreenWidth As float
    Dim fHalfScreenHeight As float

    '// Calculate the half screen width and height.
    fHalfScreenWidth = 0.5 * g_iSWidth
    fHalfScreenHeight = 0.5 * g_iSHeight

    '// Create a text, in this case to display 'ONE PLAYER'. Set the text size
    '// for this text, spacing between chars, font to use with this text, the
    '// alignment and the position of the text.
    '// Alignment: 0 = ancor point is the top/left corner of the text.
    '// Alignment: 1 = ancor point is the center/top edge of the text.
    '// Alignment: 2 = ancor point is the top/right corner of the text.
    CreateText(TXTID_INTRO_OPTION1, "ONE PLAYER")
    SetTextSize(TXTID_INTRO_OPTION1, 48.0)
    SetTextSpacing(TXTID_INTRO_OPTION1, 8.0)
    SetTextFont(TXTID_INTRO_OPTION1, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_OPTION1, 1)
    SetTextPosition(TXTID_INTRO_OPTION1, fHalfScreenWidth, 48.0)

    '// Same as above.
    CreateText(TXTID_INTRO_OPTION2, "TWO PLAYER")
    SetTextSize(TXTID_INTRO_OPTION2, 48.0)
    SetTextSpacing(TXTID_INTRO_OPTION2, 8.0)
    SetTextFont(TXTID_INTRO_OPTION2, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_OPTION2, 1)
    SetTextPosition(TXTID_INTRO_OPTION2, fHalfScreenWidth, 96.0)

    '// Same as above.
    CreateText(TXTID_INTRO_TEXT1, "PingPONG TEMPLATE")
    SetTextSize(TXTID_INTRO_TEXT1, 48.0)
    SetTextSpacing(TXTID_INTRO_TEXT1, 8.0)
    SetTextFont(TXTID_INTRO_TEXT1, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_TEXT1, 1)
    SetTextPosition(TXTID_INTRO_TEXT1, fHalfScreenWidth, fHalfScreenHeight - 48.0)

    '// Same as above.
    CreateText(TXTID_INTRO_TEXT2, "BY")
    SetTextSize(TXTID_INTRO_TEXT2, 48.0)
    SetTextSpacing(TXTID_INTRO_TEXT2, 8.0)
    SetTextFont(TXTID_INTRO_TEXT2, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_TEXT2, 1)
    SetTextPosition(TXTID_INTRO_TEXT2, fHalfScreenWidth, fHalfScreenHeight)

    '// Same as above.
    CreateText(TXTID_INTRO_TEXT3, "MADBIT")
    SetTextSize(TXTID_INTRO_TEXT3, 48.0)
    SetTextSpacing(TXTID_INTRO_TEXT3, 8.0)
    SetTextFont(TXTID_INTRO_TEXT3, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_TEXT3, 1)
    SetTextPosition(TXTID_INTRO_TEXT3, fHalfScreenWidth, fHalfScreenHeight + 48.0)

    '// Same as above.
    CreateText(TXTID_INTRO_SELECTION, "SELECT ONE OR TWO PLAYER")
    SetTextSize(TXTID_INTRO_SELECTION, 48.0)
    SetTextSpacing(TXTID_INTRO_SELECTION, 8.0)
    SetTextFont(TXTID_INTRO_SELECTION, FNTID_GAMEFONT)
    SetTextAlignment(TXTID_INTRO_SELECTION, 1)
    SetTextPosition(TXTID_INTRO_SELECTION, fHalfScreenWidth, g_iSHeight - 48.0)

    '// Now this modul is initialized. Set the variable to 1. Mark it as
    '// initialized.
    m_intro_iInit = 1
End Sub


'******************************************************************************
'*   ReleaseIntro()
'*
'*   Release all content of this module.
'*

Sub ReleaseIntro()
    '// In the main initialization (main.agc) is no text initialized. So we can
    '// delete all the text. That's all what we created in the initialization
    '// routine.
    DeleteAllText()

    '// And we must set the initialization variable to 0. Mark it as non
    '// initialized.
    m_intro_iInit = 0
End Sub
