-CC = gcc
-CPP = $(CC) -E
-# CC = cc # for HP-UX and others
-
-GAME_DIR = -DGAME_DIR=\".\" # path of the game and its data
-# JOYSTICK = -DNO_JOYSTICK # no joystick
-# SOUNDS = -DNO_SOUNDS # no sounds
-# SCORE_ENTRIES = -DONE_PER_NAME # only one score entry per name
-SCORE_ENTRIES = -DMANY_PER_NAME # many score entries per name
-
-# The XPM-Library is no longer needed to build this program,
-# but is used to load graphics if XPM_INCLUDE_FILE is defined,
-# because the GIF loading routines are still a bit beta.
-# If you use the Xpm library, convert the GIF files to Xpm
-# files (and the mask files ('*Maske.gif') to xbm files).
-XPM_INCLUDE_FILE = -DXPM_INCLUDE_FILE="<X11/xpm.h>"
-
-CONFIG = $(GAME_DIR) $(SOUNDS) $(JOYSTICK) \
- $(SCORE_ENTRIES) $(XPM_INCLUDE_FILE)
-
-# DEBUG = -DDEBUG -g -ansi -pedantic -Wall
-DEBUG = -DDEBUG -g -Wall
-# DEBUG = -O6
-
-# SYSTEM = -Aa -D_HPUX_SOURCE -Dhpux # for HP-UX (obsolete)
-# SYSTEM = -DSYSV -Ae # for HP-UX
-# INCL = -I/usr/include/X11R5 # for HP-UX and others
-# INCL = -I/usr/local/X11/include # for SunOS and others
-# LIBS = -L/usr/lib/X11R5 -lXpm -lX11 -lm
-# # for HP-UX and others
-# LIBS = -L/usr/local/X11/lib -lXpm -lX11 -lm -lsocket -R/usr/local/X11/lib
-# # for SunOS and others
-
-LIBS = -L/usr/X11R6/lib -lXpm -lX11 -lm
-# LIBS = -L/usr/X11R6/lib -lX11 -lm
-
-# CFLAGS = -O2 $(CONFIG) $(SYSTEM)
-CFLAGS = $(DEBUG) $(CONFIG) $(SYSTEM) $(INCL)
+
+CONVERT = convert
+WINDRES = windres
+
+CONVERT_ICON_ARGS = -transparent black -background transparent
+
+DEBUGGER = gdb -batch -ex "run" -ex "bt"
+
+PROGBASE = rocksndiamonds
+PROGNAME = ../$(PROGBASE)
+
+
+# -----------------------------------------------------------------------------
+# configuring platform
+# -----------------------------------------------------------------------------
+
+ifeq ($(PLATFORM),unix) # compiling on Unix/Linux (generic)
+PROFILING_FLAGS = -pg
+endif
+
+ifeq ($(PLATFORM_BASE),cross-win) # cross-compiling to Windows
+PROGNAME = ../$(PROGBASE).exe
+EXTRA_LDFLAGS = -lshfolder -lwsock32
+endif
+
+ifeq ($(PLATFORM),emscripten) # compiling with Emscripten
+PROGNAME = ../$(PROGBASE).js
+DATA_FILE = $(PROGBASE).data
+CC = emcc
+AR = emar
+RANLIB = emranlib
+STRIP = true
+FILE_PACKAGER = file_packager
+endif
+
+ifeq ($(shell uname -s),Darwin) # compiling on Mac OS X
+DEBUGGER = lldb --batch -o "run" -k "bt" -k "quit"
+SANITIZING_FLAGS = -fsanitize=undefined
+ifdef BUILD_DIST # distribution build
+MAC_TARGET_VERSION_MIN = 10.7
+EXTRA_FLAGS_MAC = -mmacosx-version-min=$(MAC_TARGET_VERSION_MIN)
+EXTRA_CFLAGS = $(EXTRA_FLAGS_MAC)
+EXTRA_LDFLAGS = $(EXTRA_FLAGS_MAC)
+MACOSX_DEPLOYMENT_TARGET = $MAC_TARGET_VERSION_MIN
+endif
+endif
+
+ifeq ($(shell uname -s),OS/2) # compiling on OS/2
+PROGNAME = ../$(PROGBASE).exe
+EXTRA_LDFLAGS = -Zomf -Zbin-files -Zmap -lcx -Zhigh-mem
+endif
+
+
+# -----------------------------------------------------------------------------
+# configuring target
+# -----------------------------------------------------------------------------
+
+ifndef TARGET # auto-detect compiling for SDL2
+ SDL_VERSION := $(shell sdl2-config --version 2> /dev/null)
+ ifdef SDL_VERSION
+ TARGET = sdl2
+ else
+ $(error SDL2 library not found)
+ endif
+endif
+
+# $(info Using SDL version $(SDL_VERSION) [TARGET == $(TARGET)])
+
+ifeq ($(TARGET),sdl2) # compiling for SDL2 target
+ifeq ($(PLATFORM),emscripten)
+SDL_LIBS = -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s USE_SDL_NET=2 -s USE_MODPLUG=1 -s USE_MPG123=1 -s USE_ZLIB=1
+SDL_FMTS = -s SDL2_IMAGE_FORMATS='["bmp","png","pcx","xpm"]' -s SDL2_MIXER_FORMATS='["mod","mp3"]'
+EXTRA_CFLAGS = $(SDL_LIBS)
+EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=81920000 -s ALLOW_MEMORY_GROWTH=1 -s FORCE_FILESYSTEM -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2 -lidbfs.js
+DATA_DIRS = conf docs levels graphics sounds music
+FILE_PACKAGER_ARGS = --preload $(DATA_DIRS) --js-output=$(DATA_FILE).js
+else
+SDL_LIBS = -lSDL2_image -lSDL2_mixer -lSDL2_net
+endif
+
+SYS_CFLAGS := -DTARGET_SDL2 $(shell sdl2-config --cflags)
+SYS_LDFLAGS := $(SDL_LIBS) $(shell sdl2-config --libs) -lm -lz
+endif
+
+
+# -----------------------------------------------------------------------------
+# configuring compile-time definitions
+# -----------------------------------------------------------------------------
+
+ifdef BASE_PATH # path to read-only game data
+CONFIG_BASE_PATH = -DBASE_PATH="\"$(BASE_PATH)\""
+endif
+
+CONFIG = $(CONFIG_BASE_PATH) $(JOYSTICK)
+
+DEBUG = -DDEBUG -g
+
+# ANALYZE = $(PROFILING_FLAGS)
+# ANALYZE = $(SANITIZING_FLAGS)
+
+# OPTIONS = $(DEBUG) -Wall # only for debugging purposes
+# OPTIONS = $(DEBUG) -O2 -Wall # only for debugging purposes
+# OPTIONS = $(DEBUG) -Wall # only for debugging purposes
+OPTIONS = $(DEBUG) -Wall -Wstrict-prototypes -Wmissing-prototypes
+# OPTIONS = $(DEBUG) -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes
+# OPTIONS = $(DEBUG) -Wall -ansi -pedantic # only for debugging purposes
+# OPTIONS = -O2 -Wall -ansi -pedantic
+# OPTIONS = -O2 -Wall
+# OPTIONS = -O2
+
+ifdef BUILD_TEST # test build
+OPTIONS := $(OPTIONS) -DTESTING
+endif
+
+ifdef BUILD_DIST # distribution build
+SYS_LDFLAGS := $(shell echo $(SYS_LDFLAGS) | \
+ sed -e "s%-rpath,[^ ]*%-rpath,'\$$ORIGIN/lib'%")
+OPTIONS = -O2 -Wall
+endif
+
+CFLAGS = $(OPTIONS) $(ANALYZE) $(SYS_CFLAGS) $(EXTRA_CFLAGS) $(CONFIG)
+LDFLAGS = $(ANALYZE) $(SYS_LDFLAGS) $(EXTRA_LDFLAGS)
+
+# !!! DEPENDENCY TO GLIB SHOULD BE REMOVED AGAIN !!!
+GLIB_CFLAGS := $(shell pkg-config --cflags glib-2.0)
+GLIB_LDFLAGS := $(shell pkg-config --libs glib-2.0)
+
+CFLAGS := $(CFLAGS) $(GLIB_CFLAGS)
+LDFLAGS := $(LDFLAGS) $(GLIB_LDFLAGS)
+# !!! DEPENDENCY TO GLIB SHOULD BE REMOVED AGAIN !!!
+