/'===============================================================================================
 Music System Example
 Copyright (c), Firelight Technologies Pty, Ltd 2004-2011.

 Demonstrates basic usage of FMOD's data-driven event library (fmod_event.dll)
==============================================================================================='/
#include "windows.bi"
#include "crt.bi"

#include "fmodex/fmod_event.bi"
#include "fmodex/fmod_errors.bi"

const as uinteger MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY = 8
const as uinteger MUSICCUE_EXAMPLES_STEALTH_ARENA = 19
const as uinteger MUSICCUE_EXAMPLES_COUNTRY_ARENA = 22
const as uinteger MUSICCUE_EXAMPLES_RETRO_ROCK_ARENA = 30


sub ERRCHECK(byval result as FMOD_RESULT)
    if (result <> FMOD_OK) then
        printf(!"FMOD error! (%d) %s\n", result, FMOD_ErrorString(result))
        end -1
end if
end sub


' program start
    dim as FMOD_EVENTSYSTEM  ptr eventsystem = any
    dim as FMOD_MUSICSYSTEM  ptr musicsystem = any
    dim as FMOD_MUSICPROMPT  ptr country = any
    dim as FMOD_MUSICPROMPT  ptr stealth = any
    dim as FMOD_MUSICPROMPT  ptr retrorock = any
    dim as FMOD_RESULT        result = any
    dim as string                key

    printf(!"======================================================================\n")
    printf(!"Music System Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"======================================================================\n")
    printf(!"Press '1'       to activate/deactivate country cue.\n")
    printf(!"Press '2'       to activate/deactivate stealth cue.\n")
    printf(!"Press '3'       to activate/deactivate retro rock cue.\n")
    printf(!"Press '<' / '>' to decrease/increase retro rock intensity parameter.\n")
    printf(!"======================================================================\n")

result = FMOD_EventSystem_Create(@eventsystem)
    ERRCHECK(result)
result = FMOD_EventSystem_Init(eventsystem, 64, FMOD_INIT_NORMAL, 0, FMOD_EVENT_INIT_NORMAL)
    ERRCHECK(result)
result = FMOD_EventSystem_SetMediaPath(eventsystem, "media\\")
    ERRCHECK(result)
result = FMOD_EventSystem_Load(eventsystem, "examples.fev", 0, 0)
    ERRCHECK(result)

result = FMOD_EventSystem_GetMusicSystem(eventsystem, @musicsystem)
    ERRCHECK(result)

result = FMOD_MusicSystem_PrepareCue(musicsystem, MUSICCUE_EXAMPLES_COUNTRY_ARENA,    @country)
    ERRCHECK(result)
result = FMOD_MusicSystem_PrepareCue(musicsystem, MUSICCUE_EXAMPLES_STEALTH_ARENA,    @stealth)
    ERRCHECK(result)
result = FMOD_MusicSystem_PrepareCue(musicsystem, MUSICCUE_EXAMPLES_RETRO_ROCK_ARENA, @retrorock)
    ERRCHECK(result)

    /'
        Start off with country cue active
    '/
    result = FMOD_MusicPrompt_Begin(country)
    ERRCHECK(result)

    do
            dim as integer active = 0

            key = inkey

            if (key = "1") then
result = FMOD_MusicPrompt_IsActive(country, @active)
                ERRCHECK(result)
                if (active = 0) then
result = FMOD_MusicPrompt_Begin(country)
                    ERRCHECK(result)
                    printf(!"Country cue begin\n")
                else
result = FMOD_MusicPrompt_End(country)
                    ERRCHECK(result)
                    printf(!"Country cue end\n")
                end if
            ElseIf (key = "2") then
result = FMOD_MusicPrompt_IsActive(stealth, @active)
                ERRCHECK(result)
                if (active = 0) then
result = FMOD_MusicPrompt_Begin(stealth)
                    ERRCHECK(result)
                    printf(!"Stealth cue begin\n")
                else
result = FMOD_MusicPrompt_End(stealth)
                    ERRCHECK(result)
                    printf(!"Stealth cue end\n")
                end if
            ElseIf (key = "3") then
result = FMOD_MusicPrompt_IsActive(retrorock, @active)
                ERRCHECK(result)
                if (active = 0) then
result = FMOD_MusicPrompt_Begin(retrorock)
                    ERRCHECK(result)
                    printf(!"Retro rock cue begin\n")
                else
result = FMOD_MusicPrompt_End(retrorock)
                    ERRCHECK(result)
                    printf(!"Retro rock cue end\n")
                end if
            end if

            if (key = "<" OrElse key = ",") then
                dim as float intensity = any

result = FMOD_MusicSystem_GetParameterValue(musicsystem, MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY, @intensity)
                ERRCHECK(result)

                intensity -= 3.3f
                if (intensity < 0.0f) then
                    intensity = 0.0f
                end if

result = FMOD_MusicSystem_SetParameterValue(musicsystem, MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY, intensity)
                ERRCHECK(result)
            end if
            if (key = ">" OrElse key = ".") then
                dim as float intensity = any

result = FMOD_MusicSystem_GetParameterValue(musicsystem, MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY, @intensity)
                ERRCHECK(result)

                intensity += 3.3f
                if (intensity > 9.9f) then
                    intensity = 9.9f
                end if

result = FMOD_MusicSystem_SetParameterValue(musicsystem, MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY, intensity)
                ERRCHECK(result)
            end if


result = FMOD_EventSystem_Update(eventsystem)
        ERRCHECK(result)
        Sleep(15)

        scope
            dim as float intensity = 0.0f

            FMOD_MusicSystem_GetParameterValue(musicsystem, MUSICPARAM_EXAMPLES_RETRO_ROCK_INTENSITY, @intensity)

            printf(!"retro rock intensity : %.02f\r", intensity)
        end scope

    loop while (key <> chr(27))

result = FMOD_EventSystem_Release(eventsystem)
    ERRCHECK(result)
