all: haunted_mansion.elf

OPT = -O2

CSTD = -std=gnu11

MAKEFILE_PATH := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
LIB ?= $(MAKEFILE_PATH)/dreamcast
CFLAGS += -D__dreamcast__
CFLAGS += -I$(MAKEFILE_PATH)/
CFLAGS += -I$(MAKEFILE_PATH)/dreamcast
CFLAGS += -Wno-error=strict-aliasing -fno-strict-aliasing
CARCH = -m4-single -ml

include dreamcast/base.mk
include dreamcast/common.mk
include dreamcast/headers.mk
include dreamcast/ip.mk

include haunted_mansion.mk

%.class.o: %.class
	$(BUILD_BINARY_O)

%.class.h: %.class
	$(BUILD_BINARY_H)

libgcc/%.o: $(LIBGCC)
	@mkdir -p $(dir $@)
	ar x --output $(dir $@) $(LIBGCC) $(notdir $@)
	sh4-none-elf-objdump -t $@ \
		| grep -E '[.]hidden' \
		| grep -vE 'UND' \
		| cut -d' ' -f10 \
		| xargs rebind --visibility=default $@

LIBGCC_OBJ =

haunted_mansion.elf: LDSCRIPT = $(LIB)/main.lds
haunted_mansion.elf: $(START_OBJ) $(HAUNTED_MANSION_OBJ) $(LIBGCC_OBJ)

model/%/collision.h: model/%/collision.obj
	python ../model_generator2/generate_cpp.py $< ${*}_collision > $@

model/%/model.h: model/%/model.obj
	python ../model_generator2/generate_cpp.py $< $* > $@

%.ppm: %.png
	magick -depth 8 $< $@

%.vq: %.ppm
	../dreamcast/gen/k_means/k_means_vq $< $@

VQ_ASSETS = \
	textures/brick_floor_diff_1k.vq \
	textures/red_plaster_weathered_diff_512p.vq \
	textures/wooden_gate_diff_1k.vq \
	textures/brick_wall_10_diff_512p.vq \
	textures/weathered_planks_diff_1k.vq \
	textures/yellow_plaster_02_diff_512p.vq \
	textures/rebar_reinforced_concrete_diff_512.vq \
	textures/white_plaster_rough_02_diff_512p.vq \
	textures/yellow_plaster_diff_512p.vq

ASSETS = \
	model/haunted_mansion/collision.h \
	model/haunted_mansion/model.h \
	model/cone/model.h \
	model/cube/model.h

asset-vq-gen: $(VQ_ASSETS)
asset-gen: $(ASSETS)
asset-clean:
	rm -f $(ASSETS)

.PHONY: asset-gen asset-vq-gen asset-clean
