cmake_minimum_required(VERSION 3.10)
project(basic1_emulator)

set(CMAKE_CXX_STANDARD 17)

find_package(SFML 3.0 COMPONENTS Graphics Window System REQUIRED)

# Lua source files
file(GLOB LUA_SOURCES "../lib/lua/*.c")
list(FILTER LUA_SOURCES EXCLUDE REGEX "lua\\.c$|luac\\.c$|loslib\\.c$|liolib\\.c$")

# Game source files
set(GAME_SOURCES
    ../games/lua_bindings.cpp
    ../games/demo_game.cpp
    ../games/tic_tac_toe.cpp
    ../games/monopoly/monopoly_game.cpp
    ../games/monopoly/player.c
    ../lib/game_launcher.cpp
    ../display/low_level_render.cpp
    ../display/low_level_gui.cpp
)

# Add source files
set(SOURCES
    main.cpp
    low_level_display_sfml.cpp
    lua_game_emulator.cpp
    lua_game_loader_emulator.cpp
    input_manager.cpp
    ${GAME_SOURCES}
    ${LUA_SOURCES}
    # Add more emulator-specific sources here
)

add_executable(basic1_emulator ${SOURCES})

# Define LUA_32BITS for 32-bit embedded mode
target_compile_definitions(basic1_emulator PRIVATE LUA_32BITS=1)

target_include_directories(basic1_emulator PRIVATE . .. ../display ../fonts ../games ../lib ../lib/lua)

target_link_libraries(basic1_emulator SFML::Graphics SFML::Window SFML::System)

# Copy Lua example files to build directory
file(GLOB LUA_EXAMPLE_FILES "../games/lua_examples/*.lua")
foreach(LUA_FILE ${LUA_EXAMPLE_FILES})
    get_filename_component(FILENAME ${LUA_FILE} NAME)
    add_custom_command(TARGET basic1_emulator POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${LUA_FILE}
        ${CMAKE_CURRENT_BINARY_DIR}/games/lua_examples/${FILENAME}
        COMMENT "Copying Lua example: ${FILENAME}"
    )
endforeach()

# Also ensure directory exists
add_custom_command(TARGET basic1_emulator POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory
    ${CMAKE_CURRENT_BINARY_DIR}/games/lua_examples
    COMMENT "Creating games/lua_examples directory"
)
