/'===============================================================================================
 Info-only Event Example
 Copyright (c), Firelight Technologies Pty, Ltd 2004-2011.

 Demonstrates usage and functionality of info-only events
==============================================================================================='/
#include "windows.bi"
#include "crt.bi"

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


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_EVENTGROUP      ptr eventgroup = any
    dim as FMOD_EVENT           ptr event = 0
    dim as FMOD_RESULT           result = any
    dim as string                   key
    dim as integer state_changed = 1

    printf(!"======================================================================\n")
    printf(!"Info-only Event Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"==============================-------=================================\n")
    printf(!"This demonstrates usage and functionality of info-only events.\n")
    printf(!"======================================================================\n\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_GetGroup(eventsystem, "examples/FeatureDemonstration/Basics", 0, @eventgroup)
    ERRCHECK(result)

    printf(!"======================================================================\n")
    printf(!"Press 'i'        to get an info-only event\n")
    printf(!"Press 'e'        to get an event instance\n")
    printf(!"Press 'u'        to unload all event data\n")
    printf(!"Press '1' - '0'  to set event volume between 10%% - 100%%\n")
    printf(!"Press Space      to start/stop the current event\n")
    printf(!"Press ESC        to quit\n")
    printf(!"======================================================================\n")


    do
            key = inkey

            if (key = lcase("i")) then
                /' Get an info-only event.
                   Notes:
                   - This is the parent from which all instances of the
                     specified event are derived.
                   - This call will not allocate memory for event instances or
                     load sample data.
                   - The resulting event cannot be played, it simply allows
                     querying and setting of event properties.
                   - Any properties which are set on the info-only event will be
                     copied to all instances which are retrieved from then on.
                 '/
result = FMOD_EventGroup_GetEvent(eventgroup, "BasicEventWithLooping", FMOD_EVENT_INFOONLY, @event)
                ERRCHECK(result)
                printf(!"getEvent(FMOD_EVENT_INFOONLY) succeeded\n")
                state_changed = 1
            ElseIf (key = lcase("e")) then
                /' Get an event instance.
                   Notes:
                   - This call will allocate memory for event instances and
                     load sample data if required.
                   - The resulting event can be played.
                   - Any properties which have been set on the info-only event
                     will be copied to this instance.
                 '/
result = FMOD_EventGroup_GetEvent(eventgroup, "BasicEventWithLooping", FMOD_EVENT_DEFAULT, @event)
                ERRCHECK(result)
                printf(!"getEvent(FMOD_EVENT_DEFAULT) succeeded\n")
                state_changed = 1
            ElseIf (key = " ") then
                if (event) then
                    dim as FMOD_EVENT_STATE state = any

                    result = FMOD_Event_GetState(event, @state)
                    if (result <> FMOD_ERR_INVALID_HANDLE) then
                        ERRCHECK(result)
                    end if

                    if (state and FMOD_EVENT_STATE_PLAYING) then
                        /' Attempt to stop the event.
                           - This will fail if the event is info-only.
                         '/
                        result = FMOD_Event_Stop(event, 0)
                        if(result = FMOD_OK) then
                            printf(!"event->stop() succeeded\n")
                            state_changed = 1
                        else
                            printf(!"event->stop() returned an error:\n\t(%d) %s\n", result, FMOD_ErrorString(result))
                        end if
                    else
                        /' Attempt to start the event.
                           - This will fail if the event is info-only.
                         '/
                        result = FMOD_Event_Start(event)
                        if (result = FMOD_OK) then
                            printf(!"event->start() succeeded\n")
                            state_changed = 1
                        else
                            printf(!"event->start() returned an error:\n\t(%d) %s\n", result, FMOD_ErrorString(result))
                        end if
                    end if
                end if
            ElseIf (key >= "0" AndAlso key <= "9") then
                /' Attempt to set the event's volume.
                   - This will succeed on both info-only events and instances.
                   - Volume set on the info-only event will be copied to
                     all instances retrieved from now on.
                   - Volume set on an instance will only apply to that instance.
                 '/
                dim as float volume = (asc(key) - asc("0"))

                if (volume = 0) then
                    volume = 10.0f
                end if

                volume /= 10.0f

                result = FMOD_Event_SetVolume(event, volume)
                if (result = FMOD_OK) then
                    printf(!"event->setVolume() succeeded\n")
                    state_changed = 1
                else
                    printf(!"event->setVolume() returned an error:\n\t(%d) %s\n", result, FMOD_ErrorString(result))
                end if
            ElseIf (key = lcase("u")) then
result = FMOD_EventGroup_FreeEventData(eventgroup, 0, 1)
                ERRCHECK(result)
                printf(!"Event data unloaded\n")
                state_changed = 1
            end if

        if (state_changed) then
            dim as integer memory_current = any, memory_max = any
result = FMOD_Memory_GetStats(@memory_current, @memory_max, 0)
            ERRCHECK(result)

            printf(!"Memory usage: current = %10d, max = %10d\n", memory_current, memory_max)

            if (event) then
                /' Attempt to get the event's volume.
                   - This will succeed on both info-only events and instances.
                 '/
                dim as float volume = any

                result = FMOD_Event_GetVolume(event, @volume)
                if (result <> FMOD_ERR_INVALID_HANDLE) then
                    ERRCHECK(result)
                    printf(!"Volume: %.2f\n", volume)
                end if
            end if

            state_changed = 0
end if
result = FMOD_EventSystem_Update(eventsystem)
        ERRCHECK(result)
        Sleep(15)

    loop while (key <> chr(27))

result = FMOD_EventGroup_FreeEventData(eventgroup, 0, 1)
    ERRCHECK(result)
result = FMOD_EventSystem_Release(eventsystem)
    ERRCHECK(result)
