/'=============================================================================
 Programmer Selected Sound Definition Example
 Copyright (c), Firelight Technologies Pty, Ltd 2007-2011.

 Demonstrates how to use the "programmer selected sound definition" feature of
 the FMOD event system
============================================================================='/
#include "windows.bi"
#include "crt.bi"

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

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

static shared as integer g_sounddef_entry_index = 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


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
    select case eType
        case FMOD_EVENT_CALLBACKTYPE_SOUNDDEF_SELECTINDEX :
            dim as zstring  ptr sname = param1                    ' [in]  (char *) name of sound definition
            dim as integer ptr index = param2                     ' [out] (int *) the sounddef entry index to use

            *index = iif((g_sounddef_entry_index < *index), g_sounddef_entry_index, *index - 1)
            printf(!"FMOD_EVENT_CALLBACKTYPE_SOUNDDEF_SELECTINDEX '%s': %d\n", sname, *index)
    end select

    return FMOD_OK
end function


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

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/SequencingAndStitching", FMOD_EVENT_DEFAULT, @eventgroup)
    ERRCHECK(result)

    printf(!"======================================================================\n")
    printf(!"Programmer Selected Sound Definition.\n")
    printf(!"Copyright (c) Firelight Technologies 2006-2011.\n")
    printf(!"----------------------------------------------------------------------\n")
    printf(!"Press 'Space' to start the 'Programmer Selected' event\n")
    printf(!"Press '>'     to increase sound definition entry index\n")
    printf(!"Press '<'     to decrease sound definition entry index\n")
    printf(!"Press 'Esc'   to quit\n")
    printf(!"======================================================================\n")
    printf(!"Sound definition entry index = %d\n", g_sounddef_entry_index)

    do
            key = inkey

            select case key
                case " " :
result = Fmod_EventGroup_GetEvent(eventgroup, "ProgrammerSelected", FMOD_EVENT_DEFAULT, @event)
                    ERRCHECK(result)
result = FMOD_Event_SetCallback(event, @eventcallback, 0)
                    ERRCHECK(result)
result = FMOD_Event_Start(event)
                    ERRCHECK(result)
                case ">", "." :
                    g_sounddef_entry_index += 1
                    printf(!"Sound definition entry index = %d\n", g_sounddef_entry_index)
                case "<", "," :
                    g_sounddef_entry_index -= 1
                    g_sounddef_entry_index = iif((g_sounddef_entry_index < 0), 0, g_sounddef_entry_index)
                    printf(!"Sound definition entry index = %d\n", g_sounddef_entry_index)
            end select


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)
