summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2022-12-28 11:23:00 -0300
committerFrederico Linhares <fred@linhares.blue>2022-12-29 16:50:53 -0300
commitb0b61c117c6e2bc7693802f005a2888e7cc073c2 (patch)
treec05f8f0174ca81fc9b2b2e623a6f8067369d874f /src
parent75337ae1c59e8b2c3bc6fbcd7ccafc7a8205d6b2 (diff)
refa Use QOI image format for textures
Diffstat (limited to 'src')
-rw-r--r--src/core.cpp19
-rw-r--r--src/core.hpp1
-rw-r--r--src/pgm_image.cpp70
-rw-r--r--src/pgm_image.hpp34
-rw-r--r--src/texture.cpp3
-rw-r--r--src/vk/texture.cpp44
6 files changed, 17 insertions, 154 deletions
diff --git a/src/core.cpp b/src/core.cpp
index bd5ea75..c4e03a4 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -175,24 +175,6 @@ unload_sdl(void *obj)
}
void
-load_sdl_image(void *obj)
-{
- int flags = IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF;
- if(!(IMG_Init(flags) & flags))
- {
- std::string error{"Could not initialize SDL image → "};
- error += IMG_GetError();
- throw CommandError{error};
- }
-}
-
-void
-unload_sdl_image(void *obj)
-{
- IMG_Quit();
-}
-
-void
load_sdl_mixer(void *obj)
{
int flags = MIX_INIT_OGG|MIX_INIT_MOD;
@@ -696,7 +678,6 @@ const CommandChain cg_sCore::loader{
{&load_mruby_symbols, nullptr},
{&load_game, nullptr},
{&load_sdl, &unload_sdl},
- {&load_sdl_image, &unload_sdl_image},
{&load_sdl_mixer, &unload_sdl_mixer},
{&load_sdl_open_audio, &unload_sdl_open_audio},
{&load_window, &unload_window},
diff --git a/src/core.hpp b/src/core.hpp
index 5d92cfa..660af27 100644
--- a/src/core.hpp
+++ b/src/core.hpp
@@ -37,7 +37,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
-#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include "command.hpp"
diff --git a/src/pgm_image.cpp b/src/pgm_image.cpp
deleted file mode 100644
index 04ff9b9..0000000
--- a/src/pgm_image.cpp
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2022 Frederico de Oliveira Linhares
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "pgm_image.hpp"
-
-#include <cstring>
-#include <charconv>
-#include <fstream>
-#include <string>
-
-bool PGMImage_constructor(PGMImage *self, const char *file_path)
-{
- self->data = nullptr;
- int wh_pos;
- std::string line;
- std::ifstream file(file_path);
-
- if(!file)
- {
- self->error = "Failed to open PGM file";
- return false;
- }
-
- // Read file magic.
- std::getline(file, line);
- if(line != "P5")
- {
- self->error = "PGM file contains a invalid magic";
- return false;
- }
-
- // Read file comment.
- std::getline(file, line);
-
- // Read file width and height.
- std::getline(file, line);
- wh_pos = line.find(" ");
- std::from_chars(line.data(), line.data() + wh_pos, self->width);
- std::from_chars(
- line.data() + wh_pos + 1, line.data() + line.size(), self->height);
-
- // Read file maximum value.
- std::getline(file, line);
- std::from_chars(line.data(), line.data() + line.size(), self->max_value);
-
- // Read file values.
- self->data_size = self->width * self->height;
- self->data = new char[self->data_size];
- file.read(self->data, self->data_size);
-
- return true;
-}
-
-void PGMImage_destructor(PGMImage *self)
-{
- if(self->data != nullptr) delete[] self->data;
-}
diff --git a/src/pgm_image.hpp b/src/pgm_image.hpp
deleted file mode 100644
index bda73ca..0000000
--- a/src/pgm_image.hpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright 2022 Frederico de Oliveira Linhares
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef CANDY_GEAR_PGM_IMAGE_H
-#define CANDY_GEAR_PGM_IMAGE_H 1
-
-struct PGMImage_s
-{
- int width, height, max_value, data_size;
- char *data;
- const char *error;
-};
-
-typedef struct PGMImage_s PGMImage;
-
-bool
-PGMImage_constructor(PGMImage *self, const char *file_path);
-void
-PGMImage_destructor(PGMImage *self);
-
-#endif /* CANDY_GEAR_PGM_IMAGE_H */
diff --git a/src/texture.cpp b/src/texture.cpp
index ac2a8e4..ce1d479 100644
--- a/src/texture.cpp
+++ b/src/texture.cpp
@@ -17,7 +17,6 @@
#include "texture.hpp"
#include "core.hpp"
-#include "pgm_image.hpp"
#include "vk/texture.hpp"
void
@@ -46,8 +45,6 @@ texture_alloc(mrb_state *mrb, mrb_value klass)
static mrb_value
cg_cTexture_from_image(mrb_state *mrb, mrb_value self)
{
- PGMImage pgm_img;
-
struct mrb_value texture = texture_alloc(mrb, self);
const char *file_path;
std::shared_ptr<VK::Texture> *ptr;
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