CC      ?= gcc
CFLAGS  ?= -O2 -Wall -Wextra -std=c11
PKGS     = glfw3 glu
CPPFLAGS += $(shell pkg-config --cflags $(PKGS))
LDLIBS   += $(shell pkg-config --libs $(PKGS)) -lGL -lm

TARGET = vectorgons
SRC    = vectorgons.c

$(TARGET): $(SRC)
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $(SRC) $(LDLIBS)

run: $(TARGET)
	./$(TARGET)

# --- Windows x64 cross-compile (MinGW-w64) ---------------------------------
# Produces a self-contained vectorgons.exe (statically links GLFW and the
# GCC/threads runtime; at run time it needs only Windows system DLLs:
# opengl32, glu32, gdi32, user32, shell32, kernel32). Override WINCC / GLFWDIR
# for your toolchain and GLFW (https://www.glfw.org/download, WIN64 binaries):
#   make windows WINCC=x86_64-w64-mingw32-gcc GLFWDIR=/path/to/glfw-3.4.bin.WIN64
WINCC    ?= x86_64-w64-mingw32-gcc
GLFWDIR  ?= /tmp/glfw-3.4.bin.WIN64
WINFLAGS  = -O2 -std=c11 -Wall -Wextra -mwindows -static -s
WINLIBS   = -lglfw3 -lglu32 -lopengl32 -lgdi32 -luser32 -lshell32

windows: $(SRC)
	$(WINCC) $(WINFLAGS) -I$(GLFWDIR)/include -o $(TARGET).exe $(SRC) \
	  -L$(GLFWDIR)/lib-mingw-w64 $(WINLIBS)

# Windows screensaver: same code with -DSCREENSAVER, output named .scr.
# Install by right-clicking the .scr in Windows -> Install (or drop it in
# C:\Windows\System32 and pick it in Settings > Lock screen > Screen saver).
screensaver: $(SRC)
	$(WINCC) $(WINFLAGS) -DSCREENSAVER -I$(GLFWDIR)/include -o $(TARGET).scr $(SRC) \
	  -L$(GLFWDIR)/lib-mingw-w64 $(WINLIBS)

# --- WebAssembly / browser build (Emscripten) ------------------------------
# Produces vectorgons.js + vectorgons.wasm (load from an HTML page; see
# web/index.html). Legacy-GL emulation maps the fixed-function + immediate-mode
# rendering onto WebGL; GLFW3 is emulated; the VBO/FBO entry points are resolved
# at run time, so GL_ENABLE_GET_PROC_ADDRESS is required. Run:
#   source /path/to/emsdk/emsdk_env.sh && make web
# Must be served over HTTP (WebAssembly will not load from file://).
EMCC     ?= emcc
EMFLAGS   = -O2 -std=c11 -sUSE_GLFW=3 -sLEGACY_GL_EMULATION=1 -sGL_UNSAFE_OPTS=0 \
            -sGL_ENABLE_GET_PROC_ADDRESS=1 -sALLOW_MEMORY_GROWTH=1 \
            -sINITIAL_MEMORY=67108864 -sEXIT_RUNTIME=0

web: web/vectorgons.js

web/vectorgons.js: $(SRC)
	mkdir -p web
	$(EMCC) $(EMFLAGS) $(SRC) -o web/vectorgons.js

clean:
	rm -f $(TARGET) $(TARGET).exe $(TARGET).scr web/vectorgons.js web/vectorgons.wasm

.PHONY: run windows screensaver web clean

# Commodore removed because it was stupid.
