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

 Demonstrates basic loading and unloading of event data per event
==============================================================================================='/
#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 memory_changed = 1

    printf(!"======================================================================\n")
    printf(!"Load Event Data Example.  Copyright (c) Firelight Technologies 2004-2011.\n")
    printf(!"==============================-------=================================\n")
    printf(!"This demonstrates loading and unloading of event data per event and\n")
    printf(!"per group.\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", 1, @eventgroup)
    ERRCHECK(result)

    printf(!"======================================================================\n")
    printf(!"Press 'e'        to load event data\n")
    printf(!"Press 'E'        to unload event data\n")
    printf(!"Press 'g'        to load group data\n")
    printf(!"Press 'G'        to unload group data\n")
    printf(!"Press ESC        to quit\n")
    printf(!"======================================================================\n")

    do
            key = inkey

            if (key = "e") then
result = FMOD_EventGroup_GetEvent(eventgroup, "SimpleEvent", FMOD_EVENT_DEFAULT, @event)
                ERRCHECK(result)
                printf(!"Event data loaded\n")
                memory_changed = 1
            ElseIf (key = "E") then
                if (event) then
                    result = FMOD_EventGroup_FreeEventData(eventgroup, event, 1)
                    if (result <> FMOD_ERR_INVALID_HANDLE) then
                        ERRCHECK(result)
                        printf(!"Event data unloaded\n")
                        memory_changed = 1
                    end if
                    event = 0
                end if
            ElseIf (key = "g") then
result = FMOD_EventGroup_LoadEventData(eventgroup, FMOD_EVENT_RESOURCE_STREAMS_AND_SAMPLES, 0)
                ERRCHECK(result)
                printf(!"Event group data loaded\n")
                memory_changed = 1
            ElseIf (key = "G") then
result = FMOD_EventGroup_FreeEventData(eventgroup, 0, 1)
                ERRCHECK(result)
                printf(!"Event group data unloaded\n")
                memory_changed = 1
            end if


        if (memory_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)
            memory_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)
