refactor: applied aaa principle
This commit is contained in:
		
							parent
							
								
									83f27e12c0
								
							
						
					
					
						commit
						2341be0c70
					
				
					 5 changed files with 22 additions and 19 deletions
				
			
		| 
						 | 
				
			
			@ -21,16 +21,18 @@ void log(Args &&...args)
 | 
			
		|||
	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)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	Assets::TextureInfo texInfo {
 | 
			
		||||
	auto texture_info = Assets::TextureInfo {
 | 
			
		||||
		.size       = static_cast<size_t>(width * height * 4),
 | 
			
		||||
		.format     = Assets::TextureFormat::RGBA8,
 | 
			
		||||
		.pixel_size = {
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +43,7 @@ bool convert_image(const std::filesystem::path &input, const std::filesystem::pa
 | 
			
		|||
		.original_file = input.string(),
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	Assets::AssetFile file = Assets::pack_texture(&texInfo, pixels);
 | 
			
		||||
	auto file = Assets::pack_texture(&texture_info, 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"
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	for (auto &p : std::filesystem::directory_iterator(argv[1]))
 | 
			
		||||
	for (const auto &p : std::filesystem::directory_iterator(argv[1]))
 | 
			
		||||
	{
 | 
			
		||||
		if (p.path().extension() == ".png")
 | 
			
		||||
		{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,11 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <asset_parser/parser.hpp>
 | 
			
		||||
 | 
			
		||||
namespace Assets {
 | 
			
		||||
 | 
			
		||||
enum class TextureFormat
 | 
			
		||||
enum class TextureFormat : uint8_t
 | 
			
		||||
{
 | 
			
		||||
	None = 0,
 | 
			
		||||
	RGBA8,
 | 
			
		||||
| 
						 | 
				
			
			@ -15,11 +16,11 @@ struct TextureInfo
 | 
			
		|||
	size_t size;
 | 
			
		||||
	CompressionMode compression_mode;
 | 
			
		||||
	TextureFormat format;
 | 
			
		||||
	uint32_t pixel_size[3];
 | 
			
		||||
	std::array<uint32_t, 3> pixel_size;
 | 
			
		||||
	std::string original_file;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
TextureInfo read_texture_info(AssetFile *file);
 | 
			
		||||
auto read_texture_info(AssetFile *file) -> TextureInfo;
 | 
			
		||||
 | 
			
		||||
void unpack_texture(
 | 
			
		||||
    TextureInfo *info,
 | 
			
		||||
| 
						 | 
				
			
			@ -28,6 +29,6 @@ void unpack_texture(
 | 
			
		|||
    void *destination
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
AssetFile pack_texture(TextureInfo *info, void *pixel_data);
 | 
			
		||||
auto pack_texture(TextureInfo *info, void *pixel_data) -> AssetFile;
 | 
			
		||||
 | 
			
		||||
} // namespace Assets
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -10,7 +10,7 @@ struct AssetFile
 | 
			
		|||
{
 | 
			
		||||
	uint32_t version;
 | 
			
		||||
 | 
			
		||||
	enum class Type : uint32_t
 | 
			
		||||
	enum class Type : uint32_t // NOLINT(performance-enum-size)
 | 
			
		||||
	{
 | 
			
		||||
		Texture,
 | 
			
		||||
		Mesh,
 | 
			
		||||
| 
						 | 
				
			
			@ -21,14 +21,14 @@ struct AssetFile
 | 
			
		|||
	std::vector<uint8_t> blob;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum class CompressionMode : uint32_t
 | 
			
		||||
enum class CompressionMode : uint32_t // NOLINT(performance-enum-size)
 | 
			
		||||
{
 | 
			
		||||
	None,
 | 
			
		||||
	LZ4,
 | 
			
		||||
	LZ4HC,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
bool save_binary_file(const char* path, const AssetFile& in_file);
 | 
			
		||||
bool load_binary_file(const char* path, AssetFile& out_file);
 | 
			
		||||
auto save_binary_file(const char *path, const AssetFile &in_file) -> bool;
 | 
			
		||||
auto load_binary_file(const char *path, AssetFile &out_file) -> bool;
 | 
			
		||||
 | 
			
		||||
} // namespace Assets
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,7 +6,7 @@ namespace Assets {
 | 
			
		|||
 | 
			
		||||
using namespace nlohmann;
 | 
			
		||||
 | 
			
		||||
TextureInfo read_texture_info(AssetFile *file)
 | 
			
		||||
auto read_texture_info(AssetFile *file) -> TextureInfo
 | 
			
		||||
{
 | 
			
		||||
	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;
 | 
			
		||||
	metadata["format"] = info->format;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@
 | 
			
		|||
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ bool save_binary_file(const char *path, const AssetFile &file)
 | 
			
		|||
	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);
 | 
			
		||||
	instream.seekg(0ull);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue