#! /usr/bin/make -f

#---
#	Project variables.
#---

name		= arena
target-g1a	= $(name).g1a

# Tools
cc	= sh3eb-elf-gcc
as	= sh3eb-elf-as
ob	= sh3eb-elf-objcopy

# Various files
file-src	= $(notdir $(wildcard src/*.[cs]))
file-res	= $(notdir $(wildcard res/*))
file-dep	= $(wildcard src/*.h)
file-obj	= $(patsubst %,build/%.o,$(file-src) $(file-res))
file-ld		= addin.ld
file-icon	= icon.bmp

# Flags
cflags		= -m3 -mb -nostdlib -I include -ffreestanding -std=c11 -Os -W \
		  -Wall
libs		= -L. -lgint -lc -lgcc -lfx
wrap		= -i $(file-icon) -n Arena --internal=@ARENA
map-options	= --tile-width 12 --tile-height 12


#---
#	Building.
#---

# Generic rules

all: build $(target-g1a)
	@echo "[ \033[32;1mOK\033[0m ] All done!"

build:
	mkdir -p $@

$(target-g1a): $(file-obj)
	$(cc) -o build/$(name).elf $(cflags) -T $(file-ld) $^ $(libs)
	$(ob) -R .comment -R .bss -O binary build/$(name).elf build/$(name).bin
	g1a-wrapper build/$(name).bin -o $@ $(wrap)
	@echo "[ \033[32;1mOK\033[0m ] demo app: `stat -c %s $@` bytes"

build/%.c.o: src/%.c $(file-dep)
	$(cc) -c $< -o $@ $(cflags)

build/font_%.bmp.o: res/font_%.bmp
	fxconv $< -o $@ --font -n $(<:res/%.bmp=res_%)

build/map_%.bmp.o: res/map_%.bmp
	fxconv $< -o $@ --map -n $(<:res/%.bmp=res_%) $(map-options)

build/%.bmp.o: res/%.bmp
	fxconv $< -o $@ -n $(<:res/%.bmp=res_%)



#---
#	Cleaning and others.
#---

clean:
	@ rm -rf build/*

mrproper: clean
	@ rm -f $(target-g1a)
	@ rm -rf build

distclean: mrproper

install:
	CasioUsbUploader -f $(target-g1a) -w -l 1


.PHONY: all clean mrproper distclean
