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

 Demonstrates basic usage of event max playbacks behaviour.
============================================================================='/
#include "windows.bi"
#include "crt.bi"

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

dim as const zstring  ptr MEDIA_PATH = @"media\\"

static shared as float g_distance = 2.0f
static shared as float g_sound    = 0.0f

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

sub setupEvent(byval event as FMOD_EVENT ptr)
    dim as FMOD_RESULT           result = any
    dim as FMOD_EVENTPARAMETER  ptr param = any
    dim as FMOD_VECTOR           position = type(0, 0, g_distance)

result = FMOD_Event_Set3DAttributes(event, @position, 0, 0)
    ERRCHECK(result)
result = FMOD_Event_GetParameter(event, "sound", @param)
    ERRCHECK(result)
result = FMOD_EventParameter_SetValue(param, g_sound)
    ERRCHECK(result)
end sub


sub stopAllEvents(byval eventgroup as FMOD_EVENTGROUP ptr)
    ERRCHECK(FMOD_EventGroup_FreeEventData(eventgroup, 0, 1))
end sub


' program start
    dim as FMOD_RESULT        result = any
    dim as FMOD_EVENTSYSTEM  ptr eventsystem = any
    dim as FMOD_EVENTGROUP   ptr eventgroup
    dim as FMOD_EVENT        ptr event = any
    dim as string                key

    enum
        StealOldest,
        StealNewest,
        StealQuietest,
        JustFail,
        JustFailIfQuietest
     end enum

      dim as integer event_behaviour = StealOldest

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_PATH)
    ERRCHECK(result)
result = FMOD_EventSystem_Load(eventsystem, "examples.fev", 0, 0)
    ERRCHECK(result)
result = FMOD_EventSystem_GetGroup(eventsystem, "examples/FeatureDemonstration/MaxPlayback", FMOD_EVENT_DEFAULT, @eventgroup)
    ERRCHECK(result)


    printf(!"======================================================================\n")
    printf(!"Max Playbacks Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"----------------------------------------------------------------------\n")
    printf(!"Press '1'   to select 'Steal oldest' behaviour\n")
    printf(!"Press '2'   to select 'Steal newest' behaviour\n")
    printf(!"Press '3'   to select 'Steal quietest' behaviour\n")
    printf(!"Press '4'   to select 'Just fail' behaviour\n")
    printf(!"Press '5'   to select 'Just fail if quietest' behaviour\n")
    printf(!"Press Space to start an event\n")
    printf(!"Press 's'   to stop all events\n")
    printf(!"Press '>'   to increase event distance\n")
    printf(!"Press '<'   to decrease event distance\n")
    printf(!"Press ESC to quit\n")
    printf(!"======================================================================\n")

    do
            dim as zstring ptr ename
            key = inkey

            select case key
                case "1" : ' 'Steal oldest'
                    event_behaviour = StealOldest
                    stopAllEvents(eventgroup)
                case "2" : ' 'Steal newest'
                    event_behaviour = StealNewest
                    stopAllEvents(eventgroup)
                case "3" : ' 'Steal quietest'
                    event_behaviour = StealQuietest
                    stopAllEvents(eventgroup)
                case "4" : ' 'Just fail'
                    event_behaviour = JustFail
                    stopAllEvents(eventgroup)
                case "5" : ' 'Just fail if quietest'
                    event_behaviour = JustFailIfQuietest
                    stopAllEvents(eventgroup)
                case " " : ' Play an event

                    select case event_behaviour
                        case StealOldest :
                            ename = @"MaxPlay-StealOldest"
                        case StealNewest :
                            ename = @"MaxPlay-StealNewest"
                        case StealQuietest :
                            ename = @"MaxPlay-StealQuietest"
                        case JustFail :
                            ename = @"MaxPlay-JustFail"
                        case JustFailIfQuietest :
                            ename = @"MaxPlay-JustFailIfQuietest"
                   end select

                    ' Clear the line
                    printf(!"%79s\r", " ")

                    if (event_behaviour = JustFailIfQuietest) then
                        /' The 'Just fail if quietest' behaviour calculates the expected
                           volume of the event based on the properties of the info-only
                           event, so we have to get the info-only event first and set it
                           up appropriately.
                         '/

                        ' get the info-only event to set up for volume calculation
result = FMOD_EventGroup_GetEvent(eventgroup, ename, FMOD_EVENT_INFOONLY, @event)
                        ERRCHECK(result)

                        /' set the desired properties on the info-only event
                           Notes:
                           - distances below the event's 3D Min Distance all give the
                             same volume in this case, getEvent will just fail
                           - we could set other volume-affecting properties here as
                             well (e.g. orientation if the event has a falloff cone)
                         '/
                        setupEvent(event)

                        ' attempt to get a real event instance
                        result = FMOD_EventGroup_GetEvent(eventgroup, ename, FMOD_EVENT_DEFAULT, @event)
                        if (result = FMOD_OK) then
                            dim as FMOD_EVENTPARAMETER ptr param = any

                            printf(!"getEvent(\"%s\") succeeded\n", ename)

                            /' we don't need to set the position of the instance,
                               as it is copied from the info-only event, but we
                               do need to set the parameter value.
                             '/
result = FMOD_Event_GetParameter(event, "sound", @param)
                            ERRCHECK(result)
result = FMOD_EventParameter_SetValue(param, g_sound)
                            ERRCHECK(result)
result = FMOD_Event_Start(event)
                            ERRCHECK(result)
                        else
                            printf(!"getEvent(\"%s\") failed\n", ename)
                        end if
                    else
                        result = FMOD_EventGroup_GetEvent(eventgroup, ename, FMOD_EVENT_DEFAULT, @event)
                        if (result = FMOD_OK) then
                            printf(!"getEvent(\"%s\") succeeded\n", ename)
                            setupEvent(event)
result = FMOD_Event_Start(event)
                            ERRCHECK(result)
                        else
                            printf(!"getEvent(\"%s\") failed\n", ename)
                        end if
                    end if

                    g_sound+=1
                    if(g_sound > 3) then
                        g_sound = 0
                    end if

                case "s" :
                    stopAllEvents(eventgroup)
                case ">", "." :
                    g_distance += 0.1f
                case "<", "," :
                    g_distance -= 0.1f
                    g_distance = iif(g_distance < 0.0f, 0.0f, g_distance)
            end select


            select case event_behaviour
                case StealOldest:
                    ename = @"Steal oldest"
                case StealNewest:
                    ename = @"Steal newest"
                case StealQuietest:
                    ename = @"Steal quietest"
                case JustFail:
                    ename = @"Just fail"
                case JustFailIfQuietest:
                    ename = @"Just fail if quietest"
           end select

            printf(!"Sound = %1.0f, Distance = %4.1f, Behaviour = %-25s\r", g_sound, g_distance, ename)

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

    loop while (key <> chr(27))

result = FMOD_EventSystem_Unload(eventsystem)
    ERRCHECK(result)
result = FMOD_EventSystem_Release(eventsystem)
    ERRCHECK(result)
