Cogs.Core
AssetSystem.h
1#pragma once
2
3#include "Systems/ComponentSystem.h"
4
5#include "Components/Core/AssetComponent.h"
6
7#include "Resources/Asset.h"
8#include "Scene/GetBounds.h"
9
10#include "Context.h"
11
12#include "DynamicComponentSystem.h"
13
14#include "Foundation/Geometry/BoundingBox.hpp"
15
16namespace Cogs::Core
17{
18 class AssetBounds final : public IGetBounds {
19 public:
20 explicit AssetBounds(AssetSystem* assetSystem) : assetSystem(assetSystem) {}
21
22 void getBounds(Context* context, Cogs::Geometry::BoundingBox& bounds) override;
23 bool getBounds(Context* context, const ComponentModel::Entity* entity, Cogs::Geometry::BoundingBox& bounds, bool ignoreVisibility) const override;
24
25 private:
26 AssetSystem* assetSystem = nullptr;
27 };
28
30 {
31 public:
32 AssetData() = default;
33 AssetData(const AssetData & other) = delete;
34
35 bool hasChanged(const AssetHandle & asset) const
36 {
37 return this->asset != asset || (asset && !isGeneration(static_cast<uint8_t>(asset->getGeneration())));
38 }
39
40 void setAsset(AssetHandle asset)
41 {
42 this->asset = asset;
43 setGeneration(asset ? static_cast<uint8_t>(asset->getGeneration()) : 0);
44 }
45
46 void setGeneration(uint8_t generation) { this->generation = generation; }
47 bool isGeneration(uint8_t generation) const { return this->generation == generation; }
48
49 AssetHandle asset;
50 std::shared_ptr<struct AssetInstanceData> instanceData;
51
52 private:
53 uint8_t generation = 0;
54 };
55
66 {
67 StringView path;
68 struct AssetInstanceData* assetInstance = nullptr;
69
70 uint32_t priority = 0;
71 uint32_t modelFileIndex = 0;
72
73 ModelHandle model;
74
75 bool canceled = false;
76 };
77
78 class COGSCORE_DLL_API AssetSystem : public ComponentSystemWithDataPools<AssetComponent, AssetData>
79 {
81 public:
82 AssetSystem(Memory::Allocator * allocator, SizeType capacity);
83 ~AssetSystem() override;
84
85 void initialize(Context * context) override;
86 void update(Context * context) override;
87 void postUpdate(Context * context) override;
88
89 ComponentHandle createComponent() override;
90 void destroyComponent(ComponentHandle component) override;
91
92 const struct AssetInstanceStats * getStats(AssetComponent * assetComponent) const;
93 const Geometry::BoundingBox * getLocalBounds(AssetComponent * assetComponent) const;
94 Geometry::BoundingBox getWorldBounds(AssetComponent* assetComponent, bool ignoreVisibility) const;
95 std::span<const AssetModelRequest* const> getPendingRequests() const;
96 std::span<const AssetModelRequest* const> getInFlightRequests() const;
97
98 private:
99 std::unique_ptr<AssetBounds> bounds;
100 std::unique_ptr<struct AssetSystemData> systemData;
101 };
102}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
void getBounds(Context *context, Cogs::Geometry::BoundingBox &bounds) override
Expand bounds including bounds of all entities in this system in world coordinates.
Component system template with multiple parallel structures per component stored in separate pools si...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Base allocator implementation.
Definition: Allocator.h:30
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
Instantiates an asset model into the scene.
uint32_t priority
Priority of request, largest number gets served first.
Definition: AssetSystem.h:70
uint32_t getGeneration() const
Get the generation count.
Definition: ResourceBase.h:380