cmake_minimum_required(VERSION 3.20)
project(opengl_tutorial C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# GLFW：Windowsは公式のビルド済みバイナリ、Linux/FreeBSDはシステムのパッケージ
# GLFW: prebuilt binaries on Windows, the system package on Linux/FreeBSD
if(WIN32)
  set(GLFW_ROOT "C:/glfw-3.4.bin.WIN64" CACHE PATH "GLFW prebuilt binaries")
  add_library(glfw STATIC IMPORTED)
  set_target_properties(glfw PROPERTIES
    IMPORTED_LOCATION "${GLFW_ROOT}/lib-vc2022/glfw3.lib"
    INTERFACE_INCLUDE_DIRECTORIES "${GLFW_ROOT}/include")
else()
  find_package(glfw3 3.3 REQUIRED)
endif()

# GLAD：生成したOpenGLローダー / the generated OpenGL loader
add_library(glad STATIC external/glad/src/glad.c)
target_include_directories(glad PUBLIC external/glad/include)

# stb_image：実装は src/stb_image_impl.cc だけ。他人のコードなので警告フラグなしの別ライブラリにする
# stb_image: the implementation lives in src/stb_image_impl.cc only. It is someone else's code,
# so it is a separate library built without our warning flags
add_library(stb_image STATIC src/stb_image_impl.cc)
target_include_directories(stb_image PUBLIC external/stb)

add_executable(game
  src/main.cc
  src/window.cc
  src/gl_debug.cc
  src/buffer.cc
  src/mesh.cc
  src/file_util.cc
  src/shader.cc
  src/texture.cc
  src/shapes.cc
  src/camera.cc
  src/material.cc
  src/lights.cc
  src/json.cc
  src/gltf.cc
  src/model.cc)
target_include_directories(game PRIVATE src external/stb external/linalg)
target_link_libraries(game PRIVATE glad stb_image glfw)

# glTF/GLBの中身を表示する道具。GPUもウィンドウも使わないのでGLFWとGLADは要らない
# A tool that prints what is inside glTF/GLB files. It uses no GPU and no window, so no GLFW and no GLAD
add_executable(gltf_dump
  src/gltf_dump.cc
  src/gltf.cc
  src/json.cc
  src/file_util.cc)
target_include_directories(gltf_dump PRIVATE src external/linalg)

# 警告を有効にする（自分のコードだけ） / enable warnings (our own code only)
foreach(target game gltf_dump)
  if(MSVC)
    target_compile_options(${target} PRIVATE /W4 /utf-8)
  else()
    target_compile_options(${target} PRIVATE -Wall -Wextra)
  endif()
endforeach()

# シェーダーとアセットは作業ディレクトリからの相対パスで読む。
# Visual Studioではプロジェクトのルートを作業ディレクトリにする。Unixではルートから ./build/game を実行。
# Shaders and assets are loaded relative to the working directory.
# Visual Studio: use the project root as the working directory. Unix: run ./build/game from the root.
set_property(TARGET game PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT game)
