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

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

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

#define   NUM_EVENTS 3

const as string MEDIA_PATH = "media\\"

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

function eventcallback(byval event as FMOD_EVENT ptr, byval eType as FMOD_EVENT_CALLBACKTYPE, byval param1 as any ptr, byval param2 as any ptr, byval userdata as any ptr) as FMOD_RESULT
    if (eType = FMOD_EVENT_CALLBACKTYPE_NET_MODIFIED) then
        dim as uinteger t = cast(uinteger, param2)
        printf(!"%p %s %f (%d)\n", event, iif(param1 = FMOD_EVENTPROPERTY_VOLUME, @"volume", iif(FMOD_EVENTPROPERTY_PITCH, @"pitch", @"???")), *cast(float ptr, @t), cast(uinteger, userdata))
    end if

    return FMOD_OK
end function

' program start
    dim as FMOD_RESULT        result = any
    dim as FMOD_EVENTSYSTEM  ptr eventsystem = any
    dim as FMOD_EVENTGROUP   ptr eventgroup = any
    dim as FMOD_EVENT        ptr event(NUM_EVENTS-1)
    dim as zstring              ptr event_name(NUM_EVENTS-1) = { _
                           @"Basics/BasicEventWithLooping", _
                           @"SequencingAndStitching/LoopLogic", _
                           @"3D Events/2D-3DPanMorph" _
                       }
    dim as string               key
                 dim as integer i = any, userdata = 0

    printf(!"======================================================================\n")
    printf(!"Realtime Tweaking. Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"======================================================================\n")
    printf(!"This example shows how to initialize the FMOD Net Event System so that\n")
    printf(!"FMOD Designer can connect to your game and tweak events as they're\n")
    printf(!"playing.\n")
    printf(!"Start some events then connect to this app using the Audition menu\n")
    printf(!"in FMOD Designer. You can use 127.0.0.1 for the IP address if you\n")
    printf(!"don't want to use two machines. Load tutorials.fdp and change the \n")
    printf(!"volume of the playing events using the volume slider in the event\n")
    printf(!"property sheet\n")
    printf(!"======================================================================\n\n")

result = FMOD_EventSystem_Create(@eventsystem)
    ERRCHECK(result)
result = FMOD_NetEventSystem_Init(eventsystem, FMOD_EVENT_NET_PORT)
    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", FMOD_EVENT_DEFAULT, @eventgroup)
    ERRCHECK(result)

    for i=0 to NUM_EVENTS-1
        event(i) = 0
    next

    printf(!"======================================================================\n")
    printf(!"Press 1 - 3  to start/stop events\n")
    printf(!"Press ESC    to quit\n")
    printf(!"======================================================================\n")

    do
            key = inkey

            if ((key >= "1") AndAlso (key <= "3")) then
                i = asc(key) - asc("1")

                if (event(i)) then
result = FMOD_Event_Stop(event(i), 1)
                    ERRCHECK(result)
                    event(i) = 0
                    printf(!"Stopping '%s'\n", event_name(i))
                else
result = FMOD_EventGroup_GetEvent(eventgroup, event_name(i), FMOD_EVENT_DEFAULT, @event(i))
                    ERRCHECK(result)
result = FMOD_Event_SetCallback(event(i), @eventcallback, cast(any ptr,userdata))
userdata +=1
                    ERRCHECK(result)
result = FMOD_Event_Start(event(i))
                    ERRCHECK(result)
                    printf(!"Starting '%s'\n", event_name(i))
                end if
            end if


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

    loop while (key <> chr(27))

result = FMOD_EventSystem_Release(eventsystem)
    ERRCHECK(result)
result = FMOD_NetEventSystem_Shutdown()
    ERRCHECK(result)
