# EDuke32 Makefile for Microsoft NMake
CPLUSPLUS=1
SRC=source
OBJ=$(SRC)\obj_win
EROOT=build
EINC=$(EROOT)\include
EOBJ=$(SRC)\eobj_win
INC=$(SRC)
RSRC=rsrc
o=obj

!ifndef WINBITS
WINBITS=32
!endif

WINLIB=\$(WINBITS)

!if ($(WINBITS)==32)
WINMACHINE=/MACHINE:X86
!elseif ($(WINBITS)==64)
WINMACHINE=/MACHINE:X64
!endif

JAUDIOLIBDIR=$(SRC)\jaudiolib
JAUDIOLIB=libjfaudiolib.lib

ENETDIR=$(SRC)\enet
ENETLIB=libenet.lib

ENGINELIB=engine.lib
EDITORLIB=build.lib

# the WDK allows us to link against msvcrt.dll instead of msvcrxxx.dll
# this path should match build\Makefile.msvc
# WDKROOT="H:\WinDDK\7600.16385.1"
PLATFORM=platform\Windows
AUDIOINC=source\jaudiolib\third-party\common
AUDIOPLATFORM=source\jaudiolib\third-party\Windows

!ifndef RENDERTYPE
RENDERTYPE=WIN
!endif
!ifndef MIXERTYPE
MIXERTYPE=WIN
!endif

!ifdef DEBUG
# debugging options
flags_cl=/Od /Zi
flags_link=/DEBUG
!else
# release options
flags_cl=/O2 /GL /MP # /I$(WDKROOT)\inc\crt /I$(WDKROOT)\inc\api
!if ($(WINBITS)!=64)
flags_cl=$(flags_cl) /arch:SSE
!endif
flags_link=/RELEASE /LTCG # /LIBPATH:$(WDKROOT)\lib\wxp\i386 /LIBPATH:$(WDKROOT)\lib\Crt\i386
!endif

ENGINEOPTS=/DUSE_OPENGL /DPOLYMER /DUSE_LIBPNG /I..\$(PLATFORM)\include

!ifdef CPLUSPLUS
ENGINEOPTS=$(ENGINEOPTS) /TP
!endif

!if ($(WINBITS)==64)
NOASM=1
!endif

CC=cl
AS=ml
LINK=link /nologo /opt:ref 
MT=mt
CFLAGS= /MT /J /nologo $(flags_cl)  \
	/I$(INC) /I$(EINC)\msvc /I$(EINC)\ /I$(SRC)\jmact /I$(JAUDIOLIBDIR)\include /I$(ENETDIR)\include \
 	/W2 $(ENGINEOPTS) \
 	/I$(PLATFORM)\include /I$(AUDIOINC)\include /DRENDERTYPE$(RENDERTYPE)=1 /DMIXERTYPE$(MIXERTYPE)=1 /DSDL_FRAMEWORK /DSDL_TARGET=2 /DUSE_LIBVPX
 	
LIBS=user32.lib gdi32.lib shell32.lib winmm.lib ws2_32.lib comctl32.lib shlwapi.lib oleaut32.lib ole32.lib imm32.lib version.lib \
     libFLAC.a libogg.a libvorbis.a libvorbisfile.a libvpx.a libpng_mini.a libz_mini.a libcompat-from-mingw-w64.a \
     dsound.lib advapi32.lib libcompat-to-msvc.a

!if ("$(RENDERTYPE)"=="SDL")
LIBS=libSDL2main.a libSDL2.a libSDL2_mixer.a $(LIBS)
!endif

LIBS=/NODEFAULTLIB:glu32.lib /NODEFAULTLIB:msvcrt.lib /NODEFAULTLIB:msvcrtd.lib /NODEFAULTLIB:libcmt.lib \
    /NODEFAULTLIB:libcmtd.lib $(LIBS)

# NOASM     When defined, uses C instead of assembly code
!ifdef NOASM
CFLAGS=$(CFLAGS) /DNOASM
!endif

ASFLAGS=/nologo /coff /c
EXESUFFIX=.exe
!ifdef DEBUG
CFLAGS=$(CFLAGS) /DDEBUGGINGAIDS /D "_CRT_SECURE_NO_DEPRECATE"
LIBS=$(LIBS)  msvcrtd.lib
!else
# comment msvcrt_winxp.obj if not using the WDK
LIBS=$(LIBS)  msvcrt.lib # msvcrt_winxp.obj
!endif

JMACTOBJ=$(OBJ)\file_lib.$o \
	$(OBJ)\control.$o \
	$(OBJ)\keyboard.$o \
	$(OBJ)\mouse.$o \
	$(OBJ)\joystick.$o \
	$(OBJ)\mathutil.$o \
	$(OBJ)\scriplib.$o
 
GAMEOBJS=$(OBJ)\game.$o \
	$(OBJ)\game_inline.$o \
	$(OBJ)\actors.$o \
	$(OBJ)\actors_inline.$o \
	$(OBJ)\anim.$o \
	$(OBJ)\animvpx.$o \
	$(OBJ)\common.$o \
	$(OBJ)\demo.$o \
	$(OBJ)\gamedef.$o \
	$(OBJ)\gameexec.$o \
	$(OBJ)\gamevars.$o \
	$(OBJ)\global.$o \
	$(OBJ)\input.$o \
	$(OBJ)\menus.$o \
	$(OBJ)\namesdyn.$o \
    $(OBJ)\net.$o \
	$(OBJ)\player.$o \
	$(OBJ)\premap.$o \
	$(OBJ)\savegame.$o \
	$(OBJ)\sector.$o \
	$(OBJ)\sector_inline.$o \
	$(OBJ)\rts.$o \
	$(OBJ)\config.$o \
	$(OBJ)\animlib.$o\
	$(OBJ)\osdfuncs.$o \
 	$(OBJ)\osdcmds.$o \
	$(OBJ)\grpscan.$o \
	$(OBJ)\winbits.$o \
	$(OBJ)\gameres.res \
	$(OBJ)\startwin.game.$o \
 	$(JMACTOBJ) \
	$(OBJ)\sounds.$o \
	$(OBJ)\soundsdyn.$o \
!ifdef DEBUG
	$(OBJ)\mdump.$o
!endif

EDITOROBJS=$(OBJ)\astub.$o \
	$(OBJ)\common.$o \
	$(OBJ)\mathutil.$o \
	$(OBJ)\m32def.$o \
	$(OBJ)\m32vars.$o \
	$(OBJ)\m32exec.$o \
	$(OBJ)\sounds_mapster32.$o \
	$(OBJ)\buildres.res \
!ifdef DEBUG
	$(OBJ)\mdump.$o
!endif

!if ("$(RENDERTYPE)"=="SDL")
GAMEOBJS=$(GAMEOBJS) $(OBJ)\game_icon.$o
EDITOROBJS=$(EDITOROBJS) $(OBJ)\build_icon.$o
!endif

!if ("$(MIXERTYPE)"=="WIN")
GAMEOBJS=$(GAMEOBJS) $(OBJ)\midi.$o $(OBJ)\music.$o $(OBJ)\mpu401.$o
!endif
!if ("$(MIXERTYPE)"=="SDL")
GAMEOBJS=$(GAMEOBJS) $(OBJ)\sdlmusic.$o
!endif

GAMEOBJS=$(GAMEOBJS) $(MUSICOBJ)
EDITOROBJS=$(EDITOROBJS) $(MUSICOBJ)


# RULES
.SUFFIXES: .masm

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

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

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

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

{$(RSRC)\}.c{$(OBJ)\}.$o:
	$(CC) /c $(CFLAGS) /Fo$@ $<

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

{$(SRC)\misc}.rc{$(OBJ)\}.res:
	$(RC) /i$(EINC)\ /i$(SRC)\ /DPOLYMER /fo$@ /r $<


# TARGETS
all: eduke32$(EXESUFFIX) mapster32$(EXESUFFIX) 

eduke32$(EXESUFFIX): $(GAMEOBJS) $(EOBJ)\$(ENGINELIB) $(JAUDIOLIBDIR)\$(JAUDIOLIB) $(ENETDIR)\$(ENETLIB)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS $(WINMACHINE) /LIBPATH:$(PLATFORM)\lib$(WINLIB) /LIBPATH:$(AUDIOPLATFORM)\lib$(WINLIB) $(flags_link) /MAP $** $(LIBS)
	$(MT) -manifest $(RSRC)$(WINLIB)\manifest.game.xml -hashupdate -outputresource:$@ -out:$@.manifest
	
mapster32$(EXESUFFIX): $(EDITOROBJS) $(EOBJ)\$(ENGINELIB) $(EOBJ)\$(EDITORLIB) $(JAUDIOLIBDIR)\$(JAUDIOLIB)
	$(LINK) /OUT:$@ /SUBSYSTEM:WINDOWS $(WINMACHINE) /LIBPATH:$(PLATFORM)\lib$(WINLIB) /LIBPATH:$(AUDIOPLATFORM)\lib$(WINLIB) $(flags_link) /MAP $** $(LIBS)
	$(MT) -manifest $(RSRC)$(WINLIB)\manifest.build.xml -hashupdate -outputresource:$@ -out:$@.manifest
	
!include Makefile.deps

enginelib editorlib: AlwaysBuild
	-mkdir $(EOBJ)
	echo OBJ=$(MAKEDIR)\$(EOBJ)\ > $(EOBJ)\overrides.mak
	echo CFLAGS=$(ENGINEOPTS) >> $(EOBJ)\overrides.mak
	echo WINBITS=$(WINBITS) >> $(EOBJ)\overrides.mak
	echo RENDERTYPE=$(RENDERTYPE) >> $(EOBJ)\overrides.mak
	echo MIXERTYPE=$(MIXERTYPE) >> $(EOBJ)\overrides.mak
	cd $(EROOT)
	nmake /f Makefile.msvc "OVERRIDES=$(MAKEDIR)\$(EOBJ)\overrides.mak" $@
	cd $(MAKEDIR)

jaudiolib:
	cd $(JAUDIOLIBDIR)
	nmake /f Makefile.msvc "MIXERTYPE=$(MIXERTYPE)" "WINBITS=$(WINBITS)"
	cd $(MAKEDIR)

enet:
	cd $(ENETDIR)
	nmake /f Makefile.msvc "WINBITS=$(WINBITS)"
	cd $(MAKEDIR)

AlwaysBuild: ;
$(EOBJ)\$(EDITORLIB): editorlib ;
$(EOBJ)\$(ENGINELIB): enginelib ;
$(JAUDIOLIBDIR)\$(JAUDIOLIB): jaudiolib ;
$(ENETDIR)\$(ENETLIB): enet ;

# PHONIES	
clean:
	-del /Q $(OBJ)\* eduke32$(EXESUFFIX) mapster32$(EXESUFFIX) \
            *.pdb *.map *.manifest
	-copy /y nul $(OBJ)\keep.me
	cd $(JAUDIOLIBDIR)
	nmake /f Makefile.msvc clean
	cd $(MAKEDIR)\$(ENETDIR)
	nmake /f Makefile.msvc clean
	cd $(MAKEDIR)
	
veryclean: clean
	-del /Q $(EOBJ)\*
	-copy /y nul $(EOBJ)\keep.me
