# Makefile for the FloorPlan3D demo — emulator-template base + raylib-esp32.
#
# Builds raylib 6.x (from lib/raylib-esp32's raylib submodule) with the rlsw
# SOFTWARE renderer behind a custom platform layer (rcore_emu.c) that blits
# the RGB565 framebuffer into the SDL host — the same library, renderer and
# SwapScreenBuffer flow that runs on the T-Deck's ESP32-S3.
#
# Build & run: make run          (from this directory)
# Smoke test:  make smoke

APP       ?= FloorPlan
APP_DIR   := examples/$(APP)

# raylib-esp32 sources (raylib core + software renderer)
RAYLIB    := ../lib/raylib-esp32/raylib

# SDL3 flags via pkg-config
SDLFLAGS := $(shell pkg-config --cflags sdl3 sdl3-ttf)
LIBS     := $(shell pkg-config --libs sdl3 sdl3-ttf)

CXX      ?= g++
CC       ?= cc
CXXFLAGS ?= -std=gnu++17 -O2 -Wall -Wextra
CFLAGS   := -std=gnu11 -O2 \
            -DGRAPHICS_API_OPENGL_SOFTWARE -DPLATFORM_CUSTOM -DEXTERNAL_CONFIG_FLAGS \
            -include raylib_emu_config.h \
            -Wno-incompatible-pointer-types -Wno-uninitialized -Wno-nonnull \
            -Wno-return-type -Wno-unused-variable -Wno-unused-function \
            -Wno-unused-label -Wno-stringop-truncation

# Include current dir for shim.h/rcore_emu.c, the sketch dir, raylib, NetDeck
INC      := -I . -I $(APP_DIR) -I $(RAYLIB)/src -I ../src/netdeck

# Preprocessor definitions: app name, .ino filename, and the sketch source
# path. main.cpp does `#include APP_SRC`, so switching APP is enough.
DEFS     += -DAPP_INO='"$(APP).ino"' -DAPP_NAME='"$(APP)"' \
            -DAPP_SRC='"$(APP_DIR)/$(APP).cpp"'

SHIMS    := $(wildcard *.h)

# Main entry point for this template (standalone, no HW deps)
EMU_MAIN := src/main.cpp

EMU       := $(APP)-emu

# --- raylib core translation units (same set as raylib-esp32 builds) -------
RL_SRCS   := rcore.c rshapes.c rtextures.c rtext.c rmodels.c
RL_SRCDIR := $(RAYLIB)/src
RLOBJS    := $(addprefix build/,$(RL_SRCS:.c=.o)) build/rcore_emu.o

all: $(EMU)

build/%.o: $(RL_SRCDIR)/%.c
	@mkdir -p build
	$(CC) $(CFLAGS) $(INC) -c $< -o $@

build/rcore_emu.o: rcore_emu.c $(SHIMS)
	@mkdir -p build
	$(CC) $(CFLAGS) $(INC) -c $< -o $@

# Compile + link: main.cpp includes the sketch .cpp, forming one TU.
$(EMU): $(EMU_MAIN) $(SHIMS) $(APP_DIR)/$(APP).cpp $(RLOBJS)
	$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(INC) $(DEFS) $(EMU_MAIN) $(RLOBJS) -o $@ $(LIBS)

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

smoke: $(EMU)
	./$(EMU) --frames 240 --dump smoke_frame.bmp

clean:
	rm -rf build $(EMU)

.PHONY: all run smoke clean
