From b0b61c117c6e2bc7693802f005a2888e7cc073c2 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Wed, 28 Dec 2022 11:23:00 -0300 Subject: refa Use QOI image format for textures --- src/vk/texture.cpp | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) (limited to 'src/vk') 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 + #include "../command.hpp" #include "../core.hpp" #include "image.hpp" @@ -76,42 +79,29 @@ load_image(void *obj) { auto self = static_cast(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(image->w); - self->texture->height = static_cast(image->h); + pixels = static_cast(pixels_ptr); + self->texture->width = static_cast(img_desc.width); + self->texture->height = static_cast(img_desc.height); self->texture->mip_levels = 1; - SDL_FreeSurface(raw_surface); + pixels = static_cast(pixels_ptr); } // Load file image into a vulkan buffer. size_t image_size{static_cast( - 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 -- cgit v1.2.3