cmake_minimum_required(VERSION 3.19)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(WIN32)
    list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
endif()

project(Main)

file(GLOB SOURCES src/*.h src/*.cpp src/*.c)
add_executable(Main ${SOURCES})

target_include_directories(Main PUBLIC include src)

# Add ImGui sources
file(GLOB IMGUI_SOURCES
    ${CMAKE_CURRENT_LIST_DIR}/include/imgui/*.cpp
)

target_sources(Main PRIVATE ${IMGUI_SOURCES})

find_package(glfw3 3.4 REQUIRED)
set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED)
find_package(glm REQUIRED)

# copy shader files
file(GLOB GLSL_SOURCE_FILES
    shaders/*.frag
    shaders/*.vert
)

add_custom_target(Shaders DEPENDS ${GLSL_SOURCE_FILES})
add_dependencies(Main Shaders)

add_custom_command(TARGET Shaders POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    "$<TARGET_PROPERTY:Main,SOURCE_DIR>/shaders"
    "$<TARGET_PROPERTY:Main,BINARY_DIR>/$<CONFIGURATION>/shaders"
)

# copy textures
file(GLOB TEX_SOURCE_FILES
  textures/*
)

add_custom_target(
  Textures
  DEPENDS ${TEX_SOURCE_FILES}
)
add_dependencies(Main Textures)

add_custom_command(TARGET Textures POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory
  "$<TARGET_PROPERTY:Main,SOURCE_DIR>/assets/textures"
  "$<TARGET_PROPERTY:Main,BINARY_DIR>/$<CONFIGURATION>/assets/textures"
)

if(UNIX)
    set(GLFW3_LIBRARY glfw)
endif()

include_directories(${GLFW3_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
target_link_libraries(Main ${GLFW3_LIBRARY} OpenGL::GL)
