summaryrefslogtreecommitdiff
path: root/Makefile
blob: b0f3b8323fde2cfab75c33665fe7ee2b4336284b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -fwrapv -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))

font_allfiles := $(wildcard src/fonts/*)
font_fnames := $(filter %.ttf,$(font_allfiles))
font_skipped := $(filter-out %.ttf %/LICENSE,$(font_allfiles))
font_obj := $(patsubst src/fonts/%.ttf,build/fonts/%.o,$(font_fnames))
font_externs := $(shell echo $$(for f in $(font_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Font *&;/'; done))

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

categories := object sprite font
category_headers := $(foreach c,$(categories),build/$(c)s.h)

all_objects := $(base_obj) $(foreach c,$(categories),$($(c)_obj))

.PHONY: all clean remake run

#keep all intermediate files!
.SECONDARY:

#we handle our own printing
.SILENT:

all: $(BIN)

clean:
	echo Cleaning
	rm -rf build $(BIN)

remake: clean all

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

kaas:
	echo $(all_objects)


$(BIN): $(all_objects)
	echo Linking
	$(CXX) $(CXXFLAGS) -o $(BIN) $^ $(LIBS)


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


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


build/objects/%.o: build/objects/%.cpp $(category_headers)
	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/sprites/%.o: build/sprites/%.cpp
	echo Building sprite $*
	mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) -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\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@"

build/sprites.h: $(sprite_fnames)
	echo Writing collected sprites header
	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"


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

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

build/fonts.h: $(font_fnames)
	echo Writing collected fonts header
	mkdir -p $(dir $@)
	printf "#include \"font.h\"\n\n$(font_externs)\n" >"$@"
	test -z "$(font_skipped)" || printf "\x1B[1mWARNING: skipped files in src/fonts: $(notdir $(font_skipped))\x1B[0m\n"