From 499206dcb39e9ed0c3e21e3c580f556218e0c693 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Thu, 28 Dec 2023 16:05:02 -0500 Subject: EntityType falls back to a default texture if not provided --- src/entities.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/entities.cpp') diff --git a/src/entities.cpp b/src/entities.cpp index 9553a89..ab4f731 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -3,6 +3,7 @@ #include "math-helpers.hpp" #include "physics.hpp" +#include "static/missing-texture.hpp" #include "window.hpp" #include @@ -102,12 +103,20 @@ void EntityType_Load(EntityType &et) { gltfPath.replace_filename(gltfData->buffers[0].uri); AssetHandle modelBinHandle = AM_Register(gltfPath.c_str(), PKE_ASSET_TYPE_UNSET); + int pixelWidth, pixelHeight, pixelChannels; + stbi_uc *pixels = nullptr; if (textureAssetHandle != AssetHandle_MAX) { const Asset *textureAsset = AM_Get(textureAssetHandle); - int pixelWidth, pixelHeight, pixelChannels; - auto *pixels = stbi_load_from_memory(static_cast(textureAsset->ptr), textureAsset->size, &pixelWidth, &pixelHeight, &pixelChannels, STBI_rgb_alpha); + pixels = stbi_load_from_memory(static_cast(textureAsset->ptr), textureAsset->size, &pixelWidth, &pixelHeight, &pixelChannels, STBI_rgb_alpha); assert(pixels != nullptr && "sbti_load failed to load image."); + } else { + pixelWidth = 2; + pixelHeight = 2; + pixelChannels = 4; + pixels = const_cast(&PKE_MISSING_TEXTURE_DATA[0]); + } + { uint32_t imageSizeBytes = pixelWidth * pixelHeight * pixelChannels; VkFormat imageFormat = VK_FORMAT_R8G8B8A8_SRGB; @@ -310,7 +319,9 @@ void EntityType_Load(EntityType &et) { // TODO double-check this? // stbi_image_free(pixels); - AM_Release(textureAssetHandle); + if (textureAssetHandle != AssetHandle_MAX) { + AM_Release(textureAssetHandle); + } // descriptor pool & sets -- cgit v1.2.3