vbtools = ~/Projects/Virtualboy/Tools
.PHONY: img
all:
	# Build the rom
	wine $(vbtools)/ISAS/ISAS32.exe -t main.vbasm -o bin\\vbdream.o
	wine $(vbtools)/ISAS/ISLK32.exe bin\\vbdream.o -t -v -o bin\\vbdream.isx
	wine $(vbtools)/vbcvtisx/vbcvtisx.exe bin\\vbdream.isx bin\\redsquare.vb
	$(vbtools)/vbshrink/shrinkvb ./bin/redsquare.vb
	$(vbtools)/vbshrink/shrinkvb ./bin/redsquare.vb
	$(vbtools)/vbshrink/shrinkvb ./bin/redsquare.vb
	$(vbtools)/vbshrink/shrinkvb ./bin/redsquare.vb
	$(vbtools)/vbshrink/shrinkvb ./bin/redsquare.vb
	@cloc --force-lang="Assembly",vbasm ./
	@make char

clean:
	rm bin/vbdream.o bin/vbdream.isx bin/vbdream.vb

NINACHAR=$(shell ls -Sl ./img/nina/*.chr | head -n 1 | cut -f5 -d' ' )
FONTCHAR=$(shell ls -Sl ./img/font/*.chr | head -n 1 | cut -f5 -d' ' )
TEXTBOXCHAR=$(shell ls -Sl ./img/textbox/*.chr | head -n 1 | cut -f5 -d' ' )
LARGESTMAPCHAR=$(shell du -ab ./img/maps/ | sort -n -r | grep chr | head -n 1| cut -f1)
TOTALSIZE=$(shell expr $(NINACHAR) + $(FONTCHAR) + $(TEXTBOXCHAR) + $(LARGESTMAPCHAR) - 8)
MAXSIZE=32768
MAXCHARS=2048
char:
	@# Calculate char sizes
	@echo "Nina Char:" $(shell expr $(NINACHAR) - 2) "bytes |" \
		$(shell expr \( $(NINACHAR) - 2 \) / 16) "chars"
	@echo "Font Char:" $(shell expr $(FONTCHAR) - 2) "bytes |" \
		$(shell expr \( $(FONTCHAR) - 2 \) / 16) "chars"
	@echo "Textbox Char:" $(shell expr $(TEXTBOXCHAR) - 2) "bytes |" \
		$(shell expr \( $(TEXTBOXCHAR) - 2 \) / 16) "chars"
	@echo "Largest Map Char:" $(shell expr $(LARGESTMAPCHAR) - 2) "bytes |" \
		$(shell expr \( $(LARGESTMAPCHAR) - 2 \) / 16) "chars"
	@echo "----------"
	@echo "The total size:" $(TOTALSIZE)"/"$(MAXSIZE) "bytes |" \
		$(shell expr \( $(TOTALSIZE) - 2 \) / 16)"/"$(MAXCHARS) "chars"
	@echo "Remaining space:" $(shell expr $(MAXSIZE) - $(TOTALSIZE)) "bytes |" \
		$(shell expr $(MAXCHARS) - \( $(TOTALSIZE) - 2 \) / 16) "chars"

img:
	# Convert the font file to the correct BG
	mkdir ./img/font/tmp
	convert ./img/font/font.png +gravity -crop 8x16 \
		./img/font/tmp/tiles_%03d.png
	montage ./img/font/tmp/tiles_*.png -tile 1x -geometry 8x16 \
		./img/font/retiled.png
	rm ./img/font/tmp/*.png
	convert ./img/font/retiled.png +gravity -crop 8x8 \
		./img/font/tmp/tiles_%03d.png
	montage ./img/font/tmp/tiles_*.png -tile 64x64 \
		-geometry 8x8 -background black ./img/font/font_map.bmp
	rm ./img/font/tmp/*.png
	rm ./img/font/retiled.png
	rmdir ./img/font/tmp/
	# Split meta-bgmaps into smaller bgmaps
	# Freezer
	convert ./img/maps/freezer/freezer_bg.png +repage +gravity -crop 512x512 \
		./img/maps/freezer/freezer_bg_%02d.bmp
	convert ./img/maps/freezer/freezer_fg.png +repage +gravity -crop 512x512 \
		./img/maps/freezer/freezer_fg_%02d.bmp
	# Forest
	convert ./img/maps/forest/forest_bg.png +repage +gravity -crop 512x512 \
		./img/maps/forest/forest_bg_%02d.bmp
	convert ./img/maps/forest/forest_fg.png +repage +gravity -crop 512x512 \
		./img/maps/forest/forest_fg_%02d.bmp
	# Pineapple
	convert ./img/maps/pineapple/pineapple_bg.png +repage +gravity -crop 512x512 \
		./img/maps/pineapple/pineapple_bg_%02d.bmp
	convert ./img/maps/pineapple/pineapple_fg.png +repage +gravity -crop 512x512 \
		./img/maps/pineapple/pineapple_fg_%02d.bmp
	# Neon
	convert ./img/maps/neon/neon_bg.png +repage +gravity -crop 512x512 \
		./img/maps/neon/neon_bg_%02d.bmp
	convert ./img/maps/neon/neon_fg.png +repage +gravity -crop 512x512 \
		./img/maps/neon/neon_fg_%02d.bmp
	# Store
	convert ./img/maps/store/store_bg.png +repage +gravity -crop 512x512 \
		./img/maps/store/store_bg_%02d.bmp
	convert ./img/maps/store/store_fg.png +repage +gravity -crop 512x512 \
		./img/maps/store/store_fg_%02d.bmp
	# Convert all the images to bmp3
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/nina/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/title/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/ending/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/warning/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/focusmap/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/controls/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/textbox/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/font/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/bishounen/*/*.bmp
	mogrify -format bmp -define bmp:format=bmp3 \
		-type truecolor ./img/maps/*/*.bmp
	# Export all images to binary formats
	# Nina
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/nina/nina_normal_charset \
		./img/nina/nina_normal
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/nina/nina_bike_charset \
		./img/nina/nina_bike
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/nina/nina_3d_glasses_charset \
		./img/nina/nina_3d_glasses
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/nina/nina_server_charset \
		./img/nina/nina_server
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/nina/nina_bishounen_charset \
		./img/nina/nina_bishounen
	# Textbox
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/textbox/textbox_charset \
		./img/textbox/textbox \
		./img/textbox/effects
	# Font
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/font/font_charset \
		./img/font/font_map
	# Focusmap
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/focusmap/focusmap_charset \
		./img/focusmap/focusmap_00 \
		./img/focusmap/focusmap_01
	# Controls
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/controls/controls_charset \
		./img/controls/controls_00 \
		./img/controls/controls_01
	# Title Screen
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/title/title_charset \
		./img/title/title_00 \
		./img/title/title_01 \
		./img/title/title_02
	# Ending
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/ending/ending_charset \
		./img/ending/ending_00 \
		./img/ending/ending_01 \
		./img/ending/ending_02 \
		./img/ending/ending_03
	# Warning Screen
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/warning/warning_charset \
		./img/warning/warning_00
	# Bedroom
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/bedroom/bedroom_charset \
		./img/maps/bedroom/bedroom_00 \
		./img/maps/bedroom/bedroom_01
	# Porch
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/porch/porch_charset \
		./img/maps/porch/porch_00 \
		./img/maps/porch/porch_01
	# Nexus
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/nexus/nexus_charset \
		./img/maps/nexus/nexus_00 \
		./img/maps/nexus/nexus_01
	# Empty
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/empty/empty_charset \
		./img/maps/empty/empty_00 \
		./img/maps/empty/empty_01 \
		./img/maps/empty/empty_02 \
		./img/maps/empty/empty_03 \
		./img/maps/empty/empty_04 \
		./img/maps/empty/empty_parallax
	# Heckmaze
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/heckmaze/heckmaze_charset \
		./img/maps/heckmaze/heckmaze_00 \
		./img/maps/heckmaze/heckmaze_01
	# Gameboy
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/gameboy/gameboy_charset \
		./img/maps/gameboy/gameboy_00 \
		./img/maps/gameboy/gameboy_01 \
		./img/maps/gameboy/gameboy_fx
	# Freezer
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/freezer/freezer_charset \
		./img/maps/freezer/freezer_fg_00 \
		./img/maps/freezer/freezer_fg_01 \
		./img/maps/freezer/freezer_fg_02 \
		./img/maps/freezer/freezer_fg_03 \
		./img/maps/freezer/freezer_bg_00 \
		./img/maps/freezer/freezer_bg_01 \
		./img/maps/freezer/freezer_bg_02 \
		./img/maps/freezer/freezer_bg_03 \
		./img/maps/freezer/freezer_fx
	# Forest
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/forest/forest_charset \
		./img/maps/forest/forest_fg_00 \
		./img/maps/forest/forest_fg_01 \
		./img/maps/forest/forest_fg_02 \
		./img/maps/forest/forest_fg_03 \
		./img/maps/forest/forest_bg_00 \
		./img/maps/forest/forest_bg_01 \
		./img/maps/forest/forest_bg_02 \
		./img/maps/forest/forest_bg_03
	# Pineapple
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/pineapple/pineapple_charset \
		./img/maps/pineapple/pineapple_fg_00 \
		./img/maps/pineapple/pineapple_fg_01 \
		./img/maps/pineapple/pineapple_fg_02 \
		./img/maps/pineapple/pineapple_fg_03 \
		./img/maps/pineapple/pineapple_bg_00 \
		./img/maps/pineapple/pineapple_bg_01 \
		./img/maps/pineapple/pineapple_bg_02 \
		./img/maps/pineapple/pineapple_bg_03 \
		./img/maps/pineapple/pineapple_parallax
	# Neon
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/neon/neon_charset \
		./img/maps/neon/neon_fg_00 \
		./img/maps/neon/neon_fg_01 \
		./img/maps/neon/neon_fg_02 \
		./img/maps/neon/neon_fg_03 \
		./img/maps/neon/neon_bg_00 \
		./img/maps/neon/neon_bg_01 \
		./img/maps/neon/neon_bg_02 \
		./img/maps/neon/neon_bg_03
	# Store
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/store/store_charset \
		./img/maps/store/store_fg_00 \
		./img/maps/store/store_fg_01 \
		./img/maps/store/store_fg_02 \
		./img/maps/store/store_fg_03 \
		./img/maps/store/store_bg_00 \
		./img/maps/store/store_bg_01 \
		./img/maps/store/store_bg_02 \
		./img/maps/store/store_bg_03
	# Boysroom
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/maps/boysroom/boysroom_charset \
		./img/maps/boysroom/boysroom_00 \
		./img/maps/boysroom/boysroom_01
	# Bishounen
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/bishounen/slide01/bishounen_slide_01_charset \
		./img/bishounen/slide01/bishounen_slide_01 \
		./img/bishounen/slide01/bishounen_frame
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/bishounen/slide02/bishounen_slide_02_charset \
		./img/bishounen/slide02/bishounen_slide_02 \
		./img/bishounen/slide02/bishounen_frame
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/bishounen/slide03/bishounen_slide_03_charset \
		./img/bishounen/slide03/bishounen_slide_03 \
		./img/bishounen/slide03/bishounen_frame
	wine $(vbtools)/vbimg/vbimgc.exe \/EC \/XB \
		./img/bishounen/slide04/bishounen_slide_04_charset \
		./img/bishounen/slide04/bishounen_slide_04 \
		./img/bishounen/slide04/bishounen_frame
	# Palette Change
	./tools/palette_selector.py -f ./img/nina/nina_normal.bgm 1
	./tools/palette_selector.py -f ./img/nina/nina_bike.bgm 1
	./tools/palette_selector.py -f ./img/nina/nina_3d_glasses.bgm 1
	./tools/palette_selector.py -f ./img/nina/nina_server.bgm 1
	./tools/palette_selector.py -f ./img/nina/nina_bishounen.bgm 1
	./tools/palette_selector.py -f ./img/font/font_map.bgm 1
	./tools/palette_selector.py -f ./img/ending/ending_02.bgm 1
	./tools/palette_selector.py -f ./img/textbox/textbox.bgm 1
	./tools/palette_selector.py -f ./img/textbox/effects.bgm 1
	./tools/palette_selector.py -f ./img/maps/bedroom/bedroom_01.bgm 1
	./tools/palette_selector.py -f ./img/maps/empty/empty_00.bgm 2
	./tools/palette_selector.py -f ./img/maps/gameboy/gameboy_01.bgm 1
	./tools/palette_selector.py -f ./img/maps/gameboy/gameboy_fx.bgm 1
	./tools/palette_selector.py -f ./img/maps/freezer/freezer_fg_00.bgm 1
	./tools/palette_selector.py -f ./img/maps/freezer/freezer_fg_01.bgm 1
	./tools/palette_selector.py -f ./img/maps/freezer/freezer_fg_02.bgm 1
	./tools/palette_selector.py -f ./img/maps/freezer/freezer_fg_03.bgm 1
	./tools/palette_selector.py -f ./img/maps/forest/forest_fg_00.bgm 2
	./tools/palette_selector.py -f ./img/maps/forest/forest_fg_01.bgm 2
	./tools/palette_selector.py -f ./img/maps/forest/forest_fg_02.bgm 2
	./tools/palette_selector.py -f ./img/maps/forest/forest_fg_03.bgm 2
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_bg_00.bgm 2
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_bg_01.bgm 2
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_bg_02.bgm 2
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_bg_03.bgm 2
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_fg_00.bgm 3
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_fg_01.bgm 3
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_fg_02.bgm 3
	./tools/palette_selector.py -f ./img/maps/pineapple/pineapple_fg_03.bgm 3
	./tools/palette_selector.py -f ./img/maps/neon/neon_fg_00.bgm 3
	./tools/palette_selector.py -f ./img/maps/neon/neon_fg_01.bgm 3
	./tools/palette_selector.py -f ./img/maps/neon/neon_fg_02.bgm 3
	./tools/palette_selector.py -f ./img/maps/neon/neon_fg_03.bgm 3
	./tools/palette_selector.py -f ./img/maps/store/store_fg_00.bgm 3
	./tools/palette_selector.py -f ./img/maps/store/store_fg_01.bgm 3
	./tools/palette_selector.py -f ./img/maps/store/store_fg_02.bgm 3
	./tools/palette_selector.py -f ./img/maps/store/store_fg_03.bgm 3
	./tools/palette_selector.py -f ./img/maps/heckmaze/heckmaze_01.bgm 2
	./tools/palette_selector.py -f ./img/maps/boysroom/boysroom_01.bgm 3
	./tools/palette_selector.py -f ./img/maps/porch/porch_01.bgm 1
	./tools/palette_selector.py -f ./img/bishounen/slide01/bishounen_frame.bgm 1
	./tools/palette_selector.py -f ./img/bishounen/slide02/bishounen_frame.bgm 1
	./tools/palette_selector.py -f ./img/bishounen/slide03/bishounen_frame.bgm 1
	./tools/palette_selector.py -f ./img/bishounen/slide04/bishounen_frame.bgm 1
	# Map Compression
	@for i in ./img/maps/*/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/focusmap/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/controls/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/nina/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/textbox/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/title/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/ending/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/warning/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@for i in ./img/bishounen/*/*.bgm; do ./tools/bgmap_compress.py -f $$i; done
	@truncate -s 512 ./img/font/font_map.bgm
