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

 Demonstrates basic usage of event effects
==============================================================================================='/
#include "windows.bi"
#include "crt.bi"

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

#define UPDATE_INTERVAL 100.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


' program start
    dim as FMOD_EVENTSYSTEM    ptr eventsystem = any
    dim as FMOD_EVENT          ptr event = any
    dim as FMOD_EVENTPARAMETER ptr param00 = any
    dim as FMOD_RESULT          result = any
    dim as string               key
    dim as float                param00_val = any, param00_min = any, param00_max = any, param00_increment = any

    printf(!"======================================================================\n")
    printf(!"Effects Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"==============================-------=================================\n")
    printf(!"This demonstrates the use of FMOD event effects. It simply plays an\n")
    printf(!"event with effects applied and allows the user to adjust the parameters.\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_GetEvent(eventsystem, "examples/FeatureDemonstration/Effects/PropertyAutomationEffectEnvelope", FMOD_EVENT_DEFAULT, @event)
    ERRCHECK(result)
result = FMOD_Event_GetParameter(event, "param00", @param00)
    ERRCHECK(result)
result = FMOD_EventParameter_GetRange(param00, @param00_min, @param00_max)
    ERRCHECK(result)
    
result = FMOD_EventParameter_SetValue(param00, 0.0f)
    ERRCHECK(result)
result = FMOD_Event_Start(event)
    ERRCHECK(result)
    
    printf(!"======================================================================\n")
    printf(!"Press '<' or ',' to decrease param00\n")
    printf(!"Press '>' or '.' to increase param00\n")
    printf(!"Press ESC        to quit\n")
    printf(!"======================================================================\n")
    printf(!"\n")

    param00_increment = (param00_max - param00_min) / UPDATE_INTERVAL
result = FMOD_EventParameter_GetValue(param00, @param00_val)
    ERRCHECK(result)


    do
            key = inkey

            if (key = "<" OrElse key = ",") then
                param00_val -= param00_increment
                if (param00_val < param00_min) then
                    param00_val = param00_min
                end if

result = FMOD_EventParameter_SetValue(param00, param00_val)
                ERRCHECK(result)
            ElseIf (key = ">" OrElse key = ".") then
                param00_val += param00_increment
                if (param00_val > param00_max) then
                    param00_val = param00_max
                end if

result = FMOD_EventParameter_SetValue(param00, param00_val)
                ERRCHECK(result)
            end if


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

        printf(!"param00 = %.4f         \r", param00_val)

    loop while (key <> chr(27))

result = FMOD_EventSystem_Release(eventsystem)
    ERRCHECK(result)
