# CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(coffee_crisis_arcade_edition VERSION 2.00)

# Establecer estándar de C++
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Establece la política CMP0072 para indicar cómo se debe seleccionar la implementación de OpenGL.
# En este caso, se elige la opción "GLVND", que utiliza bibliotecas modernas y modulares (libOpenGL, libGLX),
# en lugar de la biblioteca OpenGL clásica (libGL). Esto mejora la compatibilidad con drivers recientes
# y evita ambigüedades cuando se encuentran múltiples implementaciones de OpenGL en el sistema.
cmake_policy(SET CMP0072 NEW)
set(OpenGL_GL_PREFERENCE GLVND)

# --- GENERACIÓN DE VERSIÓN AUTOMÁTICA ---
find_package(Git QUIET)
if(GIT_FOUND)
    execute_process(
        COMMAND ${GIT_EXECUTABLE} rev-parse --short=7 HEAD
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE GIT_HASH
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
else()
    set(GIT_HASH "unknown")
endif()

# Configurar archivo de versión
configure_file(${CMAKE_SOURCE_DIR}/source/version.h.in ${CMAKE_BINARY_DIR}/version.h @ONLY)

# --- 1. LISTA EXPLÍCITA DE FUENTES ---
set(APP_SOURCES
    # --- Archivos Principales del Sistema ---
    source/asset.cpp
    source/audio.cpp
    source/director.cpp
    source/global_events.cpp
    source/global_inputs.cpp
    source/input.cpp
    source/lang.cpp
    source/main.cpp
    source/param.cpp
    source/resource.cpp
    source/resource_helper.cpp
    source/resource_loader.cpp
    source/resource_pack.cpp
    source/screen.cpp
    source/text.cpp
    source/writer.cpp
    
    # --- UI (User Interface) ---
    source/ui/menu_option.cpp
    source/ui/menu_renderer.cpp
    source/ui/notifier.cpp
    source/ui/service_menu.cpp
    source/ui/ui_message.cpp
    source/ui/window_message.cpp
    
    # --- Lógica del Juego ---
    source/balloon_formations.cpp
    source/balloon_manager.cpp
    source/balloon.cpp
    source/bullet.cpp
    source/bullet_manager.cpp
    source/enter_name.cpp
    source/explosions.cpp
    source/game_logo.cpp
    source/item.cpp
    source/manage_hiscore_table.cpp
    source/player.cpp
    source/scoreboard.cpp
    source/tabe.cpp
    
    # --- Escenas ---
    source/sections/credits.cpp
    source/sections/game.cpp
    source/sections/hiscore_table.cpp
    source/sections/instructions.cpp
    source/sections/intro.cpp
    source/sections/logo.cpp
    source/sections/title.cpp
    
    # --- Sprites y Gráficos ---
    source/animated_sprite.cpp
    source/background.cpp
    source/fade.cpp
    source/moving_sprite.cpp
    source/path_sprite.cpp
    source/smart_sprite.cpp
    source/sprite.cpp
    source/texture.cpp
    source/tiled_bg.cpp
    
    # --- Otros ---
    source/color.cpp
    source/demo.cpp
    source/define_buttons.cpp
    source/difficulty.cpp
    source/input_types.cpp
    source/mouse.cpp
    source/options.cpp
    source/shutdown.cpp
    source/stage.cpp
    source/system_utils.cpp
    source/utils.cpp
)

# Fuentes de librerías de terceros
set(EXTERNAL_SOURCES
    source/external/jail_audio.cpp
    source/external/json.hpp
    source/external/gif.cpp
)

# Fuentes del sistema de renderizado
set(RENDERING_SOURCES
    source/rendering/opengl/opengl_shader.cpp
)

# Configuración de SDL3
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")

# --- 2. AÑADIR EJECUTABLE ---
add_executable(${PROJECT_NAME} ${APP_SOURCES} ${EXTERNAL_SOURCES} ${RENDERING_SOURCES})

# --- 3. DIRECTORIOS DE INCLUSIÓN ---
target_include_directories(${PROJECT_NAME} PUBLIC
    "${CMAKE_SOURCE_DIR}/source"
    "${CMAKE_SOURCE_DIR}/source/external"
    "${CMAKE_SOURCE_DIR}/source/rendering"
    "${CMAKE_BINARY_DIR}"
)

# Enlazar la librería SDL3
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)


# --- 4. CONFIGURACIÓN PLATAFORMAS Y COMPILADOR  ---
# Configuración de flags de compilación
target_compile_options(${PROJECT_NAME} PRIVATE -Wall)
target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunction-sections -fdata-sections>)

# Definir _DEBUG en modo Debug
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)

# Descomentar la siguiente línea para activar el modo grabación de demos
# target_compile_definitions(${PROJECT_NAME} PRIVATE RECORDING)


# Configuración específica para cada plataforma
if(WIN32)
    target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
    target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32 mingw32 opengl32)
elseif(APPLE)
    target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUILD)
    target_compile_options(${PROJECT_NAME} PRIVATE -Wno-deprecated)
    set(CMAKE_OSX_ARCHITECTURES "arm64")
elseif(UNIX AND NOT APPLE)
    target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
endif()

# Configuración común para OpenGL
if(NOT WIN32)
    find_package(OpenGL REQUIRED)
    if(OPENGL_FOUND)
        message(STATUS "OpenGL encontrado: ${OPENGL_LIBRARIES}")
        target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENGL_LIBRARIES})
    else()
        message(FATAL_ERROR "OpenGL no encontrado")
    endif()
endif()

# Especificar la ubicación del ejecutable
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
