diff --git a/.ccls b/.ccls index 45eebca..5f14576 100644 --- a/.ccls +++ b/.ccls @@ -4,3 +4,4 @@ gcc -Isrc/ecs/headers -Isrc/ecs/components/headers -Icore/src/headers +-Iassets/asset_builder/src/headers diff --git a/assets/asset_builder/README.md b/assets/asset_builder/README.md new file mode 100644 index 0000000..832e61c --- /dev/null +++ b/assets/asset_builder/README.md @@ -0,0 +1,11 @@ +# Asset file structure + +| Description | Length | +| :---------------- | :---------------------------------- | +| Assets number | int (32 bits) | +| ... | | +| Asset name | const char * (variable length) | +| Asset start index | int (32 bits) | +| Asset end index | int (32 bits) | +| ... | | +| Asset data | depend on assets content and number | \ No newline at end of file diff --git a/assets/asset_builder/src/Makefile b/assets/asset_builder/src/Makefile index 8fb3ea6..5b74064 100644 --- a/assets/asset_builder/src/Makefile +++ b/assets/asset_builder/src/Makefile @@ -1,11 +1,12 @@ # Source files ASSET_BUILDER_SOURCES := \ - main.c + main.c \ + asset_file.c # Compiler and flags ASSET_BUILDER_CC = gcc #ASSET_BUILDER_CFLAGS = -std=c17 -pedantic -O3 # RELEASE -ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g \ +ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g $(shell pkg-config --cflags libpng) \ -Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \ -Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \ -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \ @@ -15,7 +16,7 @@ ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g \ -Wold-style-definition # DEVELOPEMENT ASSET_BUILDER_INCLUDE_DIRS = $(ASSET_BUILDER_DIR)/headers -ASSET_BUILDER_LDFLAGS = +ASSET_BUILDER_LDFLAGS = $(shell pkg-config --libs libpng) # Deduce objects ASSET_BUILDER_OBJECTS = $(ASSET_BUILDER_SOURCES:%.c=$(BUILD_DIR)/$(ASSET_BUILDER_DIR)/%.o) @@ -24,7 +25,7 @@ ASSET_BUILDER_OBJECTS = $(ASSET_BUILDER_SOURCES:%.c=$(BUILD_DIR)/$(ASSET_BUILDER CURRENT_ASSET_BUILDER_FILE := 0 # Build asset builder target -build_asset_builder: asset_builder_setup count_asset_builder_build $(BUILD_DIR)/$(ASSET_BUILDER_DIR)/$(ASSET_BUILDER_OUTPUT) asset_builder_end +build_asset_builder: asset_builder_setup count_asset_builder_build asset_builder_end # Source build directories asset_builder_setup: @@ -48,7 +49,7 @@ $(BUILD_DIR)/$(ASSET_BUILDER_DIR)/%.o: $(ASSET_BUILDER_DIR)/%.c $(Q)$(SRC_CC) $(SRC_CFLAGS) $(ASSET_BUILDER_INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@ # Print ending message -asset_builder_end: +asset_builder_end: $(BUILD_DIR)/$(ASSET_BUILDER_DIR)/$(ASSET_BUILDER_OUTPUT) @echo "Built target $(ASSET_BUILDER_OUTPUT)" # Source files dependencies diff --git a/assets/asset_builder/src/asset_file.c b/assets/asset_builder/src/asset_file.c new file mode 100644 index 0000000..038a3a1 --- /dev/null +++ b/assets/asset_builder/src/asset_file.c @@ -0,0 +1,81 @@ +#include "asset_file.h" + +#include +#include +#include + +int parse_png_for_header(const char *file_name, asset_def_t *asset_def); + +int parse_file_for_header(const char *file_name, asset_def_t *asset_def) +{ + const char *file_name_dot = strrchr(file_name, '.'); + + if(!file_name_dot) + { + return -1; // no extension + } + + const char *file_name_ext = file_name_dot + 1; + + if(!strcmp(file_name_ext, "png")) + { + return parse_png_for_header(file_name, asset_def); + } + else + { + return -2; // extension not supported + } + + return 0; +} + +int parse_png_for_header(const char *file_name, asset_def_t *asset_def) +{ + FILE *input_png_file = fopen(file_name, "rb"); + + if(!input_png_file) + { + fclose(input_png_file); + return -3; + } + + fclose(input_png_file); + + return 0; + + // Check png autenticity. + unsigned char sig[8]; + fread(sig, 1, 8, input_png_file); + if(!png_check_sig(sig, 8)) + { + fclose(input_png_file); + return -4; // bad signature + } + + // PNG structs + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if(!png_ptr) + { + fclose(input_png_file); + return -5; // out of memory + } + + png_infop info_ptr = png_create_info_struct(png_ptr); + if(!info_ptr) + { + png_destroy_read_struct(&png_ptr, NULL, NULL); + fclose(input_png_file); + return -5; // out of memory + } + + // Fill structs and load data + png_init_io(png_ptr, input_png_file); + png_set_sig_bytes(png_ptr, 8); + png_read_info(png_ptr, info_ptr); + + printf("%s bit depth : %d", file_name, png_get_bit_depth(png_ptr, info_ptr)); + + fclose(input_png_file); + + return 0; +} diff --git a/assets/asset_builder/src/headers/asset_file.h b/assets/asset_builder/src/headers/asset_file.h new file mode 100644 index 0000000..11e6080 --- /dev/null +++ b/assets/asset_builder/src/headers/asset_file.h @@ -0,0 +1,18 @@ +#ifndef ASSET_FILE_H +#define ASSET_FILE_H + +#include + +typedef struct asset_def_t { + const char *name; + uint32_t start, end; +} asset_def_t; + +typedef struct asset_file_header_t { + uint32_t assets_number; + asset_def_t *assets_def; +} asset_file_header_t; + +int parse_file_for_header(const char *file_name, asset_def_t *asset_def); + +#endif // ASSET_FILE_H \ No newline at end of file diff --git a/assets/asset_builder/src/main.c b/assets/asset_builder/src/main.c index 635b841..8895d0b 100644 --- a/assets/asset_builder/src/main.c +++ b/assets/asset_builder/src/main.c @@ -1,11 +1,68 @@ #include +#include "asset_file.h" + +void print_error(const char *content) +{ + printf("\033[31merror\033[m: %s\n", content); +} + int main(int argc, char *argv[]) { + // Verify arguments minimum number. printf("%d args\n", argc); - for(int i = 0; i < argc; i++) + if(argc < 3) + { + print_error("not enough args."); + return -1; + } + + for(size_t i = 0; i < (size_t)argc; i++) puts(argv[i]); + // Create and open output file. + FILE *output_asset = fopen(argv[2], "wb"); + + if(!output_asset) + { + print_error("could not create output file."); + return -1; + } + + // Parse input files to create header structure. + asset_file_header_t asset_file_header; + + asset_file_header.assets_number = argc - 2; + + for(size_t i = 2; i < (size_t)argc; i++) + { + asset_def_t asset_def; + int ret = parse_file_for_header(argv[i], &asset_def); + if(ret) + { + puts(argv[i]); + + switch(ret) + { + case -1: + print_error("file does not have an extension."); + return -1; + case -2: + print_error("extension not yet supported."); + return -1; + case -3: + print_error("failed to open file."); + return -1; + case -4: + print_error("file not valid."); + return -1; + case -5: + print_error("memory allocation failed."); + return -1; + } + } + } + return 0; } diff --git a/shell.nix b/shell.nix index 2a4c63b..8e34b9b 100644 --- a/shell.nix +++ b/shell.nix @@ -9,9 +9,10 @@ stdenv.mkDerivation { buildInputs = [ SDL2 SDL2_image + libpng ]; - shellHook = '' - export LD_LIBRARY_PATH=${pkgs.SDL2}/lib:${pkgs.SDL2_image}/lib:$LD_LIBRARY_PATH - export PKG_CONFIG_PATH=${pkgs.SDL2}/lib/pkgconfig:${pkgs.SDL2_image}/lib/pkgconfig:$PKG_CONFIG_PATH - ''; +# shellHook = '' +# export LD_LIBRARY_PATH=${pkgs.SDL2}/lib:${pkgs.SDL2_image}/lib:$LD_LIBRARY_PATH +# export PKG_CONFIG_PATH=${pkgs.SDL2}/lib/pkgconfig:${pkgs.SDL2_image}/lib/pkgconfig:$PKG_CONFIG_PATH +# ''; }