refactor: applied aaa principle

This commit is contained in:
light7734 2025-07-07 15:49:18 +03:30
parent 83f27e12c0
commit 2341be0c70
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
5 changed files with 22 additions and 19 deletions

View file

@ -21,16 +21,18 @@ void log(Args &&...args)
std::cout << '\n'; std::cout << '\n';
} }
bool convert_image(const std::filesystem::path &input, const std::filesystem::path &output) auto convert_image(const std::filesystem::path &input, const std::filesystem::path &output) -> bool
{ {
int width, height, channels; auto width = int {};
auto height = int {};
auto channels = int {};
stbi_uc *pixels = stbi_load(input.string().c_str(), &width, &height, &channels, 4); auto *pixels = stbi_load(input.string().c_str(), &width, &height, &channels, 4);
if (!pixels) if (!pixels)
return false; return false;
Assets::TextureInfo texInfo { auto texture_info = Assets::TextureInfo {
.size = static_cast<size_t>(width * height * 4), .size = static_cast<size_t>(width * height * 4),
.format = Assets::TextureFormat::RGBA8, .format = Assets::TextureFormat::RGBA8,
.pixel_size = { .pixel_size = {
@ -41,7 +43,7 @@ bool convert_image(const std::filesystem::path &input, const std::filesystem::pa
.original_file = input.string(), .original_file = input.string(),
}; };
Assets::AssetFile file = Assets::pack_texture(&texInfo, pixels); auto file = Assets::pack_texture(&texture_info, pixels);
stbi_image_free(pixels); stbi_image_free(pixels);
@ -59,7 +61,7 @@ int main(int argc, char *argv[])
"Argc MUST be 3, 1: execution-path(implicit), 2: input-directory, 3: output-directory" "Argc MUST be 3, 1: execution-path(implicit), 2: input-directory, 3: output-directory"
); );
for (auto &p : std::filesystem::directory_iterator(argv[1])) for (const auto &p : std::filesystem::directory_iterator(argv[1]))
{ {
if (p.path().extension() == ".png") if (p.path().extension() == ".png")
{ {

View file

@ -1,10 +1,11 @@
#pragma once #pragma once
#include <array>
#include <asset_parser/parser.hpp> #include <asset_parser/parser.hpp>
namespace Assets { namespace Assets {
enum class TextureFormat enum class TextureFormat : uint8_t
{ {
None = 0, None = 0,
RGBA8, RGBA8,
@ -15,11 +16,11 @@ struct TextureInfo
size_t size; size_t size;
CompressionMode compression_mode; CompressionMode compression_mode;
TextureFormat format; TextureFormat format;
uint32_t pixel_size[3]; std::array<uint32_t, 3> pixel_size;
std::string original_file; std::string original_file;
}; };
TextureInfo read_texture_info(AssetFile *file); auto read_texture_info(AssetFile *file) -> TextureInfo;
void unpack_texture( void unpack_texture(
TextureInfo *info, TextureInfo *info,
@ -28,6 +29,6 @@ void unpack_texture(
void *destination void *destination
); );
AssetFile pack_texture(TextureInfo *info, void *pixel_data); auto pack_texture(TextureInfo *info, void *pixel_data) -> AssetFile;
} // namespace Assets } // namespace Assets

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <stdint.h> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -10,7 +10,7 @@ struct AssetFile
{ {
uint32_t version; uint32_t version;
enum class Type : uint32_t enum class Type : uint32_t // NOLINT(performance-enum-size)
{ {
Texture, Texture,
Mesh, Mesh,
@ -21,14 +21,14 @@ struct AssetFile
std::vector<uint8_t> blob; std::vector<uint8_t> blob;
}; };
enum class CompressionMode : uint32_t enum class CompressionMode : uint32_t // NOLINT(performance-enum-size)
{ {
None, None,
LZ4, LZ4,
LZ4HC, LZ4HC,
}; };
bool save_binary_file(const char* path, const AssetFile& in_file); auto save_binary_file(const char *path, const AssetFile &in_file) -> bool;
bool load_binary_file(const char* path, AssetFile& out_file); auto load_binary_file(const char *path, AssetFile &out_file) -> bool;
} // namespace Assets } // namespace Assets

View file

@ -6,7 +6,7 @@ namespace Assets {
using namespace nlohmann; using namespace nlohmann;
TextureInfo read_texture_info(AssetFile *file) auto read_texture_info(AssetFile *file) -> TextureInfo
{ {
json texture_meta_data = json::parse(file->json); json texture_meta_data = json::parse(file->json);
@ -45,7 +45,7 @@ void unpack_texture(
} }
} }
AssetFile pack_texture(TextureInfo *info, void *pixel_data) auto pack_texture(TextureInfo *info, void *pixel_data) -> AssetFile
{ {
json metadata; json metadata;
metadata["format"] = info->format; metadata["format"] = info->format;

View file

@ -5,7 +5,7 @@
namespace Assets { namespace Assets {
bool save_binary_file(const char *path, const AssetFile &file) auto save_binary_file(const char *path, const AssetFile &file) -> bool
{ {
std::ofstream outstream(path, std::ios::binary | std::ios::out); std::ofstream outstream(path, std::ios::binary | std::ios::out);
@ -27,7 +27,7 @@ bool save_binary_file(const char *path, const AssetFile &file)
return true; return true;
} }
bool load_binary_file(const char *path, AssetFile &out_file) auto load_binary_file(const char *path, AssetFile &out_file) -> bool
{ {
std::ifstream instream(path, std::ios::binary); std::ifstream instream(path, std::ios::binary);
instream.seekg(0ull); instream.seekg(0ull);