cmake_minimum_required(VERSION 3.28)

include(whale)

set(MODULE_NAME core)

whl_project(${MODULE_NAME} VERSION 0.4.0)


###############################################################################
# Dependencies

# NOTE: Update config.cmake.in when changing dependencies.

find_package(GLEW CONFIG)
find_package(OpenGL)
find_package(SDL2 CONFIG)
find_package(nlohmann_json REQUIRED CONFIG)
if(NOT EMSCRIPTEN)
  find_package(PNG REQUIRED)
  find_package(ZLIB REQUIRED)
endif()
find_package(minizip-ng REQUIRED CONFIG)
find_package(Microsoft.GSL REQUIRED)


###############################################################################
# Library

whl_add_library(${MODULE_NAME})

whl_add_sources(${MODULE_NAME}
  PRIVATE
    "include/whale/core/buffer.h"
    "include/whale/core/detail/zip.h"
    "include/whale/core/error.h"
    "include/whale/core/enum.h"
    "include/whale/core/event.h"
    "include/whale/core/fs/directory.h"
    "include/whale/core/fs/file.h"
    "include/whale/core/fs/file_system.h"
    "include/whale/core/fs/local_file_system.h"
    "include/whale/core/fwd.h"
    "include/whale/core/gfx/buffer_object.h"
    "include/whale/core/gfx/canvas.h"
    "include/whale/core/gfx/frame_buffer.h"
    "include/whale/core/gfx/gl.h"
    "include/whale/core/gfx/image.h"
    "include/whale/core/gfx/image_loaders.h"
    "include/whale/core/gfx/program.h"
    "include/whale/core/gfx/render_context.h"
    "include/whale/core/gfx/shader.h"
    "include/whale/core/gfx/sprite_sheet.h"
    "include/whale/core/gfx/texture.h"
    "include/whale/core/gfx/uniform.h"
    "include/whale/core/gfx/uniform_set.h"
    "include/whale/core/gfx/vertex_attr.h"
    "include/whale/core/gfx/vertex_attr_list.h"
    "include/whale/core/math/mat.h"
    "include/whale/core/math/math.h"
    "include/whale/core/math/rect.h"
    "include/whale/core/math/vec.h"
    "include/whale/core/math/vec1.h"
    "include/whale/core/math/vec2.h"
    "include/whale/core/math/vec3.h"
    "include/whale/core/math/vec4.h"
    "include/whale/core/platform_detect.h"
    "include/whale/core/print.h"
    "include/whale/core/ptr.h"
    "include/whale/core/sha1.h"
    "include/whale/core/whale.h"
    "include/whale/core/window.h"

    "src/buffer.cpp"
    "src/detail/zip.cpp"
    "src/event.cpp"
    "src/fs/directory.cpp"
    "src/fs/file.cpp"
    "src/fs/local_file_system.cpp"
    "src/gfx/buffer_object.cpp"
    "src/gfx/canvas.cpp"
    "src/gfx/frame_buffer.cpp"
    "src/gfx/image.cpp"
    "src/gfx/image_loaders.cpp"
    "src/gfx/program.cpp"
    "src/gfx/render_context.cpp"
    "src/gfx/shader.cpp"
    "src/gfx/sprite_sheet.cpp"
    "src/gfx/texture.cpp"
    "src/gfx/uniform_set.cpp"
    "src/gfx/vertex_attr.cpp"
    "src/gfx/vertex_attr_list.cpp"
    "src/print.cpp"
    "src/whale.cpp"
    "src/window.cpp"
)

if(ANDROID)
  whl_add_sources(${MODULE_NAME}
    PRIVATE
      "include/whale/core/fs/android_asset_file_system.h"

      "src/fs/android_asset_file_system.cpp"
  )
endif()

# Libraries that come with Emscripten by default. Other platforms need to link against them explicitly.
if(NOT EMSCRIPTEN)
  whl_link_libraries(${MODULE_NAME}
    PUBLIC
      SDL2::SDL2
      PNG::PNG
      ZLIB::ZLIB
  )

  if(ANDROID)
    whl_link_libraries(${MODULE_NAME}
      PUBLIC
        -landroid
        -lGLESv3

      PRIVATE
        -llog
    )  
  else()
    whl_link_libraries(${MODULE_NAME}
      PUBLIC
        OpenGL::GL
        GLEW::GLEW

      PRIVATE
        $<$<CXX_COMPILER_ID:GNU>:stdc++fs>
        $<$<CXX_COMPILER_ID:Clang>:stdc++fs>
    )
  endif()
endif()

whl_link_libraries(${MODULE_NAME}
  PUBLIC
    Microsoft.GSL::GSL

  PRIVATE
    nlohmann_json::nlohmann_json
    MINIZIP::minizip-ng
)

whl_install(${MODULE_NAME})


###############################################################################
# Tests

add_subdirectory("test")


###############################################################################
# Examples

add_subdirectory("example")
