#include "Material/Material.h" #include bool material_collection_init(uint8_t size, material_collection_t* materials) { if (size > 254) { size = 254; } material_collection_t temp = {0}; temp.buffer = (material_t*)malloc(size * sizeof(material_t)); if (temp.buffer == NULL) { return false; } temp.size = (uint8_t)size; temp.count = 0; *materials = temp; return true; } void material_collection_resize(material_collection_t* materials, size_t size) { if (size == INVALID_MATERIAL_ID) { size = INVALID_MATERIAL_ID - 1; } if (size == materials->size) { return; } material_t* temp = (material_t*)realloc(materials->buffer, size * sizeof(material_t)); if (temp != NULL) { materials->buffer = temp; materials->size = (uint8_t)size; } } void material_collection_free(material_collection_t* materials) { if (materials->buffer != NULL) { for (uint8_t i = 0; i < materials->count; i++) { free(materials->buffer[i].properties); materials->buffer[i].properties = NULL; } free(materials->buffer); materials->buffer = NULL; } } material_handle_t material_create(const void* properties, size_t properties_size, material_render_loop_f render_loop, material_render_aov_f render_aov, material_collection_t* collection) { material_t material = {0}; if (collection->count >= collection->size) { material_collection_resize(collection, collection->size * 2); } void* temp = malloc(properties_size); if (temp == NULL) { return invalid_material_entity(); } memcpy(temp, properties, properties_size); material.properties = temp; material.properties_size = properties_size; material.render_loop = render_loop; material.render_aov = render_aov; material_handle_t entity = {.id = collection->count}; collection->buffer[collection->count] = material; collection->count++; return entity; } // void material_free(material_entity_t entity, material_collection_t* collection) // { // if (entity.id >= collection->count || !is_material_entity_valid(entity)) // { // return; // } // // free(collection->buffer[entity.id].properties); // collection->buffer[entity.id].properties = NULL; // // for (uint8_t i = entity.id; i < collection->count - 1; i++) // { // collection->buffer[i] = collection->buffer[i + 1]; // } // // collection->count--; // }