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)

# 警告を有効にする（自分のコードだけ）。エンジンとゲームの全ターゲットで使う
# Enables warnings (our own code only). Used by the engine and every game target
function(enable_warnings target)
  if(MSVC)
    target_compile_options(${target} PRIVATE /W4 /utf-8)
  else()
    target_compile_options(${target} PRIVATE -Wall -Wextra)
  endif()
endfunction()

# シェーダーとアセットは作業ディレクトリ（= プロジェクトのルート）からの相対パスで読む：
#   engine/shaders/…（エンジン）、assets/…（共有）、games/<名前>/…（各ゲーム）。
# Visual Studioでは各実行ファイルの作業ディレクトリをルートにする。Unixではルートから ./build/game を実行。
# Shaders and assets are loaded relative to the working directory (= the project root):
#   engine/shaders/... (the engine), assets/... (shared), games/<name>/... (each game).
# Visual Studio: every executable's working directory is the root. Unix: run ./build/game from the root.
function(set_working_directory target)
  set_property(TARGET ${target} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
endfunction()

# エンジン：第1〜24章のクラス＋2Dの道具（静的ライブラリ） / the engine: the classes of chapters 1-24 + the 2D toolkit
# (a static library)
add_subdirectory(engine)

# この章のデモ / this chapter's demo
add_executable(game
  demo/main.cc
  demo/title_scene.cc
  demo/playground_scene.cc
  demo/pause_scene.cc)
target_link_libraries(game PRIVATE engine)
enable_warnings(game)
set_working_directory(game)
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT game)

# glTF/GLBの中身を表示する道具。GPUもウィンドウも使わないのでエンジン全体はリンクしない（GLFWもGLADも要らない）
# A tool that prints what is inside glTF/GLB files. It uses no GPU and no window, so it does not link the whole
# engine (no GLFW, no GLAD)
add_executable(gltf_dump
  tools/gltf_dump.cc
  engine/src/gltf.cc
  engine/src/json.cc
  engine/src/file_util.cc)
target_include_directories(gltf_dump PRIVATE engine/include external/linalg)
enable_warnings(gltf_dump)

# 第24章の3D場面をエンジンで描く確認用プログラム（影・PBR・IBL・ブルーム）。TPS（第36〜39章）の土台
# A check that draws the chapter 24 3D scene through the engine (shadows, PBR, IBL, bloom). The basis of the TPS
# (chapters 36-39)
option(BUILD_SCENE3D "Build the 3D check program scene3d" ON)
if(BUILD_SCENE3D)
  add_executable(scene3d tests/scene3d/main.cc)
  target_link_libraries(scene3d PRIVATE engine)
  enable_warnings(scene3d)
  set_working_directory(scene3d)
endif()

# ゲーム（第27章から）：games/<名前>/CMakeLists.txt があれば加える。各ゲームは自分の実行ファイル
# The games (from chapter 27): added when games/<name>/CMakeLists.txt exists. Each game is its own executable
foreach(name invaders fighter rpg tps)
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/games/${name}/CMakeLists.txt")
    add_subdirectory(games/${name})
  endif()
endforeach()
