cmake_minimum_required(VERSION 3.19)

project(dp_doom LANGUAGES C)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(BACKEND "nng" CACHE STRING "Which backend to use: Xlib, SDL2 or nng")
option(WILDMIDI "Render MIDI using the WildMIDI library" ON)

add_executable(dp_doom WIN32
	"linuxdoom-1.10/am_map.c"
	"linuxdoom-1.10/am_map.h"
	"linuxdoom-1.10/d_englsh.h"
	"linuxdoom-1.10/d_event.h"
	"linuxdoom-1.10/d_french.h"
	"linuxdoom-1.10/d_items.c"
	"linuxdoom-1.10/d_items.h"
	"linuxdoom-1.10/d_main.c"
	"linuxdoom-1.10/d_main.h"
	"linuxdoom-1.10/d_net.c"
	"linuxdoom-1.10/d_net.h"
	"linuxdoom-1.10/doomdata.h"
	"linuxdoom-1.10/doomdef.c"
	"linuxdoom-1.10/doomdef.h"
	"linuxdoom-1.10/doomstat.c"
	"linuxdoom-1.10/doomstat.h"
	"linuxdoom-1.10/doomtype.h"
	"linuxdoom-1.10/d_player.h"
	"linuxdoom-1.10/dstrings.c"
	"linuxdoom-1.10/dstrings.h"
	"linuxdoom-1.10/d_textur.h"
	"linuxdoom-1.10/d_think.h"
	"linuxdoom-1.10/d_ticcmd.h"
	"linuxdoom-1.10/f_finale.c"
	"linuxdoom-1.10/f_finale.h"
	"linuxdoom-1.10/f_wipe.c"
	"linuxdoom-1.10/f_wipe.h"
	"linuxdoom-1.10/g_game.c"
	"linuxdoom-1.10/g_game.h"
	"linuxdoom-1.10/hu_lib.c"
	"linuxdoom-1.10/hu_lib.h"
	"linuxdoom-1.10/hu_stuff.c"
	"linuxdoom-1.10/hu_stuff.h"
	"linuxdoom-1.10/ib_sound.h"
	"linuxdoom-1.10/ib_system.h"
	"linuxdoom-1.10/ib_video.h"
	"linuxdoom-1.10/i_main.c"
	"linuxdoom-1.10/i_net.c"
	"linuxdoom-1.10/i_net.h"
	"linuxdoom-1.10/info.c"
	"linuxdoom-1.10/info.h"
	"linuxdoom-1.10/i_sound.c"
	"linuxdoom-1.10/i_sound.h"
	"linuxdoom-1.10/i_system.c"
	"linuxdoom-1.10/i_system.h"
	"linuxdoom-1.10/i_video.c"
	"linuxdoom-1.10/i_video.h"
	"linuxdoom-1.10/m_argv.c"
	"linuxdoom-1.10/m_argv.h"
	"linuxdoom-1.10/m_bbox.c"
	"linuxdoom-1.10/m_bbox.h"
	"linuxdoom-1.10/m_cheat.c"
	"linuxdoom-1.10/m_cheat.h"
	"linuxdoom-1.10/m_fixed.c"
	"linuxdoom-1.10/m_fixed.h"
	"linuxdoom-1.10/m_menu.c"
	"linuxdoom-1.10/m_menu.h"
	"linuxdoom-1.10/m_misc.c"
	"linuxdoom-1.10/m_misc.h"
	"linuxdoom-1.10/m_random.c"
	"linuxdoom-1.10/m_random.h"
	"linuxdoom-1.10/m_swap.c"
	"linuxdoom-1.10/m_swap.h"
	"linuxdoom-1.10/p_ceilng.c"
	"linuxdoom-1.10/p_doors.c"
	"linuxdoom-1.10/p_enemy.c"
	"linuxdoom-1.10/p_floor.c"
	"linuxdoom-1.10/p_inter.c"
	"linuxdoom-1.10/p_inter.h"
	"linuxdoom-1.10/p_lights.c"
	"linuxdoom-1.10/p_local.h"
	"linuxdoom-1.10/p_map.c"
	"linuxdoom-1.10/p_maputl.c"
	"linuxdoom-1.10/p_mobj.c"
	"linuxdoom-1.10/p_mobj.h"
	"linuxdoom-1.10/p_plats.c"
	"linuxdoom-1.10/p_pspr.c"
	"linuxdoom-1.10/p_pspr.h"
	"linuxdoom-1.10/p_saveg.c"
	"linuxdoom-1.10/p_saveg.h"
	"linuxdoom-1.10/p_setup.c"
	"linuxdoom-1.10/p_setup.h"
	"linuxdoom-1.10/p_sight.c"
	"linuxdoom-1.10/p_spec.c"
	"linuxdoom-1.10/p_spec.h"
	"linuxdoom-1.10/p_switch.c"
	"linuxdoom-1.10/p_telept.c"
	"linuxdoom-1.10/p_tick.c"
	"linuxdoom-1.10/p_tick.h"
	"linuxdoom-1.10/p_user.c"
	"linuxdoom-1.10/r_bsp.c"
	"linuxdoom-1.10/r_bsp.h"
	"linuxdoom-1.10/r_data.c"
	"linuxdoom-1.10/r_data.h"
	"linuxdoom-1.10/r_defs.h"
	"linuxdoom-1.10/r_draw.c"
	"linuxdoom-1.10/r_draw.h"
	"linuxdoom-1.10/r_local.h"
	"linuxdoom-1.10/r_main.c"
	"linuxdoom-1.10/r_main.h"
	"linuxdoom-1.10/r_plane.c"
	"linuxdoom-1.10/r_plane.h"
	"linuxdoom-1.10/r_segs.c"
	"linuxdoom-1.10/r_segs.h"
	"linuxdoom-1.10/r_sky.c"
	"linuxdoom-1.10/r_sky.h"
	"linuxdoom-1.10/r_state.h"
	"linuxdoom-1.10/r_things.c"
	"linuxdoom-1.10/r_things.h"
	"linuxdoom-1.10/sounds.c"
	"linuxdoom-1.10/sounds.h"
	"linuxdoom-1.10/s_sound.c"
	"linuxdoom-1.10/s_sound.h"
	"linuxdoom-1.10/st_lib.c"
	"linuxdoom-1.10/st_lib.h"
	"linuxdoom-1.10/st_stuff.c"
	"linuxdoom-1.10/st_stuff.h"
	"linuxdoom-1.10/tables.c"
	"linuxdoom-1.10/tables.h"
	"linuxdoom-1.10/v_video.c"
	"linuxdoom-1.10/v_video.h"
	"linuxdoom-1.10/wi_stuff.c"
	"linuxdoom-1.10/wi_stuff.h"
	"linuxdoom-1.10/w_wad.c"
	"linuxdoom-1.10/w_wad.h"
	"linuxdoom-1.10/z_zone.c"
)

set_target_properties(dp_doom PROPERTIES C_STANDARD 99)

# Handling linking/building WildMIDI
if(WILDMIDI)
	target_compile_definitions(dp_doom PRIVATE WILDMIDI)

	# TODO - Add support for system library
	#find_package(WildMidi)
	find_library(WILDMID_LIBRARY WildMidi)

	#if(NOT WildMidi-FOUND)
		# Set and hide these internal options
		set(WANT_ALSA OFF CACHE INTERNAL "" FORCE)
		set(WANT_DEVTEST OFF CACHE INTERNAL "" FORCE)
		set(WANT_OPENAL OFF CACHE INTERNAL "" FORCE)
		set(WANT_OSS OFF CACHE INTERNAL "" FORCE)
		set(WANT_OSX_DEPLOYMENT OFF CACHE INTERNAL "" FORCE)
		set(WANT_STATIC ON CACHE INTERNAL "" FORCE)

		add_subdirectory("external/wildmidi-0.4.5" EXCLUDE_FROM_ALL)
	#endif()

	target_link_libraries(dp_doom PRIVATE libwildmidi-static m)
endif()

# Handle backends
if(BACKEND STREQUAL "Xlib")
	target_sources(dp_doom PRIVATE
		"linuxdoom-1.10/ib_sound/miniaudio.c"
		"linuxdoom-1.10/ib_sound/miniaudio.h"
		"linuxdoom-1.10/ib_system/posix.c"
		"linuxdoom-1.10/ib_video/xlib.c"
	)

	# These are for the X11 stuff.
	target_link_libraries(dp_doom PRIVATE Xext X11)

	# These are for miniaudio.
	target_link_libraries(dp_doom PRIVATE m ${CMAKE_DL_LIBS} pthread)
elseif(BACKEND STREQUAL "SDL2")
	target_sources(dp_doom PRIVATE
		"linuxdoom-1.10/ib_sound/ib_sound_sdl.c"
		"linuxdoom-1.10/ib_system/ib_system_sdl.c"
		"linuxdoom-1.10/ib_video/ib_video_sdl.c"
	)

	target_link_libraries(dp_doom PRIVATE SDL2::SDL2main SDL2::SDL2-static)
elseif(BACKEND STREQUAL "nng")
	target_sources(dp_doom PRIVATE
		"linuxdoom-1.10/ib_sound/ib_sound_nng.c"
		"linuxdoom-1.10/ib_system/ib_system_nng.c"
		"linuxdoom-1.10/ib_video/ib_video_nng.c"
	)

	target_link_libraries(dp_doom
		PRIVATE SDL2::SDL2
		PRIVATE m
		PRIVATE dp_common
	)
else()
	message(FATAL_ERROR "Invalid BACKEND")
endif()
