cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(polygun)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)

list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}/cmake")

if(NOT BUILD_FLAG_SUPPRESS_WARNING_MODE)
	# Compile with highest warning mode by default
	if(MSVC)
		set(DEFAULT_COMPILE_OPTIONS /W4 /WX)
	else()
		set(DEFAULT_COMPILE_OPTIONS -Wall -Wpedantic -Werror)
	endif()
endif()

include_directories(deps)
include_directories("${CMAKE_SOURCE_DIR}/src")
include_directories("${CMAKE_BINARY_DIR}")

configure_file("config.hpp.in" "config.hpp")
option(BUILD_CLIENT "Build client" ON)
option(BUILD_SERVER "Build server" ON)
option(BUILD_TOOLS "Build tools" OFF)
if(APPLE)
	set(RENDERER_GL_DEFAULT OFF)
else()
	set(RENDERER_GL_DEFAULT ON)
endif()
option(RENDERER_GL "Enable OpenGL renderer" ${RENDERER_GL_DEFAULT})
if(NOT BUILD_CLIENT)
	set(RENDERER_GL OFF)
endif()

if(WIN32)
	set(WINDOW_WIN32_DEFAULT ON)
else()
	set(WINDOW_WIN32_DEFAULT OFF)
endif()
option(WINDOW_WIN32 "Enable Win32 window creation (Windows only)" ${WINDOW_WIN32_DEFAULT})
if(UNIX AND NOT APPLE)
	if(X11_INCLUDE_DIRS AND X11_LIBRARIES)
		set(WINDOW_X11_DEFAULT ON)
	else()
		find_package(X11 QUIET)
		if(X11_FOUND)
			set(WINDOW_X11_DEFAULT ON)
		else()
			set(WINDOW_X11_DEFAULT OFF)
		endif()
	endif()
endif()
option(WINDOW_X11 "Enable X11 window creation" ${WINDOW_X11_DEFAULT})

if(BUILD_CLIENT)
	set(CLIENT_SOURCES
		src/game/audio/audio.cpp
		src/game/audio/buffer.cpp
		src/game/audio/source.cpp
		src/game/engine/engine.cpp
		src/game/engine/greedy_meshing.cpp
		src/game/engine/player_camera.cpp
		src/game/engine/resource_manager.cpp
		src/game/engine/screen_manager.cpp
		src/game/renderer/gui_renderer.cpp
		src/game/renderer/master_renderer.cpp
		src/game/renderer/mesh.cpp
		src/game/renderer/mesh_renderer.cpp
		src/game/renderer/ortho_camera.cpp
		src/game/renderer/surface.cpp
		src/game/renderer/texture.cpp
		src/game/renderer/vertex_array.cpp
		src/game/screens/game_session_screen.cpp
		src/game/screens/main_menu_screen.cpp
		src/game/window/keyboard.cpp
		src/game/window/mouse.cpp
		src/game/window/window.cpp
		src/game/world/chunk.cpp
	)
	if(RENDERER_GL)
		list(APPEND CLIENT_SOURCES
			src/game/renderer/gl/gl_context.cpp
			src/game/renderer/gl/gl_gui_renderer.cpp
			src/game/renderer/gl/gl_mesh.cpp
			src/game/renderer/gl/gl_mesh_renderer.cpp
			src/game/renderer/gl/gl_shader.cpp
			src/game/renderer/gl/gl_texture.cpp
			src/game/renderer/gl/opengl.cpp
		)
	endif()
	if(WINDOW_WIN32)
		list(APPEND CLIENT_SOURCES
			src/game/window/win32_window.cpp
		)
		if(RENDERER_GL)
			list(APPEND CLIENT_SOURCES
				src/game/renderer/gl/win32_gl_context.cpp
			)
		endif()
	endif()
	if(WINDOW_X11)
		list(APPEND CLIENT_SOURCES
			src/game/window/x11_window.cpp
		)
		if(RENDERER_GL)
			list(APPEND CLIENT_SOURCES
				src/game/renderer/gl/x11_gl_context.cpp
			)
		endif()
	endif()
endif()
if(BUILD_SERVER)
	file(GLOB_RECURSE SERVER_SOURCES "src/server/**.cpp")

	include(CheckCXXSourceCompiles)
	check_cxx_source_compiles(
		"
		#include <csignal>
		#include <cstring>
		int main(){
			const char* sig = strsignal(SIGINT);
			return 0;
		}
		"
		HAVE_STRSIGNAL_CHECK
	)
	if(HAVE_STRSIGNAL_CHECK)
		option(HAVE_STRSIGNAL "STRSIGNAL" ON)
	endif()
endif()
if(BUILD_TOOLS)
	add_executable("extract_strings"
		tools/extract_strings.cpp
		src/common/xml/xml_node.cpp
		src/common/xml/lexer.cpp
		src/common/xml/parser.cpp
		src/common/logger.cpp
	)
	target_compile_options(extract_strings PRIVATE ${DEFAULT_COMPILE_OPTIONS})
endif()
file(GLOB_RECURSE COMMON_SOURCES "src/common/**.cpp")

if(BUILD_SERVER AND NOT BUILD_CLIENT)
	set(PROJECT_NAME "polygun_server")
endif()

if(NOT BUILD_CLIENT AND NOT BUILD_SERVER)
	message(FATAL_ERROR "You need to select at least one configuration (BUILD_CLIENT and/or BUILD_SERVER)")
endif()

add_executable(${PROJECT_NAME}
	${CLIENT_SOURCES}
	${SERVER_SOURCES}
	${COMMON_SOURCES}
	src/main.cpp
)
target_compile_options(${PROJECT_NAME} PRIVATE ${DEFAULT_COMPILE_OPTIONS})

if(BUILD_CLIENT)
	if(RENDERER_GL)
		if(NOT OPENGL_INCLUDE_DIR OR NOT OPENGL_LIBRARIES)
			find_package(OpenGL REQUIRED)
		endif()
		include_directories(${OPENGL_INCLUDE_DIR})
		target_link_libraries(${PROJECT_NAME} "${OPENGL_LIBRARIES}")
	endif()

	if(WINDOW_X11)
		if(NOT X11_INCLUDE_DIRS OR NOT X11_LIBRARIES)
			find_package(X11 REQUIRED)
		endif()
		include_directories(${X11_INCLUDE_DIRS})
		target_link_libraries(${PROJECT_NAME} "${X11_LIBRARIES}")
	endif()

	if(NOT GLM_INCLUDE_DIRS)
		find_package(GLM REQUIRED)
	endif()
	include_directories("${GLM_INCLUDE_DIRS}")

	if(NOT OPENAL_INCLUDE_DIR OR NOT OPENAL_LIBRARY)
		find_package(OpenAL REQUIRED)
	endif()
	include_directories("${OPENAL_INCLUDE_DIR}")
	target_link_libraries(${PROJECT_NAME} "${OPENAL_LIBRARY}")

	if(NOT OGG_INCLUDE_DIR OR NOT OGG_LIBRARY OR NOT VORBIS_INCLUDE_DIR OR NOT VORBIS_LIBRARY)
		find_package(OggVorbis REQUIRED)
	endif()
	include_directories("${OGG_INCLUDE_DIR}")
	include_directories("${VORBIS_INCLUDE_DIR}")
	target_link_libraries(${PROJECT_NAME} "${VORBIS_LIBRARY}")
	target_link_libraries(${PROJECT_NAME} "${OGG_LIBRARY}")

	if(NOT PNG_INCLUDE_DIRS OR NOT PNG_LIBRARIES)
		find_package(PNG REQUIRED)
	endif()
	include_directories("${PNG_INCLUDE_DIRS}")
	target_link_libraries(${PROJECT_NAME} "${PNG_LIBRARIES}")

	if(NOT ZLIB_INCLUDE_DIRS OR NOT ZLIB_LIBRARIES)
		find_package(ZLIB REQUIRED)
	endif()
	include_directories("${ZLIB_INCLUDE_DIRS}")
	target_link_libraries(${PROJECT_NAME} "${ZLIB_LIBRARIES}")

	if(UNIX)
		target_link_libraries(${PROJECT_NAME} "dl")
	endif()
endif()
if(BUILD_SERVER)
	add_subdirectory("${CMAKE_SOURCE_DIR}/external/angelscript/angelscript/projects/cmake")
	include_directories("${CMAKE_SOURCE_DIR}/external/angelscript/angelscript/include")
	include_directories("${CMAKE_SOURCE_DIR}/external/angelscript/add_on")
	target_link_libraries(${PROJECT_NAME} "angelscript")
	add_library("angelscript_addons" STATIC
		external/angelscript/add_on/scriptstdstring/scriptstdstring.cpp
		external/angelscript/add_on/scriptstdstring/scriptstdstring_utils.cpp
	)
	target_link_libraries(${PROJECT_NAME} "angelscript_addons")
endif()

if(WIN32)
	target_link_libraries(${PROJECT_NAME} "ws2_32")
endif()


find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} "${CMAKE_THREAD_LIBS_INIT}")

