VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
MeshManager.hpp
1#pragma once
2
3#include "VulkanMesh.hpp"
6#include "components/Mesh.hpp"
8
9#include "Engine.hpp"
10
11#include <unordered_map>
12#include <components/Types.hpp>
13#include <vector>
14#include <memory>
16#include <string>
17#include <iostream>
18#include <cstring>
19#include <algorithm>
20
21namespace vex {
23 public:
28 MeshManager(VulkanContext& context, std::unique_ptr<VulkanResources>& resources, VirtualFileSystem* vfs);
30
33 void init(Engine* engine){
34 m_p_engine = engine;
35
36 auto& registry = m_p_engine->getRegistry();
37 registry.on_create<MeshComponent>([this](vex::Entity entity, MeshComponent& comp) {
38 onMeshComponentConstruct(m_p_engine->getRegistry(), entity);
39 });
40 registry.on_destroy<MeshComponent>([this](vex::Entity entity, MeshComponent& comp) {
41 onMeshComponentDestroy(m_p_engine->getRegistry(), entity);
42 });
43 }
44
48 MeshComponent loadMesh(const std::string& path);
49
56 ModelObject* createModel(const std::string& name, MeshComponent meshComponent, TransformComponent transformComponent, vex::Entity parent);
57
61 void destroyModel(std::string& name, MeshComponent meshComponent);
62
65 void registerVulkanMesh(MeshComponent& meshComponent);
66
70 std::unique_ptr<VulkanMesh>& getVulkanMeshByMesh(MeshComponent& meshComponent);
71
74 void loadMeshesAsync(const std::vector<MeshComponent*>& pendingComponents);
75
79 bool isMeshLoaded(const std::string& path) const {
80 return m_vulkanMeshes.find(path) != m_vulkanMeshes.end();
81 }
82
84 void clearState();
85
86 private:
87 VulkanContext& m_r_context;
88 Engine* m_p_engine = nullptr;
89 VirtualFileSystem* m_vfs;
90 std::unique_ptr<VulkanResources>& m_p_resources;
91 vex_map<std::string, std::unique_ptr<VulkanMesh>> m_vulkanMeshes;
92 std::vector<uint32_t> m_freeModelIds;
93 uint32_t m_nextModelId = 0;
94
95 std::unordered_map<uint32_t, std::string> m_installedPaths;
96 std::unordered_map<std::string, std::pair<glm::vec3, float>> m_meshBoundsCache;
97
100
102 void onMeshComponentDestroy(vex::Registry& registry, vex::Entity entity);
103
105 void releaseMeshReference(const std::string& path, MeshComponent& ownerComp);
106 };
107}
Contains basic components like transform, camera, name components..
Main header for the Entity Component System (ECS) framework.
Contains main Engine class.
This file defines MeshData struct and all other structs needed for it.
This file defines in engine ModelObject class.
This file defines VirtualFileSystem and VPKStream classes.
This file defines VulkanMesh class.
Class for interaction with engine systems.
Definition Engine.hpp:47
vex::Registry & getRegistry()
Returns a reference to vex::Registry.
Definition Engine.hpp:147
MeshComponent loadMesh(const std::string &path)
Loads mesh from a file, creates vulkan mesh and returns a MeshComponent.
void clearState()
Clears the state of the mesh manager, resetting model IDs and clearing the mesh map.
void registerVulkanMesh(MeshComponent &meshComponent)
Registers a Vulkan mesh component.
void onMeshComponentDestroy(vex::Registry &registry, vex::Entity entity)
Internally handles the destruction of a mesh component called by entt callbacks.
std::unique_ptr< VulkanMesh > & getVulkanMeshByMesh(MeshComponent &meshComponent)
Retrieves a Vulkan mesh by mesh component.
void init(Engine *engine)
Initialize the MeshManager.
ModelObject * createModel(const std::string &name, MeshComponent meshComponent, TransformComponent transformComponent, vex::Entity parent)
Creates a model object from a mesh component, transform component, and parent entity.
void destroyModel(std::string &name, MeshComponent meshComponent)
Destroys a model object by name and mesh component.
bool isMeshLoaded(const std::string &path) const
Checks if a mesh is loaded by path.
void onMeshComponentConstruct(vex::Registry &registry, vex::Entity entity)
Internally handles the construction of a mesh component called by entt callbacks.
void loadMeshesAsync(const std::vector< MeshComponent * > &pendingComponents)
Loads meshes asynchronously from the given paths.
MeshManager(VulkanContext &context, std::unique_ptr< VulkanResources > &resources, VirtualFileSystem *vfs)
Constructor for MeshManager.
void releaseMeshReference(const std::string &path, MeshComponent &ownerComp)
Internally handles the release of a mesh reference called by entt callbacks.
ModelObject class represents a model object in the engine. Thanks to engine separation from the backe...
Central registry for managing entities and their components in the ECS system.
Definition Registry.hpp:29
void on_create(std::function< void(Entity, T &)> cb)
Registers a callback to be invoked when a component of type T is created.
Definition Registry.hpp:146
This class provides abstraction of file system needed for loading packed and unpacked assets.
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11
Struct containing raw meshData, mesh id, texture paths and material properties. It just template and ...
Struct containing transform data and methods.
Struct holding all vulkan data, like device, surface, swapchain, images, views, and more.
Definition context.hpp:26