summaryrefslogtreecommitdiff
path: root/src/entities.cpp
blob: 2887074adf4377cc2bbf1e921e952470debf6e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include "entities.hpp"
#include "ecs.hpp"
#include <vulkan/vulkan_core.h>

void EntityType_Init() {}

void EntityType_Load(EntityType &et) {
	if (et.modelFile != nullptr && et.modelFile != CAFE_BABE(char)) {
		assert(et.vkPipelineLayoutCreateInfo != nullptr && et.vkPipelineLayoutCreateInfo != CAFE_BABE(VkPipelineLayoutCreateInfo) && "EntityType with a defined model must also contain appropriate Vulkan CreateInfos");
		AssetHandle assetHandle{AM_Register(et.modelFile)};
		const Asset *asset = AM_Get(assetHandle);

		CompGrBinds &grBinds = ECS_CreateGrBinds(et.entityHandle);
		auto vkResult = vkCreatePipelineLayout(vkDevice, et.vkPipelineLayoutCreateInfo, vkAllocator, &grBinds.vkPipelineLayout);
		assert(vkResult == VK_SUCCESS);
		// TODO grBinds.vkDescriptorSet

		cgltf_options options{};
		// TODO allocator
		cgltf_data *gltfData = nullptr;
		cgltf_result result = cgltf_parse(&options, asset->ptr, asset->size, &gltfData);
		assert(result == cgltf_result_success);
		result = cgltf_validate(gltfData);
		assert(result == cgltf_result_success);

		// create buffers
		// TODO

		// create VkDeviceMemory
		VkMemoryAllocateInfo vkMemoryAllocateInfo;
		vkMemoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
		vkMemoryAllocateInfo.pNext = nullptr;
		vkMemoryAllocateInfo.allocationSize = gltfData->buffers[0].size;;
		vkMemoryAllocateInfo.memoryTypeIndex = FindMemoryTypeIndex(0, 0); // TODO

		vkAllocateMemory(vkDevice, &vkMemoryAllocateInfo, vkAllocator, &grBinds.deviceMemory);

		// TODO bind buffers to memory

		// cleanup
		cgltf_free(gltfData);
	}
}

void EntityType_Teardown() {
	long entityTypeCount = globalEntityTypes.Count();
	for (long i = 0; i < entityTypeCount; ++i) {
		if (globalEntityTypes[i].modelFile == nullptr) continue;
		auto *grBinds = ECS_GetGrBinds(globalEntityTypes[i].entityHandle);
		vkDestroyPipelineLayout(vkDevice, grBinds->vkPipelineLayout, vkAllocator);
		vkDestroyBuffer(vkDevice, grBinds->vertexBuffer, vkAllocator);
		vkDestroyBuffer(vkDevice, grBinds->vertexBuffer, vkAllocator);
		vkDestroyBuffer(vkDevice, grBinds->vertexBuffer, vkAllocator);
	}
}