diff --git a/CMakeLists.txt b/CMakeLists.txt index 290c62e..b1430f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,24 @@ if(NOT SOURCES) endif() # Configuración de SDL3 -find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) -message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}") +if(EMSCRIPTEN) + # En Emscripten, SDL3 se compila desde source con FetchContent + include(FetchContent) + FetchContent_Declare( + SDL3 + GIT_REPOSITORY https://github.com/libsdl-org/SDL.git + GIT_TAG release-3.2.12 + GIT_SHALLOW TRUE + ) + set(SDL_SHARED OFF CACHE BOOL "" FORCE) + set(SDL_STATIC ON CACHE BOOL "" FORCE) + set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(SDL3) + message(STATUS "SDL3 compilado desde source para Emscripten") +else() + find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3) + message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}") +endif() # Configuración común de salida de ejecutables en el directorio raíz set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) @@ -66,6 +82,14 @@ elseif(APPLE) -rpath @executable_path/../Frameworks/ ) endif() +elseif(EMSCRIPTEN) + target_compile_definitions(${PROJECT_NAME} PRIVATE EMSCRIPTEN_BUILD) + target_link_options(${PROJECT_NAME} PRIVATE + --preload-file ${CMAKE_SOURCE_DIR}/data@/data + -sALLOW_MEMORY_GROWTH=1 + -sMAX_WEBGL_VERSION=2 + ) + set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html") elseif(UNIX AND NOT APPLE) target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD) target_link_options(${PROJECT_NAME} PRIVATE -Wl,--gc-sections) diff --git a/Makefile b/Makefile index fb1e5e2..713b122 100644 --- a/Makefile +++ b/Makefile @@ -236,6 +236,18 @@ linux_release: # Elimina la carpeta temporal $(RMDIR) "$(RELEASE_FOLDER)" +# ============================================================================== +# COMPILACIÓN PARA WEBASSEMBLY (requiere Docker) +# ============================================================================== +wasm: + @echo "Compilando para WebAssembly - Version: $(VERSION)" + docker run --rm \ + -v $(DIR_ROOT):/src \ + -w /src \ + emscripten/emsdk:latest \ + bash -c "emcmake cmake -S . -B build_wasm -DCMAKE_BUILD_TYPE=Release && cmake --build build_wasm" + @echo "Output: build_wasm/coffee_crisis.html, .js, .wasm, .data" + # ============================================================================== # REGLAS ESPECIALES # ============================================================================== @@ -255,9 +267,10 @@ help: @echo " make windows_release - Crear release para Windows" @echo " make linux_release - Crear release para Linux" @echo " make macos_release - Crear release para macOS" + @echo " make wasm - Compilar para WebAssembly (requiere Docker)" @echo "" @echo " Otros:" @echo " make show_version - Mostrar version actual ($(VERSION))" @echo " make help - Mostrar esta ayuda" -.PHONY: all debug release windows_release macos_release linux_release show_version help +.PHONY: all debug release windows_release macos_release linux_release wasm show_version help diff --git a/source/director.cpp b/source/director.cpp index 94e7dbb..8ec53c9 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -4,8 +4,10 @@ #include // for errno, EEXIST, EACCES, ENAMETOO... #include // for printf, perror #include // for strcmp +#ifndef __EMSCRIPTEN__ #include // for mkdir, stat, S_IRWXU #include // for getuid +#endif #include // for exit, EXIT_FAILURE, srand #include // for basic_ostream, operator<<, basi... @@ -27,7 +29,7 @@ #include "title.h" // for Title #include "utils.h" // for options_t, input_t, boolToString -#ifndef _WIN32 +#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) #include #endif @@ -385,6 +387,12 @@ void Director::initOptions() { options->difficulty = DIFFICULTY_NORMAL; options->language = ba_BA; options->console = false; + +#ifdef __EMSCRIPTEN__ + // En Emscripten la ventana la gestiona el navegador + options->windowSize = 1; + options->videoMode = 0; +#endif } // Comprueba los parametros del programa @@ -402,7 +410,10 @@ void Director::checkProgramArguments(int argc, const char *argv[]) { // Crea la carpeta del sistema donde guardar datos void Director::createSystemFolder(const std::string &folder) { -#ifdef _WIN32 +#ifdef __EMSCRIPTEN__ + // En Emscripten usamos una carpeta en MEMFS (no persistente) + systemFolder = "/config/" + folder; +#elif _WIN32 systemFolder = std::string(getenv("APPDATA")) + "/" + folder; #elif __APPLE__ struct passwd *pw = getpwuid(getuid()); @@ -424,6 +435,10 @@ void Director::createSystemFolder(const std::string &folder) { } #endif +#ifdef __EMSCRIPTEN__ + // En Emscripten no necesitamos crear carpetas (MEMFS las crea automáticamente) + (void)folder; +#else struct stat st = {0}; if (stat(systemFolder.c_str(), &st) == -1) { errno = 0; @@ -453,6 +468,7 @@ void Director::createSystemFolder(const std::string &folder) { } } } +#endif } // Carga el fichero de configuración