# Build Makefile for Microsoft NMake
!ifdef OVERRIDES
!include $(OVERRIDES)
!endif

SRC=src
GAME=kenbuild
TOOLS=tools
INC=include
LIBSQUISH=libsquish
!ifndef CFLAGS
CFLAGS=/DSUPERBUILD /DPOLYMOST /DUSE_OPENGL /DDYNAMIC_OPENGL /DKSFORBUILD
!endif

o=obj
res=res
asm=masm

ENGINELIB=engine.lib
EDITORLIB=build.lib

DXROOT=$(USERPROFILE)\sdks\directx\dx81
FMODROOT=$(USERPROFILE)\sdks\fmodapi374win\api

# /D these to enable certain features of the port's compile process
# NOASM     When defined, uses C instead of assembly code
TARGETOPTS=#/DNOASM

!ifdef DEBUG
# debugging options
flags_cl=/G6 /Ot /Z7
flags_link=/DEBUG
!else
# release options
#flags_cl=/G6Fy /Ox
flags_cl=/G6 /O2
flags_link=/RELEASE
!endif

CC=cl
AS=ml
RC=rc
LINK=link /opt:nowin98 /opt:ref /nologo
CFLAGS=$(CFLAGS) /nologo /MD /J $(flags_cl) $(TARGETOPTS) /I$(INC)
ENGINECFLAGS="/I$(DXROOT)\include" /I$(LIBSQUISH)
GAMECFLAGS=/I$(GAME) "/I$(FMODROOT)\inc"
LIBS=fmodvc.lib #opengl32.lib
ASFLAGS=/nologo /coff
EXESUFFIX=.exe

ENGINEOBJS=$(SRC)\a.$o \
	$(SRC)\baselayer.$o \
	$(SRC)\cache1d.$o \
	$(SRC)\compat.$o \
	$(SRC)\crc32.$o \
	$(SRC)\defs.$o \
	$(SRC)\engine.$o \
	$(SRC)\polymost.$o \
	$(SRC)\polymosttex.$o \
	$(SRC)\polymosttex-squish.$o \
	$(SRC)\polymosttexcache.$o \
	$(SRC)\hightile.$o \
	$(SRC)\mdsprite.$o \
	$(SRC)\glbuild.$o \
	$(SRC)\kplib.$o \
	$(SRC)\lzf_c.$o \
	$(SRC)\lzf_d.$o \
	$(SRC)\md4.$o \
	$(SRC)\mmulti.$o \
	$(SRC)\osd.$o \
	$(SRC)\pragmas.$o \
	$(SRC)\scriptfile.$o \
	$(SRC)\textfont.$o \
	$(SRC)\smalltextfont.$o \
	$(SRC)\winlayer.$o

LIBSQUISHOBJS=$(LIBSQUISH)\alpha.$o $(LIBSQUISH)\clusterfit.$o \
	      $(LIBSQUISH)\colourblock.$o $(LIBSQUISH)\colourfit.$o \
	      $(LIBSQUISH)\colourset.$o $(LIBSQUISH)\maths.$o \
	      $(LIBSQUISH)\rangefit.$o $(LIBSQUISH)\singlecolourfit.$o \
	      $(LIBSQUISH)\squish.$o $(LIBSQUISH)\msvc.$o

EDITOROBJS=$(SRC)\build.$o \
	$(SRC)\startwin.editor.$o \
	$(SRC)\config.$o

GAMEEXEOBJS=$(GAME)\config.$o \
	$(GAME)\game.$o \
	$(GAME)\gameres.$(res) \
	$(GAME)\startwin.game.$o \
	$(GAME)\sound.$o \
	$(SRC)\$(ENGINELIB)

EDITOREXEOBJS=$(GAME)\bstub.$o \
	$(GAME)\buildres.$(res) \
	$(SRC)\$(EDITORLIB) \
	$(SRC)\$(ENGINELIB)
	
RENDERTYPE=WIN
LIBS=$(LIBS) user32.lib gdi32.lib shell32.lib dxguid.lib wsock32.lib comctl32.lib
CFLAGS=$(CFLAGS) /DRENDERTYPE$(RENDERTYPE)=1

# RULES
.SUFFIXES: .masm .cc

{$(SRC)}.masm{$(SRC)}.$o:
	$(AS) /c $(ASFLAGS) /Fo$@ $<

{$(SRC)}.c{$(SRC)}.$o:
	$(CC) /c $(CFLAGS) $(ENGINECFLAGS) /Fo$@ $<

{$(SRC)}.cc{$(SRC)}.$o:
	$(CC) /TP /c $(CFLAGS) $(ENGINECFLAGS) /Fo$@ $<

{$(SRC)}.cpp{$(SRC)}.$o:
	$(CC) /c $(CFLAGS) $(ENGINECFLAGS) /Fo$@ $<

{$(LIBSQUISH)}.cpp{$(LIBSQUISH)}.$o:
	$(CC) /TP /c $(CFLAGS) /I$(LIBSQUISH) /Fo$@ $<

{$(TOOLS)}.c{$(TOOLS)}.$o:
	$(CC) /c $(CFLAGS) /Fo$@ $<

{$(GAME)}.c{$(GAME)}.$o:
	$(CC) /c $(CFLAGS) $(GAMECFLAGS) /Fo$@ $<

{$(GAME)}.cpp{$(GAME)}.$o:
	$(CC) /c $(CFLAGS) $(GAMECFLAGS) /Fo$@ $<

{$(GAME)\rsrc}.rc{$(GAME)}.$(res):
	$(RC) /i$(INC) /i$(GAME) /fo$@ /r $<

# TARGETS
UTILS=kextract$(EXESUFFIX) kgroup$(EXESUFFIX) transpal$(EXESUFFIX) wad2map$(EXESUFFIX) wad2map$(EXESUFFIX)
 
all: game$(EXESUFFIX) build$(EXESUFFIX) ;
utils: $(UTILS) ;

enginelib: $(SRC)\$(ENGINELIB) ;
$(SRC)\$(ENGINELIB): $(ENGINEOBJS) $(LIBSQUISHOBJS)
	lib /out:$@ /nologo $**

editorlib: $(SRC)\$(EDITORLIB) ;
$(SRC)\$(EDITORLIB): $(EDITOROBJS)
	lib /out:$@ /nologo $**


game$(EXESUFFIX): $(GAMEEXEOBJS)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS /LIBPATH:$(DXROOT)\lib /LIBPATH:$(FMODROOT)\lib $(flags_link) /MAP $** $(LIBS) msvcrt.lib
	
build$(EXESUFFIX): $(EDITOREXEOBJS)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS /LIBPATH:$(DXROOT)\lib /LIBPATH:$(FMODROOT)\lib $(flags_link) /MAP $** $(LIBS) msvcrt.lib

# the tools
kextract$(EXESUFFIX): $(TOOLS)\kextract.$o $(SRC)\compat.$o
	$(LINK) /OUT:$@ /SUBSYSTEM:CONSOLE $(flags_link) /MAP $** msvcrt.lib

kgroup$(EXESUFFIX): $(TOOLS)\kgroup.$o $(SRC)\compat.$o
	$(LINK) /OUT:$@ /SUBSYSTEM:CONSOLE $(flags_link) /MAP $** msvcrt.lib

transpal$(EXESUFFIX): $(TOOLS)\transpal.$o $(SRC)\pragmas.$o $(SRC)\compat.$o
	$(LINK) /OUT:$@ /SUBSYSTEM:CONSOLE $(flags_link) /MAP $** msvcrt.lib

wad2map$(EXESUFFIX): $(TOOLS)\wad2map.$o $(SRC)\pragmas.$o $(SRC)\compat.$o
	$(LINK) /OUT:$@ /SUBSYSTEM:CONSOLE $(flags_link) /MAP $** msvcrt.lib

wad2art$(EXESUFFIX): $(TOOLS)\wad2art.$o $(SRC)\pragmas.$o $(SRC)\compat.$o
	$(LINK) /OUT:$@ /SUBSYSTEM:CONSOLE $(flags_link) /MAP $** msvcrt.lib

# DEPENDENCIES
!include Makefile.deps

# PHONIES	
clean:
	-del /q $(ENGINEOBJS) $(LIBSQUISHOBJS) $(EDITOROBJS) $(GAMEEXEOBJS) $(EDITOREXEOBJS)
veryclean: clean
	-del /q $(SRC)\$(ENGINELIB) $(SRC)\$(EDITORLIB) game$(EXESUFFIX) build$(EXESUFFIX) $(UTILS)

