Cogs.Core
AudioExtension.cpp
1#include "ExtensionRegistry.h"
2#include "EntityDefinitions.h"
3
4#include "Context.h"
5#include "Services/Services.h"
6
7#include "AudioSystem.h"
8#include "AudioListenerComponent.h"
9
10#include "SoundManager.h"
11#include "WaveSoundLoader.h"
12
13#include "Serialization/ResourceReader.h"
14
15#include "Services/Services.h"
16
17#include "AudioFunctions.h"
18
19namespace Cogs
20{
21 namespace Core
22 {
23 struct AudioExtension : public Extension
24 {
25 AudioExtension() { ExtensionRegistry::add(this, COGS_CORE_VERSION_STRING); }
26
27 bool initializeStatic() override {
28 Reflection::TypeDatabase::createType<SoundHandle>();
29
30 AudioComponent::registerType();
31 AudioListenerComponent::registerType();
32
33 Reflection::TypeDatabase::createType<SoundManager>();
34
35 return true;
36 }
37
38 bool initialize(Context * context) override {
39 auto manager = std::make_unique<SoundManager>(context);
40 manager->registerLoader(new WaveSoundLoader());
41 auto soundManager = manager.get();
42 context->engine->addResourceManager(std::move(manager));
43
44 context->services->registerServiceReference(soundManager);
45
46 ExtensionRegistry::registerExtensionSystem<AudioSystem>(context, SystemPriority::PreTransform, 128);
47
48 context->registerDynamicComponentType("AudioListenerComponent");
49
50 ReaderExtension readerExtension;
51 readerExtension.readCallback = [=](const StringView & name, const Value & jsonValue)
52 {
53 for (auto & m : jsonValue.GetObject()) {
54 auto key = toKey(m.name);
55
56 if (key == "path") {
57 auto sound = soundManager->loadSound(toKey(m.value));
58
59 sound->setName(name.to_string());
60 }
61 }
62 };
63
64 readerExtension.setCallback = [=](void * component, const Reflection::Field * field, const FieldValue & value)
65 {
66 auto fieldValue = field->getPtr<SoundHandle>(component);
67
68 *fieldValue = soundManager->loadSound(value.value);
69 };
70
71 registerExtensionReader(context, "Sound", Reflection::TypeDatabase::getType<SoundHandle>(), readerExtension);
72 return true;
73 }
74
75 const char * getExtensionKey() const override { return "Audio"; }
76 } audioExtensionInstance;
77 }
78}
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
void registerDynamicComponentType(const StringView &typeName)
Register a dynamic component type with the given typeName.
Definition: Context.cpp:380
std::unique_ptr< class Engine > engine
Engine instance.
Definition: Context.h:222
static void add(Extension *extension, StringView version)
Adds the given extension to the registry, ensuring the initialization methods are called at appropria...
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
std::string to_string() const
String conversion method.
Definition: StringView.cpp:9
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
const char * getExtensionKey() const override
Get the extensions unique key, used to check for extension presence and retrieve extension specific d...
bool initialize(Context *context) override
Initialize extension for the given context.
bool initializeStatic() override
Initialize extension statically.
Defines an extension to Cogs.Core and provides methods to override in order to initialize extension c...
Defines a value to apply to a field.
@ PreTransform
Run before transformations are updated.
Definition: Engine.h:50