
CC = gcc
CXX = g++
CFLAGS = -Wall -W -ansi -pedantic -Wshadow -Wno-long-long -O2
LDFLAGS = -s
SJASM = ./bin/sjasm
EPCOMPRESS = ./bin/epcompress
EPIMGCONV = ./bin/epimgconv
MIDICONV = ./bin/midiconv_linux64
DAVECONV = ./bin/daveconv_linux64
MPLAYER = mplayer

CHRSET_FIXED = 60
FRAMES_PER_BLOCK = 7
FRAME_RATE = 5

all: badapple.com badappld.com badappl4.com videochr.m4 vid2dchr.m4 vid4_chr.m4

chrconv: chrconv.cpp
	$(CXX) $(CFLAGS) -DCHRSET_FIXED=$(CHRSET_FIXED)		\
		-DFRAMES_PER_BLOCK=$(FRAMES_PER_BLOCK) $< -o $@ $(LDFLAGS)

videochr.bin vid2dchr.bin vid4_chr.bin: %chr.bin: %dat.bin chrconv
	./chrconv < $< > $@

videochr.m4 vid2dchr.m4 vid4_chr.m4: %.m4: %.bin
	rm -f $@.tmp1 $@.tmp2 $@
	dd if=$< bs=8 count=$(CHRSET_FIXED) of=$@.tmp1
	dd if=$< bs=8 skip=$(CHRSET_FIXED) of=$@.tmp2
	$(EPCOMPRESS) -raw -m4 -X -blocksize 65536 $@.tmp1 $@.tmp1
	$(EPCOMPRESS) -raw -m4 -X -blocksize 8192 -maxoffs 32768 $@.tmp2 $@.tmp2
	cat $@.tmp1 $@.tmp2 > $@
	rm -f $@.tmp1 $@.tmp2

PNGFILES = $(shell seq -f "png/%08g.png" 2 2191)

pngfiles: $(PNGFILES)

$(PNGFILES): bad_apple.mkv
	-mkdir -p png
	$(MPLAYER) -nosound -fps 1000 -vo png:z=1:outdir=png -sws 2	\
		   -aspect 0.6667 -vf framestep=3,scale=256:384:::0.0:0.75 $<

pngclean:
	-rm -f png/*.png

PICFILES = $(subst png,pic,$(PNGFILES))
PICFILES_D = $(subst pic/,pic_d/,$(PICFILES))
PICFILES_4 = $(subst pic/,pic4/,$(PICFILES))

$(PICFILES): pic/%.pic: png/%.png
	$(EPIMGCONV) -outfmt 6 -mode 0 -size 16 192 -quality 9		\
		     -nointerp 1 -scalemode 1 -dither 0 0 -palres 0	\
		     -color0 0 -color1 255 $< $@

$(PICFILES_D): pic_d/%.pic: png/%.png
	$(EPIMGCONV) -outfmt 6 -mode 0 -size 16 192 -quality 9		\
		     -nointerp 1 -scalemode 1 -dither 2 0 -palres 0	\
		     -color0 0 -color1 255 $< $@

$(PICFILES_4): pic4/%.pic: png/%.png
	$(EPIMGCONV) -outfmt 6 -mode 1 -size 16 192 -quality 9		\
		     -nointerp 1 -scalemode 1 -dither 0 0 -palres 0	\
		     -color0 0 -color1 56 -color2 199 -color3 255 $< $@

picclean:
	-rm $(PICFILES) $(PICFILES_D) $(PICFILES_4)

videodat.bin: $(PICFILES)
	cat $(PICFILES) > $@

vid2ddat.bin: $(PICFILES_D)
	cat $(PICFILES_D) > $@

vid4_dat.bin: $(PICFILES_4)
	cat $(PICFILES_4) > $@

badapple.com: badapple.asm musicdat.m4
	$(SJASM) -DCHRSET_FIXED=$(CHRSET_FIXED)				\
		 -DFRAMES_PER_BLOCK=$(FRAMES_PER_BLOCK)			\
		 -DFRAME_RATE=$(FRAME_RATE) -DCOLOR4_MODE=0		\
		 -DVIDEOFILE_NAME=\"VIDEOCHR.M4\" $< $@

badappld.com: badapple.asm musicdat.m4
	$(SJASM) -DCHRSET_FIXED=$(CHRSET_FIXED)				\
		 -DFRAMES_PER_BLOCK=$(FRAMES_PER_BLOCK)			\
		 -DFRAME_RATE=$(FRAME_RATE) -DCOLOR4_MODE=0		\
		 -DVIDEOFILE_NAME=\"VID2DCHR.M4\" $< $@

badappl4.com: badapple.asm musicdat.m4
	$(SJASM) -DCHRSET_FIXED=$(CHRSET_FIXED)				\
		 -DFRAMES_PER_BLOCK=$(FRAMES_PER_BLOCK)			\
		 -DFRAME_RATE=$(FRAME_RATE) -DCOLOR4_MODE=1		\
		 -DVIDEOFILE_NAME=\"VID4_CHR.M4\" $< $@

musicdat.m4: musicdat.xr8
	$(EPCOMPRESS) -raw -m4 -X $< $@

musicdat.xr8: musicdat.dav
	$(DAVECONV) $@ $<:e4,n,f 0x0000

musicdat.dav: eleclove.mid envelope.txt
	$(MIDICONV) $< $@ envelope.txt -quant4 -render

package: all
	rm -f bad_apple.7z
	7za a -m0=lzma -mx=9 bad_apple.7z				\
		Makefile badapple.com badappld.com badappl4.com		\
		musicdat.m4 videochr.m4 vid2dchr.m4 vid4_chr.m4		\
		badapple.asm chrconv.cpp eleclove.mid envelope.txt	\
		ep128emu midiplay sjasmep bin musicdat.dav musicdat.xr8	\
		pic videodat.bin pic_d vid2ddat.bin pic4 vid4_dat.bin	\
		vid2dchr.bin vid4_chr.bin videochr.bin # bad_apple.mkv png

clean:
	-rm chrconv videochr.bin videochr.m4 vid2dchr.bin vid2dchr.m4
	-rm vid4_chr.bin vid4_chr.m4 *.tmp1 *.tmp2
	-rm videodat.bin vid2ddat.bin vid4_dat.bin
	-rm badapple.com badappld.com badappl4.com
	-rm *.lst *.out musicdat.dav musicdat.xr8 musicdat.m4

