CC ?= cc
CFLAGS ?= -O2 -std=c99 -Wall -Wextra -Wpedantic
SDL_CFLAGS ?= $(shell pkg-config --cflags sdl3)
SDL_LIBS ?= $(shell pkg-config --libs sdl3)
all: stonewell-blocks
stonewell-blocks: src/icon_data.h src/main.c src/game.c src/game.h src/scores.c src/scores.h src/arcade.c src/arcade.h src/music.c src/music.h src/audio.c src/audio.h src/font.c src/font.h src/font_atlas.h src/controller.c src/controller.h src/well_data.c src/settings.c src/settings.h src/well_data.h
	$(CC) $(CFLAGS) $(SDL_CFLAGS) src/main.c src/game.c src/scores.c src/arcade.c src/music.c src/audio.c src/font.c src/controller.c src/well_data.c src/settings.c -o $@ $(SDL_LIBS) -lm
test: tests/test-game tests/test-scores tests/test-music tests/test-controller tests/test-ui
	./tests/test-game
	./tests/test-scores
	./tests/test-music
	./tests/test-controller
	./tests/test-ui
tests/test-game: tests/test_game.c src/game.c src/game.h
	$(CC) $(CFLAGS) tests/test_game.c src/game.c -o $@
tests/test-scores: tests/test_scores.c src/scores.c src/scores.h src/arcade.c src/arcade.h
	$(CC) $(CFLAGS) tests/test_scores.c src/scores.c src/arcade.c -o $@
tests/test-music: tests/test_music.c src/music.c src/music.h
	$(CC) $(CFLAGS) tests/test_music.c src/music.c -lm -o $@
tests/test-controller: tests/test_controller.c src/controller.c src/controller.h src/settings.c src/settings.h
	$(CC) $(CFLAGS) $(SDL_CFLAGS) tests/test_controller.c src/controller.c src/settings.c -o $@ $(SDL_LIBS)
tests/test-ui: tests/test_ui.c $(wildcard src/*.c) $(wildcard src/*.h)
	$(CC) $(CFLAGS) $(SDL_CFLAGS) tests/test_ui.c src/game.c src/scores.c src/arcade.c src/music.c src/audio.c src/font.c src/controller.c src/well_data.c src/settings.c -o $@ $(SDL_LIBS) -lm
clean:
	rm -f stonewell-blocks tests/test-game tests/test-scores tests/test-music tests/test-controller tests/test-ui
.PHONY: all test clean

PREFIX ?= /usr/local
install: stonewell-blocks
	install -Dm755 stonewell-blocks "$(DESTDIR)$(PREFIX)/bin/stonewell-blocks"
	install -Dm644 packaging/stonewell-blocks.desktop "$(DESTDIR)$(PREFIX)/share/applications/stonewell-blocks.desktop"
	install -Dm644 assets/stonewell-blocks.svg "$(DESTDIR)$(PREFIX)/share/icons/hicolor/scalable/apps/stonewell-blocks.svg"
	install -Dm644 assets/stonewell-blocks.png "$(DESTDIR)$(PREFIX)/share/icons/hicolor/256x256/apps/stonewell-blocks.png"
uninstall:
	rm -f "$(DESTDIR)$(PREFIX)/bin/stonewell-blocks" "$(DESTDIR)$(PREFIX)/share/applications/stonewell-blocks.desktop" "$(DESTDIR)$(PREFIX)/share/icons/hicolor/scalable/apps/stonewell-blocks.svg" "$(DESTDIR)$(PREFIX)/share/icons/hicolor/256x256/apps/stonewell-blocks.png"
windows:
	./windows/build-mingw.sh
installer:
	./windows/build-installer.sh
.PHONY: install uninstall windows installer
