Cogs.Core
AudioFunctions.cpp
1#include "AudioFunctions.h"
2
3#include "Context.h"
4#include "Bridge/FieldSetter.h"
5#include "ExtensionRegistry.h"
6#include "Services/Services.h"
7
8#include "AudioSystem.h"
9
10#include "SoundManager.h"
11
12using namespace Cogs::Core;
13
14ResourceId loadSound(BridgeContext * ctx, const char * resourceName, int /*flags*/)
15{
16 auto context = static_cast<Context*>(ctx);
17 auto soundManager = context->services->getService<SoundManager>();
18 auto id = soundManager->getNextResourceId();
19
20 SoundLoadInfo & loadInfo = *soundManager->createLoadInfo();
21 loadInfo.handle = soundManager->create();
22 loadInfo.resourcePath = resourceName;
23 loadInfo.resourceId = id;
24
25 static_cast<void>(soundManager->loadResource(&loadInfo));
26
27 return id;
28}
29
30void setSoundField(BridgeContext * ctx, EntityId entityId, const ComponentId componentId, const FieldId fieldId, const ResourceId resourceId)
31{
32 auto context = static_cast<Context*>(ctx);
33 auto soundManager = context->services->getService<SoundManager>();
34
35 auto sound = soundManager->getHandle(resourceId);
36
37 auto field = Cogs::getField<SoundHandle>(context, entityId, componentId, fieldId);
38
39 *field = sound;
40}
41
42void playSound(BridgeContext * ctx, EntityId entityId, const ComponentId /*componentId*/)
43{
44 auto context = static_cast<Context*>(ctx);
45 const Entity* entity = context->store->getEntityPtr(entityId);
46 if (!entity) {
47 return;
48 }
49
50 const AudioComponent* component = entity->getComponent<AudioComponent>();
51 if (!component) {
52 return;
53 }
54
55 AudioSystem* audioSystem = ExtensionRegistry::getExtensionSystem<AudioSystem>(context);
56
57 Cogs::Core::AudioData & data = audioSystem->getData(component);
58
59 if (data.state == AudioState::Playing || data.state == AudioState::Stopped) {
60 data.state = AudioState::StartPlaying;
61 }
62}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
std::unique_ptr< class Services > services
Services.
Definition: Context.h:174
ResourceId getNextResourceId() override
Get the next unique resource id.
ResourceHandle getHandle(const ResourceId id) const
Get a resource handle to the resource with the given id.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::string resourcePath
Resource path. Used to locate resource.
ResourceId resourceId
Unique resource identifier. Must be unique among resources of the same kind.
ResourceHandleBase handle
Handle to resource structure for holding actual resource data.