From c8a110cdeca8c23b88d6908f0b06231847c9e722 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 29 Dec 2016 11:18:04 +0100 Subject: Enable retrieving of sprite image length Without having to know the name of the symbol at compile time... --- Makefile | 6 +++--- sprite.h | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 sprite.h diff --git a/Makefile b/Makefile index 09b0e36..995f07f 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ sprite_allfiles := $(wildcard src/sprites/*) sprite_fnames := $(filter %.png,$(sprite_allfiles)) sprite_skipped := $(filter-out %.png,$(sprite_allfiles)) sprite_obj := $(patsubst src/sprites/%.png,build/sprites/%.o,$(sprite_fnames)) -sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern const unsigned char &[];/'; done)) +sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern const Sprite &;/'; done)) base_src := $(wildcard *_base.cpp) base_src += library.cpp main.cpp @@ -80,10 +80,10 @@ build/sprites/%.o: build/sprites/%.cpp build/sprites/%.cpp: src/sprites/%.png echo Generating for sprite $* mkdir -p $(dir $@) - printf "const unsigned char $*[]={\n%s, 0\n};\nconst unsigned int _$*_len=%d;\n" "$$(xxd -i <"$<")" $$(stat -f '%z' "$<") >"$@" + printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s, 0\n};\n\nSprite $*=(Sprite){.image=data,.image_len=%d};\n" "$$(xxd -i <"$<")" $$(stat -f '%z' "$<") >"$@" build/sprites.h: echo Collecting generated sprite files mkdir -p $(dir $@) - echo "$(sprite_externs)" >"$@" + printf "#include \"sprite.h\"\n\n$(sprite_externs)\n" >"$@" test -z "$(sprite_skipped)" || printf "\x1B[1mWARNING: skipped files in src/sprites: $(notdir $(sprite_skipped))\x1B[0m\n" diff --git a/sprite.h b/sprite.h new file mode 100644 index 0000000..e901c9a --- /dev/null +++ b/sprite.h @@ -0,0 +1,6 @@ +#pragma once + +struct Sprite{ + const unsigned char *image; + unsigned int image_len; +}; -- cgit v1.2.3-70-g09d2