diff options
author | Frederico Linhares <fred@linhares.blue> | 2022-12-28 11:23:00 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2022-12-29 16:50:53 -0300 |
commit | b0b61c117c6e2bc7693802f005a2888e7cc073c2 (patch) | |
tree | c05f8f0174ca81fc9b2b2e623a6f8067369d874f /src/vk | |
parent | 75337ae1c59e8b2c3bc6fbcd7ccafc7a8205d6b2 (diff) |
refa Use QOI image format for textures
Diffstat (limited to 'src/vk')
-rw-r--r-- | src/vk/texture.cpp | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/src/vk/texture.cpp b/src/vk/texture.cpp index 54cf801..020dd21 100644 --- a/src/vk/texture.cpp +++ b/src/vk/texture.cpp @@ -16,6 +16,9 @@ #include "texture.hpp" +#define QOI_IMPLEMENTATION 0 +#include <qoi.h> + #include "../command.hpp" #include "../core.hpp" #include "image.hpp" @@ -76,42 +79,29 @@ load_image(void *obj) { auto self = static_cast<TextureBuilder*>(obj); - SDL_Surface *image{nullptr}; - - // Load file image from file. - { - SDL_Surface *raw_surface{nullptr}; - raw_surface = IMG_Load(self->texture_path.c_str()); - if(raw_surface == nullptr) - { - std::string error{"Failed to load image. SDL2_image Error: "}; - error += IMG_GetError(); - throw CommandError{error}; - } + qoi_desc img_desc; + const int num_channels = 4; // all images are converted to RGBA + unsigned char *pixels; - image = SDL_ConvertSurfaceFormat(raw_surface, SDL_PIXELFORMAT_ARGB8888, 0); - if(image == nullptr) - { - std::string error{"Failed to convert image. SDL2 Error: "}; - error += SDL_GetError(); - throw CommandError{error}; - } + { // Load file image from file. + void *pixels_ptr = qoi_read(self->texture_path.c_str(), &img_desc, 4); + if(pixels_ptr == NULL) throw CommandError{"Failed to open image."}; - self->texture->width = static_cast<uint32_t>(image->w); - self->texture->height = static_cast<uint32_t>(image->h); + pixels = static_cast<unsigned char*>(pixels_ptr); + self->texture->width = static_cast<uint32_t>(img_desc.width); + self->texture->height = static_cast<uint32_t>(img_desc.height); self->texture->mip_levels = 1; - SDL_FreeSurface(raw_surface); + pixels = static_cast<unsigned char*>(pixels_ptr); } // Load file image into a vulkan buffer. size_t image_size{static_cast<size_t>( - image->format->BytesPerPixel * image->w * image->h)}; + img_desc.width * img_desc.height * num_channels)}; VK::SourceBuffer source_image_buffer{ - cg_core.vk_device_with_swapchain, image->pixels, image_size}; + cg_core.vk_device_with_swapchain, pixels, image_size}; - // Create vulkan image. - { + { // Create vulkan image. try { VkExtent3D vk_extent3d{}; @@ -176,7 +166,7 @@ load_image(void *obj) } // Free resources. - SDL_FreeSurface(image); + QOI_FREE(pixels); } void |