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

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

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

#define UPDATE_INTERVAL 100.0


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_EVENTCATEGORY   ptr mastercategory = any
    dim as FMOD_EVENT           ptr car = any
    dim as FMOD_EVENTPARAMETER  ptr rpm = any
    dim as FMOD_EVENTPARAMETER  ptr load = any
    dim as FMOD_RESULT           result = any
    dim as string                   key
    dim as float                 rpm_val = any, rpm_min = any, rpm_max = any, rpm_increment = any, load_val = any, load_min = any, load_max = any, load_increment = any

    printf(!"======================================================================\n")
    printf(!"Parameters Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"==============================-------=================================\n")
    printf(!"This demonstrates the use of FMOD event parameters. It simply plays an\n")
    printf(!"event, retrieves the parameters and allows the user to adjust them.\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/AdvancedTechniques", FMOD_EVENT_DEFAULT, @eventgroup)
    ERRCHECK(result)
result = FMOD_EventGroup_GetEvent(eventgroup, "car", FMOD_EVENT_DEFAULT, @car)
    ERRCHECK(result)

result = FMOD_EventSystem_GetCategory(eventsystem, "master", @mastercategory)
    ERRCHECK(result)

result = FMOD_Event_GetParameter(car, "load", @load)
    ERRCHECK(result)
result = FMOD_EventParameter_GetRange(load, @load_min, @load_max)
    ERRCHECK(result)
result = FMOD_EventParameter_SetValue(load, load_max)
    ERRCHECK(result)

result = FMOD_Event_GetParameterByIndex(car, 0, @rpm)
    ERRCHECK(result)
result = FMOD_EventParameter_GetRange(rpm, @rpm_min, @rpm_max)
    ERRCHECK(result)
result = FMOD_EventParameter_SetValue(rpm, 1000.0f)
    ERRCHECK(result)

result = FMOD_Event_Start(car)
    ERRCHECK(result)

    printf(!"======================================================================\n")
    printf(!"Press '<' or ',' to decrease RPM\n")
    printf(!"Press '>' or '.' to increase RPM\n")
    printf(!"Press '-' or '_' to decrease load\n")
    printf(!"Press '+' or '=' to increase load\n")
    printf(!"Press ESC        to quit\n")
    printf(!"======================================================================\n")

    rpm_increment   = (rpm_max - rpm_min) / UPDATE_INTERVAL
result = FMOD_EventParameter_GetValue(rpm, @rpm_val)
    ERRCHECK(result)
    load_increment  = (load_max - load_min) / UPDATE_INTERVAL
result = FMOD_EventParameter_GetValue(load, @load_val)
    ERRCHECK(result)

    do
            key = inkey

            if (key = "<" OrElse key = ",") then
                rpm_val -= rpm_increment
                if (rpm_val < rpm_min) then
                    rpm_val = rpm_min
                end if

result = FMOD_EventParameter_SetValue(rpm, rpm_val)
                ERRCHECK(result)
            ElseIf (key = ">" OrElse key = ".") then
                rpm_val += rpm_increment
                if (rpm_val > rpm_max) then
                    rpm_val = rpm_max
                end if

result = FMOD_EventParameter_SetValue(rpm, rpm_val)
                ERRCHECK(result)
            end if
            if (key = "-" OrElse key = "_") then
                load_val -= load_increment
                if (load_val < load_min) then
                    load_val = load_min
                end if

result = FMOD_EventParameter_SetValue(load, load_val)
                ERRCHECK(result)
            ElseIf (key = "+" OrElse key = "=") then
                load_val += load_increment
                if (load_val > load_max) then
                    load_val = load_max
                end if

result = FMOD_EventParameter_SetValue(load, load_val)
                ERRCHECK(result)
            ElseIf (key = " ") then
                dim as integer paused = any

result = FMOD_EventCategory_GetPaused(mastercategory, @paused)
                ERRCHECK(result)
                paused xor= 1
result = FMOD_EventCategory_SetPaused(mastercategory, paused)
                ERRCHECK(result)
            end if

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

        printf(!"RPM = %.4f, load = %.4f        \r", rpm_val, load_val)

    loop while (key <> chr(27))

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