summaryrefslogtreecommitdiff
path: root/Makefile
blob: a5f59629bbe1a963d587b10b6cd0bd7205df9a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I. -I/usr/local/lib
LIBS := -L/usr/local/lib -lsfml-{system,window,graphics}
BIN := game

BIN2C := $(shell command -v bin2c 2>/dev/null)
ifeq ($(BIN2C),)
	BIN2C := xxd -i
endif

object_src := $(wildcard src/objects/*.cpp)
object_obj := $(patsubst src/objects/%.cpp,build/objects/%.o,$(object_src))
object_head := $(patsubst src/objects/%.cpp,build/objects/%.h,$(object_src))
object_xlist := $(shell echo $$(for f in $(object_src); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/X(&)/'; done))

sprite_allfiles := $(wildcard src/sprites/*)
sprite_fnames := $(filter %.png,$(sprite_allfiles))
sprite_skipped := $(filter-out %.png,$(sprite_allfiles))
sprite_obj := $(patsubst src/sprites/%.png,build/sprites/%.o,$(sprite_fnames))
sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Sprite *&;/'; done))

base_src := $(wildcard *_base.cpp)
base_src += library.cpp sprite.cpp main.cpp
base_obj := $(patsubst %.cpp,build/%.o,$(base_src))

.PHONY: all clean remake run

#keep all intermediate files!
.SECONDARY:

#we handle our own printing
.SILENT:

all: $(BIN)

clean:
	rm -rf build $(BIN)

remake: clean all

run: all
	printf "\x1B[1mRUNNING...\x1B[0m\n"
	./$(BIN)


$(BIN): $(base_obj) $(object_obj) $(sprite_obj)
	echo Linking
	$(CXX) $(CXXFLAGS) $(LIBS) -o $(BIN) $^

build/main.o: main.cpp build/objects.h build/sprites.h
	echo Building main
	mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -c -o "$@" "$<" "-DOBJECT_CLASSES_XLIST=$(object_xlist)" "-DGAME_NAME=\"$(BIN)\""

build/objects/%.o: build/objects/%.cpp build/objects.h build/sprites.h
	echo Building $*
	mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -c -o "$@" "$<"

build/objects/%.cpp: src/objects/%.cpp
	echo Generating source for $*
	mkdir -p $(dir $@)
	$(CXX) -E object_wrapper.cpp "-D__OBJECT_FNAME__=\"$<\"" "-D__OBJECT_NAME__=$(basename $(notdir $<))" | sed 's/^# [0-9]\+ ".*//' >"$@"

build/objects/%.h: src/objects/%.cpp
	echo Generating header for $*
	mkdir -p $(dir $@)
	$(CXX) -E object_header_maker.cpp "-D__OBJECT_NAME__=$(basename $(notdir $<))" "-DEVENT_XLIST=$$($(CXX) -E object_header_extractor.cpp "-D__OBJECT_FNAME__=\"$<\"" | sed 's/__________EVENT_NEWLINE__________/\n/g' | grep -oe '__________EVENT_COUNTER_TAG_[^_]*__________' | sed 's/[^G]*G_\(.*\)__________.*/X(\1)/g' | xargs)" | sed 's/^# [0-9]\+ ".*//' >"$@"

build/objects.h: $(object_head)
	echo Collecting header files
	echo $(object_head) | xargs -n 1 basename | sed 's,.*,#include "objects/&",' >"$@"

build/%.o: %.cpp
	echo Compiling $*
	mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -c -o "$@" $^


build/sprites/%.o: build/sprites/%.cpp
	echo Building sprite $*
	mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -Wno-unused-const-variable -c -o "$@" "$<"

build/sprites/%.cpp: src/sprites/%.png
	echo Generating for sprite $*
	mkdir -p $(dir $@)
	printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s, 0\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@"

build/sprites.h:
	echo Collecting generated sprite files
	mkdir -p $(dir $@)
	printf "#include \"sprite.h\"\n\n$(sprite_externs)\n" >"$@"
	test -z "$(sprite_skipped)" || printf "\x1B[1mWARNING: skipped files in src/sprites: $(notdir $(sprite_skipped))\x1B[0m\n"