Starting to read png files in asset builder.
This commit is contained in:
parent
7ba5e651cf
commit
ff90c5b683
1
.ccls
1
.ccls
|
|
@ -4,3 +4,4 @@ gcc
|
||||||
-Isrc/ecs/headers
|
-Isrc/ecs/headers
|
||||||
-Isrc/ecs/components/headers
|
-Isrc/ecs/components/headers
|
||||||
-Icore/src/headers
|
-Icore/src/headers
|
||||||
|
-Iassets/asset_builder/src/headers
|
||||||
|
|
|
||||||
|
|
@ -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 |
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
# Source files
|
# Source files
|
||||||
ASSET_BUILDER_SOURCES := \
|
ASSET_BUILDER_SOURCES := \
|
||||||
main.c
|
main.c \
|
||||||
|
asset_file.c
|
||||||
|
|
||||||
# Compiler and flags
|
# Compiler and flags
|
||||||
ASSET_BUILDER_CC = gcc
|
ASSET_BUILDER_CC = gcc
|
||||||
#ASSET_BUILDER_CFLAGS = -std=c17 -pedantic -O3 # RELEASE
|
#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 \
|
-Wall -Wno-missing-braces -Wextra -Wno-missing-field-initializers \
|
||||||
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
|
-Wformat=2 -Wswitch-default -Wswitch-enum -Wcast-align \
|
||||||
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
|
-Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
|
||||||
|
|
@ -15,7 +16,7 @@ ASSET_BUILDER_CFLAGS = -std=c17 --pedantic -O0 -g \
|
||||||
-Wold-style-definition # DEVELOPEMENT
|
-Wold-style-definition # DEVELOPEMENT
|
||||||
|
|
||||||
ASSET_BUILDER_INCLUDE_DIRS = $(ASSET_BUILDER_DIR)/headers
|
ASSET_BUILDER_INCLUDE_DIRS = $(ASSET_BUILDER_DIR)/headers
|
||||||
ASSET_BUILDER_LDFLAGS =
|
ASSET_BUILDER_LDFLAGS = $(shell pkg-config --libs libpng)
|
||||||
|
|
||||||
# Deduce objects
|
# Deduce objects
|
||||||
ASSET_BUILDER_OBJECTS = $(ASSET_BUILDER_SOURCES:%.c=$(BUILD_DIR)/$(ASSET_BUILDER_DIR)/%.o)
|
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
|
CURRENT_ASSET_BUILDER_FILE := 0
|
||||||
|
|
||||||
# Build asset builder target
|
# 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
|
# Source build directories
|
||||||
asset_builder_setup:
|
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 $@
|
$(Q)$(SRC_CC) $(SRC_CFLAGS) $(ASSET_BUILDER_INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
# Print ending message
|
# Print ending message
|
||||||
asset_builder_end:
|
asset_builder_end: $(BUILD_DIR)/$(ASSET_BUILDER_DIR)/$(ASSET_BUILDER_OUTPUT)
|
||||||
@echo "Built target $(ASSET_BUILDER_OUTPUT)"
|
@echo "Built target $(ASSET_BUILDER_OUTPUT)"
|
||||||
|
|
||||||
# Source files dependencies
|
# Source files dependencies
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
#include "asset_file.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <png.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef ASSET_FILE_H
|
||||||
|
#define ASSET_FILE_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -1,11 +1,68 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "asset_file.h"
|
||||||
|
|
||||||
|
void print_error(const char *content)
|
||||||
|
{
|
||||||
|
printf("\033[31merror\033[m: %s\n", content);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
// Verify arguments minimum number.
|
||||||
printf("%d args\n", argc);
|
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]);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,10 @@ stdenv.mkDerivation {
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
SDL2
|
SDL2
|
||||||
SDL2_image
|
SDL2_image
|
||||||
|
libpng
|
||||||
];
|
];
|
||||||
shellHook = ''
|
# shellHook = ''
|
||||||
export LD_LIBRARY_PATH=${pkgs.SDL2}/lib:${pkgs.SDL2_image}/lib:$LD_LIBRARY_PATH
|
# 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
|
# export PKG_CONFIG_PATH=${pkgs.SDL2}/lib/pkgconfig:${pkgs.SDL2_image}/lib/pkgconfig:$PKG_CONFIG_PATH
|
||||||
'';
|
# '';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue