# SDL3 emulator for T-Deck example sketches (run the app on your PC)
#
#   make                          build the MissileCommand emulator (default)
#   make APP=CrtTerminal          build the CRT terminal emulator
#   make run [APP=...]            build + launch the window
#   make smoke [APP=...]          headless test: N frames, dump the last frame
#   make clean [APP=...]          remove the binary + smoke frame
#
# The sketch is included by src/main.cpp as a single translation unit:
#   -DAPP_INO='"<App>.ino"'. Apps with a sound_engine.cpp get it linked
#   automatically (wildcard).

APP       ?= MissileCommand
APP_DIR   := ../examples/$(APP)

ifeq ($(APP),MissileCommand)
APP_TITLE := Missile Command
DEFS      += -DEMU_APP_MISSILECOMMAND
endif
ifeq ($(APP),CrtTerminal)
APP_TITLE := CRT Terminal
endif
ifeq ($(APP),NetDeck)
APP_TITLE := NetDeck // Breach & Board
# the sketch moved to src/netdeck (next to nd_layout.h); its hardware
# shim header utilities.h stays with the pio project in examples/NetDeck
APP_DIR   := ../src/netdeck
APP_EXTRA_INC := ../examples/NetDeck
endif
ifeq ($(APP),Lotus3)
APP_TITLE := Lotus 3
DEFS      += -DEMU_APP_LOTUS3
ifdef AUTOPLAY
DEFS      += -DAUTOPLAY
endif
endif
APP_TITLE ?= $(APP)

CXX      ?= g++
CXXFLAGS ?= -std=gnu++17 -O2 -Wall -Wextra
SDLFLAGS := $(shell pkg-config --cflags sdl3 sdl3-ttf)
INC      := -Isrc/shim -I$(APP_DIR) $(if $(APP_EXTRA_INC),-I$(APP_EXTRA_INC))
LIBS     := $(shell pkg-config --libs sdl3 sdl3-ttf)

DEFS += -DAPP_INO='"$(APP).ino"' -DAPP_NAME='"$(APP_TITLE)"'

SHIMS     := $(wildcard src/shim/*.h) $(wildcard src/shim/driver/*.h)
SKETCH_SRCS := $(APP_DIR)/$(APP).ino $(wildcard $(APP_DIR)/*.h)
SOUND_SRC := $(wildcard $(APP_DIR)/sound_engine.cpp)
EMU       := $(APP)-emu

# Headless test binaries (tests/): same single-TU sketch inclusion as
# main.cpp, no window. Unit+integration share one fast binary; E2E is a
# separate build driving the real loop() through the device input pipeline.
TESTS     := $(wildcard tests/*.h tests/*.cpp tests/e2e/*.cpp)
TESTS_INC := $(INC) -Itests

.PHONY: all run smoke test test-unit test-e2e clean

all: $(EMU)

$(EMU): src/main.cpp $(SOUND_SRC) $(APP_DIR)/$(APP).ino $(SHIMS)
	$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(INC) $(DEFS) \
		src/main.cpp $(SOUND_SRC) -o $@ $(LIBS)

run: $(EMU)
	./$(EMU)

# Headless smoke test: dummy video/audio, run N frames, save the last frame.
# Feed keys via --type "text" (queued a few chars per frame).
smoke: $(EMU)
	./$(EMU) --frames 240 --dump smoke_frame.bmp

# --- tests -----------------------------------------------------------------
# fast layer: unit + integration (deterministic, sim stepped directly)
$(APP)-tests: tests/test_main.cpp tests/shim_globals.cpp tests/harness.h \
              tests/sim_helpers.h tests/unit_tests.h tests/integration_tests.h \
              $(SHIMS) $(SKETCH_SRCS)
	$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(TESTS_INC) $(DEFS) \
		tests/shim_globals.cpp tests/test_main.cpp -o $@ $(LIBS)

# slow layer: E2E journeys through the real loop() + input pipeline (own build)
$(APP)-e2e: tests/e2e_tests.cpp tests/shim_globals.cpp tests/harness.h \
            tests/sim_helpers.h $(SHIMS) $(SKETCH_SRCS)
	$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(TESTS_INC) $(DEFS) \
		tests/shim_globals.cpp tests/e2e_tests.cpp -o $@ $(LIBS)

test-unit: $(APP)-tests
	./$(APP)-tests unit

test: test-unit
	./$(APP)-tests integration
test-e2e: $(APP)-e2e
	SDL_VIDEODRIVER=dummy ./$(APP)-e2e

clean:
	rm -f $(EMU) $(APP)-tests $(APP)-e2e smoke_frame.bmp