Compare commits
6 Commits
2026-04-03
...
4ac34b8583
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ac34b8583 | |||
| cf7ea6cc9c | |||
| f5da35bfb2 | |||
| c0accd25e2 | |||
| ad8ad7e756 | |||
| 673587230e |
37
.clang-tidy
37
.clang-tidy
@@ -1,28 +1,25 @@
|
|||||||
Checks: >
|
Checks:
|
||||||
readability-*,
|
- readability-*
|
||||||
modernize-*,
|
- modernize-*
|
||||||
performance-*,
|
- performance-*
|
||||||
bugprone-unchecked-optional-access,
|
- bugprone-*
|
||||||
bugprone-sizeof-expression,
|
- -readability-identifier-length
|
||||||
bugprone-suspicious-missing-comma,
|
- -readability-magic-numbers
|
||||||
bugprone-suspicious-index,
|
- -bugprone-integer-division
|
||||||
bugprone-undefined-memory-manipulation,
|
- -bugprone-easily-swappable-parameters
|
||||||
bugprone-use-after-move,
|
- -bugprone-narrowing-conversions
|
||||||
bugprone-out-of-bound-access,
|
- -modernize-avoid-c-arrays,-warnings-as-errors
|
||||||
-readability-identifier-length,
|
|
||||||
-readability-magic-numbers,
|
|
||||||
-bugprone-narrowing-conversions,
|
|
||||||
-performance-enum-size,
|
|
||||||
-performance-inefficient-string-concatenation,
|
|
||||||
-bugprone-integer-division,
|
|
||||||
-bugprone-easily-swappable-parameters,
|
|
||||||
|
|
||||||
WarningsAsErrors: '*'
|
WarningsAsErrors: '*'
|
||||||
# Solo incluir archivos de tu código fuente
|
# Solo incluir archivos de tu código fuente (external tiene su propio .clang-tidy)
|
||||||
HeaderFilterRegex: '^source/(sections|ui)/.*'
|
# Excluye los headers SPIR-V generados en rendering/sdl3gpu/
|
||||||
|
HeaderFilterRegex: 'source/(?!external/|rendering/sdl3gpu/.*_spv\.h).*'
|
||||||
FormatStyle: file
|
FormatStyle: file
|
||||||
|
|
||||||
CheckOptions:
|
CheckOptions:
|
||||||
|
# bugprone-empty-catch: aceptar catches vacíos marcados con @INTENTIONAL en un comentario
|
||||||
|
- { key: bugprone-empty-catch.IgnoreCatchWithKeywords, value: '@INTENTIONAL' }
|
||||||
|
|
||||||
# Variables locales en snake_case
|
# Variables locales en snake_case
|
||||||
- { key: readability-identifier-naming.VariableCase, value: lower_case }
|
- { key: readability-identifier-naming.VariableCase, value: lower_case }
|
||||||
|
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,3 @@
|
|||||||
.vscode/
|
|
||||||
.claude/
|
.claude/
|
||||||
.cache/
|
.cache/
|
||||||
build/
|
build/
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"C_Cpp.default.compileCommands": "${workspaceFolder}/build/compile_commands.json"
|
||||||
|
}
|
||||||
@@ -106,7 +106,6 @@ set(APP_SOURCES
|
|||||||
|
|
||||||
# Fuentes de librerías de terceros
|
# Fuentes de librerías de terceros
|
||||||
set(EXTERNAL_SOURCES
|
set(EXTERNAL_SOURCES
|
||||||
source/external/jail_audio.cpp
|
|
||||||
source/external/json.hpp
|
source/external/json.hpp
|
||||||
source/external/gif.cpp
|
source/external/gif.cpp
|
||||||
)
|
)
|
||||||
@@ -140,13 +139,30 @@ if(NOT APPLE)
|
|||||||
set(ALL_SHADER_HEADERS "${SHADER_VERT_H}" "${SHADER_FRAG_H}" "${SHADER_CRTPI_H}" "${SHADER_UPSCALE_H}" "${SHADER_DOWNSCALE_H}")
|
set(ALL_SHADER_HEADERS "${SHADER_VERT_H}" "${SHADER_FRAG_H}" "${SHADER_CRTPI_H}" "${SHADER_UPSCALE_H}" "${SHADER_DOWNSCALE_H}")
|
||||||
|
|
||||||
if(GLSLC_EXE)
|
if(GLSLC_EXE)
|
||||||
|
set(COMPILE_SHADER_SCRIPT "${CMAKE_SOURCE_DIR}/tools/shaders/compile_shader.cmake")
|
||||||
|
|
||||||
|
macro(add_shader SRC_FILE OUT_H VAR_NAME)
|
||||||
|
cmake_parse_arguments(S "" "STAGE" "" ${ARGN})
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${ALL_SHADER_HEADERS}
|
OUTPUT "${OUT_H}"
|
||||||
COMMAND "${CMAKE_SOURCE_DIR}/tools/shaders/compile_spirv.sh"
|
COMMAND ${CMAKE_COMMAND}
|
||||||
DEPENDS ${ALL_SHADER_SOURCES}
|
"-DGLSLC=${GLSLC_EXE}"
|
||||||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
|
"-DSRC=${SRC_FILE}"
|
||||||
COMMENT "Compilando shaders SPIR-V..."
|
"-DOUT_H=${OUT_H}"
|
||||||
|
"-DVAR=${VAR_NAME}"
|
||||||
|
"-DSTAGE=${S_STAGE}"
|
||||||
|
-P "${COMPILE_SHADER_SCRIPT}"
|
||||||
|
DEPENDS "${SRC_FILE}" "${COMPILE_SHADER_SCRIPT}"
|
||||||
|
COMMENT "Compilando shader: ${VAR_NAME}"
|
||||||
)
|
)
|
||||||
|
endmacro()
|
||||||
|
|
||||||
|
add_shader("${SHADER_VERT_SRC}" "${SHADER_VERT_H}" "postfx_vert_spv")
|
||||||
|
add_shader("${SHADER_FRAG_SRC}" "${SHADER_FRAG_H}" "postfx_frag_spv")
|
||||||
|
add_shader("${SHADER_CRTPI_SRC}" "${SHADER_CRTPI_H}" "crtpi_frag_spv" STAGE fragment)
|
||||||
|
add_shader("${SHADER_UPSCALE_SRC}" "${SHADER_UPSCALE_H}" "upscale_frag_spv")
|
||||||
|
add_shader("${SHADER_DOWNSCALE_SRC}" "${SHADER_DOWNSCALE_H}" "downscale_frag_spv")
|
||||||
|
|
||||||
add_custom_target(shaders DEPENDS ${ALL_SHADER_HEADERS})
|
add_custom_target(shaders DEPENDS ${ALL_SHADER_HEADERS})
|
||||||
message(STATUS "glslc encontrado: shaders se compilarán automáticamente")
|
message(STATUS "glslc encontrado: shaders se compilarán automáticamente")
|
||||||
else()
|
else()
|
||||||
@@ -226,6 +242,7 @@ set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAK
|
|||||||
|
|
||||||
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
|
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
|
||||||
find_program(CLANG_FORMAT_EXE NAMES clang-format)
|
find_program(CLANG_FORMAT_EXE NAMES clang-format)
|
||||||
|
find_program(CPPCHECK_EXE NAMES cppcheck)
|
||||||
|
|
||||||
# Recopilar todos los archivos fuente, excluyendo external/
|
# Recopilar todos los archivos fuente, excluyendo external/
|
||||||
file(GLOB_RECURSE ALL_SOURCE_FILES
|
file(GLOB_RECURSE ALL_SOURCE_FILES
|
||||||
@@ -235,6 +252,14 @@ file(GLOB_RECURSE ALL_SOURCE_FILES
|
|||||||
)
|
)
|
||||||
list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX ".*/external/.*")
|
list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX ".*/external/.*")
|
||||||
|
|
||||||
|
# Para cppcheck, pasar solo .cpp (los headers se procesan transitivamente).
|
||||||
|
# Si pasamos .hpp como TUs independientes, cppcheck reporta falsos positivos de
|
||||||
|
# 'unusedStructMember' porque no hace análisis cross-TU y ve miembros de clase
|
||||||
|
# cuyo uso vive en un .cpp distinto.
|
||||||
|
set(CPPCHECK_SOURCES ${ALL_SOURCE_FILES})
|
||||||
|
list(FILTER CPPCHECK_SOURCES INCLUDE REGEX ".*\\.cpp$")
|
||||||
|
list(FILTER CPPCHECK_SOURCES EXCLUDE REGEX ".*_spv\\.h$")
|
||||||
|
|
||||||
# Targets de clang-tidy
|
# Targets de clang-tidy
|
||||||
if(CLANG_TIDY_EXE)
|
if(CLANG_TIDY_EXE)
|
||||||
# En macOS con clang-tidy de Homebrew LLVM, es necesario pasar el sysroot
|
# En macOS con clang-tidy de Homebrew LLVM, es necesario pasar el sysroot
|
||||||
@@ -291,3 +316,28 @@ if(CLANG_FORMAT_EXE)
|
|||||||
else()
|
else()
|
||||||
message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles")
|
message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# Targets de cppcheck
|
||||||
|
if(CPPCHECK_EXE)
|
||||||
|
add_custom_target(cppcheck
|
||||||
|
COMMAND ${CPPCHECK_EXE}
|
||||||
|
--enable=warning,style,performance,portability
|
||||||
|
--std=c++20
|
||||||
|
--language=c++
|
||||||
|
--inline-suppr
|
||||||
|
--suppress=missingIncludeSystem
|
||||||
|
--suppress=toomanyconfigs
|
||||||
|
-D_DEBUG
|
||||||
|
-DLINUX_BUILD
|
||||||
|
--quiet
|
||||||
|
-I ${CMAKE_SOURCE_DIR}/source
|
||||||
|
-I ${CMAKE_SOURCE_DIR}/source/external
|
||||||
|
-I ${CMAKE_SOURCE_DIR}/source/rendering
|
||||||
|
-I ${CMAKE_SOURCE_DIR}/source/rendering/sdl3gpu
|
||||||
|
${CPPCHECK_SOURCES}
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
COMMENT "Running cppcheck..."
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
message(STATUS "cppcheck no encontrado - target 'cppcheck' no disponible")
|
||||||
|
endif()
|
||||||
|
|||||||
8
Makefile
8
Makefile
@@ -8,7 +8,11 @@ DIR_TOOLS := $(addsuffix /, $(DIR_ROOT)tools)
|
|||||||
# TARGET NAMES
|
# TARGET NAMES
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
TARGET_NAME := coffee_crisis_arcade_edition
|
TARGET_NAME := coffee_crisis_arcade_edition
|
||||||
TARGET_FILE := $(DIR_ROOT)$(TARGET_NAME)
|
ifeq ($(OS),Windows_NT)
|
||||||
|
TARGET_FILE := $(DIR_ROOT)$(TARGET_NAME).exe
|
||||||
|
else
|
||||||
|
TARGET_FILE := $(DIR_ROOT)$(TARGET_NAME)
|
||||||
|
endif
|
||||||
APP_NAME := Coffee Crisis Arcade Edition
|
APP_NAME := Coffee Crisis Arcade Edition
|
||||||
DIST_DIR := dist
|
DIST_DIR := dist
|
||||||
RELEASE_FOLDER := dist/_tmp
|
RELEASE_FOLDER := dist/_tmp
|
||||||
@@ -138,7 +142,7 @@ windows_release:
|
|||||||
@powershell -Command "Copy-Item 'LICENSE' -Destination '$(RELEASE_FOLDER)'"
|
@powershell -Command "Copy-Item 'LICENSE' -Destination '$(RELEASE_FOLDER)'"
|
||||||
@powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'"
|
@powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'"
|
||||||
@powershell -Command "Copy-Item 'release\windows\dll\*.dll' -Destination '$(RELEASE_FOLDER)'"
|
@powershell -Command "Copy-Item 'release\windows\dll\*.dll' -Destination '$(RELEASE_FOLDER)'"
|
||||||
@powershell -Command "Copy-Item -Path '$(TARGET_FILE)' -Destination '\"$(WIN_RELEASE_FILE).exe\"'"
|
@powershell -Command "Copy-Item -Path '$(TARGET_FILE)' -Destination '$(WIN_RELEASE_FILE).exe'"
|
||||||
strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded
|
strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded
|
||||||
|
|
||||||
# Crea el fichero .zip
|
# Crea el fichero .zip
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
@@ -5,13 +5,13 @@
|
|||||||
#include <algorithm> // Para min
|
#include <algorithm> // Para min
|
||||||
#include <cstddef> // Para size_t
|
#include <cstddef> // Para size_t
|
||||||
#include <fstream> // Para basic_istream, basic_ifstream, istream, basic_ios, ifstream, istringstream, stringstream
|
#include <fstream> // Para basic_istream, basic_ifstream, istream, basic_ios, ifstream, istringstream, stringstream
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <sstream> // Para basic_istringstream, basic_stringstream
|
#include <sstream> // Para basic_istringstream, basic_stringstream
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <utility> // Para move, pair
|
#include <utility> // Para move, pair
|
||||||
|
|
||||||
#include "resource_helper.hpp" // Para loadFile
|
#include "resource_helper.hpp" // Para loadFile
|
||||||
#include "texture.hpp" // Para Texture
|
#include "texture.hpp" // Para Texture
|
||||||
#include "ui/logger.hpp" // Para dots
|
|
||||||
|
|
||||||
// Carga las animaciones en un vector(Animations) desde un fichero
|
// Carga las animaciones en un vector(Animations) desde un fichero
|
||||||
auto loadAnimationsFromFile(const std::string& file_path) -> AnimationsFileBuffer {
|
auto loadAnimationsFromFile(const std::string& file_path) -> AnimationsFileBuffer {
|
||||||
@@ -31,15 +31,13 @@ auto loadAnimationsFromFile(const std::string& file_path) -> AnimationsFileBuffe
|
|||||||
if (!using_resource_data) {
|
if (!using_resource_data) {
|
||||||
file.open(file_path);
|
file.open(file_path);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
std::cout << "Error: Fichero no encontrado " << file_path << '\n';
|
||||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::istream& input_stream = using_resource_data ? stream : static_cast<std::istream&>(file);
|
std::istream& input_stream = using_resource_data ? stream : static_cast<std::istream&>(file);
|
||||||
|
|
||||||
Logger::dots("Animation : ", file_path.substr(file_path.find_last_of("\\/") + 1), "[ LOADED ]");
|
|
||||||
|
|
||||||
std::vector<std::string> buffer;
|
std::vector<std::string> buffer;
|
||||||
std::string line;
|
std::string line;
|
||||||
while (std::getline(input_stream, line)) {
|
while (std::getline(input_stream, line)) {
|
||||||
@@ -82,7 +80,7 @@ auto AnimatedSprite::getAnimationIndex(const std::string& name) -> int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Si no se encuentra, muestra una advertencia y devuelve -1
|
// Si no se encuentra, muestra una advertencia y devuelve -1
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "** Warning: could not find \"%s\" animation", name.c_str());
|
std::cout << "** Warning: could not find \"" << name << "\" animation" << '\n';
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +217,7 @@ void AnimatedSprite::processConfigLine(const std::string& line, AnimationConfig&
|
|||||||
config.frame_height = value;
|
config.frame_height = value;
|
||||||
updateFrameCalculations(config);
|
updateFrameCalculations(config);
|
||||||
} else {
|
} else {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
|
std::cout << "Warning: unknown parameter " << key << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,7 +273,7 @@ void AnimatedSprite::processAnimationParameter(const std::string& line, Animatio
|
|||||||
} else if (key == "frames") {
|
} else if (key == "frames") {
|
||||||
parseFramesParameter(value, animation, config);
|
parseFramesParameter(value, animation, config);
|
||||||
} else {
|
} else {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: unknown parameter %s", key.c_str());
|
std::cout << "Warning: unknown parameter " << key << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
|
|
||||||
#include "resource_helper.hpp" // Para loadFile
|
#include "resource_helper.hpp" // Para loadFile
|
||||||
#include "ui/logger.hpp" // Para info, section, CYAN
|
|
||||||
#include "utils.hpp" // Para getFileName
|
#include "utils.hpp" // Para getFileName
|
||||||
|
|
||||||
// Singleton
|
// Singleton
|
||||||
@@ -37,9 +36,7 @@ void Asset::addToMap(const std::string& file_path, Type type, bool required, boo
|
|||||||
|
|
||||||
// Verificar si ya existe el archivo
|
// Verificar si ya existe el archivo
|
||||||
if (file_list_.contains(filename)) {
|
if (file_list_.contains(filename)) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Warning: Asset '" << filename << "' already exists, overwriting" << '\n';
|
||||||
"Warning: Asset '%s' already exists, overwriting",
|
|
||||||
filename.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file_list_.emplace(filename, Item{std::move(full_path), type, required});
|
file_list_.emplace(filename, Item{std::move(full_path), type, required});
|
||||||
@@ -54,9 +51,7 @@ void Asset::add(const std::string& file_path, Type type, bool required, bool abs
|
|||||||
void Asset::loadFromFile(const std::string& config_file_path, const std::string& prefix, const std::string& system_folder) {
|
void Asset::loadFromFile(const std::string& config_file_path, const std::string& prefix, const std::string& system_folder) {
|
||||||
std::ifstream file(config_file_path);
|
std::ifstream file(config_file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Error: Cannot open config file: " << config_file_path << '\n';
|
||||||
"Error: Cannot open config file: %s",
|
|
||||||
config_file_path.c_str());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,9 +84,7 @@ void Asset::loadFromFile(const std::string& config_file_path, const std::string&
|
|||||||
|
|
||||||
// Verificar que tenemos al menos tipo y ruta
|
// Verificar que tenemos al menos tipo y ruta
|
||||||
if (parts.size() < 2) {
|
if (parts.size() < 2) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Warning: Malformed line " << line_number << " in config file (insufficient fields)" << '\n';
|
||||||
"Warning: Malformed line %d in config file (insufficient fields)",
|
|
||||||
line_number);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,14 +111,10 @@ void Asset::loadFromFile(const std::string& config_file_path, const std::string&
|
|||||||
addToMap(path, type, required, absolute);
|
addToMap(path, type, required, absolute);
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Error parsing line " << line_number << " in config file: " << e.what() << '\n';
|
||||||
"Error parsing line %d in config file: %s",
|
|
||||||
line_number,
|
|
||||||
e.what());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Loaded " << file_list_.size() << " assets from config file" << '\n';
|
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +125,7 @@ auto Asset::getPath(const std::string& filename) const -> std::string {
|
|||||||
return it->second.file;
|
return it->second.file;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: file %s not found", filename.c_str());
|
std::cout << "Warning: file " << filename << " not found" << '\n';
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +136,7 @@ auto Asset::loadData(const std::string& filename) const -> std::vector<uint8_t>
|
|||||||
return ResourceHelper::loadFile(it->second.file);
|
return ResourceHelper::loadFile(it->second.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning: file %s not found for data loading", filename.c_str());
|
std::cout << "Warning: file " << filename << " not found for data loading" << '\n';
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,9 +149,6 @@ auto Asset::exists(const std::string& filename) const -> bool {
|
|||||||
auto Asset::check() const -> bool {
|
auto Asset::check() const -> bool {
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n** CHECKING FILES");
|
|
||||||
Logger::section("CHECKING FILES", Logger::CYAN);
|
|
||||||
|
|
||||||
// Agrupar por tipo para mostrar organizado
|
// Agrupar por tipo para mostrar organizado
|
||||||
std::unordered_map<Type, std::vector<const Item*>> by_type;
|
std::unordered_map<Type, std::vector<const Item*>> by_type;
|
||||||
|
|
||||||
@@ -177,19 +163,11 @@ auto Asset::check() const -> bool {
|
|||||||
Type asset_type = static_cast<Type>(type);
|
Type asset_type = static_cast<Type>(type);
|
||||||
|
|
||||||
if (by_type.contains(asset_type)) {
|
if (by_type.contains(asset_type)) {
|
||||||
Logger::info(getTypeName(asset_type) + " FILES");
|
|
||||||
|
|
||||||
bool type_success = true;
|
|
||||||
for (const auto* item : by_type[asset_type]) {
|
for (const auto* item : by_type[asset_type]) {
|
||||||
if (!checkFile(item->file)) {
|
if (!checkFile(item->file)) {
|
||||||
success = false;
|
success = false;
|
||||||
type_success = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type_success) {
|
|
||||||
Logger::info("All files are OK.\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
@@ -215,9 +193,7 @@ auto Asset::checkFile(const std::string& path) const -> bool {
|
|||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Error: Could not open file: " << path << '\n';
|
||||||
"Error: Could not open file: %s",
|
|
||||||
path.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
|
|||||||
@@ -3,11 +3,25 @@
|
|||||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_G...
|
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_G...
|
||||||
|
|
||||||
#include <algorithm> // Para clamp
|
#include <algorithm> // Para clamp
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
|
|
||||||
#include "external/jail_audio.h" // Para JA_FadeOutMusic, JA_Init, JA_PauseM...
|
// Implementación de stb_vorbis (debe estar ANTES de incluir jail_audio.hpp).
|
||||||
|
// clang-format off
|
||||||
|
#undef STB_VORBIS_HEADER_ONLY
|
||||||
|
#include "external/stb_vorbis.h"
|
||||||
|
// stb_vorbis.h filtra les macros L, C i R (i PLAYBACK_*) al TU. Les netegem
|
||||||
|
// perquè xocarien amb noms de paràmetres de plantilla en json.hpp i altres.
|
||||||
|
#undef L
|
||||||
|
#undef C
|
||||||
|
#undef R
|
||||||
|
#undef PLAYBACK_MONO
|
||||||
|
#undef PLAYBACK_LEFT
|
||||||
|
#undef PLAYBACK_RIGHT
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
#include "external/jail_audio.hpp" // Para JA_FadeOutMusic, JA_Init, JA_PauseM...
|
||||||
#include "options.hpp" // Para AudioOptions, audio, MusicOptions
|
#include "options.hpp" // Para AudioOptions, audio, MusicOptions
|
||||||
#include "resource.hpp" // Para Resource
|
#include "resource.hpp" // Para Resource
|
||||||
#include "ui/logger.hpp" // Para logger
|
|
||||||
|
|
||||||
// Singleton
|
// Singleton
|
||||||
Audio* Audio::instance = nullptr;
|
Audio* Audio::instance = nullptr;
|
||||||
@@ -140,11 +154,9 @@ void Audio::enable(bool value) {
|
|||||||
// Inicializa SDL Audio
|
// Inicializa SDL Audio
|
||||||
void Audio::initSDLAudio() {
|
void Audio::initSDLAudio() {
|
||||||
if (!SDL_Init(SDL_INIT_AUDIO)) {
|
if (!SDL_Init(SDL_INIT_AUDIO)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
|
std::cout << "SDL_AUDIO could not initialize! SDL Error: " << SDL_GetError() << '\n';
|
||||||
} else {
|
} else {
|
||||||
JA_Init(FREQUENCY, SDL_AUDIO_S16LE, 2);
|
JA_Init(FREQUENCY, SDL_AUDIO_S16LE, 2);
|
||||||
enable(Options::audio.enabled);
|
enable(Options::audio.enabled);
|
||||||
|
|
||||||
Logger::info("Audio system initialized successfully");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
#include <SDL3/SDL.h> // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO
|
#include <SDL3/SDL.h> // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO
|
||||||
|
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
|
|
||||||
#include "resource_helper.hpp" // Para ResourceHelper
|
#include "resource_helper.hpp" // Para ResourceHelper
|
||||||
#include "ui/logger.hpp"
|
#include "utils.hpp" // Para getFileName
|
||||||
#include "utils.hpp" // Para printWithDots, getFileName
|
|
||||||
|
|
||||||
// Carga el fichero de datos para la demo
|
// Carga el fichero de datos para la demo
|
||||||
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData {
|
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData {
|
||||||
@@ -24,10 +24,9 @@ auto loadDemoDataFromFile(const std::string& file_path) -> DemoData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (file == nullptr) {
|
if (file == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
std::cout << "Error: Fichero no encontrado " << file_path << '\n';
|
||||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||||
}
|
}
|
||||||
Logger::dots("DemoData : ", getFileName(file_path), "[ LOADED ]");
|
|
||||||
|
|
||||||
// Lee todos los datos del fichero y los deja en el destino
|
// Lee todos los datos del fichero y los deja en el destino
|
||||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
|
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
|
||||||
@@ -52,18 +51,15 @@ bool saveDemoFile(const std::string& file_path, const DemoData& dd) {
|
|||||||
// Guarda los datos
|
// Guarda los datos
|
||||||
for (const auto& data : dd) {
|
for (const auto& data : dd) {
|
||||||
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
|
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
|
std::cout << "Error al escribir el fichero " << getFileName(file_path) << '\n';
|
||||||
success = false;
|
success = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (success) {
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
|
|
||||||
}
|
|
||||||
// Cierra el fichero
|
// Cierra el fichero
|
||||||
SDL_CloseIO(file);
|
SDL_CloseIO(file);
|
||||||
} else {
|
} else {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
std::cout << "Error: Unable to save " << getFileName(file_path) << " file! " << SDL_GetError() << '\n';
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,16 @@
|
|||||||
|
|
||||||
#include <cstdlib> // Para srand, exit, rand, EXIT_FAILURE
|
#include <cstdlib> // Para srand, exit, rand, EXIT_FAILURE
|
||||||
#include <ctime> // Para time
|
#include <ctime> // Para time
|
||||||
#include <filesystem> // Para path, absolute
|
|
||||||
#include <fstream> // Para ifstream, ofstream
|
#include <fstream> // Para ifstream, ofstream
|
||||||
#include <iostream> // Para basic_ostream, operator<<, cerr
|
#include <iostream> // Para basic_ostream, operator<<, cerr
|
||||||
#include <memory> // Para make_unique, unique_ptr
|
#include <memory> // Para make_unique, unique_ptr
|
||||||
#include <span> // Para span
|
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string> // Para allocator, basic_string, char_traits, operator+, string, operator==
|
#include <string> // Para allocator, basic_string, char_traits, operator+, string, operator==
|
||||||
|
|
||||||
#include "asset.hpp" // Para Asset
|
#include "asset.hpp" // Para Asset
|
||||||
#include "audio.hpp" // Para Audio
|
#include "audio.hpp" // Para Audio
|
||||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||||
|
#include "global_events.hpp" // Para GlobalEvents::handle
|
||||||
#include "input.hpp" // Para Input
|
#include "input.hpp" // Para Input
|
||||||
#include "lang.hpp" // Para setLanguage
|
#include "lang.hpp" // Para setLanguage
|
||||||
#include "manage_hiscore_table.hpp" // Para ManageHiScoreTable
|
#include "manage_hiscore_table.hpp" // Para ManageHiScoreTable
|
||||||
@@ -35,26 +34,25 @@
|
|||||||
#include "sections/title.hpp" // Para Title
|
#include "sections/title.hpp" // Para Title
|
||||||
#include "shutdown.hpp" // Para resultToString, shutdownSystem, ShutdownResult
|
#include "shutdown.hpp" // Para resultToString, shutdownSystem, ShutdownResult
|
||||||
#include "system_utils.hpp" // Para createApplicationFolder, resultToString, Result
|
#include "system_utils.hpp" // Para createApplicationFolder, resultToString, Result
|
||||||
#include "ui/logger.hpp" // Para section, put
|
|
||||||
#include "ui/notifier.hpp" // Para Notifier
|
#include "ui/notifier.hpp" // Para Notifier
|
||||||
#include "ui/service_menu.hpp" // Para ServiceMenu
|
#include "ui/service_menu.hpp" // Para ServiceMenu
|
||||||
#include "utils.hpp" // Para Overrides, overrides
|
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Director::Director(int argc, std::span<char*> argv) {
|
Director::Director() {
|
||||||
Section::attract_mode = Section::AttractMode::TITLE_TO_DEMO;
|
Section::attract_mode = Section::AttractMode::TITLE_TO_DEMO;
|
||||||
|
|
||||||
// Establece el nivel de prioridad de la categoría de registro
|
// Establece el nivel de prioridad de la categoría de registro
|
||||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR);
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_TEST, SDL_LOG_PRIORITY_ERROR);
|
||||||
|
|
||||||
Logger::put("Game start\n");
|
|
||||||
|
|
||||||
// Inicia la semilla aleatoria usando el tiempo actual en segundos
|
// Inicia la semilla aleatoria usando el tiempo actual en segundos
|
||||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||||
|
|
||||||
// Comprueba los parametros del programa
|
std::cout << "Game start\n";
|
||||||
checkProgramArguments(argc, argv);
|
|
||||||
|
// Obtener la ruta del ejecutable desde SDL
|
||||||
|
const char* base_path = SDL_GetBasePath();
|
||||||
|
executable_path_ = (base_path != nullptr) ? base_path : "";
|
||||||
|
|
||||||
// Crea la carpeta del sistema donde guardar los datos persistentes
|
// Crea la carpeta del sistema donde guardar los datos persistentes
|
||||||
createSystemFolder("jailgames");
|
createSystemFolder("jailgames");
|
||||||
@@ -75,8 +73,10 @@ Director::Director(int argc, std::span<char*> argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Director::~Director() {
|
Director::~Director() {
|
||||||
|
// Libera las secciones primero: sus destructores pueden tocar Audio/Resource/Screen,
|
||||||
|
// que close() destruye a continuación.
|
||||||
|
resetActiveSection();
|
||||||
close();
|
close();
|
||||||
Logger::put("\nBye!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inicializa todo
|
// Inicializa todo
|
||||||
@@ -102,10 +102,8 @@ void Director::init() {
|
|||||||
|
|
||||||
loadAssets(); // Crea el índice de archivos
|
loadAssets(); // Crea el índice de archivos
|
||||||
|
|
||||||
Logger::section("INIT INPUT");
|
|
||||||
Input::init(Asset::get()->getPath("gamecontrollerdb.txt"), Asset::get()->getPath("controllers.json")); // Carga configuración de controles
|
Input::init(Asset::get()->getPath("gamecontrollerdb.txt"), Asset::get()->getPath("controllers.json")); // Carga configuración de controles
|
||||||
|
|
||||||
Logger::section("INIT CONFIG");
|
|
||||||
Options::setConfigFile(Asset::get()->getPath("config.yaml")); // Establece el fichero de configuración
|
Options::setConfigFile(Asset::get()->getPath("config.yaml")); // Establece el fichero de configuración
|
||||||
Options::setControllersFile(Asset::get()->getPath("controllers.json")); // Establece el fichero de configuración de mandos
|
Options::setControllersFile(Asset::get()->getPath("controllers.json")); // Establece el fichero de configuración de mandos
|
||||||
Options::setPostFXFile(Asset::get()->getPath("postfx.yaml")); // Establece el fichero de presets PostFX
|
Options::setPostFXFile(Asset::get()->getPath("postfx.yaml")); // Establece el fichero de presets PostFX
|
||||||
@@ -119,13 +117,10 @@ void Director::init() {
|
|||||||
// Inicialización de subsistemas principales
|
// Inicialización de subsistemas principales
|
||||||
Lang::setLanguage(Options::settings.language); // Carga el archivo de idioma
|
Lang::setLanguage(Options::settings.language); // Carga el archivo de idioma
|
||||||
|
|
||||||
Logger::section("INIT VIDEO");
|
|
||||||
Screen::init(); // Inicializa la pantalla y el sistema de renderizado
|
Screen::init(); // Inicializa la pantalla y el sistema de renderizado
|
||||||
|
|
||||||
Logger::section("INIT AUDIO");
|
|
||||||
Audio::init(); // Activa el sistema de audio
|
Audio::init(); // Activa el sistema de audio
|
||||||
|
|
||||||
Logger::section("INIT RESOURCES");
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
Resource::init(debug_config.resource_loading == "lazy" ? Resource::LoadingMode::LAZY_LOAD : Resource::LoadingMode::PRELOAD);
|
Resource::init(debug_config.resource_loading == "lazy" ? Resource::LoadingMode::LAZY_LOAD : Resource::LoadingMode::PRELOAD);
|
||||||
#else
|
#else
|
||||||
@@ -134,8 +129,6 @@ void Director::init() {
|
|||||||
ServiceMenu::init(); // Inicializa el menú de servicio
|
ServiceMenu::init(); // Inicializa el menú de servicio
|
||||||
Notifier::init(std::string(), Resource::get()->getText("8bithud")); // Inicialización del sistema de notificaciones
|
Notifier::init(std::string(), Resource::get()->getText("8bithud")); // Inicialización del sistema de notificaciones
|
||||||
Screen::get()->getSingletons(); // Obtiene los punteros al resto de singletones
|
Screen::get()->getSingletons(); // Obtiene los punteros al resto de singletones
|
||||||
|
|
||||||
Logger::section("GAME LOG");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cierra todo y libera recursos del sistema y de los singletons
|
// Cierra todo y libera recursos del sistema y de los singletons
|
||||||
@@ -152,6 +145,8 @@ void Director::close() {
|
|||||||
Screen::destroy(); // Libera el sistema de pantalla y renderizado
|
Screen::destroy(); // Libera el sistema de pantalla y renderizado
|
||||||
Asset::destroy(); // Libera el gestor de archivos
|
Asset::destroy(); // Libera el gestor de archivos
|
||||||
|
|
||||||
|
std::cout << "\nBye!\n";
|
||||||
|
|
||||||
// Libera todos los recursos de SDL
|
// Libera todos los recursos de SDL
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|
||||||
@@ -176,11 +171,7 @@ void Director::loadScoreFile() {
|
|||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
manager->clear();
|
manager->clear();
|
||||||
#else
|
#else
|
||||||
if (overrides.clear_hi_score_table) {
|
|
||||||
manager->clear();
|
|
||||||
} else {
|
|
||||||
manager->loadFromFile(Asset::get()->getPath("score.bin"));
|
manager->loadFromFile(Asset::get()->getPath("score.bin"));
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,37 +187,12 @@ void Director::loadAssets() {
|
|||||||
std::string config_path = executable_path_ + PREFIX + "/config/assets.txt";
|
std::string config_path = executable_path_ + PREFIX + "/config/assets.txt";
|
||||||
Asset::get()->loadFromFile(config_path, PREFIX, system_folder_);
|
Asset::get()->loadFromFile(config_path, PREFIX, system_folder_);
|
||||||
|
|
||||||
Logger::put("Assets configuration loaded successfully");
|
|
||||||
|
|
||||||
// Si falta algun fichero, sale del programa
|
// Si falta algun fichero, sale del programa
|
||||||
if (!Asset::get()->check()) {
|
if (!Asset::get()->check()) {
|
||||||
throw std::runtime_error("Falta algun fichero");
|
throw std::runtime_error("Falta algun fichero");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba los parametros del programa
|
|
||||||
void Director::checkProgramArguments(int argc, std::span<char*> argv) {
|
|
||||||
// Obtener la ruta absoluta del ejecutable
|
|
||||||
std::filesystem::path exe_path = std::filesystem::absolute(argv[0]);
|
|
||||||
executable_path_ = exe_path.parent_path().string();
|
|
||||||
|
|
||||||
// Asegurar que termine con separador de directorio
|
|
||||||
if (!executable_path_.empty() && executable_path_.back() != '/' && executable_path_.back() != '\\') {
|
|
||||||
executable_path_ += "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comprueba el resto de parámetros
|
|
||||||
for (int i = 1; i < argc; ++i) {
|
|
||||||
std::string arg = argv[i];
|
|
||||||
|
|
||||||
if (arg == "--320x240") {
|
|
||||||
overrides.param_file = arg;
|
|
||||||
} else if (arg == "--clear_score") {
|
|
||||||
overrides.clear_hi_score_table = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Carga debug.yaml desde la carpeta del sistema (solo en _DEBUG)
|
// Carga debug.yaml desde la carpeta del sistema (solo en _DEBUG)
|
||||||
void Director::loadDebugConfig() {
|
void Director::loadDebugConfig() {
|
||||||
const std::string DEBUG_FILE = system_folder_ + "/debug.yaml";
|
const std::string DEBUG_FILE = system_folder_ + "/debug.yaml";
|
||||||
@@ -251,7 +217,6 @@ void Director::loadDebugConfig() {
|
|||||||
out << "show_render_info: true\n";
|
out << "show_render_info: true\n";
|
||||||
out << "resource_loading: preload\n";
|
out << "resource_loading: preload\n";
|
||||||
out.close();
|
out.close();
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Debug config created: %s", DEBUG_FILE.c_str());
|
|
||||||
}
|
}
|
||||||
// Usar defaults de DebugConfig
|
// Usar defaults de DebugConfig
|
||||||
} else {
|
} else {
|
||||||
@@ -284,9 +249,8 @@ void Director::loadDebugConfig() {
|
|||||||
debug_config.resource_loading = yaml["resource_loading"].get_value<std::string>();
|
debug_config.resource_loading = yaml["resource_loading"].get_value<std::string>();
|
||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
}
|
}
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Debug config loaded: section=%s options=%s stage=%d", debug_config.initial_section.c_str(), debug_config.initial_options.c_str(), debug_config.initial_stage);
|
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Error parsing debug.yaml, using defaults");
|
std::cout << "Error parsing debug.yaml, using defaults" << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,28 +299,48 @@ void Director::createSystemFolder(const std::string& folder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ejecuta la sección con el logo
|
// Libera todos los unique_ptr de sección (solo uno tiene propiedad a la vez)
|
||||||
void Director::runLogo() {
|
void Director::resetActiveSection() {
|
||||||
auto logo = std::make_unique<Logo>();
|
logo_.reset();
|
||||||
logo->run();
|
intro_.reset();
|
||||||
|
title_.reset();
|
||||||
|
game_.reset();
|
||||||
|
instructions_.reset();
|
||||||
|
hi_score_table_.reset();
|
||||||
|
credits_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ejecuta la sección con la secuencia de introducción
|
// Destruye la sección anterior y construye la nueva cuando Section::name cambia
|
||||||
void Director::runIntro() {
|
void Director::handleSectionTransition() {
|
||||||
auto intro = std::make_unique<Intro>();
|
// RESET: recarga recursos y vuelve a LOGO (el propio reset() cambia Section::name)
|
||||||
intro->run();
|
if (Section::name == Section::Name::RESET) {
|
||||||
}
|
resetActiveSection(); // libera recursos actuales antes del reload
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
// Ejecuta la sección con el título del juego
|
if (Section::name == last_built_section_name_) {
|
||||||
void Director::runTitle() {
|
return; // ya tenemos la sección correcta viva
|
||||||
auto title = std::make_unique<Title>();
|
}
|
||||||
title->run();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ejecuta la sección donde se juega al juego
|
// Destruye la sección anterior
|
||||||
void Director::runGame() {
|
resetActiveSection();
|
||||||
|
|
||||||
|
// Construye la nueva
|
||||||
|
switch (Section::name) {
|
||||||
|
case Section::Name::LOGO:
|
||||||
|
logo_ = std::make_unique<Logo>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::INTRO:
|
||||||
|
intro_ = std::make_unique<Intro>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::TITLE:
|
||||||
|
title_ = std::make_unique<Title>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::GAME: {
|
||||||
Player::Id player_id = Player::Id::PLAYER1;
|
Player::Id player_id = Player::Id::PLAYER1;
|
||||||
|
|
||||||
switch (Section::options) {
|
switch (Section::options) {
|
||||||
case Section::Options::GAME_PLAY_1P:
|
case Section::Options::GAME_PLAY_1P:
|
||||||
player_id = Player::Id::PLAYER1;
|
player_id = Player::Id::PLAYER1;
|
||||||
@@ -370,40 +354,41 @@ void Director::runGame() {
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
const int CURRENT_STAGE = debug_config.initial_stage;
|
const int CURRENT_STAGE = debug_config.initial_stage;
|
||||||
#else
|
#else
|
||||||
constexpr int CURRENT_STAGE = 0;
|
constexpr int CURRENT_STAGE = 0;
|
||||||
#endif
|
#endif
|
||||||
auto game = std::make_unique<Game>(player_id, CURRENT_STAGE, Game::DEMO_OFF);
|
game_ = std::make_unique<Game>(player_id, CURRENT_STAGE, Game::DEMO_OFF);
|
||||||
game->run();
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ejecuta la sección donde se muestran las instrucciones
|
case Section::Name::GAME_DEMO: {
|
||||||
void Director::runInstructions() {
|
|
||||||
auto instructions = std::make_unique<Instructions>();
|
|
||||||
instructions->run();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ejecuta la sección donde se muestran los creditos del programa
|
|
||||||
void Director::runCredits() {
|
|
||||||
auto credits = std::make_unique<Credits>();
|
|
||||||
credits->run();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ejecuta la sección donde se muestra la tabla de puntuaciones
|
|
||||||
void Director::runHiScoreTable() {
|
|
||||||
auto hi_score_table = std::make_unique<HiScoreTable>();
|
|
||||||
hi_score_table->run();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ejecuta el juego en modo demo
|
|
||||||
void Director::runDemoGame() {
|
|
||||||
const auto PLAYER_ID = static_cast<Player::Id>((rand() % 2) + 1);
|
const auto PLAYER_ID = static_cast<Player::Id>((rand() % 2) + 1);
|
||||||
constexpr auto CURRENT_STAGE = 0;
|
constexpr auto CURRENT_STAGE = 0;
|
||||||
auto game = std::make_unique<Game>(PLAYER_ID, CURRENT_STAGE, Game::DEMO_ON);
|
game_ = std::make_unique<Game>(PLAYER_ID, CURRENT_STAGE, Game::DEMO_ON);
|
||||||
game->run();
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Section::Name::INSTRUCTIONS:
|
||||||
|
instructions_ = std::make_unique<Instructions>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::CREDITS:
|
||||||
|
credits_ = std::make_unique<Credits>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::HI_SCORE_TABLE:
|
||||||
|
hi_score_table_ = std::make_unique<HiScoreTable>();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Section::Name::RESET:
|
||||||
|
case Section::Name::QUIT:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_built_section_name_ = Section::name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reinicia objetos y vuelve a la sección inicial
|
// Reinicia objetos y vuelve a la sección inicial
|
||||||
@@ -418,43 +403,58 @@ void Director::reset() {
|
|||||||
Section::name = Section::Name::LOGO;
|
Section::name = Section::Name::LOGO;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Director::run() -> int {
|
// Avanza un frame de la sección activa (llamado desde SDL_AppIterate)
|
||||||
// Bucle principal
|
auto Director::iterate() -> SDL_AppResult {
|
||||||
while (Section::name != Section::Name::QUIT) {
|
if (Section::name == Section::Name::QUIT) {
|
||||||
switch (Section::name) {
|
return SDL_APP_SUCCESS;
|
||||||
case Section::Name::RESET:
|
|
||||||
reset();
|
|
||||||
break;
|
|
||||||
case Section::Name::LOGO:
|
|
||||||
runLogo();
|
|
||||||
break;
|
|
||||||
case Section::Name::INTRO:
|
|
||||||
runIntro();
|
|
||||||
break;
|
|
||||||
case Section::Name::TITLE:
|
|
||||||
runTitle();
|
|
||||||
break;
|
|
||||||
case Section::Name::GAME:
|
|
||||||
runGame();
|
|
||||||
break;
|
|
||||||
case Section::Name::HI_SCORE_TABLE:
|
|
||||||
runHiScoreTable();
|
|
||||||
break;
|
|
||||||
case Section::Name::GAME_DEMO:
|
|
||||||
runDemoGame();
|
|
||||||
break;
|
|
||||||
case Section::Name::INSTRUCTIONS:
|
|
||||||
runInstructions();
|
|
||||||
break;
|
|
||||||
case Section::Name::CREDITS:
|
|
||||||
runCredits();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
// Gestiona las transiciones entre secciones (destruye la anterior y construye la nueva)
|
||||||
|
handleSectionTransition();
|
||||||
|
|
||||||
|
// Ejecuta un frame de la sección activa
|
||||||
|
if (logo_) {
|
||||||
|
logo_->iterate();
|
||||||
|
} else if (intro_) {
|
||||||
|
intro_->iterate();
|
||||||
|
} else if (title_) {
|
||||||
|
title_->iterate();
|
||||||
|
} else if (game_) {
|
||||||
|
game_->iterate();
|
||||||
|
} else if (instructions_) {
|
||||||
|
instructions_->iterate();
|
||||||
|
} else if (hi_score_table_) {
|
||||||
|
hi_score_table_->iterate();
|
||||||
|
} else if (credits_) {
|
||||||
|
credits_->iterate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Section::name == Section::Name::QUIT) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento SDL (llamado desde SDL_AppEvent)
|
||||||
|
auto Director::handleEvent(SDL_Event& event) -> SDL_AppResult {
|
||||||
|
// Eventos globales (SDL_EVENT_QUIT, resize, render target reset, hotplug, service menu, ratón)
|
||||||
|
GlobalEvents::handle(event);
|
||||||
|
|
||||||
|
// Reenvía a la sección activa
|
||||||
|
if (logo_) {
|
||||||
|
logo_->handleEvent(event);
|
||||||
|
} else if (intro_) {
|
||||||
|
intro_->handleEvent(event);
|
||||||
|
} else if (title_) {
|
||||||
|
title_->handleEvent(event);
|
||||||
|
} else if (game_) {
|
||||||
|
game_->handleEvent(event);
|
||||||
|
} else if (instructions_) {
|
||||||
|
instructions_->handleEvent(event);
|
||||||
|
} else if (hi_score_table_) {
|
||||||
|
hi_score_table_->handleEvent(event);
|
||||||
|
} else if (credits_) {
|
||||||
|
credits_->handleEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Section::name == Section::Name::QUIT) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apaga el sistema de forma segura
|
// Apaga el sistema de forma segura
|
||||||
|
|||||||
@@ -1,21 +1,35 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <span> // Para Span
|
#include <SDL3/SDL.h> // Para SDL_AppResult, SDL_Event
|
||||||
|
|
||||||
|
#include <memory> // Para unique_ptr
|
||||||
#include <string> // Para string
|
#include <string> // Para string
|
||||||
|
|
||||||
|
#include "section.hpp" // Para Section::Name
|
||||||
|
|
||||||
namespace Lang {
|
namespace Lang {
|
||||||
enum class Code : int;
|
enum class Code : int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Declaraciones adelantadas de las secciones
|
||||||
|
class Logo;
|
||||||
|
class Intro;
|
||||||
|
class Title;
|
||||||
|
class Game;
|
||||||
|
class Instructions;
|
||||||
|
class HiScoreTable;
|
||||||
|
class Credits;
|
||||||
|
|
||||||
// --- Clase Director: gestor principal de la aplicación ---
|
// --- Clase Director: gestor principal de la aplicación ---
|
||||||
class Director {
|
class Director {
|
||||||
public:
|
public:
|
||||||
// --- Constructor y destructor ---
|
// --- Constructor y destructor ---
|
||||||
Director(int argc, std::span<char*> argv);
|
Director();
|
||||||
~Director();
|
~Director();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para SDL_MAIN_USE_CALLBACKS ---
|
||||||
static auto run() -> int;
|
auto iterate() -> SDL_AppResult; // Avanza un frame de la sección activa
|
||||||
|
auto handleEvent(SDL_Event& event) -> SDL_AppResult; // Procesa un evento SDL
|
||||||
|
|
||||||
// --- Debug config (accesible desde otras clases) ---
|
// --- Debug config (accesible desde otras clases) ---
|
||||||
struct DebugConfig {
|
struct DebugConfig {
|
||||||
@@ -37,6 +51,16 @@ class Director {
|
|||||||
std::string executable_path_; // Ruta del ejecutable
|
std::string executable_path_; // Ruta del ejecutable
|
||||||
std::string system_folder_; // Carpeta del sistema para almacenar datos
|
std::string system_folder_; // Carpeta del sistema para almacenar datos
|
||||||
|
|
||||||
|
// --- Sección activa (una y sólo una viva en cada momento) ---
|
||||||
|
std::unique_ptr<Logo> logo_;
|
||||||
|
std::unique_ptr<Intro> intro_;
|
||||||
|
std::unique_ptr<Title> title_;
|
||||||
|
std::unique_ptr<Game> game_;
|
||||||
|
std::unique_ptr<Instructions> instructions_;
|
||||||
|
std::unique_ptr<HiScoreTable> hi_score_table_;
|
||||||
|
std::unique_ptr<Credits> credits_;
|
||||||
|
Section::Name last_built_section_name_ = Section::Name::RESET;
|
||||||
|
|
||||||
// --- Inicialización y cierre del sistema ---
|
// --- Inicialización y cierre del sistema ---
|
||||||
void init(); // Inicializa la aplicación
|
void init(); // Inicializa la aplicación
|
||||||
static void close(); // Cierra y libera recursos
|
static void close(); // Cierra y libera recursos
|
||||||
@@ -49,17 +73,10 @@ class Director {
|
|||||||
|
|
||||||
// --- Gestión de entrada y archivos ---
|
// --- Gestión de entrada y archivos ---
|
||||||
void loadAssets(); // Crea el índice de archivos disponibles
|
void loadAssets(); // Crea el índice de archivos disponibles
|
||||||
void checkProgramArguments(int argc, std::span<char*> argv); // Verifica los parámetros del programa // NOLINT(modernize-avoid-c-arrays)
|
|
||||||
|
|
||||||
// --- Secciones del programa ---
|
// --- Gestión de secciones ---
|
||||||
static void runLogo(); // Ejecuta la pantalla con el logo
|
void handleSectionTransition(); // Destruye la sección anterior y construye la nueva si Section::name ha cambiado
|
||||||
static void runIntro(); // Ejecuta la introducción del juego
|
void resetActiveSection(); // Libera todos los unique_ptr de sección
|
||||||
static void runTitle(); // Ejecuta la pantalla de título
|
|
||||||
static void runGame(); // Inicia el juego
|
|
||||||
static void runInstructions(); // Muestra las instrucciones
|
|
||||||
static void runCredits(); // Muestra los créditos del juego
|
|
||||||
static void runHiScoreTable(); // Muestra la tabla de puntuaciones
|
|
||||||
static void runDemoGame(); // Ejecuta el modo demo
|
|
||||||
static void reset(); // Reinicia objetos y vuelve a la sección inicial
|
static void reset(); // Reinicia objetos y vuelve a la sección inicial
|
||||||
|
|
||||||
// --- Gestión de archivos de idioma ---
|
// --- Gestión de archivos de idioma ---
|
||||||
|
|||||||
20
source/external/gif.cpp
vendored
20
source/external/gif.cpp
vendored
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <SDL3/SDL.h> // Para SDL_LogError, SDL_LogCategory, SDL_LogInfo
|
#include <SDL3/SDL.h> // Para SDL_LogError, SDL_LogCategory, SDL_LogInfo
|
||||||
#include <cstring> // Para memcpy, size_t
|
#include <cstring> // Para memcpy, size_t
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string> // Para char_traits, operator==, basic_string, string
|
#include <string> // Para char_traits, operator==, basic_string, string
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ inline void readBytes(const uint8_t *&buffer, void *dst, size_t size) {
|
|||||||
|
|
||||||
void Gif::decompress(int code_length, const uint8_t *input, int input_length, uint8_t *out) {
|
void Gif::decompress(int code_length, const uint8_t *input, int input_length, uint8_t *out) {
|
||||||
if (code_length < 2 || code_length > 12) {
|
if (code_length < 2 || code_length > 12) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid LZW code length: %d", code_length);
|
std::cout << "Invalid LZW code length: " << code_length << '\n';
|
||||||
throw std::runtime_error("Invalid LZW code length");
|
throw std::runtime_error("Invalid LZW code length");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ void Gif::decompress(int code_length, const uint8_t *input, int input_length, ui
|
|||||||
int code = 0;
|
int code = 0;
|
||||||
for (i = 0; i < (code_length + 1); i++) {
|
for (i = 0; i < (code_length + 1); i++) {
|
||||||
if (input_length <= 0) {
|
if (input_length <= 0) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unexpected end of input in decompress");
|
std::cout << "Unexpected end of input in decompress" << '\n';
|
||||||
throw std::runtime_error("Unexpected end of input in decompress");
|
throw std::runtime_error("Unexpected end of input in decompress");
|
||||||
}
|
}
|
||||||
bit = ((*input & mask) != 0) ? 1 : 0;
|
bit = ((*input & mask) != 0) ? 1 : 0;
|
||||||
@@ -69,7 +70,7 @@ void Gif::decompress(int code_length, const uint8_t *input, int input_length, ui
|
|||||||
|
|
||||||
if (prev > -1 && code_length < 12) {
|
if (prev > -1 && code_length < 12) {
|
||||||
if (code > dictionary_ind) {
|
if (code > dictionary_ind) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "LZW error: code (%d) exceeds dictionary_ind (%d)", code, dictionary_ind);
|
std::cout << "LZW error: code (" << code << ") exceeds dictionary_ind (" << dictionary_ind << ")" << '\n';
|
||||||
throw std::runtime_error("LZW error: code exceeds dictionary_ind.");
|
throw std::runtime_error("LZW error: code exceeds dictionary_ind.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +99,7 @@ void Gif::decompress(int code_length, const uint8_t *input, int input_length, ui
|
|||||||
prev = code;
|
prev = code;
|
||||||
|
|
||||||
if (code < 0 || static_cast<size_t>(code) >= dictionary.size()) {
|
if (code < 0 || static_cast<size_t>(code) >= dictionary.size()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid LZW code %d, dictionary size %lu", code, static_cast<unsigned long>(dictionary.size()));
|
std::cout << "Invalid LZW code " << code << ", dictionary size " << static_cast<unsigned long>(dictionary.size()) << '\n';
|
||||||
throw std::runtime_error("LZW error: invalid code encountered");
|
throw std::runtime_error("LZW error: invalid code encountered");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +108,7 @@ void Gif::decompress(int code_length, const uint8_t *input, int input_length, ui
|
|||||||
while (curCode != -1) {
|
while (curCode != -1) {
|
||||||
out[dictionary[curCode].len - 1] = dictionary[curCode].byte;
|
out[dictionary[curCode].len - 1] = dictionary[curCode].byte;
|
||||||
if (dictionary[curCode].prev == curCode) {
|
if (dictionary[curCode].prev == curCode) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Internal error; self-reference detected.");
|
std::cout << "Internal error; self-reference detected." << '\n';
|
||||||
throw std::runtime_error("Internal error in decompress: self-reference");
|
throw std::runtime_error("Internal error in decompress: self-reference");
|
||||||
}
|
}
|
||||||
curCode = dictionary[curCode].prev;
|
curCode = dictionary[curCode].prev;
|
||||||
@@ -175,20 +176,16 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t *buffer, uint16_t &w, u
|
|||||||
|
|
||||||
std::string headerStr(reinterpret_cast<char *>(header), 6);
|
std::string headerStr(reinterpret_cast<char *>(header), 6);
|
||||||
if (headerStr != "GIF87a" && headerStr != "GIF89a") {
|
if (headerStr != "GIF87a" && headerStr != "GIF89a") {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Formato de archivo GIF inválido: %s", headerStr.c_str());
|
std::cout << "Formato de archivo GIF inválido: " << headerStr << '\n';
|
||||||
throw std::runtime_error("Formato de archivo GIF inválido.");
|
throw std::runtime_error("Formato de archivo GIF inválido.");
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "Procesando GIF con cabecera: %s", headerStr.c_str());
|
|
||||||
|
|
||||||
ScreenDescriptor screen_descriptor;
|
ScreenDescriptor screen_descriptor;
|
||||||
readBytes(buffer, &screen_descriptor, sizeof(ScreenDescriptor));
|
readBytes(buffer, &screen_descriptor, sizeof(ScreenDescriptor));
|
||||||
|
|
||||||
w = screen_descriptor.width;
|
w = screen_descriptor.width;
|
||||||
h = screen_descriptor.height;
|
h = screen_descriptor.height;
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "Resolución del GIF: %dx%d", w, h);
|
|
||||||
|
|
||||||
int color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
|
int color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1;
|
||||||
std::vector<RGB> global_color_table;
|
std::vector<RGB> global_color_table;
|
||||||
if (screen_descriptor.fields & 0x80) {
|
if (screen_descriptor.fields & 0x80) {
|
||||||
@@ -239,13 +236,12 @@ std::vector<uint8_t> Gif::processGifStream(const uint8_t *buffer, uint16_t &w, u
|
|||||||
} else if (block_type == IMAGE_DESCRIPTOR) {
|
} else if (block_type == IMAGE_DESCRIPTOR) {
|
||||||
return processImageDescriptor(buffer, global_color_table, color_resolution_bits);
|
return processImageDescriptor(buffer, global_color_table, color_resolution_bits);
|
||||||
} else {
|
} else {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized block type: 0x%X", block_type);
|
std::cout << "Unrecognized block type: 0x" << std::hex << static_cast<int>(block_type) << std::dec << '\n';
|
||||||
return std::vector<uint8_t>{};
|
return std::vector<uint8_t>{};
|
||||||
}
|
}
|
||||||
block_type = *buffer++;
|
block_type = *buffer++;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "GIF procesado correctamente.");
|
|
||||||
return std::vector<uint8_t>{};
|
return std::vector<uint8_t>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
477
source/external/jail_audio.cpp
vendored
477
source/external/jail_audio.cpp
vendored
@@ -1,477 +0,0 @@
|
|||||||
#ifndef JA_USESDLMIXER
|
|
||||||
#include "jail_audio.h"
|
|
||||||
|
|
||||||
#include <SDL3/SDL.h> // Para SDL_AudioFormat, SDL_BindAudioStream, SDL_SetAudioStreamGain, SDL_PutAudioStreamData, SDL_DestroyAudioStream, SDL_GetAudioStreamAvailable, Uint8, SDL_CreateAudioStream, SDL_UnbindAudioStream, Uint32, SDL_CloseAudioDevice, SDL_GetTicks, SDL_Log, SDL_free, SDL_AudioSpec, SDL_AudioStream, SDL_IOFromMem, SDL_LoadWAV, SDL_LoadWAV_IO, SDL_OpenAudioDevice, SDL_clamp, SDL_malloc, SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, SDL_AudioDeviceID, SDL_memcpy
|
|
||||||
#include <stdint.h> // Para uint32_t, uint8_t
|
|
||||||
#include <stdio.h> // Para NULL, fseek, printf, fclose, fopen, fread, ftell, FILE, SEEK_END, SEEK_SET
|
|
||||||
#include <stdlib.h> // Para free, malloc
|
|
||||||
#include <string.h> // Para strcpy, strlen
|
|
||||||
|
|
||||||
#include "stb_vorbis.h" // Para stb_vorbis_decode_memory
|
|
||||||
|
|
||||||
#define JA_MAX_SIMULTANEOUS_CHANNELS 20
|
|
||||||
#define JA_MAX_GROUPS 2
|
|
||||||
|
|
||||||
struct JA_Sound_t
|
|
||||||
{
|
|
||||||
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
|
|
||||||
Uint32 length { 0 };
|
|
||||||
Uint8 *buffer { NULL };
|
|
||||||
};
|
|
||||||
|
|
||||||
struct JA_Channel_t
|
|
||||||
{
|
|
||||||
JA_Sound_t *sound { nullptr };
|
|
||||||
int pos { 0 };
|
|
||||||
int times { 0 };
|
|
||||||
int group { 0 };
|
|
||||||
SDL_AudioStream *stream { nullptr };
|
|
||||||
JA_Channel_state state { JA_CHANNEL_FREE };
|
|
||||||
};
|
|
||||||
|
|
||||||
struct JA_Music_t
|
|
||||||
{
|
|
||||||
SDL_AudioSpec spec { SDL_AUDIO_S16, 2, 48000 };
|
|
||||||
Uint32 length { 0 };
|
|
||||||
Uint8 *buffer { nullptr };
|
|
||||||
char *filename { nullptr };
|
|
||||||
|
|
||||||
int pos { 0 };
|
|
||||||
int times { 0 };
|
|
||||||
SDL_AudioStream *stream { nullptr };
|
|
||||||
JA_Music_state state { JA_MUSIC_INVALID };
|
|
||||||
};
|
|
||||||
|
|
||||||
JA_Music_t *current_music { nullptr };
|
|
||||||
JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
|
||||||
|
|
||||||
SDL_AudioSpec JA_audioSpec { SDL_AUDIO_S16, 2, 48000 };
|
|
||||||
float JA_musicVolume { 1.0f };
|
|
||||||
float JA_soundVolume[JA_MAX_GROUPS];
|
|
||||||
bool JA_musicEnabled { true };
|
|
||||||
bool JA_soundEnabled { true };
|
|
||||||
SDL_AudioDeviceID sdlAudioDevice { 0 };
|
|
||||||
//SDL_TimerID JA_timerID { 0 };
|
|
||||||
|
|
||||||
bool fading = false;
|
|
||||||
int fade_start_time;
|
|
||||||
int fade_duration;
|
|
||||||
int fade_initial_volume;
|
|
||||||
|
|
||||||
|
|
||||||
void JA_Update()
|
|
||||||
{
|
|
||||||
if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING)
|
|
||||||
{
|
|
||||||
if (fading) {
|
|
||||||
int time = SDL_GetTicks();
|
|
||||||
if (time > (fade_start_time+fade_duration)) {
|
|
||||||
fading = false;
|
|
||||||
JA_StopMusic();
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
const int time_passed = time - fade_start_time;
|
|
||||||
const float percent = (float)time_passed / (float)fade_duration;
|
|
||||||
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume*(1.0 - percent));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_music->times != 0)
|
|
||||||
{
|
|
||||||
if ((Uint32)SDL_GetAudioStreamAvailable(current_music->stream) < (current_music->length/2)) {
|
|
||||||
SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length);
|
|
||||||
}
|
|
||||||
if (current_music->times>0) current_music->times--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (SDL_GetAudioStreamAvailable(current_music->stream) == 0) JA_StopMusic();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JA_soundEnabled)
|
|
||||||
{
|
|
||||||
for (int i=0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i)
|
|
||||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
|
||||||
{
|
|
||||||
if (channels[i].times != 0)
|
|
||||||
{
|
|
||||||
if ((Uint32)SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length/2)) {
|
|
||||||
SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length);
|
|
||||||
if (channels[i].times>0) channels[i].times--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels)
|
|
||||||
{
|
|
||||||
#ifdef DEBUG
|
|
||||||
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
JA_audioSpec = {format, num_channels, freq };
|
|
||||||
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
|
|
||||||
sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec);
|
|
||||||
if (sdlAudioDevice==0) SDL_Log("Failed to initialize SDL audio!");
|
|
||||||
for (int i=0; i<JA_MAX_SIMULTANEOUS_CHANNELS; ++i) channels[i].state = JA_CHANNEL_FREE;
|
|
||||||
for (int i=0; i<JA_MAX_GROUPS; ++i) JA_soundVolume[i] = 0.5f;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_Quit()
|
|
||||||
{
|
|
||||||
if (!sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice);
|
|
||||||
sdlAudioDevice = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length)
|
|
||||||
{
|
|
||||||
JA_Music_t *music = new JA_Music_t();
|
|
||||||
|
|
||||||
int chan, samplerate;
|
|
||||||
short *output;
|
|
||||||
music->length = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &output) * chan * 2;
|
|
||||||
|
|
||||||
music->spec.channels = chan;
|
|
||||||
music->spec.freq = samplerate;
|
|
||||||
music->spec.format = SDL_AUDIO_S16;
|
|
||||||
music->buffer = (Uint8*)SDL_malloc(music->length);
|
|
||||||
SDL_memcpy(music->buffer, output, music->length);
|
|
||||||
free(output);
|
|
||||||
music->pos = 0;
|
|
||||||
music->state = JA_MUSIC_STOPPED;
|
|
||||||
|
|
||||||
return music;
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Music_t *JA_LoadMusic(const char* filename)
|
|
||||||
{
|
|
||||||
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
|
||||||
FILE *f = fopen(filename, "rb");
|
|
||||||
fseek(f, 0, SEEK_END);
|
|
||||||
long fsize = ftell(f);
|
|
||||||
fseek(f, 0, SEEK_SET);
|
|
||||||
Uint8 *buffer = (Uint8*)malloc(fsize + 1);
|
|
||||||
if (fread(buffer, fsize, 1, f)!=1) return NULL;
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
JA_Music_t *music = JA_LoadMusic(buffer, fsize);
|
|
||||||
music->filename = (char*)malloc(strlen(filename)+1);
|
|
||||||
strcpy(music->filename, filename);
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
return music;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_PlayMusic(JA_Music_t *music, const int loop)
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return;
|
|
||||||
|
|
||||||
JA_StopMusic();
|
|
||||||
|
|
||||||
current_music = music;
|
|
||||||
current_music->pos = 0;
|
|
||||||
current_music->state = JA_MUSIC_PLAYING;
|
|
||||||
current_music->times = loop;
|
|
||||||
|
|
||||||
current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec);
|
|
||||||
if (!SDL_PutAudioStreamData(current_music->stream, current_music->buffer, current_music->length)) printf("[ERROR] SDL_PutAudioStreamData failed!\n");
|
|
||||||
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
|
|
||||||
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
|
|
||||||
//SDL_ResumeAudioStreamDevice(current_music->stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *JA_GetMusicFilename(JA_Music_t *music)
|
|
||||||
{
|
|
||||||
if (!music) music = current_music;
|
|
||||||
return music->filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_PauseMusic()
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return;
|
|
||||||
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
|
|
||||||
|
|
||||||
current_music->state = JA_MUSIC_PAUSED;
|
|
||||||
//SDL_PauseAudioStreamDevice(current_music->stream);
|
|
||||||
SDL_UnbindAudioStream(current_music->stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_ResumeMusic()
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return;
|
|
||||||
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
|
|
||||||
|
|
||||||
current_music->state = JA_MUSIC_PLAYING;
|
|
||||||
//SDL_ResumeAudioStreamDevice(current_music->stream);
|
|
||||||
SDL_BindAudioStream(sdlAudioDevice, current_music->stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_StopMusic()
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return;
|
|
||||||
if (!current_music || current_music->state == JA_MUSIC_INVALID) return;
|
|
||||||
|
|
||||||
current_music->pos = 0;
|
|
||||||
current_music->state = JA_MUSIC_STOPPED;
|
|
||||||
//SDL_PauseAudioStreamDevice(current_music->stream);
|
|
||||||
SDL_DestroyAudioStream(current_music->stream);
|
|
||||||
current_music->stream = nullptr;
|
|
||||||
free(current_music->filename);
|
|
||||||
current_music->filename = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_FadeOutMusic(const int milliseconds)
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return;
|
|
||||||
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
|
||||||
|
|
||||||
fading = true;
|
|
||||||
fade_start_time = SDL_GetTicks();
|
|
||||||
fade_duration = milliseconds;
|
|
||||||
fade_initial_volume = JA_musicVolume;
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Music_state JA_GetMusicState()
|
|
||||||
{
|
|
||||||
if (!JA_musicEnabled) return JA_MUSIC_DISABLED;
|
|
||||||
if (!current_music) return JA_MUSIC_INVALID;
|
|
||||||
|
|
||||||
return current_music->state;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_DeleteMusic(JA_Music_t *music)
|
|
||||||
{
|
|
||||||
if (current_music == music) current_music = nullptr;
|
|
||||||
SDL_free(music->buffer);
|
|
||||||
if (music->stream) SDL_DestroyAudioStream(music->stream);
|
|
||||||
delete music;
|
|
||||||
}
|
|
||||||
|
|
||||||
float JA_SetMusicVolume(float volume)
|
|
||||||
{
|
|
||||||
JA_musicVolume = SDL_clamp( volume, 0.0f, 1.0f );
|
|
||||||
if (current_music) SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
|
|
||||||
return JA_musicVolume;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_SetMusicPosition(float value)
|
|
||||||
{
|
|
||||||
if (!current_music) return;
|
|
||||||
current_music->pos = value * current_music->spec.freq;
|
|
||||||
}
|
|
||||||
|
|
||||||
float JA_GetMusicPosition()
|
|
||||||
{
|
|
||||||
if (!current_music) return 0;
|
|
||||||
return float(current_music->pos)/float(current_music->spec.freq);
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_EnableMusic(const bool value)
|
|
||||||
{
|
|
||||||
if ( !value && current_music && (current_music->state==JA_MUSIC_PLAYING) ) JA_StopMusic();
|
|
||||||
|
|
||||||
JA_musicEnabled = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length)
|
|
||||||
{
|
|
||||||
JA_Sound_t *sound = new JA_Sound_t();
|
|
||||||
sound->buffer = buffer;
|
|
||||||
sound->length = length;
|
|
||||||
return sound;
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size)
|
|
||||||
{
|
|
||||||
JA_Sound_t *sound = new JA_Sound_t();
|
|
||||||
SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size),1, &sound->spec, &sound->buffer, &sound->length);
|
|
||||||
|
|
||||||
return sound;
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Sound_t *JA_LoadSound(const char* filename)
|
|
||||||
{
|
|
||||||
JA_Sound_t *sound = new JA_Sound_t();
|
|
||||||
SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length);
|
|
||||||
|
|
||||||
return sound;
|
|
||||||
}
|
|
||||||
|
|
||||||
int JA_PlaySound(JA_Sound_t *sound, const int loop, const int group)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return -1;
|
|
||||||
|
|
||||||
int channel = 0;
|
|
||||||
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
|
|
||||||
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) channel = 0;
|
|
||||||
JA_StopChannel(channel);
|
|
||||||
|
|
||||||
channels[channel].sound = sound;
|
|
||||||
channels[channel].times = loop;
|
|
||||||
channels[channel].pos = 0;
|
|
||||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
|
||||||
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
|
|
||||||
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
|
|
||||||
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
|
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
|
||||||
|
|
||||||
return channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop, const int group)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return -1;
|
|
||||||
|
|
||||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1;
|
|
||||||
JA_StopChannel(channel);
|
|
||||||
|
|
||||||
channels[channel].sound = sound;
|
|
||||||
channels[channel].times = loop;
|
|
||||||
channels[channel].pos = 0;
|
|
||||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
|
||||||
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
|
|
||||||
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
|
|
||||||
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
|
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
|
||||||
|
|
||||||
return channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_DeleteSound(JA_Sound_t *sound)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
|
||||||
if (channels[i].sound == sound) JA_StopChannel(i);
|
|
||||||
}
|
|
||||||
SDL_free(sound->buffer);
|
|
||||||
delete sound;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_PauseChannel(const int channel)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return;
|
|
||||||
|
|
||||||
if (channel == -1)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
|
||||||
if (channels[i].state == JA_CHANNEL_PLAYING)
|
|
||||||
{
|
|
||||||
channels[i].state = JA_CHANNEL_PAUSED;
|
|
||||||
//SDL_PauseAudioStreamDevice(channels[i].stream);
|
|
||||||
SDL_UnbindAudioStream(channels[i].stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
|
||||||
{
|
|
||||||
if (channels[channel].state == JA_CHANNEL_PLAYING)
|
|
||||||
{
|
|
||||||
channels[channel].state = JA_CHANNEL_PAUSED;
|
|
||||||
//SDL_PauseAudioStreamDevice(channels[channel].stream);
|
|
||||||
SDL_UnbindAudioStream(channels[channel].stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_ResumeChannel(const int channel)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return;
|
|
||||||
|
|
||||||
if (channel == -1)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
|
||||||
if (channels[i].state == JA_CHANNEL_PAUSED)
|
|
||||||
{
|
|
||||||
channels[i].state = JA_CHANNEL_PLAYING;
|
|
||||||
//SDL_ResumeAudioStreamDevice(channels[i].stream);
|
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[i].stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
|
||||||
{
|
|
||||||
if (channels[channel].state == JA_CHANNEL_PAUSED)
|
|
||||||
{
|
|
||||||
channels[channel].state = JA_CHANNEL_PLAYING;
|
|
||||||
//SDL_ResumeAudioStreamDevice(channels[channel].stream);
|
|
||||||
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_StopChannel(const int channel)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return;
|
|
||||||
|
|
||||||
if (channel == -1)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
|
||||||
if (channels[i].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[i].stream);
|
|
||||||
channels[i].stream = nullptr;
|
|
||||||
channels[i].state = JA_CHANNEL_FREE;
|
|
||||||
channels[i].pos = 0;
|
|
||||||
channels[i].sound = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS)
|
|
||||||
{
|
|
||||||
if (channels[channel].state != JA_CHANNEL_FREE) SDL_DestroyAudioStream(channels[channel].stream);
|
|
||||||
channels[channel].stream = nullptr;
|
|
||||||
channels[channel].state = JA_CHANNEL_FREE;
|
|
||||||
channels[channel].pos = 0;
|
|
||||||
channels[channel].sound = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JA_Channel_state JA_GetChannelState(const int channel)
|
|
||||||
{
|
|
||||||
if (!JA_soundEnabled) return JA_SOUND_DISABLED;
|
|
||||||
|
|
||||||
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
|
|
||||||
|
|
||||||
return channels[channel].state;
|
|
||||||
}
|
|
||||||
|
|
||||||
float JA_SetSoundVolume(float volume, const int group)
|
|
||||||
{
|
|
||||||
const float v = SDL_clamp( volume, 0.0f, 1.0f );
|
|
||||||
for (int i = 0; i < JA_MAX_GROUPS; ++i) {
|
|
||||||
if (group==-1 || group==i) JA_soundVolume[i]=v;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
|
||||||
if ( ((channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED)) &&
|
|
||||||
((group==-1) || (channels[i].group==group)) )
|
|
||||||
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume[i]);
|
|
||||||
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
void JA_EnableSound(const bool value)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
|
||||||
{
|
|
||||||
if (channels[i].state == JA_CHANNEL_PLAYING) JA_StopChannel(i);
|
|
||||||
}
|
|
||||||
JA_soundEnabled = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
float JA_SetVolume(float volume)
|
|
||||||
{
|
|
||||||
JA_SetSoundVolume(JA_SetMusicVolume(volume) / 2.0f);
|
|
||||||
|
|
||||||
return JA_musicVolume;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
43
source/external/jail_audio.h
vendored
43
source/external/jail_audio.h
vendored
@@ -1,43 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
enum JA_Channel_state { JA_CHANNEL_INVALID, JA_CHANNEL_FREE, JA_CHANNEL_PLAYING, JA_CHANNEL_PAUSED, JA_SOUND_DISABLED };
|
|
||||||
enum JA_Music_state { JA_MUSIC_INVALID, JA_MUSIC_PLAYING, JA_MUSIC_PAUSED, JA_MUSIC_STOPPED, JA_MUSIC_DISABLED };
|
|
||||||
|
|
||||||
struct JA_Sound_t;
|
|
||||||
struct JA_Music_t;
|
|
||||||
|
|
||||||
void JA_Update();
|
|
||||||
|
|
||||||
void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels);
|
|
||||||
void JA_Quit();
|
|
||||||
|
|
||||||
JA_Music_t *JA_LoadMusic(const char* filename);
|
|
||||||
JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length);
|
|
||||||
void JA_PlayMusic(JA_Music_t *music, const int loop = -1);
|
|
||||||
char *JA_GetMusicFilename(JA_Music_t *music = nullptr);
|
|
||||||
void JA_PauseMusic();
|
|
||||||
void JA_ResumeMusic();
|
|
||||||
void JA_StopMusic();
|
|
||||||
void JA_FadeOutMusic(const int milliseconds);
|
|
||||||
JA_Music_state JA_GetMusicState();
|
|
||||||
void JA_DeleteMusic(JA_Music_t *music);
|
|
||||||
float JA_SetMusicVolume(float volume);
|
|
||||||
void JA_SetMusicPosition(float value);
|
|
||||||
float JA_GetMusicPosition();
|
|
||||||
void JA_EnableMusic(const bool value);
|
|
||||||
|
|
||||||
JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length);
|
|
||||||
JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length);
|
|
||||||
JA_Sound_t *JA_LoadSound(const char* filename);
|
|
||||||
int JA_PlaySound(JA_Sound_t *sound, const int loop = 0, const int group=0);
|
|
||||||
int JA_PlaySoundOnChannel(JA_Sound_t *sound, const int channel, const int loop = 0, const int group=0);
|
|
||||||
void JA_PauseChannel(const int channel);
|
|
||||||
void JA_ResumeChannel(const int channel);
|
|
||||||
void JA_StopChannel(const int channel);
|
|
||||||
JA_Channel_state JA_GetChannelState(const int channel);
|
|
||||||
void JA_DeleteSound(JA_Sound_t *sound);
|
|
||||||
float JA_SetSoundVolume(float volume, const int group=0);
|
|
||||||
void JA_EnableSound(const bool value);
|
|
||||||
|
|
||||||
float JA_SetVolume(float volume);
|
|
||||||
557
source/external/jail_audio.hpp
vendored
Normal file
557
source/external/jail_audio.hpp
vendored
Normal file
@@ -0,0 +1,557 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// --- Includes ---
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <stdint.h> // Para uint32_t, uint8_t
|
||||||
|
#include <stdio.h> // Para NULL, fseek, printf, fclose, fopen, fread, ftell, FILE, SEEK_END, SEEK_SET
|
||||||
|
#include <stdlib.h> // Para free, malloc
|
||||||
|
#include <string.h> // Para strcpy, strlen
|
||||||
|
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
|
|
||||||
|
#define STB_VORBIS_HEADER_ONLY
|
||||||
|
#include "external/stb_vorbis.h" // Para stb_vorbis_decode_memory
|
||||||
|
|
||||||
|
// --- Public Enums ---
|
||||||
|
enum JA_Channel_state { JA_CHANNEL_INVALID,
|
||||||
|
JA_CHANNEL_FREE,
|
||||||
|
JA_CHANNEL_PLAYING,
|
||||||
|
JA_CHANNEL_PAUSED,
|
||||||
|
JA_SOUND_DISABLED };
|
||||||
|
enum JA_Music_state { JA_MUSIC_INVALID,
|
||||||
|
JA_MUSIC_PLAYING,
|
||||||
|
JA_MUSIC_PAUSED,
|
||||||
|
JA_MUSIC_STOPPED,
|
||||||
|
JA_MUSIC_DISABLED };
|
||||||
|
|
||||||
|
// --- Struct Definitions ---
|
||||||
|
#define JA_MAX_SIMULTANEOUS_CHANNELS 20
|
||||||
|
#define JA_MAX_GROUPS 2
|
||||||
|
|
||||||
|
struct JA_Sound_t {
|
||||||
|
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
|
||||||
|
Uint32 length{0};
|
||||||
|
Uint8* buffer{NULL};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct JA_Channel_t {
|
||||||
|
JA_Sound_t* sound{nullptr};
|
||||||
|
int pos{0};
|
||||||
|
int times{0};
|
||||||
|
int group{0};
|
||||||
|
SDL_AudioStream* stream{nullptr};
|
||||||
|
JA_Channel_state state{JA_CHANNEL_FREE};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct JA_Music_t {
|
||||||
|
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
|
||||||
|
|
||||||
|
// OGG comprimit en memòria. Propietat nostra; es copia des del fitxer una
|
||||||
|
// sola vegada en JA_LoadMusic i es descomprimix en chunks per streaming.
|
||||||
|
Uint8* ogg_data{nullptr};
|
||||||
|
Uint32 ogg_length{0};
|
||||||
|
stb_vorbis* vorbis{nullptr}; // Handle del decoder, viu tot el cicle del JA_Music_t
|
||||||
|
|
||||||
|
char* filename{nullptr};
|
||||||
|
|
||||||
|
int times{0}; // Loops restants (-1 = infinit, 0 = un sol play)
|
||||||
|
SDL_AudioStream* stream{nullptr};
|
||||||
|
JA_Music_state state{JA_MUSIC_INVALID};
|
||||||
|
};
|
||||||
|
|
||||||
|
// --- Internal Global State ---
|
||||||
|
// Marcado 'inline' (C++17) para asegurar una única instancia.
|
||||||
|
|
||||||
|
inline JA_Music_t* current_music{nullptr};
|
||||||
|
inline JA_Channel_t channels[JA_MAX_SIMULTANEOUS_CHANNELS];
|
||||||
|
|
||||||
|
inline SDL_AudioSpec JA_audioSpec{SDL_AUDIO_S16, 2, 48000};
|
||||||
|
inline float JA_musicVolume{1.0f};
|
||||||
|
inline float JA_soundVolume[JA_MAX_GROUPS];
|
||||||
|
inline bool JA_musicEnabled{true};
|
||||||
|
inline bool JA_soundEnabled{true};
|
||||||
|
inline SDL_AudioDeviceID sdlAudioDevice{0};
|
||||||
|
|
||||||
|
inline bool fading{false};
|
||||||
|
inline int fade_start_time{0};
|
||||||
|
inline int fade_duration{0};
|
||||||
|
inline float fade_initial_volume{0.0f}; // Corregido de 'int' a 'float'
|
||||||
|
|
||||||
|
// --- Forward Declarations ---
|
||||||
|
inline void JA_StopMusic();
|
||||||
|
inline void JA_StopChannel(const int channel);
|
||||||
|
inline int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop = 0, const int group = 0);
|
||||||
|
|
||||||
|
// --- Music streaming internals ---
|
||||||
|
// Bytes-per-sample per canal (sempre s16)
|
||||||
|
static constexpr int JA_MUSIC_BYTES_PER_SAMPLE = 2;
|
||||||
|
// Quants shorts decodifiquem per crida a get_samples_short_interleaved.
|
||||||
|
// 8192 shorts = 4096 samples/channel en estèreo ≈ 85ms de so a 48kHz.
|
||||||
|
static constexpr int JA_MUSIC_CHUNK_SHORTS = 8192;
|
||||||
|
// Umbral d'audio per davant del cursor de reproducció. Mantenim ≥ 0.5 s a
|
||||||
|
// l'SDL_AudioStream per absorbir jitter de frame i evitar underruns.
|
||||||
|
static constexpr float JA_MUSIC_LOW_WATER_SECONDS = 0.5f;
|
||||||
|
|
||||||
|
// Decodifica un chunk del vorbis i el volca a l'stream. Retorna samples
|
||||||
|
// decodificats per canal (0 = EOF de l'stream vorbis).
|
||||||
|
inline int JA_FeedMusicChunk(JA_Music_t* music) {
|
||||||
|
if (!music || !music->vorbis || !music->stream) return 0;
|
||||||
|
|
||||||
|
short chunk[JA_MUSIC_CHUNK_SHORTS];
|
||||||
|
const int channels = music->spec.channels;
|
||||||
|
const int samples_per_channel = stb_vorbis_get_samples_short_interleaved(
|
||||||
|
music->vorbis,
|
||||||
|
channels,
|
||||||
|
chunk,
|
||||||
|
JA_MUSIC_CHUNK_SHORTS);
|
||||||
|
if (samples_per_channel <= 0) return 0;
|
||||||
|
|
||||||
|
const int bytes = samples_per_channel * channels * JA_MUSIC_BYTES_PER_SAMPLE;
|
||||||
|
SDL_PutAudioStreamData(music->stream, chunk, bytes);
|
||||||
|
return samples_per_channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reompli l'stream fins que tinga ≥ JA_MUSIC_LOW_WATER_SECONDS bufferats.
|
||||||
|
// En arribar a EOF del vorbis, aplica el loop (times) o deixa drenar.
|
||||||
|
inline void JA_PumpMusic(JA_Music_t* music) {
|
||||||
|
if (!music || !music->vorbis || !music->stream) return;
|
||||||
|
|
||||||
|
const int bytes_per_second = music->spec.freq * music->spec.channels * JA_MUSIC_BYTES_PER_SAMPLE;
|
||||||
|
const int low_water_bytes = static_cast<int>(JA_MUSIC_LOW_WATER_SECONDS * static_cast<float>(bytes_per_second));
|
||||||
|
|
||||||
|
while (SDL_GetAudioStreamAvailable(music->stream) < low_water_bytes) {
|
||||||
|
const int decoded = JA_FeedMusicChunk(music);
|
||||||
|
if (decoded > 0) continue;
|
||||||
|
|
||||||
|
// EOF: si queden loops, rebobinar; si no, tallar i deixar drenar.
|
||||||
|
if (music->times != 0) {
|
||||||
|
stb_vorbis_seek_start(music->vorbis);
|
||||||
|
if (music->times > 0) music->times--;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Core Functions ---
|
||||||
|
|
||||||
|
inline void JA_Update() {
|
||||||
|
if (JA_musicEnabled && current_music && current_music->state == JA_MUSIC_PLAYING) {
|
||||||
|
if (fading) {
|
||||||
|
int time = SDL_GetTicks();
|
||||||
|
if (time > (fade_start_time + fade_duration)) {
|
||||||
|
fading = false;
|
||||||
|
JA_StopMusic();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
const int time_passed = time - fade_start_time;
|
||||||
|
const float percent = (float)time_passed / (float)fade_duration;
|
||||||
|
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume * (1.0 - percent));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Streaming: rellenem l'stream fins al low-water-mark i parem si el
|
||||||
|
// vorbis s'ha esgotat i no queden loops.
|
||||||
|
JA_PumpMusic(current_music);
|
||||||
|
if (current_music->times == 0 && SDL_GetAudioStreamAvailable(current_music->stream) == 0) {
|
||||||
|
JA_StopMusic();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JA_soundEnabled) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i)
|
||||||
|
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||||
|
if (channels[i].times != 0) {
|
||||||
|
if ((Uint32)SDL_GetAudioStreamAvailable(channels[i].stream) < (channels[i].sound->length / 2)) {
|
||||||
|
SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer, channels[i].sound->length);
|
||||||
|
if (channels[i].times > 0) channels[i].times--;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (SDL_GetAudioStreamAvailable(channels[i].stream) == 0) JA_StopChannel(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_Init(const int freq, const SDL_AudioFormat format, const int num_channels) {
|
||||||
|
#ifdef _DEBUG
|
||||||
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
JA_audioSpec = {format, num_channels, freq};
|
||||||
|
if (sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); // Corregido: !sdlAudioDevice -> sdlAudioDevice
|
||||||
|
sdlAudioDevice = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &JA_audioSpec);
|
||||||
|
if (sdlAudioDevice == 0) std::cout << "Failed to initialize SDL audio!" << '\n';
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; ++i) channels[i].state = JA_CHANNEL_FREE;
|
||||||
|
for (int i = 0; i < JA_MAX_GROUPS; ++i) JA_soundVolume[i] = 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_Quit() {
|
||||||
|
if (sdlAudioDevice) SDL_CloseAudioDevice(sdlAudioDevice); // Corregido: !sdlAudioDevice -> sdlAudioDevice
|
||||||
|
sdlAudioDevice = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Music Functions ---
|
||||||
|
|
||||||
|
inline JA_Music_t* JA_LoadMusic(const Uint8* buffer, Uint32 length) {
|
||||||
|
if (!buffer || length == 0) return nullptr;
|
||||||
|
|
||||||
|
// Còpia del OGG comprimit: stb_vorbis llig de forma persistent aquesta
|
||||||
|
// memòria mentre el handle estiga viu, així que hem de posseir-la nosaltres.
|
||||||
|
Uint8* ogg_copy = static_cast<Uint8*>(SDL_malloc(length));
|
||||||
|
if (!ogg_copy) return nullptr;
|
||||||
|
SDL_memcpy(ogg_copy, buffer, length);
|
||||||
|
|
||||||
|
int error = 0;
|
||||||
|
stb_vorbis* vorbis = stb_vorbis_open_memory(ogg_copy, static_cast<int>(length), &error, nullptr);
|
||||||
|
if (!vorbis) {
|
||||||
|
SDL_free(ogg_copy);
|
||||||
|
std::cout << "JA_LoadMusic: stb_vorbis_open_memory failed (error " << error << ")" << '\n';
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto* music = new JA_Music_t();
|
||||||
|
music->ogg_data = ogg_copy;
|
||||||
|
music->ogg_length = length;
|
||||||
|
music->vorbis = vorbis;
|
||||||
|
|
||||||
|
const stb_vorbis_info info = stb_vorbis_get_info(vorbis);
|
||||||
|
music->spec.channels = info.channels;
|
||||||
|
music->spec.freq = static_cast<int>(info.sample_rate);
|
||||||
|
music->spec.format = SDL_AUDIO_S16;
|
||||||
|
music->state = JA_MUSIC_STOPPED;
|
||||||
|
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline JA_Music_t* JA_LoadMusic(const char* filename) {
|
||||||
|
// [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid.
|
||||||
|
FILE* f = fopen(filename, "rb");
|
||||||
|
if (!f) return NULL; // Añadida comprobación de apertura
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
long fsize = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
auto* buffer = static_cast<Uint8*>(malloc(fsize + 1));
|
||||||
|
if (!buffer) { // Añadida comprobación de malloc
|
||||||
|
fclose(f);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (fread(buffer, fsize, 1, f) != 1) {
|
||||||
|
fclose(f);
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
JA_Music_t* music = JA_LoadMusic(buffer, fsize);
|
||||||
|
if (music) { // Comprobar que JA_LoadMusic tuvo éxito
|
||||||
|
music->filename = static_cast<char*>(malloc(strlen(filename) + 1));
|
||||||
|
if (music->filename) {
|
||||||
|
strcpy(music->filename, filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_PlayMusic(JA_Music_t* music, const int loop = -1) {
|
||||||
|
if (!JA_musicEnabled || !music || !music->vorbis) return;
|
||||||
|
|
||||||
|
JA_StopMusic();
|
||||||
|
|
||||||
|
current_music = music;
|
||||||
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
|
current_music->times = loop;
|
||||||
|
|
||||||
|
// Rebobinem l'stream de vorbis al principi. Cobreix tant play-per-primera-
|
||||||
|
// vegada com replays/canvis de track que tornen a la mateixa pista.
|
||||||
|
stb_vorbis_seek_start(current_music->vorbis);
|
||||||
|
|
||||||
|
current_music->stream = SDL_CreateAudioStream(¤t_music->spec, &JA_audioSpec);
|
||||||
|
if (!current_music->stream) {
|
||||||
|
std::cout << "Failed to create audio stream!" << '\n';
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
|
||||||
|
|
||||||
|
// Pre-cargem el buffer abans de bindejar per evitar un underrun inicial.
|
||||||
|
JA_PumpMusic(current_music);
|
||||||
|
|
||||||
|
if (!SDL_BindAudioStream(sdlAudioDevice, current_music->stream)) printf("[ERROR] SDL_BindAudioStream failed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
inline char* JA_GetMusicFilename(const JA_Music_t* music = nullptr) {
|
||||||
|
if (!music) music = current_music;
|
||||||
|
if (!music) return nullptr; // Añadida comprobación
|
||||||
|
return music->filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_PauseMusic() {
|
||||||
|
if (!JA_musicEnabled) return;
|
||||||
|
if (!current_music || current_music->state != JA_MUSIC_PLAYING) return; // Comprobación mejorada
|
||||||
|
|
||||||
|
current_music->state = JA_MUSIC_PAUSED;
|
||||||
|
SDL_UnbindAudioStream(current_music->stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_ResumeMusic() {
|
||||||
|
if (!JA_musicEnabled) return;
|
||||||
|
if (!current_music || current_music->state != JA_MUSIC_PAUSED) return; // Comprobación mejorada
|
||||||
|
|
||||||
|
current_music->state = JA_MUSIC_PLAYING;
|
||||||
|
SDL_BindAudioStream(sdlAudioDevice, current_music->stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_StopMusic() {
|
||||||
|
if (!current_music || current_music->state == JA_MUSIC_INVALID || current_music->state == JA_MUSIC_STOPPED) return;
|
||||||
|
|
||||||
|
current_music->state = JA_MUSIC_STOPPED;
|
||||||
|
if (current_music->stream) {
|
||||||
|
SDL_DestroyAudioStream(current_music->stream);
|
||||||
|
current_music->stream = nullptr;
|
||||||
|
}
|
||||||
|
// Deixem el handle de vorbis viu — es tanca en JA_DeleteMusic.
|
||||||
|
// Rebobinem perquè un futur JA_PlayMusic comence des del principi.
|
||||||
|
if (current_music->vorbis) {
|
||||||
|
stb_vorbis_seek_start(current_music->vorbis);
|
||||||
|
}
|
||||||
|
// No liberem filename aquí; es fa en JA_DeleteMusic.
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_FadeOutMusic(const int milliseconds) {
|
||||||
|
if (!JA_musicEnabled) return;
|
||||||
|
if (current_music == NULL || current_music->state == JA_MUSIC_INVALID) return;
|
||||||
|
|
||||||
|
fading = true;
|
||||||
|
fade_start_time = SDL_GetTicks();
|
||||||
|
fade_duration = milliseconds;
|
||||||
|
fade_initial_volume = JA_musicVolume;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline JA_Music_state JA_GetMusicState() {
|
||||||
|
if (!JA_musicEnabled) return JA_MUSIC_DISABLED;
|
||||||
|
if (!current_music) return JA_MUSIC_INVALID;
|
||||||
|
|
||||||
|
return current_music->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_DeleteMusic(JA_Music_t* music) {
|
||||||
|
if (!music) return;
|
||||||
|
if (current_music == music) {
|
||||||
|
JA_StopMusic();
|
||||||
|
current_music = nullptr;
|
||||||
|
}
|
||||||
|
if (music->stream) SDL_DestroyAudioStream(music->stream);
|
||||||
|
if (music->vorbis) stb_vorbis_close(music->vorbis);
|
||||||
|
SDL_free(music->ogg_data);
|
||||||
|
free(music->filename); // filename es libera aquí
|
||||||
|
delete music;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float JA_SetMusicVolume(float volume) {
|
||||||
|
JA_musicVolume = SDL_clamp(volume, 0.0f, 1.0f);
|
||||||
|
if (current_music && current_music->stream) {
|
||||||
|
SDL_SetAudioStreamGain(current_music->stream, JA_musicVolume);
|
||||||
|
}
|
||||||
|
return JA_musicVolume;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_SetMusicPosition(float /*value*/) {
|
||||||
|
// No implementat amb el backend de streaming. Mai va arribar a usar-se
|
||||||
|
// en el codi existent, així que es manté com a stub.
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float JA_GetMusicPosition() {
|
||||||
|
// Veure nota a JA_SetMusicPosition.
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_EnableMusic(const bool value) {
|
||||||
|
if (!value && current_music && (current_music->state == JA_MUSIC_PLAYING)) JA_StopMusic();
|
||||||
|
|
||||||
|
JA_musicEnabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Sound Functions ---
|
||||||
|
|
||||||
|
inline JA_Sound_t* JA_NewSound(Uint8* buffer, Uint32 length) {
|
||||||
|
JA_Sound_t* sound = new JA_Sound_t();
|
||||||
|
sound->buffer = buffer;
|
||||||
|
sound->length = length;
|
||||||
|
// Nota: spec se queda con los valores por defecto.
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline JA_Sound_t* JA_LoadSound(uint8_t* buffer, uint32_t size) {
|
||||||
|
JA_Sound_t* sound = new JA_Sound_t();
|
||||||
|
if (!SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size), 1, &sound->spec, &sound->buffer, &sound->length)) {
|
||||||
|
std::cout << "Failed to load WAV from memory: " << SDL_GetError() << '\n';
|
||||||
|
delete sound;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline JA_Sound_t* JA_LoadSound(const char* filename) {
|
||||||
|
JA_Sound_t* sound = new JA_Sound_t();
|
||||||
|
if (!SDL_LoadWAV(filename, &sound->spec, &sound->buffer, &sound->length)) {
|
||||||
|
std::cout << "Failed to load WAV file: " << SDL_GetError() << '\n';
|
||||||
|
delete sound;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int JA_PlaySound(JA_Sound_t* sound, const int loop = 0, const int group = 0) {
|
||||||
|
if (!JA_soundEnabled || !sound) return -1;
|
||||||
|
|
||||||
|
int channel = 0;
|
||||||
|
while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; }
|
||||||
|
if (channel == JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
// No hay canal libre, reemplazamos el primero
|
||||||
|
channel = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return JA_PlaySoundOnChannel(sound, channel, loop, group);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int loop, const int group) {
|
||||||
|
if (!JA_soundEnabled || !sound) return -1;
|
||||||
|
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return -1;
|
||||||
|
|
||||||
|
JA_StopChannel(channel); // Detiene y limpia el canal si estaba en uso
|
||||||
|
|
||||||
|
channels[channel].sound = sound;
|
||||||
|
channels[channel].times = loop;
|
||||||
|
channels[channel].pos = 0;
|
||||||
|
channels[channel].group = group; // Asignar grupo
|
||||||
|
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
|
channels[channel].stream = SDL_CreateAudioStream(&channels[channel].sound->spec, &JA_audioSpec);
|
||||||
|
|
||||||
|
if (!channels[channel].stream) {
|
||||||
|
std::cout << "Failed to create audio stream for sound!" << '\n';
|
||||||
|
channels[channel].state = JA_CHANNEL_FREE;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
|
||||||
|
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
|
||||||
|
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_DeleteSound(JA_Sound_t* sound) {
|
||||||
|
if (!sound) return;
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].sound == sound) JA_StopChannel(i);
|
||||||
|
}
|
||||||
|
SDL_free(sound->buffer);
|
||||||
|
delete sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_PauseChannel(const int channel) {
|
||||||
|
if (!JA_soundEnabled) return;
|
||||||
|
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||||
|
if (channels[i].state == JA_CHANNEL_PLAYING) {
|
||||||
|
channels[i].state = JA_CHANNEL_PAUSED;
|
||||||
|
SDL_UnbindAudioStream(channels[i].stream);
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
if (channels[channel].state == JA_CHANNEL_PLAYING) {
|
||||||
|
channels[channel].state = JA_CHANNEL_PAUSED;
|
||||||
|
SDL_UnbindAudioStream(channels[channel].stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_ResumeChannel(const int channel) {
|
||||||
|
if (!JA_soundEnabled) return;
|
||||||
|
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++)
|
||||||
|
if (channels[i].state == JA_CHANNEL_PAUSED) {
|
||||||
|
channels[i].state = JA_CHANNEL_PLAYING;
|
||||||
|
SDL_BindAudioStream(sdlAudioDevice, channels[i].stream);
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
if (channels[channel].state == JA_CHANNEL_PAUSED) {
|
||||||
|
channels[channel].state = JA_CHANNEL_PLAYING;
|
||||||
|
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_StopChannel(const int channel) {
|
||||||
|
if (channel == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if (channels[i].state != JA_CHANNEL_FREE) {
|
||||||
|
if (channels[i].stream) SDL_DestroyAudioStream(channels[i].stream);
|
||||||
|
channels[i].stream = nullptr;
|
||||||
|
channels[i].state = JA_CHANNEL_FREE;
|
||||||
|
channels[i].pos = 0;
|
||||||
|
channels[i].sound = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (channel >= 0 && channel < JA_MAX_SIMULTANEOUS_CHANNELS) {
|
||||||
|
if (channels[channel].state != JA_CHANNEL_FREE) {
|
||||||
|
if (channels[channel].stream) SDL_DestroyAudioStream(channels[channel].stream);
|
||||||
|
channels[channel].stream = nullptr;
|
||||||
|
channels[channel].state = JA_CHANNEL_FREE;
|
||||||
|
channels[channel].pos = 0;
|
||||||
|
channels[channel].sound = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline JA_Channel_state JA_GetChannelState(const int channel) {
|
||||||
|
if (!JA_soundEnabled) return JA_SOUND_DISABLED;
|
||||||
|
if (channel < 0 || channel >= JA_MAX_SIMULTANEOUS_CHANNELS) return JA_CHANNEL_INVALID;
|
||||||
|
|
||||||
|
return channels[channel].state;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float JA_SetSoundVolume(float volume, const int group = -1) // -1 para todos los grupos
|
||||||
|
{
|
||||||
|
const float v = SDL_clamp(volume, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
if (group == -1) {
|
||||||
|
for (int i = 0; i < JA_MAX_GROUPS; ++i) {
|
||||||
|
JA_soundVolume[i] = v;
|
||||||
|
}
|
||||||
|
} else if (group >= 0 && group < JA_MAX_GROUPS) {
|
||||||
|
JA_soundVolume[group] = v;
|
||||||
|
} else {
|
||||||
|
return v; // Grupo inválido
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aplicar volumen a canales activos
|
||||||
|
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
|
||||||
|
if ((channels[i].state == JA_CHANNEL_PLAYING) || (channels[i].state == JA_CHANNEL_PAUSED)) {
|
||||||
|
if (group == -1 || channels[i].group == group) {
|
||||||
|
if (channels[i].stream) {
|
||||||
|
SDL_SetAudioStreamGain(channels[i].stream, JA_soundVolume[channels[i].group]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void JA_EnableSound(const bool value) {
|
||||||
|
if (!value) {
|
||||||
|
JA_StopChannel(-1); // Detener todos los canales
|
||||||
|
}
|
||||||
|
JA_soundEnabled = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float JA_SetVolume(float volume) {
|
||||||
|
float v = JA_SetMusicVolume(volume);
|
||||||
|
JA_SetSoundVolume(v, -1); // Aplicar a todos los grupos de sonido
|
||||||
|
return v;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <SDL3/SDL.h> // Para SDL_EventType, SDL_Event, SDL_LogInfo, SDL_LogCategory
|
#include <SDL3/SDL.h> // Para SDL_EventType, SDL_Event, SDL_LogInfo, SDL_LogCategory
|
||||||
|
|
||||||
#include <cstddef> // Para size_t
|
#include <cstddef> // Para size_t
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <string> // Para allocator, operator+, string
|
#include <string> // Para allocator, operator+, string
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
|
|
||||||
@@ -50,7 +51,7 @@ namespace GlobalEvents {
|
|||||||
|
|
||||||
case SDL_EVENT_RENDER_DEVICE_RESET:
|
case SDL_EVENT_RENDER_DEVICE_RESET:
|
||||||
case SDL_EVENT_RENDER_TARGETS_RESET:
|
case SDL_EVENT_RENDER_TARGETS_RESET:
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "SDL_RENDER_TARGETS_RESET");
|
std::cout << "SDL_RENDER_TARGETS_RESET" << '\n';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_RESIZED:
|
case SDL_EVENT_WINDOW_RESIZED:
|
||||||
|
|||||||
@@ -8,8 +8,6 @@
|
|||||||
#include <unordered_map> // Para unordered_map, _Node_iterator, operator==, _Node_iterator_base, _Node_const_iterator
|
#include <unordered_map> // Para unordered_map, _Node_iterator, operator==, _Node_iterator_base, _Node_const_iterator
|
||||||
#include <utility> // Para pair, move
|
#include <utility> // Para pair, move
|
||||||
|
|
||||||
#include "ui/logger.hpp" // Para info
|
|
||||||
|
|
||||||
// Singleton
|
// Singleton
|
||||||
Input* Input::instance = nullptr;
|
Input* Input::instance = nullptr;
|
||||||
|
|
||||||
@@ -306,21 +304,27 @@ void Input::addGamepadMappingsFromFile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Input::discoverGamepads() {
|
void Input::discoverGamepads() {
|
||||||
SDL_Event event;
|
// Enumera los gamepads ya conectados sin drenar la cola de eventos de SDL
|
||||||
while (SDL_PollEvent(&event)) {
|
// (necesario con SDL_MAIN_USE_CALLBACKS, que entrega los eventos por SDL_AppEvent).
|
||||||
handleEvent(event); // Comprueba mandos conectados
|
int count = 0;
|
||||||
|
SDL_JoystickID* joysticks = SDL_GetGamepads(&count);
|
||||||
|
if (joysticks == nullptr) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
addGamepad(joysticks[i]);
|
||||||
|
}
|
||||||
|
SDL_free(joysticks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Input::initSDLGamePad() {
|
void Input::initSDLGamePad() {
|
||||||
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1) {
|
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1) {
|
||||||
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
|
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD could not initialize! SDL Error: %s", SDL_GetError());
|
std::cout << "SDL_GAMEPAD could not initialize! SDL Error: " << SDL_GetError() << '\n';
|
||||||
} else {
|
} else {
|
||||||
addGamepadMappingsFromFile();
|
addGamepadMappingsFromFile();
|
||||||
loadGamepadConfigs();
|
loadGamepadConfigs();
|
||||||
discoverGamepads();
|
discoverGamepads();
|
||||||
Logger::info("Input System initialized successfully");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,24 @@ Actualizando a la versión "Arcade Edition" en 08/05/2024
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <memory> // Para make_unique, unique_ptr
|
#define SDL_MAIN_USE_CALLBACKS 1
|
||||||
#include <span> // Para span
|
#include <SDL3/SDL_main.h>
|
||||||
|
|
||||||
#include "director.hpp" // Para Director
|
#include "director.hpp" // Para Director
|
||||||
|
|
||||||
auto main(int argc, char* argv[]) -> int {
|
SDL_AppResult SDL_AppInit(void** appstate, int /*argc*/, char** /*argv*/) {
|
||||||
// Crea el objeto Director
|
*appstate = new Director();
|
||||||
auto director = std::make_unique<Director>(argc, std::span<char*>(argv, argc));
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
// Bucle principal
|
|
||||||
return Director::run();
|
SDL_AppResult SDL_AppIterate(void* appstate) {
|
||||||
|
return static_cast<Director*>(appstate)->iterate();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
||||||
|
return static_cast<Director*>(appstate)->handleEvent(*event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SDL_AppQuit(void* appstate, SDL_AppResult /*result*/) {
|
||||||
|
delete static_cast<Director*>(appstate);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,12 @@
|
|||||||
#include <algorithm> // Para __sort_fn, sort
|
#include <algorithm> // Para __sort_fn, sort
|
||||||
#include <array> // Para array
|
#include <array> // Para array
|
||||||
#include <functional> // Para identity
|
#include <functional> // Para identity
|
||||||
|
#include <iomanip> // Para std::setw, std::setfill
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <iterator> // Para distance
|
#include <iterator> // Para distance
|
||||||
#include <ranges> // Para __find_if_fn, find_if
|
#include <ranges> // Para __find_if_fn, find_if
|
||||||
#include <utility> // Para move
|
#include <utility> // Para move
|
||||||
|
|
||||||
#include "ui/logger.hpp" // Para info
|
|
||||||
#include "utils.hpp" // Para getFileName
|
#include "utils.hpp" // Para getFileName
|
||||||
|
|
||||||
// Resetea la tabla a los valores por defecto
|
// Resetea la tabla a los valores por defecto
|
||||||
@@ -105,7 +106,7 @@ auto ManageHiScoreTable::loadFromFile(const std::string& file_path) -> bool {
|
|||||||
auto* file = SDL_IOFromFile(file_path.c_str(), "rb");
|
auto* file = SDL_IOFromFile(file_path.c_str(), "rb");
|
||||||
|
|
||||||
if (file == nullptr) {
|
if (file == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to load %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
std::cout << "Error: Unable to load " << getFileName(file_path) << " file! " << SDL_GetError() << '\n';
|
||||||
clear();
|
clear();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -152,9 +153,8 @@ auto ManageHiScoreTable::loadFromFile(const std::string& file_path) -> bool {
|
|||||||
// Si todo fue bien, actualizar la tabla; si no, usar valores por defecto
|
// Si todo fue bien, actualizar la tabla; si no, usar valores por defecto
|
||||||
if (success) {
|
if (success) {
|
||||||
table_ = std::move(temp_table);
|
table_ = std::move(temp_table);
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s (loaded %d entries successfully)", getFileName(file_path).c_str(), table_size);
|
|
||||||
} else {
|
} else {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "File %s is corrupted - loading default values", getFileName(file_path).c_str());
|
std::cout << "File " << getFileName(file_path) << " is corrupted - loading default values" << '\n';
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,7 +166,7 @@ auto ManageHiScoreTable::loadFromFile(const std::string& file_path) -> bool {
|
|||||||
auto ManageHiScoreTable::validateMagicNumber(SDL_IOStream* file, const std::string& file_path) -> bool {
|
auto ManageHiScoreTable::validateMagicNumber(SDL_IOStream* file, const std::string& file_path) -> bool {
|
||||||
std::array<char, 4> magic;
|
std::array<char, 4> magic;
|
||||||
if (SDL_ReadIO(file, magic.data(), 4) != 4 || magic[0] != 'C' || magic[1] != 'C' || magic[2] != 'A' || magic[3] != 'E') {
|
if (SDL_ReadIO(file, magic.data(), 4) != 4 || magic[0] != 'C' || magic[1] != 'C' || magic[2] != 'A' || magic[3] != 'E') {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Invalid magic number in %s - file may be corrupted or old format", getFileName(file_path).c_str());
|
std::cout << "Error: Invalid magic number in " << getFileName(file_path) << " - file may be corrupted or old format" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -175,12 +175,12 @@ auto ManageHiScoreTable::validateMagicNumber(SDL_IOStream* file, const std::stri
|
|||||||
auto ManageHiScoreTable::validateVersion(SDL_IOStream* file, const std::string& file_path) -> bool {
|
auto ManageHiScoreTable::validateVersion(SDL_IOStream* file, const std::string& file_path) -> bool {
|
||||||
int version = 0;
|
int version = 0;
|
||||||
if (SDL_ReadIO(file, &version, sizeof(int)) != sizeof(int)) {
|
if (SDL_ReadIO(file, &version, sizeof(int)) != sizeof(int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read version in %s", getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read version in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version != FILE_VERSION) {
|
if (version != FILE_VERSION) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unsupported file version %d in %s (expected %d)", version, getFileName(file_path).c_str(), FILE_VERSION);
|
std::cout << "Error: Unsupported file version " << version << " in " << getFileName(file_path) << " (expected " << FILE_VERSION << ")" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -188,12 +188,12 @@ auto ManageHiScoreTable::validateVersion(SDL_IOStream* file, const std::string&
|
|||||||
|
|
||||||
auto ManageHiScoreTable::readTableSize(SDL_IOStream* file, const std::string& file_path, int& table_size) -> bool {
|
auto ManageHiScoreTable::readTableSize(SDL_IOStream* file, const std::string& file_path, int& table_size) -> bool {
|
||||||
if (SDL_ReadIO(file, &table_size, sizeof(int)) != sizeof(int)) {
|
if (SDL_ReadIO(file, &table_size, sizeof(int)) != sizeof(int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read table size in %s", getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read table size in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (table_size < 0 || table_size > MAX_TABLE_SIZE) {
|
if (table_size < 0 || table_size > MAX_TABLE_SIZE) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Invalid table size %d in %s (expected 0-%d)", table_size, getFileName(file_path).c_str(), MAX_TABLE_SIZE);
|
std::cout << "Error: Invalid table size " << table_size << " in " << getFileName(file_path) << " (expected 0-" << MAX_TABLE_SIZE << ")" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -202,31 +202,31 @@ auto ManageHiScoreTable::readTableSize(SDL_IOStream* file, const std::string& fi
|
|||||||
auto ManageHiScoreTable::readEntry(SDL_IOStream* file, const std::string& file_path, int index, HiScoreEntry& entry) -> bool {
|
auto ManageHiScoreTable::readEntry(SDL_IOStream* file, const std::string& file_path, int index, HiScoreEntry& entry) -> bool {
|
||||||
// Leer y validar puntuación
|
// Leer y validar puntuación
|
||||||
if (SDL_ReadIO(file, &entry.score, sizeof(int)) != sizeof(int)) {
|
if (SDL_ReadIO(file, &entry.score, sizeof(int)) != sizeof(int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read score for entry %d in %s", index, getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read score for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entry.score < 0 || entry.score > MAX_SCORE) {
|
if (entry.score < 0 || entry.score > MAX_SCORE) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Invalid score %d for entry %d in %s", entry.score, index, getFileName(file_path).c_str());
|
std::cout << "Error: Invalid score " << entry.score << " for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leer y validar tamaño del nombre
|
// Leer y validar tamaño del nombre
|
||||||
int name_size = 0;
|
int name_size = 0;
|
||||||
if (SDL_ReadIO(file, &name_size, sizeof(int)) != sizeof(int)) {
|
if (SDL_ReadIO(file, &name_size, sizeof(int)) != sizeof(int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read name size for entry %d in %s", index, getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read name size for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name_size < 0 || name_size > MAX_NAME_SIZE) {
|
if (name_size < 0 || name_size > MAX_NAME_SIZE) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Invalid name size %d for entry %d in %s", name_size, index, getFileName(file_path).c_str());
|
std::cout << "Error: Invalid name size " << name_size << " for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leer el nombre
|
// Leer el nombre
|
||||||
std::vector<char> name_buffer(name_size + 1);
|
std::vector<char> name_buffer(name_size + 1);
|
||||||
if (SDL_ReadIO(file, name_buffer.data(), name_size) != static_cast<size_t>(name_size)) {
|
if (SDL_ReadIO(file, name_buffer.data(), name_size) != static_cast<size_t>(name_size)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read name for entry %d in %s", index, getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read name for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
name_buffer[name_size] = '\0';
|
name_buffer[name_size] = '\0';
|
||||||
@@ -235,7 +235,7 @@ auto ManageHiScoreTable::readEntry(SDL_IOStream* file, const std::string& file_p
|
|||||||
// Leer one_credit_complete
|
// Leer one_credit_complete
|
||||||
int occ_value = 0;
|
int occ_value = 0;
|
||||||
if (SDL_ReadIO(file, &occ_value, sizeof(int)) != sizeof(int)) {
|
if (SDL_ReadIO(file, &occ_value, sizeof(int)) != sizeof(int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read one_credit_complete for entry %d in %s", index, getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read one_credit_complete for entry " << index << " in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
entry.one_credit_complete = (occ_value != 0);
|
entry.one_credit_complete = (occ_value != 0);
|
||||||
@@ -246,13 +246,13 @@ auto ManageHiScoreTable::readEntry(SDL_IOStream* file, const std::string& file_p
|
|||||||
auto ManageHiScoreTable::verifyChecksum(SDL_IOStream* file, const std::string& file_path, const Table& temp_table) -> bool {
|
auto ManageHiScoreTable::verifyChecksum(SDL_IOStream* file, const std::string& file_path, const Table& temp_table) -> bool {
|
||||||
unsigned int stored_checksum = 0;
|
unsigned int stored_checksum = 0;
|
||||||
if (SDL_ReadIO(file, &stored_checksum, sizeof(unsigned int)) != sizeof(unsigned int)) {
|
if (SDL_ReadIO(file, &stored_checksum, sizeof(unsigned int)) != sizeof(unsigned int)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Cannot read checksum in %s", getFileName(file_path).c_str());
|
std::cout << "Error: Cannot read checksum in " << getFileName(file_path) << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int calculated_checksum = calculateChecksum(temp_table);
|
unsigned int calculated_checksum = calculateChecksum(temp_table);
|
||||||
if (stored_checksum != calculated_checksum) {
|
if (stored_checksum != calculated_checksum) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Checksum mismatch in %s (stored: 0x%08X, calculated: 0x%08X) - file is corrupted", getFileName(file_path).c_str(), stored_checksum, calculated_checksum);
|
std::cout << "Error: Checksum mismatch in " << getFileName(file_path) << " (stored: 0x" << std::hex << std::setw(8) << std::setfill('0') << stored_checksum << ", calculated: 0x" << std::setw(8) << std::setfill('0') << calculated_checksum << std::dec << ") - file is corrupted" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -317,11 +317,9 @@ auto ManageHiScoreTable::saveToFile(const std::string& file_path) -> bool {
|
|||||||
unsigned int checksum = calculateChecksum(table_);
|
unsigned int checksum = calculateChecksum(table_);
|
||||||
SDL_WriteIO(file, &checksum, sizeof(unsigned int));
|
SDL_WriteIO(file, &checksum, sizeof(unsigned int));
|
||||||
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file: %s", getFileName(file_path).c_str());
|
|
||||||
Logger::info("Writing file: " + getFileName(file_path));
|
|
||||||
SDL_CloseIO(file);
|
SDL_CloseIO(file);
|
||||||
} else {
|
} else {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
std::cout << "Error: Unable to save " << getFileName(file_path) << " file! " << SDL_GetError() << '\n';
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <algorithm> // Para clamp
|
#include <algorithm> // Para clamp
|
||||||
#include <cstddef> // Para size_t
|
#include <cstddef> // Para size_t
|
||||||
#include <fstream> // Para ifstream, ofstream
|
#include <fstream> // Para ifstream, ofstream
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <string> // Para string
|
#include <string> // Para string
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
|
|
||||||
@@ -12,7 +13,6 @@
|
|||||||
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
#include "external/fkyaml_node.hpp" // Para fkyaml::node
|
||||||
#include "input.hpp" // Para Input
|
#include "input.hpp" // Para Input
|
||||||
#include "lang.hpp" // Para getText, Code
|
#include "lang.hpp" // Para getText, Code
|
||||||
#include "ui/logger.hpp" // Para info
|
|
||||||
#include "utils.hpp" // Para boolToString, getFileName
|
#include "utils.hpp" // Para boolToString, getFileName
|
||||||
|
|
||||||
namespace Options {
|
namespace Options {
|
||||||
@@ -63,7 +63,6 @@ namespace Options {
|
|||||||
|
|
||||||
std::ifstream file(postfx_file_path);
|
std::ifstream file(postfx_file_path);
|
||||||
if (!file.good()) {
|
if (!file.good()) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "PostFX file not found, creating default: %s", postfx_file_path.c_str());
|
|
||||||
return savePostFXToFile();
|
return savePostFXToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,11 +109,10 @@ namespace Options {
|
|||||||
video.shader.current_postfx_preset = 0;
|
video.shader.current_postfx_preset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "PostFX file loaded: %zu preset(s)", postfx_presets.size());
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (const fkyaml::exception& e) {
|
} catch (const fkyaml::exception& e) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Error parsing PostFX YAML: %s. Recreating defaults.", e.what());
|
std::cout << "Error parsing PostFX YAML: " << e.what() << ". Recreating defaults." << '\n';
|
||||||
return savePostFXToFile();
|
return savePostFXToFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,7 +125,7 @@ namespace Options {
|
|||||||
|
|
||||||
std::ofstream file(postfx_file_path);
|
std::ofstream file(postfx_file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: %s can't be opened for writing", postfx_file_path.c_str());
|
std::cout << "Error: " << postfx_file_path << " can't be opened for writing" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,8 +198,6 @@ namespace Options {
|
|||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "PostFX file created with defaults: %s", postfx_file_path.c_str());
|
|
||||||
|
|
||||||
// Cargar los presets recién escritos
|
// Cargar los presets recién escritos
|
||||||
postfx_presets.clear();
|
postfx_presets.clear();
|
||||||
postfx_presets.push_back({"CRT", 0.15F, 0.7F, 0.2F, 0.5F, 0.1F, 0.0F, 0.0F, 0.0F});
|
postfx_presets.push_back({"CRT", 0.15F, 0.7F, 0.2F, 0.5F, 0.1F, 0.0F, 0.0F, 0.0F});
|
||||||
@@ -247,7 +243,7 @@ namespace Options {
|
|||||||
if (crtpi_file_path.empty()) { return false; }
|
if (crtpi_file_path.empty()) { return false; }
|
||||||
std::ofstream file(crtpi_file_path);
|
std::ofstream file(crtpi_file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: %s can't be opened for writing", crtpi_file_path.c_str());
|
std::cout << "Error: " << crtpi_file_path << " can't be opened for writing" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
file << "# Coffee Crisis Arcade Edition - CrtPi Shader Presets\n";
|
file << "# Coffee Crisis Arcade Edition - CrtPi Shader Presets\n";
|
||||||
@@ -266,7 +262,6 @@ namespace Options {
|
|||||||
file << " - name: \"Sharp\"\n scanline_weight: 6.0\n scanline_gap_brightness: 0.12\n bloom_factor: 3.5\n input_gamma: 2.4\n output_gamma: 2.2\n mask_brightness: 0.80\n curvature_x: 0.05\n curvature_y: 0.10\n mask_type: 2\n enable_scanlines: true\n enable_multisample: false\n enable_gamma: true\n enable_curvature: false\n enable_sharper: true\n";
|
file << " - name: \"Sharp\"\n scanline_weight: 6.0\n scanline_gap_brightness: 0.12\n bloom_factor: 3.5\n input_gamma: 2.4\n output_gamma: 2.2\n mask_brightness: 0.80\n curvature_x: 0.05\n curvature_y: 0.10\n mask_type: 2\n enable_scanlines: true\n enable_multisample: false\n enable_gamma: true\n enable_curvature: false\n enable_sharper: true\n";
|
||||||
file << " - name: \"Minimal\"\n scanline_weight: 8.0\n scanline_gap_brightness: 0.05\n bloom_factor: 2.0\n input_gamma: 2.4\n output_gamma: 2.2\n mask_brightness: 1.00\n curvature_x: 0.0\n curvature_y: 0.0\n mask_type: 0\n enable_scanlines: true\n enable_multisample: false\n enable_gamma: false\n enable_curvature: false\n enable_sharper: false\n";
|
file << " - name: \"Minimal\"\n scanline_weight: 8.0\n scanline_gap_brightness: 0.05\n bloom_factor: 2.0\n input_gamma: 2.4\n output_gamma: 2.2\n mask_brightness: 1.00\n curvature_x: 0.0\n curvature_y: 0.0\n mask_type: 0\n enable_scanlines: true\n enable_multisample: false\n enable_gamma: false\n enable_curvature: false\n enable_sharper: false\n";
|
||||||
file.close();
|
file.close();
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "CrtPi file created with defaults: %s", crtpi_file_path.c_str());
|
|
||||||
populateDefaultCrtPiPresets();
|
populateDefaultCrtPiPresets();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -277,7 +272,6 @@ namespace Options {
|
|||||||
|
|
||||||
std::ifstream file(crtpi_file_path);
|
std::ifstream file(crtpi_file_path);
|
||||||
if (!file.good()) {
|
if (!file.good()) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "CrtPi file not found, creating default: %s", crtpi_file_path.c_str());
|
|
||||||
return saveCrtPiDefaults();
|
return saveCrtPiDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,11 +324,10 @@ namespace Options {
|
|||||||
video.shader.current_crtpi_preset = 0;
|
video.shader.current_crtpi_preset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "CrtPi file loaded: %zu preset(s)", crtpi_presets.size());
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (const fkyaml::exception& e) {
|
} catch (const fkyaml::exception& e) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Error parsing CrtPi YAML: %s. Recreating defaults.", e.what());
|
std::cout << "Error parsing CrtPi YAML: " << e.what() << ". Recreating defaults." << '\n';
|
||||||
return saveCrtPiDefaults();
|
return saveCrtPiDefaults();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -581,12 +574,10 @@ namespace Options {
|
|||||||
|
|
||||||
std::ifstream file(settings.config_file);
|
std::ifstream file(settings.config_file);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Config file not found. Creating default settings.");
|
|
||||||
saveToFile();
|
saveToFile();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::info("Reading file: " + getFileName(settings.config_file));
|
|
||||||
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
@@ -601,7 +592,7 @@ namespace Options {
|
|||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
}
|
}
|
||||||
if (file_version != Settings::CURRENT_CONFIG_VERSION) {
|
if (file_version != Settings::CURRENT_CONFIG_VERSION) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Config version %d != expected %d. Recreating defaults.", file_version, Settings::CURRENT_CONFIG_VERSION);
|
std::cout << "Config version " << file_version << " != expected " << Settings::CURRENT_CONFIG_VERSION << ". Recreating defaults." << '\n';
|
||||||
init();
|
init();
|
||||||
saveToFile();
|
saveToFile();
|
||||||
return true;
|
return true;
|
||||||
@@ -615,7 +606,7 @@ namespace Options {
|
|||||||
loadKeyboardFromYaml(yaml);
|
loadKeyboardFromYaml(yaml);
|
||||||
|
|
||||||
} catch (const fkyaml::exception& e) {
|
} catch (const fkyaml::exception& e) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Error parsing YAML config: %s. Using defaults.", e.what());
|
std::cout << "Error parsing YAML config: " << e.what() << ". Using defaults." << '\n';
|
||||||
init();
|
init();
|
||||||
saveToFile();
|
saveToFile();
|
||||||
return true;
|
return true;
|
||||||
@@ -630,12 +621,10 @@ namespace Options {
|
|||||||
std::ofstream file(settings.config_file);
|
std::ofstream file(settings.config_file);
|
||||||
|
|
||||||
if (!file.good()) {
|
if (!file.good()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: %s can't be opened", getFileName(settings.config_file).c_str());
|
std::cout << "Error: " << getFileName(settings.config_file) << " can't be opened" << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::info("Writing file: " + getFileName(settings.config_file));
|
|
||||||
|
|
||||||
applyPendingChanges();
|
applyPendingChanges();
|
||||||
|
|
||||||
file << "# Coffee Crisis Arcade Edition - Configuration File\n";
|
file << "# Coffee Crisis Arcade Edition - Configuration File\n";
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <fstream> // Para basic_istream, basic_ifstream, ifstream, istringstream
|
#include <fstream> // Para basic_istream, basic_ifstream, ifstream, istringstream
|
||||||
#include <functional> // Para function
|
#include <functional> // Para function
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <sstream> // Para basic_istringstream
|
#include <sstream> // Para basic_istringstream
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string> // Para string, basic_string, stoi, stof, hash, allocator, operator==, char_traits, operator+, operator>>, getline
|
#include <string> // Para string, basic_string, stoi, stof, hash, allocator, operator==, char_traits, operator+, operator>>, getline
|
||||||
@@ -11,7 +12,6 @@
|
|||||||
#include <utility> // Para pair
|
#include <utility> // Para pair
|
||||||
|
|
||||||
#include "color.hpp" // Para Color
|
#include "color.hpp" // Para Color
|
||||||
#include "ui/logger.hpp" // Para info, YELLOW
|
|
||||||
#include "ui/notifier.hpp" // Para Notifier
|
#include "ui/notifier.hpp" // Para Notifier
|
||||||
#include "utils.hpp" // Para Zone, stringToBool, getFileName
|
#include "utils.hpp" // Para Zone, stringToBool, getFileName
|
||||||
|
|
||||||
@@ -50,13 +50,10 @@ void loadParamsFromFile(const std::string& file_path) {
|
|||||||
|
|
||||||
std::ifstream file(file_path);
|
std::ifstream file(file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo abrir el archivo %s", file_path.c_str());
|
std::cout << "Error: No se pudo abrir el archivo " << file_path << '\n';
|
||||||
throw std::runtime_error("No se pudo abrir el archivo: " + file_path);
|
throw std::runtime_error("No se pudo abrir el archivo: " + file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nReading file: %s", getFileName(file_path).c_str());
|
|
||||||
Logger::info("Reading file: " + getFileName(file_path));
|
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
std::string param_name;
|
std::string param_name;
|
||||||
std::string param_value;
|
std::string param_value;
|
||||||
@@ -72,7 +69,7 @@ void loadParamsFromFile(const std::string& file_path) {
|
|||||||
std::istringstream iss(line);
|
std::istringstream iss(line);
|
||||||
if (iss >> param_name >> param_value) {
|
if (iss >> param_name >> param_value) {
|
||||||
if (!setParams(param_name, param_value)) {
|
if (!setParams(param_name, param_value)) {
|
||||||
Logger::info("WARNING: Parámetro desconocido: " + param_name, Logger::YELLOW);
|
std::cout << "WARNING: Parámetro desconocido: " << param_name << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -208,7 +205,7 @@ namespace {
|
|||||||
static const std::unordered_map<std::string, std::function<void(const std::string&)>> STRING_PARAMS = {
|
static const std::unordered_map<std::string, std::function<void(const std::string&)>> STRING_PARAMS = {
|
||||||
{"balloon.color[0]", [validate_balloon_color](const std::string& v) -> void {
|
{"balloon.color[0]", [validate_balloon_color](const std::string& v) -> void {
|
||||||
if (!validate_balloon_color(v)) {
|
if (!validate_balloon_color(v)) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Color de globo inválido '%s'. Usando 'blue' por defecto.", v.c_str());
|
std::cout << "Color de globo inválido '" << v << "'. Usando 'blue' por defecto." << '\n';
|
||||||
param.balloon.color.at(0) = "blue";
|
param.balloon.color.at(0) = "blue";
|
||||||
} else {
|
} else {
|
||||||
param.balloon.color.at(0) = v;
|
param.balloon.color.at(0) = v;
|
||||||
@@ -216,7 +213,7 @@ namespace {
|
|||||||
}},
|
}},
|
||||||
{"balloon.color[1]", [validate_balloon_color](const std::string& v) -> void {
|
{"balloon.color[1]", [validate_balloon_color](const std::string& v) -> void {
|
||||||
if (!validate_balloon_color(v)) {
|
if (!validate_balloon_color(v)) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Color de globo inválido '%s'. Usando 'orange' por defecto.", v.c_str());
|
std::cout << "Color de globo inválido '" << v << "'. Usando 'orange' por defecto." << '\n';
|
||||||
param.balloon.color.at(1) = "orange";
|
param.balloon.color.at(1) = "orange";
|
||||||
} else {
|
} else {
|
||||||
param.balloon.color.at(1) = v;
|
param.balloon.color.at(1) = v;
|
||||||
@@ -224,7 +221,7 @@ namespace {
|
|||||||
}},
|
}},
|
||||||
{"balloon.color[2]", [validate_balloon_color](const std::string& v) -> void {
|
{"balloon.color[2]", [validate_balloon_color](const std::string& v) -> void {
|
||||||
if (!validate_balloon_color(v)) {
|
if (!validate_balloon_color(v)) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Color de globo inválido '%s'. Usando 'red' por defecto.", v.c_str());
|
std::cout << "Color de globo inválido '" << v << "'. Usando 'red' por defecto." << '\n';
|
||||||
param.balloon.color.at(2) = "red";
|
param.balloon.color.at(2) = "red";
|
||||||
} else {
|
} else {
|
||||||
param.balloon.color.at(2) = v;
|
param.balloon.color.at(2) = v;
|
||||||
@@ -232,7 +229,7 @@ namespace {
|
|||||||
}},
|
}},
|
||||||
{"balloon.color[3]", [validate_balloon_color](const std::string& v) -> void {
|
{"balloon.color[3]", [validate_balloon_color](const std::string& v) -> void {
|
||||||
if (!validate_balloon_color(v)) {
|
if (!validate_balloon_color(v)) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Color de globo inválido '%s'. Usando 'green' por defecto.", v.c_str());
|
std::cout << "Color de globo inválido '" << v << "'. Usando 'green' por defecto." << '\n';
|
||||||
param.balloon.color.at(3) = "green";
|
param.balloon.color.at(3) = "green";
|
||||||
} else {
|
} else {
|
||||||
param.balloon.color.at(3) = v;
|
param.balloon.color.at(3) = v;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <algorithm> // std::min, std::max, std::floor
|
#include <algorithm> // std::min, std::max, std::floor
|
||||||
#include <cmath> // std::floor, std::ceil
|
#include <cmath> // std::floor, std::ceil
|
||||||
#include <cstring> // memcpy, strlen
|
#include <cstring> // memcpy, strlen
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
|
|
||||||
#ifndef __APPLE__
|
#ifndef __APPLE__
|
||||||
#include "rendering/sdl3gpu/crtpi_frag_spv.h"
|
#include "rendering/sdl3gpu/crtpi_frag_spv.h"
|
||||||
@@ -377,7 +378,7 @@ namespace Rendering {
|
|||||||
|
|
||||||
// 1. GPU disabled by config
|
// 1. GPU disabled by config
|
||||||
if (preferred_driver_ == "none") {
|
if (preferred_driver_ == "none") {
|
||||||
SDL_Log("SDL3GPUShader: GPU disabled by config, using SDL_Renderer fallback");
|
std::cout << "SDL3GPUShader: GPU disabled by config, using SDL_Renderer fallback" << '\n';
|
||||||
driver_name_ = "";
|
driver_name_ = "";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -392,18 +393,18 @@ namespace Rendering {
|
|||||||
const char* preferred = preferred_driver_.empty() ? nullptr : preferred_driver_.c_str();
|
const char* preferred = preferred_driver_.empty() ? nullptr : preferred_driver_.c_str();
|
||||||
device_ = SDL_CreateGPUDevice(PREFERRED, false, preferred);
|
device_ = SDL_CreateGPUDevice(PREFERRED, false, preferred);
|
||||||
if (device_ == nullptr && preferred != nullptr) {
|
if (device_ == nullptr && preferred != nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: driver '%s' not available, falling back to auto", preferred);
|
std::cout << "SDL3GPUShader: driver '" << preferred << "' not available, falling back to auto" << '\n';
|
||||||
device_ = SDL_CreateGPUDevice(PREFERRED, false, nullptr);
|
device_ = SDL_CreateGPUDevice(PREFERRED, false, nullptr);
|
||||||
}
|
}
|
||||||
if (device_ == nullptr) {
|
if (device_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: SDL_CreateGPUDevice failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: SDL_CreateGPUDevice failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
driver_name_ = SDL_GetGPUDeviceDriver(device_);
|
driver_name_ = SDL_GetGPUDeviceDriver(device_);
|
||||||
SDL_Log("SDL3GPUShader: driver = %s", driver_name_.c_str());
|
std::cout << "SDL3GPUShader: driver = " << driver_name_ << '\n';
|
||||||
|
|
||||||
if (!SDL_ClaimWindowForGPUDevice(device_, window_)) {
|
if (!SDL_ClaimWindowForGPUDevice(device_, window_)) {
|
||||||
SDL_Log("SDL3GPUShader: SDL_ClaimWindowForGPUDevice failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: SDL_ClaimWindowForGPUDevice failed: " << SDL_GetError() << '\n';
|
||||||
SDL_DestroyGPUDevice(device_);
|
SDL_DestroyGPUDevice(device_);
|
||||||
device_ = nullptr;
|
device_ = nullptr;
|
||||||
return false;
|
return false;
|
||||||
@@ -422,7 +423,7 @@ namespace Rendering {
|
|||||||
tex_info.num_levels = 1;
|
tex_info.num_levels = 1;
|
||||||
scene_texture_ = SDL_CreateGPUTexture(device_, &tex_info);
|
scene_texture_ = SDL_CreateGPUTexture(device_, &tex_info);
|
||||||
if (scene_texture_ == nullptr) {
|
if (scene_texture_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create scene texture: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create scene texture: " << SDL_GetError() << '\n';
|
||||||
cleanup();
|
cleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -435,7 +436,7 @@ namespace Rendering {
|
|||||||
tb_info.size = static_cast<Uint32>(game_width_ * game_height_ * 4);
|
tb_info.size = static_cast<Uint32>(game_width_ * game_height_ * 4);
|
||||||
upload_buffer_ = SDL_CreateGPUTransferBuffer(device_, &tb_info);
|
upload_buffer_ = SDL_CreateGPUTransferBuffer(device_, &tb_info);
|
||||||
if (upload_buffer_ == nullptr) {
|
if (upload_buffer_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create upload buffer: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create upload buffer: " << SDL_GetError() << '\n';
|
||||||
cleanup();
|
cleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -450,7 +451,7 @@ namespace Rendering {
|
|||||||
samp_info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
samp_info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
||||||
sampler_ = SDL_CreateGPUSampler(device_, &samp_info);
|
sampler_ = SDL_CreateGPUSampler(device_, &samp_info);
|
||||||
if (sampler_ == nullptr) {
|
if (sampler_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create sampler: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create sampler: " << SDL_GetError() << '\n';
|
||||||
cleanup();
|
cleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -464,7 +465,7 @@ namespace Rendering {
|
|||||||
lsamp_info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
lsamp_info.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE;
|
||||||
linear_sampler_ = SDL_CreateGPUSampler(device_, &lsamp_info);
|
linear_sampler_ = SDL_CreateGPUSampler(device_, &lsamp_info);
|
||||||
if (linear_sampler_ == nullptr) {
|
if (linear_sampler_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create linear sampler: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create linear sampler: " << SDL_GetError() << '\n';
|
||||||
cleanup();
|
cleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -480,7 +481,7 @@ namespace Rendering {
|
|||||||
}
|
}
|
||||||
|
|
||||||
is_initialized_ = true;
|
is_initialized_ = true;
|
||||||
SDL_Log("SDL3GPUShader: initialized OK (%dx%d)", game_width_, game_height_);
|
std::cout << "SDL3GPUShader: initialized OK (" << game_width_ << "x" << game_height_ << ")" << '\n';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -500,7 +501,7 @@ namespace Rendering {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ((vert == nullptr) || (frag == nullptr)) {
|
if ((vert == nullptr) || (frag == nullptr)) {
|
||||||
SDL_Log("SDL3GPUShader: failed to compile PostFX shaders");
|
std::cout << "SDL3GPUShader: failed to compile PostFX shaders" << '\n';
|
||||||
if (vert != nullptr) { SDL_ReleaseGPUShader(device_, vert); }
|
if (vert != nullptr) { SDL_ReleaseGPUShader(device_, vert); }
|
||||||
if (frag != nullptr) { SDL_ReleaseGPUShader(device_, frag); }
|
if (frag != nullptr) { SDL_ReleaseGPUShader(device_, frag); }
|
||||||
return false;
|
return false;
|
||||||
@@ -529,7 +530,7 @@ namespace Rendering {
|
|||||||
SDL_ReleaseGPUShader(device_, frag);
|
SDL_ReleaseGPUShader(device_, frag);
|
||||||
|
|
||||||
if (pipeline_ == nullptr) {
|
if (pipeline_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: PostFX pipeline creation failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: PostFX pipeline creation failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,7 +566,7 @@ namespace Rendering {
|
|||||||
SDL_ReleaseGPUShader(device_, ufrag);
|
SDL_ReleaseGPUShader(device_, ufrag);
|
||||||
|
|
||||||
if (upscale_pipeline_ == nullptr) {
|
if (upscale_pipeline_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: upscale pipeline creation failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: upscale pipeline creation failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -601,7 +602,7 @@ namespace Rendering {
|
|||||||
SDL_ReleaseGPUShader(device_, offrag);
|
SDL_ReleaseGPUShader(device_, offrag);
|
||||||
|
|
||||||
if (postfx_offscreen_pipeline_ == nullptr) {
|
if (postfx_offscreen_pipeline_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: PostFX offscreen pipeline creation failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: PostFX offscreen pipeline creation failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -637,7 +638,7 @@ namespace Rendering {
|
|||||||
SDL_ReleaseGPUShader(device_, dfrag);
|
SDL_ReleaseGPUShader(device_, dfrag);
|
||||||
|
|
||||||
if (downscale_pipeline_ == nullptr) {
|
if (downscale_pipeline_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: downscale pipeline creation failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: downscale pipeline creation failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -683,7 +684,7 @@ namespace Rendering {
|
|||||||
SDL_ReleaseGPUShader(device_, frag);
|
SDL_ReleaseGPUShader(device_, frag);
|
||||||
|
|
||||||
if (crtpi_pipeline_ == nullptr) {
|
if (crtpi_pipeline_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: CrtPi pipeline creation failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: CrtPi pipeline creation failed: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -697,7 +698,7 @@ namespace Rendering {
|
|||||||
|
|
||||||
void* mapped = SDL_MapGPUTransferBuffer(device_, upload_buffer_, false);
|
void* mapped = SDL_MapGPUTransferBuffer(device_, upload_buffer_, false);
|
||||||
if (mapped == nullptr) {
|
if (mapped == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: SDL_MapGPUTransferBuffer failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: SDL_MapGPUTransferBuffer failed: " << SDL_GetError() << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -726,7 +727,7 @@ namespace Rendering {
|
|||||||
|
|
||||||
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device_);
|
SDL_GPUCommandBuffer* cmd = SDL_AcquireGPUCommandBuffer(device_);
|
||||||
if (cmd == nullptr) {
|
if (cmd == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: SDL_AcquireGPUCommandBuffer failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: SDL_AcquireGPUCommandBuffer failed: " << SDL_GetError() << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -773,7 +774,7 @@ namespace Rendering {
|
|||||||
Uint32 sw = 0;
|
Uint32 sw = 0;
|
||||||
Uint32 sh = 0;
|
Uint32 sh = 0;
|
||||||
if (!SDL_AcquireGPUSwapchainTexture(cmd, window_, &swapchain, &sw, &sh)) {
|
if (!SDL_AcquireGPUSwapchainTexture(cmd, window_, &swapchain, &sw, &sh)) {
|
||||||
SDL_Log("SDL3GPUShader: SDL_AcquireGPUSwapchainTexture failed: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: SDL_AcquireGPUSwapchainTexture failed: " << SDL_GetError() << '\n';
|
||||||
SDL_SubmitGPUCommandBuffer(cmd);
|
SDL_SubmitGPUCommandBuffer(cmd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -998,7 +999,7 @@ namespace Rendering {
|
|||||||
info.num_uniform_buffers = num_uniform_buffers;
|
info.num_uniform_buffers = num_uniform_buffers;
|
||||||
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
|
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
|
||||||
if (shader == nullptr) {
|
if (shader == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: MSL shader '%s' failed: %s", entrypoint, SDL_GetError());
|
std::cout << "SDL3GPUShader: MSL shader '" << entrypoint << "' failed: " << SDL_GetError() << '\n';
|
||||||
}
|
}
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
@@ -1020,7 +1021,7 @@ namespace Rendering {
|
|||||||
info.num_uniform_buffers = num_uniform_buffers;
|
info.num_uniform_buffers = num_uniform_buffers;
|
||||||
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
|
SDL_GPUShader* shader = SDL_CreateGPUShader(device, &info);
|
||||||
if (shader == nullptr) {
|
if (shader == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: SPIRV shader '%s' failed: %s", entrypoint, SDL_GetError());
|
std::cout << "SDL3GPUShader: SPIRV shader '" << entrypoint << "' failed: " << SDL_GetError() << '\n';
|
||||||
}
|
}
|
||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
@@ -1135,7 +1136,7 @@ namespace Rendering {
|
|||||||
tex_info.num_levels = 1;
|
tex_info.num_levels = 1;
|
||||||
scene_texture_ = SDL_CreateGPUTexture(device_, &tex_info);
|
scene_texture_ = SDL_CreateGPUTexture(device_, &tex_info);
|
||||||
if (scene_texture_ == nullptr) {
|
if (scene_texture_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: reinit — failed to create scene texture: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: reinit — failed to create scene texture: " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1144,13 +1145,13 @@ namespace Rendering {
|
|||||||
tb_info.size = static_cast<Uint32>(game_width_ * game_height_ * 4);
|
tb_info.size = static_cast<Uint32>(game_width_ * game_height_ * 4);
|
||||||
upload_buffer_ = SDL_CreateGPUTransferBuffer(device_, &tb_info);
|
upload_buffer_ = SDL_CreateGPUTransferBuffer(device_, &tb_info);
|
||||||
if (upload_buffer_ == nullptr) {
|
if (upload_buffer_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: reinit — failed to create upload buffer: %s", SDL_GetError());
|
std::cout << "SDL3GPUShader: reinit — failed to create upload buffer: " << SDL_GetError() << '\n';
|
||||||
SDL_ReleaseGPUTexture(device_, scene_texture_);
|
SDL_ReleaseGPUTexture(device_, scene_texture_);
|
||||||
scene_texture_ = nullptr;
|
scene_texture_ = nullptr;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Log("SDL3GPUShader: reinit — scene %dx%d, SS %s", game_width_, game_height_, oversample_ > 1 ? "on" : "off");
|
std::cout << "SDL3GPUShader: reinit — scene " << game_width_ << "x" << game_height_ << ", SS " << (oversample_ > 1 ? "on" : "off") << '\n';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1185,20 +1186,20 @@ namespace Rendering {
|
|||||||
|
|
||||||
scaled_texture_ = SDL_CreateGPUTexture(device_, &info);
|
scaled_texture_ = SDL_CreateGPUTexture(device_, &info);
|
||||||
if (scaled_texture_ == nullptr) {
|
if (scaled_texture_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create scaled texture %dx%d: %s", W, H, SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create scaled texture " << W << "x" << H << ": " << SDL_GetError() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
postfx_texture_ = SDL_CreateGPUTexture(device_, &info);
|
postfx_texture_ = SDL_CreateGPUTexture(device_, &info);
|
||||||
if (postfx_texture_ == nullptr) {
|
if (postfx_texture_ == nullptr) {
|
||||||
SDL_Log("SDL3GPUShader: failed to create postfx texture %dx%d: %s", W, H, SDL_GetError());
|
std::cout << "SDL3GPUShader: failed to create postfx texture " << W << "x" << H << ": " << SDL_GetError() << '\n';
|
||||||
SDL_ReleaseGPUTexture(device_, scaled_texture_);
|
SDL_ReleaseGPUTexture(device_, scaled_texture_);
|
||||||
scaled_texture_ = nullptr;
|
scaled_texture_ = nullptr;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ss_factor_ = factor;
|
ss_factor_ = factor;
|
||||||
SDL_Log("SDL3GPUShader: scaled+postfx textures %dx%d (factor %dx)", W, H, factor);
|
std::cout << "SDL3GPUShader: scaled+postfx textures " << W << "x" << H << " (factor " << factor << "x)" << '\n';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,19 +7,19 @@
|
|||||||
#include <exception> // Para exception
|
#include <exception> // Para exception
|
||||||
#include <filesystem> // Para exists, path, remove
|
#include <filesystem> // Para exists, path, remove
|
||||||
#include <fstream> // Para basic_ofstream, basic_ios, basic_ostream::write, ios, ofstream
|
#include <fstream> // Para basic_ofstream, basic_ios, basic_ostream::write, ios, ofstream
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <ranges> // Para __find_if_fn, find_if, __find_fn, find
|
#include <ranges> // Para __find_if_fn, find_if, __find_fn, find
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <utility> // Para move
|
#include <utility> // Para move
|
||||||
|
|
||||||
#include "asset.hpp" // Para Asset
|
#include "asset.hpp" // Para Asset
|
||||||
#include "color.hpp" // Para Color, NO_COLOR_MOD
|
#include "color.hpp" // Para Color, NO_COLOR_MOD
|
||||||
#include "external/jail_audio.h" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
|
#include "external/jail_audio.hpp" // Para JA_LoadMusic, JA_LoadSound, JA_DeleteMusic, JA_DeleteSound
|
||||||
#include "lang.hpp" // Para getText
|
#include "lang.hpp" // Para getText
|
||||||
#include "param.hpp" // Para Param, param, ParamPlayer, ParamResource, ParamGame
|
#include "param.hpp" // Para Param, param, ParamPlayer, ParamResource, ParamGame
|
||||||
#include "resource_helper.hpp" // Para loadFile
|
#include "resource_helper.hpp" // Para loadFile
|
||||||
#include "screen.hpp" // Para Screen
|
#include "screen.hpp" // Para Screen
|
||||||
#include "text.hpp" // Para Text
|
#include "text.hpp" // Para Text
|
||||||
#include "ui/logger.hpp" // Para info, CR, dots
|
|
||||||
#include "utils.hpp" // Para getFileName
|
#include "utils.hpp" // Para getFileName
|
||||||
#include "version.h" // Para APP_NAME, GIT_HASH
|
#include "version.h" // Para APP_NAME, GIT_HASH
|
||||||
|
|
||||||
@@ -80,21 +80,16 @@ Resource::~Resource() {
|
|||||||
|
|
||||||
// Carga los recursos esenciales que siempre se necesitan (modo lazy)
|
// Carga los recursos esenciales que siempre se necesitan (modo lazy)
|
||||||
void Resource::loadEssentialResources() {
|
void Resource::loadEssentialResources() {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading essential resources for lazy mode");
|
|
||||||
|
|
||||||
// Cargar recursos de texto básicos que se usan para crear texturas
|
// Cargar recursos de texto básicos que se usan para crear texturas
|
||||||
loadTextFilesQuiet(); // <- VERSIÓN SILENCIOSA
|
loadTextFilesQuiet(); // <- VERSIÓN SILENCIOSA
|
||||||
loadEssentialTextures(); // Ya es silenciosa
|
loadEssentialTextures(); // Ya es silenciosa
|
||||||
createText(); // Crear objetos de texto
|
createText(); // Crear objetos de texto
|
||||||
createTextTextures(); // Crear texturas generadas (game_text_xxx)
|
createTextTextures(); // Crear texturas generadas (game_text_xxx)
|
||||||
createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta
|
createPlayerTextures(); // Crea las texturas de jugadores con todas sus variantes de paleta
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Essential resources loaded");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga los ficheros de texto del juego (versión silenciosa)
|
// Carga los ficheros de texto del juego (versión silenciosa)
|
||||||
void Resource::loadTextFilesQuiet() {
|
void Resource::loadTextFilesQuiet() {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> TEXT FILES (quiet load)");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||||
|
|
||||||
for (const auto& l : list) {
|
for (const auto& l : list) {
|
||||||
@@ -103,7 +98,6 @@ void Resource::loadTextFilesQuiet() {
|
|||||||
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) -> auto { return t.name == name; });
|
auto it = std::ranges::find_if(text_files_, [&name](const auto& t) -> auto { return t.name == name; });
|
||||||
if (it != text_files_.end()) {
|
if (it != text_files_.end()) {
|
||||||
it->text_file = Text::loadFile(l);
|
it->text_file = Text::loadFile(l);
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Text file loaded: %s", name.c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,8 +135,6 @@ void Resource::loadEssentialTextures() {
|
|||||||
|
|
||||||
// Inicializa las listas de recursos sin cargar el contenido (modo lazy)
|
// Inicializa las listas de recursos sin cargar el contenido (modo lazy)
|
||||||
void Resource::initResourceLists() {
|
void Resource::initResourceLists() {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Initializing resource lists for lazy loading");
|
|
||||||
|
|
||||||
// Inicializa lista de sonidos
|
// Inicializa lista de sonidos
|
||||||
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||||
sounds_.clear();
|
sounds_.clear();
|
||||||
@@ -200,8 +192,6 @@ void Resource::initResourceLists() {
|
|||||||
for (const auto& text_name : TEXT_OBJECTS) {
|
for (const auto& text_name : TEXT_OBJECTS) {
|
||||||
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
|
texts_.emplace_back(text_name); // Constructor con nullptr por defecto
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Resource lists initialized for lazy loading");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene el sonido a partir de un nombre (con carga perezosa)
|
// Obtiene el sonido a partir de un nombre (con carga perezosa)
|
||||||
@@ -216,7 +206,7 @@ auto Resource::getSound(const std::string& name) -> JA_Sound_t* {
|
|||||||
return it->sound;
|
return it->sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Sonido no encontrado %s", name.c_str());
|
std::cout << "Error: Sonido no encontrado " << name << '\n';
|
||||||
throw std::runtime_error("Sonido no encontrado: " + name);
|
throw std::runtime_error("Sonido no encontrado: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,7 +222,7 @@ auto Resource::getMusic(const std::string& name) -> JA_Music_t* {
|
|||||||
return it->music;
|
return it->music;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Música no encontrada %s", name.c_str());
|
std::cout << "Error: Música no encontrada " << name << '\n';
|
||||||
throw std::runtime_error("Música no encontrada: " + name);
|
throw std::runtime_error("Música no encontrada: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +238,7 @@ auto Resource::getTexture(const std::string& name) -> std::shared_ptr<Texture> {
|
|||||||
return it->texture;
|
return it->texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Imagen no encontrada %s", name.c_str());
|
std::cout << "Error: Imagen no encontrada " << name << '\n';
|
||||||
throw std::runtime_error("Imagen no encontrada: " + name);
|
throw std::runtime_error("Imagen no encontrada: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +254,7 @@ auto Resource::getTextFile(const std::string& name) -> std::shared_ptr<Text::Fil
|
|||||||
return it->text_file;
|
return it->text_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: TextFile no encontrado %s", name.c_str());
|
std::cout << "Error: TextFile no encontrado " << name << '\n';
|
||||||
throw std::runtime_error("TextFile no encontrado: " + name);
|
throw std::runtime_error("TextFile no encontrado: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +270,7 @@ auto Resource::getText(const std::string& name) -> std::shared_ptr<Text> {
|
|||||||
return it->text;
|
return it->text;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Text no encontrado %s", name.c_str());
|
std::cout << "Error: Text no encontrado " << name << '\n';
|
||||||
throw std::runtime_error("Text no encontrado: " + name);
|
throw std::runtime_error("Text no encontrado: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,14 +286,14 @@ auto Resource::getAnimation(const std::string& name) -> AnimationsFileBuffer& {
|
|||||||
return it->animation;
|
return it->animation;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Animación no encontrada %s", name.c_str());
|
std::cout << "Error: Animación no encontrada " << name << '\n';
|
||||||
throw std::runtime_error("Animación no encontrada: " + name);
|
throw std::runtime_error("Animación no encontrada: " + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
|
// Obtiene el fichero con los datos para el modo demostración a partir de un índice
|
||||||
auto Resource::getDemoData(int index) -> DemoData& {
|
auto Resource::getDemoData(int index) -> DemoData& {
|
||||||
if (index < 0 || std::cmp_greater_equal(index, demos_.size())) {
|
if (index < 0 || std::cmp_greater_equal(index, demos_.size())) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index %d out of range for demo data (size: %d)", index, static_cast<int>(demos_.size()));
|
std::cout << "Index " << index << " out of range for demo data (size: " << static_cast<int>(demos_.size()) << ")" << '\n';
|
||||||
static DemoData empty_demo_;
|
static DemoData empty_demo_;
|
||||||
return empty_demo_;
|
return empty_demo_;
|
||||||
}
|
}
|
||||||
@@ -313,7 +303,6 @@ auto Resource::getDemoData(int index) -> DemoData& {
|
|||||||
// --- Métodos de carga perezosa ---
|
// --- Métodos de carga perezosa ---
|
||||||
|
|
||||||
auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
|
auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading sound lazily: %s", name.c_str());
|
|
||||||
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
auto sound_list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||||
for (const auto& file : sound_list) {
|
for (const auto& file : sound_list) {
|
||||||
if (getFileName(file) == name) {
|
if (getFileName(file) == name) {
|
||||||
@@ -329,7 +318,6 @@ auto Resource::loadSoundLazy(const std::string& name) -> JA_Sound_t* {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
|
auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading music lazily: %s", name.c_str());
|
|
||||||
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
auto music_list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
||||||
for (const auto& file : music_list) {
|
for (const auto& file : music_list) {
|
||||||
if (getFileName(file) == name) {
|
if (getFileName(file) == name) {
|
||||||
@@ -345,7 +333,6 @@ auto Resource::loadMusicLazy(const std::string& name) -> JA_Music_t* {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
|
auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Texture> {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading texture lazily: %s", name.c_str());
|
|
||||||
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
auto texture_list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||||
for (const auto& file : texture_list) {
|
for (const auto& file : texture_list) {
|
||||||
if (getFileName(file) == name) {
|
if (getFileName(file) == name) {
|
||||||
@@ -356,7 +343,6 @@ auto Resource::loadTextureLazy(const std::string& name) -> std::shared_ptr<Textu
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
|
auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text::File> {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text file lazily: %s", name.c_str());
|
|
||||||
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
auto text_file_list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||||
for (const auto& file : text_file_list) {
|
for (const auto& file : text_file_list) {
|
||||||
if (getFileName(file) == name) {
|
if (getFileName(file) == name) {
|
||||||
@@ -367,8 +353,6 @@ auto Resource::loadTextFileLazy(const std::string& name) -> std::shared_ptr<Text
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
|
auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading text object lazily: %s", name.c_str());
|
|
||||||
|
|
||||||
// Mapeo de objetos de texto a sus recursos
|
// Mapeo de objetos de texto a sus recursos
|
||||||
struct TextMapping {
|
struct TextMapping {
|
||||||
std::string key;
|
std::string key;
|
||||||
@@ -406,7 +390,6 @@ auto Resource::loadTextLazy(const std::string& name) -> std::shared_ptr<Text> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
|
auto Resource::loadAnimationLazy(const std::string& name) -> AnimationsFileBuffer {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Loading animation lazily: %s", name.c_str());
|
|
||||||
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
auto animation_list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
||||||
for (const auto& file : animation_list) {
|
for (const auto& file : animation_list) {
|
||||||
if (getFileName(file) == name) {
|
if (getFileName(file) == name) {
|
||||||
@@ -467,7 +450,6 @@ void Resource::reload() {
|
|||||||
|
|
||||||
// Carga los sonidos del juego
|
// Carga los sonidos del juego
|
||||||
void Resource::loadSounds() {
|
void Resource::loadSounds() {
|
||||||
Logger::info("SOUND FILES");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
|
auto list = Asset::get()->getListByType(Asset::Type::SOUND);
|
||||||
sounds_.clear();
|
sounds_.clear();
|
||||||
|
|
||||||
@@ -479,18 +461,17 @@ void Resource::loadSounds() {
|
|||||||
if (!audio_data.data.empty()) {
|
if (!audio_data.data.empty()) {
|
||||||
sound = JA_LoadSound(audio_data.data.data(), audio_data.data.size());
|
sound = JA_LoadSound(audio_data.data.data(), audio_data.data.size());
|
||||||
} else {
|
} else {
|
||||||
// Fallback a cargar desde disco si no está en pack
|
|
||||||
sound = JA_LoadSound(l.c_str());
|
sound = JA_LoadSound(l.c_str());
|
||||||
}
|
}
|
||||||
|
if (sound == nullptr) {
|
||||||
|
std::cout << "Sound load failed: " << name << '\n';
|
||||||
|
}
|
||||||
sounds_.emplace_back(name, sound);
|
sounds_.emplace_back(name, sound);
|
||||||
Logger::dots("Sound : ", name, "[ LOADED ]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga las músicas del juego
|
// Carga las músicas del juego
|
||||||
void Resource::loadMusics() {
|
void Resource::loadMusics() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("MUSIC FILES");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
auto list = Asset::get()->getListByType(Asset::Type::MUSIC);
|
||||||
musics_.clear();
|
musics_.clear();
|
||||||
|
|
||||||
@@ -502,18 +483,17 @@ void Resource::loadMusics() {
|
|||||||
if (!audio_data.data.empty()) {
|
if (!audio_data.data.empty()) {
|
||||||
music = JA_LoadMusic(audio_data.data.data(), audio_data.data.size());
|
music = JA_LoadMusic(audio_data.data.data(), audio_data.data.size());
|
||||||
} else {
|
} else {
|
||||||
// Fallback a cargar desde disco si no está en pack
|
|
||||||
music = JA_LoadMusic(l.c_str());
|
music = JA_LoadMusic(l.c_str());
|
||||||
}
|
}
|
||||||
|
if (music == nullptr) {
|
||||||
|
std::cout << "Music load failed: " << name << '\n';
|
||||||
|
}
|
||||||
musics_.emplace_back(name, music);
|
musics_.emplace_back(name, music);
|
||||||
Logger::dots("Music : ", name, "[ LOADED ]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga las texturas del juego
|
// Carga las texturas del juego
|
||||||
void Resource::loadTextures() {
|
void Resource::loadTextures() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("TEXTURES");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
auto list = Asset::get()->getListByType(Asset::Type::BITMAP);
|
||||||
textures_.clear();
|
textures_.clear();
|
||||||
|
|
||||||
@@ -526,8 +506,6 @@ void Resource::loadTextures() {
|
|||||||
|
|
||||||
// Carga los ficheros de texto del juego
|
// Carga los ficheros de texto del juego
|
||||||
void Resource::loadTextFiles() {
|
void Resource::loadTextFiles() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("TEXT FILES");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
auto list = Asset::get()->getListByType(Asset::Type::FONT);
|
||||||
text_files_.clear();
|
text_files_.clear();
|
||||||
|
|
||||||
@@ -540,8 +518,6 @@ void Resource::loadTextFiles() {
|
|||||||
|
|
||||||
// Carga las animaciones del juego
|
// Carga las animaciones del juego
|
||||||
void Resource::loadAnimations() {
|
void Resource::loadAnimations() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("ANIMATIONS");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
auto list = Asset::get()->getListByType(Asset::Type::ANIMATION);
|
||||||
animations_.clear();
|
animations_.clear();
|
||||||
|
|
||||||
@@ -554,8 +530,6 @@ void Resource::loadAnimations() {
|
|||||||
|
|
||||||
// Carga los datos para el modo demostración
|
// Carga los datos para el modo demostración
|
||||||
void Resource::loadDemoData() {
|
void Resource::loadDemoData() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("DEMO FILES");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
||||||
demos_.clear();
|
demos_.clear();
|
||||||
|
|
||||||
@@ -568,9 +542,6 @@ void Resource::loadDemoData() {
|
|||||||
|
|
||||||
// Crea las texturas de jugadores con todas sus variantes de paleta
|
// Crea las texturas de jugadores con todas sus variantes de paleta
|
||||||
void Resource::createPlayerTextures() {
|
void Resource::createPlayerTextures() {
|
||||||
Logger::cr();
|
|
||||||
Logger::info("CREATING PLAYER TEXTURES");
|
|
||||||
|
|
||||||
// Configuración de jugadores y sus paletas
|
// Configuración de jugadores y sus paletas
|
||||||
struct PlayerConfig {
|
struct PlayerConfig {
|
||||||
std::string base_texture;
|
std::string base_texture;
|
||||||
@@ -641,7 +612,6 @@ void Resource::createPlayerTextures() {
|
|||||||
// Guardar con nombre específico
|
// Guardar con nombre específico
|
||||||
std::string texture_name = player.name_prefix + "_pal" + std::to_string(palette_idx);
|
std::string texture_name = player.name_prefix + "_pal" + std::to_string(palette_idx);
|
||||||
textures_.emplace_back(texture_name, texture);
|
textures_.emplace_back(texture_name, texture);
|
||||||
Logger::dots("Player Texture : ", texture_name, "[ DONE ]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -657,9 +627,6 @@ void Resource::createTextTextures() {
|
|||||||
text(std::move(text_init)) {}
|
text(std::move(text_init)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger::cr();
|
|
||||||
Logger::info("CREATING TEXTURES");
|
|
||||||
|
|
||||||
// Texturas de tamaño normal con outline
|
// Texturas de tamaño normal con outline
|
||||||
std::vector<NameAndText> strings1 = {
|
std::vector<NameAndText> strings1 = {
|
||||||
{"game_text_1000_points", "1.000"},
|
{"game_text_1000_points", "1.000"},
|
||||||
@@ -673,7 +640,6 @@ void Resource::createTextTextures() {
|
|||||||
auto text1 = getText("04b_25_enhanced");
|
auto text1 = getText("04b_25_enhanced");
|
||||||
for (const auto& s : strings1) {
|
for (const auto& s : strings1) {
|
||||||
textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
textures_.emplace_back(s.name, text1->writeDXToTexture(Text::STROKE, s.text, -2, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||||
Logger::dots("Texture : ", s.name, "[ DONE ]");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Texturas de tamaño doble
|
// Texturas de tamaño doble
|
||||||
@@ -688,7 +654,6 @@ void Resource::createTextTextures() {
|
|||||||
auto text2 = getText("04b_25_2x_enhanced");
|
auto text2 = getText("04b_25_2x_enhanced");
|
||||||
for (const auto& s : strings2) {
|
for (const auto& s : strings2) {
|
||||||
textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
textures_.emplace_back(s.name, text2->writeDXToTexture(Text::STROKE, s.text, -4, Colors::NO_COLOR_MOD, 1, param.game.item_text_outline_color));
|
||||||
Logger::dots("Texture : ", s.name, "[ DONE ]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -707,9 +672,6 @@ void Resource::createText() {
|
|||||||
white_texture_file(std::move(w_file)) {}
|
white_texture_file(std::move(w_file)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger::cr();
|
|
||||||
Logger::info("CREATING TEXT OBJECTS");
|
|
||||||
|
|
||||||
std::vector<ResourceInfo> resources = {
|
std::vector<ResourceInfo> resources = {
|
||||||
{"04b_25", "04b_25.png", "04b_25.txt"},
|
{"04b_25", "04b_25.png", "04b_25.txt"},
|
||||||
{"04b_25_enhanced", "04b_25.png", "04b_25.txt", "04b_25_white.png"}, // Nueva fuente con textura blanca
|
{"04b_25_enhanced", "04b_25.png", "04b_25.txt", "04b_25_white.png"}, // Nueva fuente con textura blanca
|
||||||
@@ -735,7 +697,6 @@ void Resource::createText() {
|
|||||||
// Crear texto normal
|
// Crear texto normal
|
||||||
texts_.emplace_back(resource.key, std::make_shared<Text>(getTexture(resource.texture_file), getTextFile(resource.text_file)));
|
texts_.emplace_back(resource.key, std::make_shared<Text>(getTexture(resource.texture_file), getTextFile(resource.text_file)));
|
||||||
}
|
}
|
||||||
Logger::dots("Text : ", resource.key, "[ DONE ]");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -853,26 +814,8 @@ void Resource::renderProgress() {
|
|||||||
screen->coreRender();
|
screen->coreRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba los eventos durante la carga (permite salir con ESC o cerrar ventana)
|
|
||||||
void Resource::checkEvents() {
|
|
||||||
SDL_Event event;
|
|
||||||
while (SDL_PollEvent(&event)) {
|
|
||||||
switch (event.type) {
|
|
||||||
case SDL_EVENT_QUIT:
|
|
||||||
exit(0);
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_KEY_DOWN:
|
|
||||||
if (event.key.key == SDLK_ESCAPE) {
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Carga los datos para el modo demostración (sin mostrar progreso)
|
// Carga los datos para el modo demostración (sin mostrar progreso)
|
||||||
void Resource::loadDemoDataQuiet() {
|
void Resource::loadDemoDataQuiet() {
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\n>> DEMO FILES (quiet load)");
|
|
||||||
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
auto list = Asset::get()->getListByType(Asset::Type::DEMODATA);
|
||||||
demos_.clear();
|
demos_.clear();
|
||||||
|
|
||||||
@@ -892,13 +835,12 @@ void Resource::initProgressBar() {
|
|||||||
loading_full_rect_ = {.x = X_PADDING, .y = BAR_Y_POSITION, .w = FULL_BAR_WIDTH, .h = BAR_HEIGHT};
|
loading_full_rect_ = {.x = X_PADDING, .y = BAR_Y_POSITION, .w = FULL_BAR_WIDTH, .h = BAR_HEIGHT};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza el progreso de carga, muestra la barra y procesa eventos
|
// Actualiza el progreso de carga y muestra la barra
|
||||||
void Resource::updateLoadingProgress(std::string name) {
|
void Resource::updateLoadingProgress(std::string name) {
|
||||||
loading_resource_name_ = std::move(name);
|
loading_resource_name_ = std::move(name);
|
||||||
loading_count_.increase();
|
loading_count_.increase();
|
||||||
updateProgressBar();
|
updateProgressBar();
|
||||||
renderProgress();
|
renderProgress();
|
||||||
checkEvents();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza la barra de estado
|
// Actualiza la barra de estado
|
||||||
|
|||||||
@@ -174,7 +174,6 @@ class Resource {
|
|||||||
// --- Métodos internos para gestionar el progreso ---
|
// --- Métodos internos para gestionar el progreso ---
|
||||||
void calculateTotalResources(); // Calcula el número de recursos para cargar
|
void calculateTotalResources(); // Calcula el número de recursos para cargar
|
||||||
void renderProgress(); // Muestra el progreso de carga
|
void renderProgress(); // Muestra el progreso de carga
|
||||||
static void checkEvents(); // Comprueba los eventos durante la carga
|
|
||||||
void updateLoadingProgress(std::string name); // Actualiza el progreso de carga
|
void updateLoadingProgress(std::string name); // Actualiza el progreso de carga
|
||||||
void initProgressBar(); // Inicializa los rectangulos que definen la barra de progreso
|
void initProgressBar(); // Inicializa los rectangulos que definen la barra de progreso
|
||||||
void updateProgressBar(); // Actualiza la barra de estado
|
void updateProgressBar(); // Actualiza la barra de estado
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallba
|
|||||||
if (std::filesystem::exists(pack_file)) {
|
if (std::filesystem::exists(pack_file)) {
|
||||||
resource_pack_ = new ResourcePack();
|
resource_pack_ = new ResourcePack();
|
||||||
if (resource_pack_->loadPack(pack_file)) {
|
if (resource_pack_->loadPack(pack_file)) {
|
||||||
std::cout << "Resource pack loaded successfully: " << pack_file << '\n';
|
|
||||||
std::cout << "Resources available: " << resource_pack_->getResourceCount() << '\n';
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
delete resource_pack_;
|
delete resource_pack_;
|
||||||
@@ -43,7 +41,6 @@ auto ResourceLoader::initialize(const std::string& pack_file, bool enable_fallba
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fallback_to_files_) {
|
if (fallback_to_files_) {
|
||||||
std::cout << "Using fallback mode: loading resources from data/ directory" << '\n';
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <algorithm> // Para min, max
|
#include <algorithm> // Para min, max
|
||||||
#include <cstring> // Para memcpy
|
#include <cstring> // Para memcpy
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <memory> // Para allocator, shared_ptr, unique_ptr, __shared_ptr_access, make_shared, make_unique
|
#include <memory> // Para allocator, shared_ptr, unique_ptr, __shared_ptr_access, make_shared, make_unique
|
||||||
#include <string> // Para basic_string, operator+, char_traits, to_string, string
|
#include <string> // Para basic_string, operator+, char_traits, to_string, string
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
@@ -17,7 +18,6 @@
|
|||||||
#include "resource.hpp" // Para Resource
|
#include "resource.hpp" // Para Resource
|
||||||
#include "text.hpp" // Para Text
|
#include "text.hpp" // Para Text
|
||||||
#include "texture.hpp" // Para Texture
|
#include "texture.hpp" // Para Texture
|
||||||
#include "ui/logger.hpp" // Para info
|
|
||||||
#include "ui/notifier.hpp" // Para Notifier
|
#include "ui/notifier.hpp" // Para Notifier
|
||||||
#include "ui/service_menu.hpp" // Para ServiceMenu
|
#include "ui/service_menu.hpp" // Para ServiceMenu
|
||||||
#include "utils.hpp" // Para toLower
|
#include "utils.hpp" // Para toLower
|
||||||
@@ -290,7 +290,7 @@ void Screen::initShaders() {
|
|||||||
#ifndef NO_SHADERS
|
#ifndef NO_SHADERS
|
||||||
auto* self = Screen::get();
|
auto* self = Screen::get();
|
||||||
if (self == nullptr) {
|
if (self == nullptr) {
|
||||||
SDL_Log("Screen::initShaders: instance is null, skipping");
|
std::cout << "Screen::initShaders: instance is null, skipping" << '\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!self->shader_backend_) {
|
if (!self->shader_backend_) {
|
||||||
@@ -301,7 +301,7 @@ void Screen::initShaders() {
|
|||||||
}
|
}
|
||||||
if (!self->shader_backend_->isHardwareAccelerated()) {
|
if (!self->shader_backend_->isHardwareAccelerated()) {
|
||||||
const bool ok = self->shader_backend_->init(self->window_, self->game_canvas_, "", "");
|
const bool ok = self->shader_backend_->init(self->window_, self->game_canvas_, "", "");
|
||||||
SDL_Log("Screen::initShaders: SDL3GPUShader::init() = %s", ok ? "OK" : "FAILED");
|
std::cout << "Screen::initShaders: SDL3GPUShader::init() = " << (ok ? "OK" : "FAILED") << '\n';
|
||||||
}
|
}
|
||||||
if (self->shader_backend_ && self->shader_backend_->isHardwareAccelerated()) {
|
if (self->shader_backend_ && self->shader_backend_->isHardwareAccelerated()) {
|
||||||
self->shader_backend_->setLinearUpscale(Options::video.supersampling.linear_upscale);
|
self->shader_backend_->setLinearUpscale(Options::video.supersampling.linear_upscale);
|
||||||
@@ -371,9 +371,7 @@ void Screen::renderAttenuate() {
|
|||||||
auto Screen::initSDLVideo() -> bool {
|
auto Screen::initSDLVideo() -> bool {
|
||||||
// Inicializar SDL
|
// Inicializar SDL
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "FATAL: Failed to initialize SDL_VIDEO! SDL Error: " << SDL_GetError() << '\n';
|
||||||
"FATAL: Failed to initialize SDL_VIDEO! SDL Error: %s",
|
|
||||||
SDL_GetError());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -383,8 +381,7 @@ auto Screen::initSDLVideo() -> bool {
|
|||||||
// Configurar hint para renderizado
|
// Configurar hint para renderizado
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")) {
|
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "Warning: Failed to set Metal hint!" << '\n';
|
||||||
"Warning: Failed to set Metal hint!");
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -405,9 +402,7 @@ auto Screen::initSDLVideo() -> bool {
|
|||||||
window_flags);
|
window_flags);
|
||||||
|
|
||||||
if (window_ == nullptr) {
|
if (window_ == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "FATAL: Failed to create window! SDL Error: " << SDL_GetError() << '\n';
|
||||||
"FATAL: Failed to create window! SDL Error: %s",
|
|
||||||
SDL_GetError());
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -415,9 +410,7 @@ auto Screen::initSDLVideo() -> bool {
|
|||||||
// Crear renderer
|
// Crear renderer
|
||||||
renderer_ = SDL_CreateRenderer(window_, nullptr);
|
renderer_ = SDL_CreateRenderer(window_, nullptr);
|
||||||
if (renderer_ == nullptr) {
|
if (renderer_ == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
std::cout << "FATAL: Failed to create renderer! SDL Error: " << SDL_GetError() << '\n';
|
||||||
"FATAL: Failed to create renderer! SDL Error: %s",
|
|
||||||
SDL_GetError());
|
|
||||||
SDL_DestroyWindow(window_);
|
SDL_DestroyWindow(window_);
|
||||||
window_ = nullptr;
|
window_ = nullptr;
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
@@ -429,24 +422,14 @@ auto Screen::initSDLVideo() -> bool {
|
|||||||
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
|
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
|
||||||
SDL_SetRenderVSync(renderer_, Options::video.vsync ? 1 : SDL_RENDERER_VSYNC_DISABLED);
|
SDL_SetRenderVSync(renderer_, Options::video.vsync ? 1 : SDL_RENDERER_VSYNC_DISABLED);
|
||||||
|
|
||||||
Logger::info("Video system initialized successfully");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtiene información sobre la pantalla
|
// Obtiene información sobre la pantalla
|
||||||
void Screen::getDisplayInfo() {
|
void Screen::getDisplayInfo() {
|
||||||
int i;
|
|
||||||
int num_displays = 0;
|
int num_displays = 0;
|
||||||
SDL_DisplayID* displays = SDL_GetDisplays(&num_displays);
|
SDL_DisplayID* displays = SDL_GetDisplays(&num_displays);
|
||||||
if (displays != nullptr) {
|
if (displays != nullptr) {
|
||||||
for (i = 0; i < num_displays; ++i) {
|
|
||||||
SDL_DisplayID instance_id = displays[i];
|
|
||||||
const char* name = SDL_GetDisplayName(instance_id);
|
|
||||||
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Display %" SDL_PRIu32 ": %s", instance_id, (name != nullptr) ? name : "Unknown");
|
|
||||||
Logger::info(std::string("Display ") + std::to_string(instance_id) + ": " + (name != nullptr ? name : "Unknown"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto* dm = SDL_GetCurrentDisplayMode(displays[0]);
|
const auto* dm = SDL_GetCurrentDisplayMode(displays[0]);
|
||||||
|
|
||||||
// Guarda información del monitor en display_monitor_
|
// Guarda información del monitor en display_monitor_
|
||||||
@@ -465,13 +448,6 @@ void Screen::getDisplayInfo() {
|
|||||||
std::to_string(dm->h) + " @ " +
|
std::to_string(dm->h) + " @ " +
|
||||||
std::to_string(static_cast<int>(dm->refresh_rate)) + " Hz";
|
std::to_string(static_cast<int>(dm->refresh_rate)) + " Hz";
|
||||||
|
|
||||||
// Muestra información sobre el tamaño de la pantalla y de la ventana de juego
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Current display mode: %dx%d @ %dHz", static_cast<int>(dm->w), static_cast<int>(dm->h), static_cast<int>(dm->refresh_rate));
|
|
||||||
Logger::info("Current display mode: " + Options::video.info);
|
|
||||||
|
|
||||||
// SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Window resolution: %dx%d x%d", static_cast<int>(param.game.width), static_cast<int>(param.game.height), Options::window.zoom);
|
|
||||||
Logger::info("Window resolution: " + std::to_string(static_cast<int>(param.game.width)) + "x" + std::to_string(static_cast<int>(param.game.height)) + "x" + std::to_string(Options::window.zoom));
|
|
||||||
|
|
||||||
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
|
// Calcula el máximo factor de zoom que se puede aplicar a la pantalla
|
||||||
const int MAX_ZOOM = std::min(dm->w / param.game.width, (dm->h - WINDOWS_DECORATIONS) / param.game.height);
|
const int MAX_ZOOM = std::min(dm->w / param.game.width, (dm->h - WINDOWS_DECORATIONS) / param.game.height);
|
||||||
|
|
||||||
@@ -563,11 +539,7 @@ void Screen::applyCurrentPostFXPreset() {
|
|||||||
p.curvature = preset.curvature;
|
p.curvature = preset.curvature;
|
||||||
p.bleeding = preset.bleeding;
|
p.bleeding = preset.bleeding;
|
||||||
p.flicker = preset.flicker;
|
p.flicker = preset.flicker;
|
||||||
SDL_Log("Screen::applyCurrentPostFXPreset: preset='%s' scan=%.2f vign=%.2f chroma=%.2f",
|
std::cout << "Screen::applyCurrentPostFXPreset: preset='" << preset.name << "' scan=" << p.scanlines << " vign=" << p.vignette << " chroma=" << p.chroma << '\n';
|
||||||
preset.name.c_str(),
|
|
||||||
p.scanlines,
|
|
||||||
p.vignette,
|
|
||||||
p.chroma);
|
|
||||||
}
|
}
|
||||||
shader_backend_->setPostFXParams(p);
|
shader_backend_->setPostFXParams(p);
|
||||||
}
|
}
|
||||||
@@ -594,7 +566,7 @@ void Screen::applyCurrentCrtPiPreset() {
|
|||||||
.enable_sharper = preset.enable_sharper,
|
.enable_sharper = preset.enable_sharper,
|
||||||
};
|
};
|
||||||
shader_backend_->setCrtPiParams(p);
|
shader_backend_->setCrtPiParams(p);
|
||||||
SDL_Log("Screen::applyCurrentCrtPiPreset: preset='%s'", preset.name.c_str());
|
std::cout << "Screen::applyCurrentCrtPiPreset: preset='" << preset.name << "'" << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ Credits::Credits()
|
|||||||
}
|
}
|
||||||
initVars();
|
initVars();
|
||||||
startCredits();
|
startCredits();
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time para el primer frame del callback
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -69,7 +72,20 @@ auto Credits::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME;
|
return DELTA_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle principal
|
// Avanza un frame (llamado desde Director::iterate)
|
||||||
|
void Credits::iterate() {
|
||||||
|
checkInput();
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void Credits::handleEvent(const SDL_Event& /*event*/) {
|
||||||
|
// Eventos globales ya gestionados por Director::handleEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle principal legacy (fallback)
|
||||||
void Credits::run() {
|
void Credits::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,11 @@ class Credits {
|
|||||||
Credits();
|
Credits();
|
||||||
~Credits();
|
~Credits();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -131,6 +131,9 @@ Game::Game(Player::Id player_id, int current_stage, bool demo_enabled)
|
|||||||
#ifdef RECORDING
|
#ifdef RECORDING
|
||||||
setState(State::PLAYING);
|
setState(State::PLAYING);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time para el primer frame del callback
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::~Game() {
|
Game::~Game() {
|
||||||
@@ -978,7 +981,33 @@ auto Game::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME_MS / 1000.0F; // Convertir de milisegundos a segundos
|
return DELTA_TIME_MS / 1000.0F; // Convertir de milisegundos a segundos
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle para el juego
|
// Avanza un frame del juego (llamado desde Director::iterate)
|
||||||
|
void Game::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento del juego (llamado desde Director::handleEvent)
|
||||||
|
void Game::handleEvent(const SDL_Event& event) {
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
||||||
|
pause_manager_->setFocusLossPause(!demo_.enabled);
|
||||||
|
break;
|
||||||
|
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
||||||
|
pause_manager_->setFocusLossPause(false);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
handleDebugEvents(event);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle para el juego (fallback legacy)
|
||||||
void Game::run() {
|
void Game::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,11 @@ class Game {
|
|||||||
Game(Player::Id player_id, int current_stage, bool demo_enabled); // Constructor principal
|
Game(Player::Id player_id, int current_stage, bool demo_enabled); // Constructor principal
|
||||||
~Game(); // Destructor
|
~Game(); // Destructor
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run(); // Ejecuta el bucle principal del juego
|
void run(); // Ejecuta el bucle principal del juego
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -45,6 +45,10 @@ HiScoreTable::HiScoreTable()
|
|||||||
initBackground();
|
initBackground();
|
||||||
iniEntryColors();
|
iniEntryColors();
|
||||||
createSprites();
|
createSprites();
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time y arranca la música
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
|
Audio::get()->playMusic("title.ogg");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -126,7 +130,20 @@ auto HiScoreTable::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME;
|
return DELTA_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle para la pantalla de instrucciones
|
// Avanza un frame (llamado desde Director::iterate)
|
||||||
|
void HiScoreTable::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void HiScoreTable::handleEvent(const SDL_Event& /*event*/) {
|
||||||
|
// Eventos globales ya gestionados por Director::handleEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle para la pantalla de puntuaciones (fallback legacy)
|
||||||
void HiScoreTable::run() {
|
void HiScoreTable::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
Audio::get()->playMusic("title.ogg");
|
Audio::get()->playMusic("title.ogg");
|
||||||
|
|||||||
@@ -27,7 +27,11 @@ class HiScoreTable {
|
|||||||
HiScoreTable();
|
HiScoreTable();
|
||||||
~HiScoreTable();
|
~HiScoreTable();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ Instructions::Instructions()
|
|||||||
|
|
||||||
// Inicializa los sprites de los items
|
// Inicializa los sprites de los items
|
||||||
iniSprites();
|
iniSprites();
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time y arranca la música
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
|
Audio::get()->playMusic("title.ogg");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -262,7 +266,20 @@ auto Instructions::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME;
|
return DELTA_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle para la pantalla de instrucciones
|
// Avanza un frame (llamado desde Director::iterate)
|
||||||
|
void Instructions::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void Instructions::handleEvent(const SDL_Event& /*event*/) {
|
||||||
|
// Eventos globales ya gestionados por Director::handleEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle para la pantalla de instrucciones (fallback legacy)
|
||||||
void Instructions::run() {
|
void Instructions::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
Audio::get()->playMusic("title.ogg");
|
Audio::get()->playMusic("title.ogg");
|
||||||
|
|||||||
@@ -48,7 +48,11 @@ class Instructions {
|
|||||||
Instructions();
|
Instructions();
|
||||||
~Instructions();
|
~Instructions();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ Intro::Intro()
|
|||||||
// Configura el fondo
|
// Configura el fondo
|
||||||
tiled_bg_->setSpeed(TILED_BG_SPEED);
|
tiled_bg_->setSpeed(TILED_BG_SPEED);
|
||||||
tiled_bg_->setColor(bg_color_);
|
tiled_bg_->setColor(bg_color_);
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time y arranca la música
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
|
Audio::get()->playMusic("intro.ogg", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba los eventos
|
// Comprueba los eventos
|
||||||
@@ -265,7 +269,20 @@ auto Intro::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME;
|
return DELTA_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle principal
|
// Avanza un frame (llamado desde Director::iterate)
|
||||||
|
void Intro::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void Intro::handleEvent(const SDL_Event& /*event*/) {
|
||||||
|
// Eventos globales ya gestionados por Director::handleEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle principal legacy (fallback)
|
||||||
void Intro::run() {
|
void Intro::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
Audio::get()->playMusic("intro.ogg", 0);
|
Audio::get()->playMusic("intro.ogg", 0);
|
||||||
|
|||||||
@@ -30,7 +30,11 @@ class Intro {
|
|||||||
Intro();
|
Intro();
|
||||||
~Intro() = default;
|
~Intro() = default;
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ Logo::Logo()
|
|||||||
jail_sprite_.push_back(std::move(temp));
|
jail_sprite_.push_back(std::move(temp));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time para el primer frame del callback
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
|
|
||||||
// Inicializa el vector de colores con la paleta ZX Spectrum
|
// Inicializa el vector de colores con la paleta ZX Spectrum
|
||||||
color_.emplace_back(SPECTRUM_BLACK);
|
color_.emplace_back(SPECTRUM_BLACK);
|
||||||
color_.emplace_back(SPECTRUM_BLUE);
|
color_.emplace_back(SPECTRUM_BLUE);
|
||||||
@@ -169,7 +172,20 @@ auto Logo::calculateDeltaTime() -> float {
|
|||||||
return DELTA_TIME;
|
return DELTA_TIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle para el logo del juego
|
// Avanza un frame del logo (llamado desde Director::iterate)
|
||||||
|
void Logo::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void Logo::handleEvent(const SDL_Event& /*event*/) {
|
||||||
|
// Eventos globales (QUIT, resize, hotplug) ya gestionados por Director::handleEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle para el logo del juego (fallback legacy)
|
||||||
void Logo::run() {
|
void Logo::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ class Logo {
|
|||||||
Logo();
|
Logo();
|
||||||
~Logo();
|
~Logo();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -59,6 +59,9 @@ Title::Title()
|
|||||||
anchor_.mini_logo = (param.game.height / MINI_LOGO_Y_DIVISOR * MINI_LOGO_Y_FACTOR) + BLOCK;
|
anchor_.mini_logo = (param.game.height / MINI_LOGO_Y_DIVISOR * MINI_LOGO_Y_FACTOR) + BLOCK;
|
||||||
mini_logo_sprite_->setY(anchor_.mini_logo);
|
mini_logo_sprite_->setY(anchor_.mini_logo);
|
||||||
anchor_.copyright_text = anchor_.mini_logo + mini_logo_sprite_->getHeight() + COPYRIGHT_TEXT_SPACING;
|
anchor_.copyright_text = anchor_.mini_logo + mini_logo_sprite_->getHeight() + COPYRIGHT_TEXT_SPACING;
|
||||||
|
|
||||||
|
// Inicializa el timer de delta time para el primer frame del callback
|
||||||
|
last_time_ = SDL_GetTicks();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -213,7 +216,22 @@ void Title::activatePlayerAndSetState(Player::Id player_id) {
|
|||||||
counter_time_ = 0.0F;
|
counter_time_ = 0.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bucle para el titulo del juego
|
// Avanza un frame (llamado desde Director::iterate)
|
||||||
|
void Title::iterate() {
|
||||||
|
const float DELTA_TIME = calculateDeltaTime();
|
||||||
|
checkInput();
|
||||||
|
update(DELTA_TIME);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Procesa un evento (llamado desde Director::handleEvent)
|
||||||
|
void Title::handleEvent(const SDL_Event& event) {
|
||||||
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||||
|
handleKeyDownEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bucle para el titulo del juego (fallback legacy)
|
||||||
void Title::run() {
|
void Title::run() {
|
||||||
last_time_ = SDL_GetTicks();
|
last_time_ = SDL_GetTicks();
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,11 @@ class Title {
|
|||||||
Title();
|
Title();
|
||||||
~Title();
|
~Title();
|
||||||
|
|
||||||
// --- Bucle principal ---
|
// --- Callbacks para el bucle SDL_MAIN_USE_CALLBACKS ---
|
||||||
|
void iterate(); // Ejecuta un frame
|
||||||
|
void handleEvent(const SDL_Event& event); // Procesa un evento
|
||||||
|
|
||||||
|
// --- Bucle principal legacy (fallback) ---
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
#include "screen.hpp" // Para Screen
|
#include "screen.hpp" // Para Screen
|
||||||
#include "sprite.hpp" // Para Sprite
|
#include "sprite.hpp" // Para Sprite
|
||||||
#include "texture.hpp" // Para Texture
|
#include "texture.hpp" // Para Texture
|
||||||
#include "ui/logger.hpp" // Para dots
|
|
||||||
#include "utils.hpp" // Para getFileName
|
#include "utils.hpp" // Para getFileName
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -432,7 +431,6 @@ auto Text::loadFile(const std::string& file_path) -> std::shared_ptr<Text::File>
|
|||||||
line_read++;
|
line_read++;
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger::dots("Text File : ", getFileName(file_path), "[ LOADED ]");
|
|
||||||
// Cierra el fichero si se usó
|
// Cierra el fichero si se usó
|
||||||
if (!using_resource_data && file.is_open()) {
|
if (!using_resource_data && file.is_open()) {
|
||||||
file.close();
|
file.close();
|
||||||
|
|||||||
@@ -6,17 +6,17 @@
|
|||||||
#include <cstdint> // Para uint32_t
|
#include <cstdint> // Para uint32_t
|
||||||
#include <cstring> // Para memcpy
|
#include <cstring> // Para memcpy
|
||||||
#include <fstream> // Para basic_ifstream, basic_istream, basic_ios
|
#include <fstream> // Para basic_ifstream, basic_istream, basic_ios
|
||||||
|
#include <iostream> // Para std::cout
|
||||||
#include <sstream> // Para basic_istringstream
|
#include <sstream> // Para basic_istringstream
|
||||||
#include <stdexcept> // Para runtime_error
|
#include <stdexcept> // Para runtime_error
|
||||||
#include <string> // Para basic_string, char_traits, operator+, string
|
#include <string> // Para basic_string, char_traits, operator+, string
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
|
|
||||||
#include "color.hpp" // Para getFileName, Color, printWithDots
|
#include "color.hpp" // Para getFileName, Color
|
||||||
#include "external/gif.hpp" // Para Gif
|
#include "external/gif.hpp" // Para Gif
|
||||||
#include "resource_helper.hpp" // Para ResourceHelper
|
#include "resource_helper.hpp" // Para ResourceHelper
|
||||||
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_alpha
|
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_alpha
|
||||||
#include "ui/logger.hpp"
|
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -39,7 +39,7 @@ Texture::Texture(SDL_Renderer* renderer, std::string path)
|
|||||||
surface_ = loadSurface(path_);
|
surface_ = loadSurface(path_);
|
||||||
|
|
||||||
// Añade la propia paleta del fichero a la lista
|
// Añade la propia paleta del fichero a la lista
|
||||||
addPaletteFromGifFile(path_, true); // Usar modo silencioso
|
addPaletteFromGifFile(path_);
|
||||||
|
|
||||||
// Crea la textura, establece el BlendMode y copia la surface a la textura
|
// Crea la textura, establece el BlendMode y copia la surface a la textura
|
||||||
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
|
createBlank(width_, height_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
|
||||||
@@ -80,10 +80,9 @@ auto Texture::loadFromFile(const std::string& file_path) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data == nullptr) {
|
if (data == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", getFileName(file_path).c_str());
|
std::cout << "Error: Fichero no encontrado " << getFileName(file_path) << '\n';
|
||||||
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
|
||||||
}
|
}
|
||||||
Logger::dots("Texture : ", getFileName(file_path), "[ LOADED ]");
|
|
||||||
|
|
||||||
int pitch;
|
int pitch;
|
||||||
SDL_PixelFormat pixel_format;
|
SDL_PixelFormat pixel_format;
|
||||||
@@ -99,12 +98,12 @@ auto Texture::loadFromFile(const std::string& file_path) -> bool {
|
|||||||
// Carga la imagen desde una ruta específica
|
// Carga la imagen desde una ruta específica
|
||||||
auto* loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void*>(data), pitch);
|
auto* loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, static_cast<void*>(data), pitch);
|
||||||
if (loaded_surface == nullptr) {
|
if (loaded_surface == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load image %s", file_path.c_str());
|
std::cout << "Unable to load image " << file_path << '\n';
|
||||||
} else {
|
} else {
|
||||||
// Crea la textura desde los pixels de la surface
|
// Crea la textura desde los pixels de la surface
|
||||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||||
if (new_texture == nullptr) {
|
if (new_texture == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create texture from %s! SDL Error: %s", file_path.c_str(), SDL_GetError());
|
std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << '\n';
|
||||||
} else {
|
} else {
|
||||||
// Obtiene las dimensiones de la imagen
|
// Obtiene las dimensiones de la imagen
|
||||||
width_ = loaded_surface->w;
|
width_ = loaded_surface->w;
|
||||||
@@ -126,7 +125,7 @@ auto Texture::createBlank(int width, int height, SDL_PixelFormat format, SDL_Tex
|
|||||||
// Crea una textura sin inicializar
|
// Crea una textura sin inicializar
|
||||||
texture_ = SDL_CreateTexture(renderer_, format, access, width, height);
|
texture_ = SDL_CreateTexture(renderer_, format, access, width, height);
|
||||||
if (texture_ == nullptr) {
|
if (texture_ == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create blank texture! SDL Error: %s", SDL_GetError());
|
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << '\n';
|
||||||
} else {
|
} else {
|
||||||
width_ = width;
|
width_ = width;
|
||||||
height_ = height;
|
height_ = height;
|
||||||
@@ -236,7 +235,7 @@ auto Texture::loadSurface(const std::string& file_path) -> std::shared_ptr<Surfa
|
|||||||
// Fallback a filesystem directo
|
// Fallback a filesystem directo
|
||||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
std::cout << "Error: Fichero no encontrado " << file_path << '\n';
|
||||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +246,7 @@ auto Texture::loadSurface(const std::string& file_path) -> std::shared_ptr<Surfa
|
|||||||
// Leer el contenido del archivo en un buffer
|
// Leer el contenido del archivo en un buffer
|
||||||
buffer.resize(size);
|
buffer.resize(size);
|
||||||
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al leer el fichero %s", file_path.c_str());
|
std::cout << "Error al leer el fichero " << file_path << '\n';
|
||||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,7 +257,7 @@ auto Texture::loadSurface(const std::string& file_path) -> std::shared_ptr<Surfa
|
|||||||
Uint16 h = 0;
|
Uint16 h = 0;
|
||||||
std::vector<Uint8> raw_pixels = gif.loadGif(buffer.data(), w, h);
|
std::vector<Uint8> raw_pixels = gif.loadGif(buffer.data(), w, h);
|
||||||
if (raw_pixels.empty()) {
|
if (raw_pixels.empty()) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo cargar el GIF %s", file_path.c_str());
|
std::cout << "Error: No se pudo cargar el GIF " << file_path << '\n';
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +272,6 @@ auto Texture::loadSurface(const std::string& file_path) -> std::shared_ptr<Surfa
|
|||||||
width_ = w;
|
width_ = w;
|
||||||
height_ = h;
|
height_ = h;
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "GIF %s cargado correctamente.", file_path.c_str());
|
|
||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +300,7 @@ void Texture::setPaletteColor(int palette, int index, Uint32 color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Carga una paleta desde un fichero
|
// Carga una paleta desde un fichero
|
||||||
auto Texture::loadPaletteFromFile(const std::string& file_path, bool quiet) -> Palette {
|
auto Texture::loadPaletteFromFile(const std::string& file_path) -> Palette {
|
||||||
Palette palette;
|
Palette palette;
|
||||||
|
|
||||||
std::vector<Uint8> buffer;
|
std::vector<Uint8> buffer;
|
||||||
@@ -315,7 +313,7 @@ auto Texture::loadPaletteFromFile(const std::string& file_path, bool quiet) -> P
|
|||||||
// Fallback a filesystem directo
|
// Fallback a filesystem directo
|
||||||
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
std::cout << "Error: Fichero no encontrado " << file_path << '\n';
|
||||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,20 +323,16 @@ auto Texture::loadPaletteFromFile(const std::string& file_path, bool quiet) -> P
|
|||||||
|
|
||||||
buffer.resize(size);
|
buffer.resize(size);
|
||||||
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: No se pudo leer completamente el fichero %s", file_path.c_str());
|
std::cout << "Error: No se pudo leer completamente el fichero " << file_path << '\n';
|
||||||
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
throw std::runtime_error("Error al leer el fichero: " + file_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!quiet) {
|
|
||||||
Logger::dots("Palette : ", getFileName(file_path), "[ LOADED ]");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
|
// Usar la nueva función loadPalette, que devuelve un vector<uint32_t>
|
||||||
GIF::Gif gif;
|
GIF::Gif gif;
|
||||||
std::vector<uint32_t> pal = gif.loadPalette(buffer.data());
|
std::vector<uint32_t> pal = gif.loadPalette(buffer.data());
|
||||||
if (pal.empty()) {
|
if (pal.empty()) {
|
||||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Advertencia: No se encontró paleta en el archivo %s", file_path.c_str());
|
std::cout << "Advertencia: No se encontró paleta en el archivo " << file_path << '\n';
|
||||||
return palette; // Devuelve un vector vacío si no hay paleta
|
return palette; // Devuelve un vector vacío si no hay paleta
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,19 +341,18 @@ auto Texture::loadPaletteFromFile(const std::string& file_path, bool quiet) -> P
|
|||||||
palette[i] = (pal[i] << 8) | 0xFF; // Resultado: 0xRRGGBBAA
|
palette[i] = (pal[i] << 8) | 0xFF; // Resultado: 0xRRGGBBAA
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LogInfo(SDL_LOG_CATEGORY_TEST, "Paleta cargada correctamente desde %s", file_path.c_str());
|
|
||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade una paleta a la lista
|
// Añade una paleta a la lista
|
||||||
void Texture::addPaletteFromGifFile(const std::string& path, bool quiet) {
|
void Texture::addPaletteFromGifFile(const std::string& path) {
|
||||||
palettes_.emplace_back(loadPaletteFromFile(path, quiet));
|
palettes_.emplace_back(loadPaletteFromFile(path));
|
||||||
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
|
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade una paleta a la lista
|
// Añade una paleta a la lista
|
||||||
void Texture::addPaletteFromPalFile(const std::string& path) {
|
void Texture::addPaletteFromPalFile(const std::string& path) {
|
||||||
palettes_.emplace_back(readPalFile(path, true)); // Usar modo silencioso
|
palettes_.emplace_back(readPalFile(path));
|
||||||
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
|
setPaletteColor(palettes_.size() - 1, 0, 0x00000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,7 +368,7 @@ void Texture::setPalette(size_t palette) {
|
|||||||
auto Texture::getRenderer() -> SDL_Renderer* { return renderer_; }
|
auto Texture::getRenderer() -> SDL_Renderer* { return renderer_; }
|
||||||
|
|
||||||
// Carga una paleta desde un archivo .pal
|
// Carga una paleta desde un archivo .pal
|
||||||
auto Texture::readPalFile(const std::string& file_path, bool quiet) -> Palette {
|
auto Texture::readPalFile(const std::string& file_path) -> Palette {
|
||||||
Palette palette{};
|
Palette palette{};
|
||||||
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)
|
palette.fill(0); // Inicializar todo con 0 (transparente por defecto)
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class Texture {
|
|||||||
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
|
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
|
||||||
|
|
||||||
// --- Paletas ---
|
// --- Paletas ---
|
||||||
void addPaletteFromGifFile(const std::string& path, bool quiet = false); // Añade una paleta a la lista
|
void addPaletteFromGifFile(const std::string& path); // Añade una paleta a la lista
|
||||||
void addPaletteFromPalFile(const std::string& path); // Añade una paleta a la lista
|
void addPaletteFromPalFile(const std::string& path); // Añade una paleta a la lista
|
||||||
void setPaletteColor(int palette, int index, Uint32 color); // Establece un color de la paleta
|
void setPaletteColor(int palette, int index, Uint32 color); // Establece un color de la paleta
|
||||||
void setPalette(size_t palette); // Cambia la paleta de la textura
|
void setPalette(size_t palette); // Cambia la paleta de la textura
|
||||||
@@ -76,8 +76,8 @@ class Texture {
|
|||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
auto loadSurface(const std::string& file_path) -> std::shared_ptr<Surface>; // Crea una surface desde un fichero .gif
|
auto loadSurface(const std::string& file_path) -> std::shared_ptr<Surface>; // Crea una surface desde un fichero .gif
|
||||||
void flipSurface(); // Vuelca la surface en la textura
|
void flipSurface(); // Vuelca la surface en la textura
|
||||||
static auto loadPaletteFromFile(const std::string& file_path, bool quiet = false) -> Palette; // Carga una paleta desde un fichero
|
static auto loadPaletteFromFile(const std::string& file_path) -> Palette; // Carga una paleta desde un fichero
|
||||||
void unloadTexture(); // Libera la memoria de la textura
|
void unloadTexture(); // Libera la memoria de la textura
|
||||||
void unloadSurface(); // Libera la surface actual
|
void unloadSurface(); // Libera la surface actual
|
||||||
static auto readPalFile(const std::string& file_path, bool quiet = false) -> Palette; // Carga una paleta desde un archivo .pal
|
static auto readPalFile(const std::string& file_path) -> Palette; // Carga una paleta desde un archivo .pal
|
||||||
};
|
};
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace Logger {
|
|
||||||
|
|
||||||
// Colores ANSI
|
|
||||||
inline constexpr const char* RESET = "\033[0m";
|
|
||||||
inline constexpr const char* RED = "\033[31m";
|
|
||||||
inline constexpr const char* GREEN = "\033[32m";
|
|
||||||
inline constexpr const char* YELLOW = "\033[33m";
|
|
||||||
inline constexpr const char* BLUE = "\033[34m";
|
|
||||||
inline constexpr const char* MAGENTA = "\033[35m";
|
|
||||||
inline constexpr const char* CYAN = "\033[36m";
|
|
||||||
inline constexpr const char* WHITE = "\033[37m";
|
|
||||||
|
|
||||||
// Ancho total global para alineación
|
|
||||||
inline constexpr size_t TOTAL_WIDTH = 52;
|
|
||||||
|
|
||||||
// Sección
|
|
||||||
inline void section(const std::string& title, const std::string& color = CYAN) {
|
|
||||||
std::cout << "\n"
|
|
||||||
<< color
|
|
||||||
<< "========================================\n"
|
|
||||||
<< " " << title << "\n"
|
|
||||||
<< "========================================"
|
|
||||||
<< RESET << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info
|
|
||||||
inline void info(const std::string& msg, const std::string& color = WHITE) {
|
|
||||||
std::cout << " " << color << msg << RESET << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put
|
|
||||||
inline void put(const std::string& msg, const std::string& color = WHITE) {
|
|
||||||
std::cout << color << msg << RESET << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error
|
|
||||||
inline void error(const std::string& msg) {
|
|
||||||
std::cout << RED << msg << RESET << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// CR
|
|
||||||
inline void cr() {
|
|
||||||
std::cout << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dots genérico
|
|
||||||
inline void dots(const std::string& prefix,
|
|
||||||
const std::string& middle,
|
|
||||||
const std::string& suffix,
|
|
||||||
const std::string& suffix_color = GREEN) {
|
|
||||||
size_t field_width = TOTAL_WIDTH > (prefix.size() + suffix.size())
|
|
||||||
? TOTAL_WIDTH - prefix.size() - suffix.size()
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
std::string field_text;
|
|
||||||
if (middle.size() < field_width) {
|
|
||||||
field_text = middle + std::string(field_width - middle.size(), '.');
|
|
||||||
} else {
|
|
||||||
field_text = middle.substr(0, field_width);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << " " << prefix << field_text
|
|
||||||
<< suffix_color << suffix << RESET
|
|
||||||
<< "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Status con true/false, usando dots y sufijos fijos
|
|
||||||
inline void status(const std::string& name, bool ok) {
|
|
||||||
// Ambos sufijos tienen 9 caracteres → alineación perfecta
|
|
||||||
constexpr const char* OK_LABEL = "[ OK ]";
|
|
||||||
constexpr const char* ERROR_LABEL = "[ ERROR ]";
|
|
||||||
|
|
||||||
if (ok) {
|
|
||||||
dots(" ", name, OK_LABEL, GREEN);
|
|
||||||
} else {
|
|
||||||
dots(" ", name, ERROR_LABEL, RED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Logger
|
|
||||||
@@ -365,14 +365,15 @@ void ServiceMenu::initializeOptions() {
|
|||||||
|
|
||||||
// Shader: Desactivat / PostFX / CrtPi
|
// Shader: Desactivat / PostFX / CrtPi
|
||||||
{
|
{
|
||||||
const std::string DISABLED_TEXT = Lang::getText("[SERVICE_MENU] SHADER_DISABLED");
|
std::string disabled_text = Lang::getText("[SERVICE_MENU] SHADER_DISABLED");
|
||||||
std::vector<std::string> shader_values = {DISABLED_TEXT, "PostFX", "CrtPi"};
|
std::vector<std::string> shader_values = {disabled_text, "PostFX", "CrtPi"};
|
||||||
auto shader_getter = [DISABLED_TEXT]() -> std::string {
|
auto shader_getter = [disabled_text]() -> std::string {
|
||||||
if (!Options::video.shader.enabled) { return DISABLED_TEXT; }
|
// NOLINTNEXTLINE(performance-no-automatic-move) -- captura por valor en lambda const, no se puede mover
|
||||||
|
if (!Options::video.shader.enabled) { return disabled_text; }
|
||||||
return (Options::video.shader.current_shader == Rendering::ShaderType::CRTPI) ? "CrtPi" : "PostFX";
|
return (Options::video.shader.current_shader == Rendering::ShaderType::CRTPI) ? "CrtPi" : "PostFX";
|
||||||
};
|
};
|
||||||
auto shader_setter = [DISABLED_TEXT](const std::string& val) {
|
auto shader_setter = [disabled_text](const std::string& val) {
|
||||||
if (val == DISABLED_TEXT) {
|
if (val == disabled_text) {
|
||||||
Options::video.shader.enabled = false;
|
Options::video.shader.enabled = false;
|
||||||
} else {
|
} else {
|
||||||
Options::video.shader.enabled = true;
|
Options::video.shader.enabled = true;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <SDL3/SDL.h> // Para SDL_Event
|
#include <SDL3/SDL.h> // Para SDL_Event
|
||||||
|
|
||||||
#include <cstddef> // Para size_t
|
#include <cstddef> // Para size_t
|
||||||
|
#include <cstdint> // Para std::uint8_t
|
||||||
#include <functional> // Para function
|
#include <functional> // Para function
|
||||||
#include <iterator> // Para pair
|
#include <iterator> // Para pair
|
||||||
#include <memory> // Para unique_ptr
|
#include <memory> // Para unique_ptr
|
||||||
@@ -19,7 +20,7 @@ class MenuRenderer;
|
|||||||
class ServiceMenu {
|
class ServiceMenu {
|
||||||
public:
|
public:
|
||||||
// --- Enums y constantes ---
|
// --- Enums y constantes ---
|
||||||
enum class SettingsGroup {
|
enum class SettingsGroup : std::uint8_t {
|
||||||
CONTROLS,
|
CONTROLS,
|
||||||
VIDEO,
|
VIDEO,
|
||||||
AUDIO,
|
AUDIO,
|
||||||
@@ -27,7 +28,7 @@ class ServiceMenu {
|
|||||||
SYSTEM,
|
SYSTEM,
|
||||||
MAIN
|
MAIN
|
||||||
};
|
};
|
||||||
enum class GroupAlignment {
|
enum class GroupAlignment : std::uint8_t {
|
||||||
CENTERED,
|
CENTERED,
|
||||||
LEFT
|
LEFT
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <SDL3/SDL.h> // Para SDL_FPoint, SDL_FRect
|
#include <SDL3/SDL.h> // Para SDL_FPoint, SDL_FRect
|
||||||
|
|
||||||
#include <algorithm> // Para min
|
#include <algorithm> // Para min
|
||||||
|
#include <cstdint> // Para std::uint8_t
|
||||||
#include <memory> // Para allocator, shared_ptr
|
#include <memory> // Para allocator, shared_ptr
|
||||||
#include <string> // Para string
|
#include <string> // Para string
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
@@ -13,7 +14,7 @@
|
|||||||
|
|
||||||
class WindowMessage {
|
class WindowMessage {
|
||||||
public:
|
public:
|
||||||
enum class PositionMode {
|
enum class PositionMode : std::uint8_t {
|
||||||
CENTERED, // La ventana se centra en el punto especificado
|
CENTERED, // La ventana se centra en el punto especificado
|
||||||
FIXED // La esquina superior izquierda coincide con el punto
|
FIXED // La esquina superior izquierda coincide con el punto
|
||||||
};
|
};
|
||||||
@@ -176,7 +177,7 @@ class WindowMessage {
|
|||||||
|
|
||||||
// Animación de mostrar/ocultar
|
// Animación de mostrar/ocultar
|
||||||
struct ShowHideAnimation {
|
struct ShowHideAnimation {
|
||||||
enum class Type { NONE,
|
enum class Type : std::uint8_t { NONE,
|
||||||
SHOWING,
|
SHOWING,
|
||||||
HIDING };
|
HIDING };
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// NOLINTNEXTLINE(bugprone-reserved-identifier) -- requerido por <cmath> para exponer M_PI en MSVC
|
||||||
#define _USE_MATH_DEFINES
|
#define _USE_MATH_DEFINES
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
@@ -13,9 +14,6 @@
|
|||||||
|
|
||||||
#include "lang.hpp" // Para getText
|
#include "lang.hpp" // Para getText
|
||||||
|
|
||||||
// Variables
|
|
||||||
Overrides overrides = Overrides();
|
|
||||||
|
|
||||||
// Calcula el cuadrado de la distancia entre dos puntos
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
auto distanceSquared(int x1, int y1, int x2, int y2) -> double {
|
auto distanceSquared(int x1, int y1, int x2, int y2) -> double {
|
||||||
const int DELTA_X = x2 - x1;
|
const int DELTA_X = x2 - x1;
|
||||||
|
|||||||
@@ -12,13 +12,6 @@
|
|||||||
constexpr int BLOCK = 8;
|
constexpr int BLOCK = 8;
|
||||||
|
|
||||||
// --- Estructuras ---
|
// --- Estructuras ---
|
||||||
struct Overrides {
|
|
||||||
std::string param_file; // Fichero de parametros a utilizar
|
|
||||||
bool clear_hi_score_table{false}; // Reinicia la tabla de records
|
|
||||||
|
|
||||||
Overrides() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Circle {
|
struct Circle {
|
||||||
int x, y, r; // Coordenadas y radio
|
int x, y, r; // Coordenadas y radio
|
||||||
Circle()
|
Circle()
|
||||||
@@ -41,9 +34,6 @@ struct Zone {
|
|||||||
float third_quarter_y; // Anclaje al 75% del eje Y
|
float third_quarter_y; // Anclaje al 75% del eje Y
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Variables ---
|
|
||||||
extern Overrides overrides; // Configuración global de overrides
|
|
||||||
|
|
||||||
// --- Funciones ---
|
// --- Funciones ---
|
||||||
|
|
||||||
// Colisiones y geometría
|
// Colisiones y geometría
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
SECONDS=0
|
|
||||||
make linux_debug
|
|
||||||
duration=$SECONDS
|
|
||||||
echo "$((duration / 60)) minutes and $((duration % 60)) seconds elapsed."
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
SOURCEPATH=../source/
|
|
||||||
|
|
||||||
for i in "$SOURCEPATH"/*.cpp
|
|
||||||
do
|
|
||||||
include-what-you-use -D _DEBUG -std=c++20 -Wall "$i"
|
|
||||||
done
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
SOURCEPATH=../source/
|
|
||||||
|
|
||||||
for i in "$SOURCEPATH"/*.cpp
|
|
||||||
do
|
|
||||||
include-what-you-use -D DEBUG -std=c++20 -Wall "$i"
|
|
||||||
read -r -p "Presiona cualquier tecla para continuar..."
|
|
||||||
clear
|
|
||||||
done
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Verifica que se haya proporcionado un archivo como argumento
|
|
||||||
if [ $# -eq 0 ]; then
|
|
||||||
echo "Uso: $0 <archivo.cpp>"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
FILE="$1"
|
|
||||||
|
|
||||||
include-what-you-use -D _DEBUG -std=c++20 -Wall "$FILE" \
|
|
||||||
-I../source \
|
|
||||||
-Xiwyu --mapping_file=sdl3_mapping.imp \
|
|
||||||
-Xiwyu --update_comments \
|
|
||||||
-Xiwyu --verbose=3
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
# Per a un fitxer, desde l'arrel del projecte executar:
|
|
||||||
clang-tidy source/fitxer.cpp -p build/ --fix
|
|
||||||
|
|
||||||
# Per a varios fitxers, desde l'arrel:
|
|
||||||
find source/ \( -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) | \
|
|
||||||
xargs -P4 -I{} bash -c 'echo "Procesando: {}"; clang-tidy {} -p build/ --fix'
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
*:/home/sergio/gitea/coffee_crisis_arcade_edition/source/stb*
|
|
||||||
*:/home/sergio/gitea/coffee_crisis_arcade_edition/source/gif.c
|
|
||||||
*:/home/sergio/gitea/coffee_crisis_arcade_edition/source/jail*
|
|
||||||
*:/usr/include/*
|
|
||||||
*:../source/stb*
|
|
||||||
*:../source/gif.c
|
|
||||||
*:../source/jail*
|
|
||||||
*:/usr/include/*
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# 🏁 Ruta base del proyecto
|
|
||||||
BASE_DIR="/home/sergio/gitea/coffee_crisis_arcade_edition"
|
|
||||||
|
|
||||||
# 📁 Ruta al build
|
|
||||||
BUILD_DIR="$BASE_DIR/build"
|
|
||||||
|
|
||||||
# 📄 Archivo de mapping personalizado
|
|
||||||
MAPPING_FILE="$BASE_DIR/linux_utils/sdl3_mapping.imp"
|
|
||||||
|
|
||||||
# 📦 Generar compile_commands.json
|
|
||||||
echo "🔧 Generando compile_commands.json..."
|
|
||||||
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S "$BASE_DIR" -B "$BUILD_DIR"
|
|
||||||
|
|
||||||
@@ -1,553 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
##===--- iwyu_tool.py -----------------------------------------------------===##
|
|
||||||
#
|
|
||||||
# The LLVM Compiler Infrastructure
|
|
||||||
#
|
|
||||||
# This file is distributed under the University of Illinois Open Source
|
|
||||||
# License. See LICENSE.TXT for details.
|
|
||||||
#
|
|
||||||
##===----------------------------------------------------------------------===##
|
|
||||||
|
|
||||||
""" Driver to consume a Clang compilation database and invoke IWYU.
|
|
||||||
|
|
||||||
Example usage with CMake:
|
|
||||||
|
|
||||||
# Unix systems
|
|
||||||
$ mkdir build && cd build
|
|
||||||
$ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ...
|
|
||||||
$ iwyu_tool.py -p .
|
|
||||||
|
|
||||||
# Windows systems
|
|
||||||
$ mkdir build && cd build
|
|
||||||
$ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \
|
|
||||||
-DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \
|
|
||||||
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
|
|
||||||
-G Ninja ...
|
|
||||||
$ python3 iwyu_tool.py -p .
|
|
||||||
|
|
||||||
See iwyu_tool.py -h for more details on command-line arguments.
|
|
||||||
"""
|
|
||||||
from __future__ import print_function
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
import shlex
|
|
||||||
import shutil
|
|
||||||
import argparse
|
|
||||||
import tempfile
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
|
|
||||||
CORRECT_RE = re.compile(r'^\((.*?) has correct #includes/fwd-decls\)$')
|
|
||||||
SHOULD_ADD_RE = re.compile(r'^(.*?) should add these lines:$')
|
|
||||||
ADD_RE = re.compile('^(.*?) +// (.*)$')
|
|
||||||
SHOULD_REMOVE_RE = re.compile(r'^(.*?) should remove these lines:$')
|
|
||||||
FULL_LIST_RE = re.compile(r'The full include-list for (.*?):$')
|
|
||||||
END_RE = re.compile(r'^---$')
|
|
||||||
LINES_RE = re.compile(r'^- (.*?) // lines ([0-9]+)-[0-9]+$')
|
|
||||||
|
|
||||||
|
|
||||||
GENERAL, ADD, REMOVE, LIST = range(4)
|
|
||||||
|
|
||||||
|
|
||||||
def clang_formatter(output, style):
|
|
||||||
""" Process iwyu's output into something clang-like. """
|
|
||||||
formatted = []
|
|
||||||
|
|
||||||
state = (GENERAL, None)
|
|
||||||
for line in output.splitlines():
|
|
||||||
match = CORRECT_RE.match(line)
|
|
||||||
if match:
|
|
||||||
# See PR#1806 for more info
|
|
||||||
continue
|
|
||||||
match = SHOULD_ADD_RE.match(line)
|
|
||||||
if match:
|
|
||||||
state = (ADD, match.group(1))
|
|
||||||
continue
|
|
||||||
match = SHOULD_REMOVE_RE.match(line)
|
|
||||||
if match:
|
|
||||||
state = (REMOVE, match.group(1))
|
|
||||||
continue
|
|
||||||
match = FULL_LIST_RE.match(line)
|
|
||||||
if match:
|
|
||||||
state = (LIST, match.group(1))
|
|
||||||
elif END_RE.match(line):
|
|
||||||
state = (GENERAL, None)
|
|
||||||
elif not line.strip():
|
|
||||||
continue
|
|
||||||
elif state[0] == GENERAL:
|
|
||||||
formatted.append(line)
|
|
||||||
elif state[0] == ADD:
|
|
||||||
match = ADD_RE.match(line)
|
|
||||||
if match:
|
|
||||||
formatted.append("%s:1:1: %s: add '%s' (%s)" %
|
|
||||||
(state[1],
|
|
||||||
style,
|
|
||||||
match.group(1),
|
|
||||||
match.group(2)))
|
|
||||||
else:
|
|
||||||
formatted.append("%s:1:1: %s: add '%s'" %
|
|
||||||
(state[1], style, line))
|
|
||||||
elif state[0] == REMOVE:
|
|
||||||
match = LINES_RE.match(line)
|
|
||||||
line_no = match.group(2) if match else '1'
|
|
||||||
formatted.append("%s:%s:1: %s: superfluous '%s'" %
|
|
||||||
(state[1], line_no, style, match.group(1)))
|
|
||||||
|
|
||||||
return os.linesep.join(formatted)
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_FORMAT = 'iwyu'
|
|
||||||
FORMATTERS = {
|
|
||||||
'iwyu': lambda output: output,
|
|
||||||
'clang': lambda output: clang_formatter(output, style="error"),
|
|
||||||
'clang-warning': lambda output: clang_formatter(output, style="warning"),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
# Case-insensitive match on Windows
|
|
||||||
def normcase(s):
|
|
||||||
return s.lower()
|
|
||||||
else:
|
|
||||||
def normcase(s):
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def is_subpath_of(path, parent):
|
|
||||||
""" Return True if path is equal to or fully contained within parent.
|
|
||||||
|
|
||||||
Assumes both paths are canonicalized with os.path.realpath.
|
|
||||||
"""
|
|
||||||
parent = normcase(parent)
|
|
||||||
path = normcase(path)
|
|
||||||
|
|
||||||
if path == parent:
|
|
||||||
return True
|
|
||||||
|
|
||||||
if not path.startswith(parent):
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Now we know parent is a prefix of path, but they only share lineage if the
|
|
||||||
# difference between them starts with a path separator, e.g. /a/b/c/file
|
|
||||||
# is not a parent of /a/b/c/file.cpp, but /a/b/c and /a/b/c/ are.
|
|
||||||
parent = parent.rstrip(os.path.sep)
|
|
||||||
suffix = path[len(parent):]
|
|
||||||
return suffix.startswith(os.path.sep)
|
|
||||||
|
|
||||||
|
|
||||||
def is_msvc_driver(compile_command):
|
|
||||||
""" Return True if compile_command matches an MSVC CL-style driver. """
|
|
||||||
compile_command = normcase(compile_command)
|
|
||||||
|
|
||||||
if compile_command.endswith('cl.exe'):
|
|
||||||
# Native MSVC compiler or clang-cl.exe
|
|
||||||
return True
|
|
||||||
|
|
||||||
if compile_command.endswith('clang-cl'):
|
|
||||||
# Cross clang-cl on non-Windows
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def win_split(cmdline):
|
|
||||||
""" Minimal implementation of shlex.split for Windows following
|
|
||||||
https://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft.aspx.
|
|
||||||
"""
|
|
||||||
def split_iter(cmdline):
|
|
||||||
in_quotes = False
|
|
||||||
backslashes = 0
|
|
||||||
arg = ''
|
|
||||||
for c in cmdline:
|
|
||||||
if c == '\\':
|
|
||||||
# MSDN: Backslashes are interpreted literally, unless they
|
|
||||||
# immediately precede a double quotation mark.
|
|
||||||
# Buffer them until we know what comes next.
|
|
||||||
backslashes += 1
|
|
||||||
elif c == '"':
|
|
||||||
# Quotes can either be an escaped quote or the start of a quoted
|
|
||||||
# string. Paraphrasing MSDN:
|
|
||||||
# Before quotes, place one backslash in the arg for every pair
|
|
||||||
# of leading backslashes. If the number of backslashes is odd,
|
|
||||||
# retain the double quotation mark, otherwise interpret it as a
|
|
||||||
# string delimiter and switch state.
|
|
||||||
arg += '\\' * (backslashes // 2)
|
|
||||||
if backslashes % 2 == 1:
|
|
||||||
arg += c
|
|
||||||
else:
|
|
||||||
in_quotes = not in_quotes
|
|
||||||
backslashes = 0
|
|
||||||
elif c in (' ', '\t') and not in_quotes:
|
|
||||||
# MSDN: Arguments are delimited by white space, which is either
|
|
||||||
# a space or a tab [but only outside of a string].
|
|
||||||
# Flush any buffered backslashes and yield arg, unless empty.
|
|
||||||
arg += '\\' * backslashes
|
|
||||||
if arg:
|
|
||||||
yield arg
|
|
||||||
arg = ''
|
|
||||||
backslashes = 0
|
|
||||||
else:
|
|
||||||
# Flush buffered backslashes and append.
|
|
||||||
arg += '\\' * backslashes
|
|
||||||
arg += c
|
|
||||||
backslashes = 0
|
|
||||||
|
|
||||||
if arg:
|
|
||||||
arg += '\\' * backslashes
|
|
||||||
yield arg
|
|
||||||
|
|
||||||
return list(split_iter(cmdline))
|
|
||||||
|
|
||||||
|
|
||||||
def split_command(cmdstr):
|
|
||||||
""" Split a command string into a list, respecting shell quoting. """
|
|
||||||
if sys.platform.startswith('win'):
|
|
||||||
# shlex.split does not work for Windows command-lines, so special-case
|
|
||||||
# to our own implementation.
|
|
||||||
cmd = win_split(cmdstr)
|
|
||||||
else:
|
|
||||||
cmd = shlex.split(cmdstr)
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
|
|
||||||
|
|
||||||
def find_include_what_you_use():
|
|
||||||
""" Find IWYU executable and return its full pathname. """
|
|
||||||
env_iwyu_path = os.environ.get('IWYU_BINARY')
|
|
||||||
if env_iwyu_path:
|
|
||||||
return os.path.realpath(env_iwyu_path)
|
|
||||||
|
|
||||||
# Search in same dir as this script.
|
|
||||||
iwyu_path = shutil.which('include-what-you-use',
|
|
||||||
path=os.path.dirname(__file__))
|
|
||||||
if iwyu_path:
|
|
||||||
return os.path.realpath(iwyu_path)
|
|
||||||
|
|
||||||
# Search the system PATH.
|
|
||||||
iwyu_path = shutil.which('include-what-you-use')
|
|
||||||
if iwyu_path:
|
|
||||||
return os.path.realpath(iwyu_path)
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
IWYU_EXECUTABLE = find_include_what_you_use()
|
|
||||||
|
|
||||||
|
|
||||||
class Process(object):
|
|
||||||
""" Manages an IWYU process in flight """
|
|
||||||
def __init__(self, proc, outfile):
|
|
||||||
self.proc = proc
|
|
||||||
self.outfile = outfile
|
|
||||||
self.output = None
|
|
||||||
|
|
||||||
def poll(self):
|
|
||||||
""" Return the exit code if the process has completed, None otherwise.
|
|
||||||
"""
|
|
||||||
return self.proc.poll()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def returncode(self):
|
|
||||||
return self.proc.returncode
|
|
||||||
|
|
||||||
def get_output(self):
|
|
||||||
""" Return stdout+stderr output of the process.
|
|
||||||
|
|
||||||
This call blocks until the process is complete, then returns the output.
|
|
||||||
"""
|
|
||||||
if not self.output:
|
|
||||||
self.proc.wait()
|
|
||||||
self.outfile.seek(0)
|
|
||||||
self.output = self.outfile.read().decode("utf-8")
|
|
||||||
self.outfile.close()
|
|
||||||
|
|
||||||
return self.output
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def start(cls, invocation):
|
|
||||||
""" Start a Process for the invocation and capture stdout+stderr. """
|
|
||||||
outfile = tempfile.TemporaryFile(prefix='iwyu')
|
|
||||||
process = subprocess.Popen(
|
|
||||||
invocation.command,
|
|
||||||
cwd=invocation.cwd,
|
|
||||||
stdout=outfile,
|
|
||||||
stderr=subprocess.STDOUT)
|
|
||||||
return cls(process, outfile)
|
|
||||||
|
|
||||||
|
|
||||||
KNOWN_COMPILER_WRAPPERS=frozenset([
|
|
||||||
"ccache"
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
class Invocation(object):
|
|
||||||
""" Holds arguments of an IWYU invocation. """
|
|
||||||
def __init__(self, command, cwd):
|
|
||||||
self.command = command
|
|
||||||
self.cwd = cwd
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return ' '.join(self.command)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_compile_command(cls, entry, extra_args):
|
|
||||||
""" Parse a JSON compilation database entry into new Invocation. """
|
|
||||||
if 'arguments' in entry:
|
|
||||||
# arguments is a command-line in list form.
|
|
||||||
command = entry['arguments']
|
|
||||||
elif 'command' in entry:
|
|
||||||
# command is a command-line in string form, split to list.
|
|
||||||
command = split_command(entry['command'])
|
|
||||||
else:
|
|
||||||
raise ValueError('Invalid compilation database entry: %s' % entry)
|
|
||||||
|
|
||||||
if command[0] in KNOWN_COMPILER_WRAPPERS:
|
|
||||||
# Remove the compiler wrapper from the command.
|
|
||||||
command = command[1:]
|
|
||||||
|
|
||||||
# Rewrite the compile command for IWYU
|
|
||||||
compile_command, compile_args = command[0], command[1:]
|
|
||||||
if is_msvc_driver(compile_command):
|
|
||||||
# If the compiler is cl-compatible, let IWYU be cl-compatible.
|
|
||||||
extra_args = ['--driver-mode=cl'] + extra_args
|
|
||||||
|
|
||||||
command = [IWYU_EXECUTABLE] + extra_args + compile_args
|
|
||||||
return cls(command, entry['directory'])
|
|
||||||
|
|
||||||
def start(self, verbose):
|
|
||||||
""" Run invocation and collect output. """
|
|
||||||
if verbose:
|
|
||||||
print('# %s' % self, file=sys.stderr)
|
|
||||||
|
|
||||||
return Process.start(self)
|
|
||||||
|
|
||||||
|
|
||||||
def fixup_compilation_db(compilation_db):
|
|
||||||
""" Canonicalize paths in JSON compilation database. """
|
|
||||||
for entry in compilation_db:
|
|
||||||
# Convert relative paths to absolute ones if possible, based on the entry's directory.
|
|
||||||
if 'directory' in entry and not os.path.isabs(entry['file']):
|
|
||||||
entry['file'] = os.path.join(entry['directory'], entry['file'])
|
|
||||||
|
|
||||||
# Expand relative paths and symlinks
|
|
||||||
entry['file'] = os.path.realpath(entry['file'])
|
|
||||||
|
|
||||||
return compilation_db
|
|
||||||
|
|
||||||
|
|
||||||
def select_compilation_db(compilation_db, selection):
|
|
||||||
""" Return a new compilation database reduced to the paths in selection. """
|
|
||||||
if not selection:
|
|
||||||
return compilation_db
|
|
||||||
|
|
||||||
# Canonicalize selection paths to match compilation database.
|
|
||||||
selection = [os.path.realpath(p) for p in selection]
|
|
||||||
|
|
||||||
new_db = []
|
|
||||||
for path in selection:
|
|
||||||
if not os.path.exists(path):
|
|
||||||
print('warning: \'%s\' not found on disk.' % path, file=sys.stderr)
|
|
||||||
continue
|
|
||||||
|
|
||||||
found = [e for e in compilation_db if is_subpath_of(e['file'], path)]
|
|
||||||
if not found:
|
|
||||||
print('warning: \'%s\' not found in compilation database.' % path,
|
|
||||||
file=sys.stderr)
|
|
||||||
continue
|
|
||||||
|
|
||||||
new_db.extend(found)
|
|
||||||
|
|
||||||
return new_db
|
|
||||||
|
|
||||||
def slice_compilation_db(compilation_db, selection, exclude):
|
|
||||||
""" Return a new compilation database with filtered entries. """
|
|
||||||
|
|
||||||
new_db = select_compilation_db(compilation_db, selection)
|
|
||||||
|
|
||||||
# Canonicalize selection paths to match compilation database.
|
|
||||||
exclude = [os.path.realpath(p) for p in exclude]
|
|
||||||
|
|
||||||
for path in exclude:
|
|
||||||
if not os.path.exists(path):
|
|
||||||
print('warning: excluded path \'%s\' not found on disk.' % path,
|
|
||||||
file=sys.stderr)
|
|
||||||
continue
|
|
||||||
|
|
||||||
new_db = [e for e in new_db if not is_subpath_of(e['file'], path)]
|
|
||||||
|
|
||||||
return new_db
|
|
||||||
|
|
||||||
|
|
||||||
def worst_exit_code(worst, cur):
|
|
||||||
"""Return the most extreme exit code of two.
|
|
||||||
|
|
||||||
Negative exit codes occur if the program exits due to a signal (Unix) or
|
|
||||||
structured exception (Windows). If we've seen a negative one before, keep
|
|
||||||
it, as it usually indicates a critical error.
|
|
||||||
|
|
||||||
Otherwise return the biggest positive exit code.
|
|
||||||
"""
|
|
||||||
if cur < 0:
|
|
||||||
# Negative results take precedence, return the minimum
|
|
||||||
return min(worst, cur)
|
|
||||||
elif worst < 0:
|
|
||||||
# We know cur is non-negative, negative worst must be minimum
|
|
||||||
return worst
|
|
||||||
else:
|
|
||||||
# We know neither are negative, return the maximum
|
|
||||||
return max(worst, cur)
|
|
||||||
|
|
||||||
|
|
||||||
def execute(invocations, verbose, formatter, jobs, max_load_average=0):
|
|
||||||
""" Launch processes described by invocations. """
|
|
||||||
exit_code = 0
|
|
||||||
if jobs == 1:
|
|
||||||
for invocation in invocations:
|
|
||||||
proc = invocation.start(verbose)
|
|
||||||
print(formatter(proc.get_output()))
|
|
||||||
exit_code = worst_exit_code(exit_code, proc.returncode)
|
|
||||||
return exit_code
|
|
||||||
|
|
||||||
pending = []
|
|
||||||
while invocations or pending:
|
|
||||||
# Collect completed IWYU processes and print results.
|
|
||||||
complete = [proc for proc in pending if proc.poll() is not None]
|
|
||||||
for proc in complete:
|
|
||||||
pending.remove(proc)
|
|
||||||
print(formatter(proc.get_output()))
|
|
||||||
exit_code = worst_exit_code(exit_code, proc.returncode)
|
|
||||||
|
|
||||||
# Schedule new processes if there's room.
|
|
||||||
capacity = jobs - len(pending)
|
|
||||||
|
|
||||||
if max_load_average > 0:
|
|
||||||
one_min_load_average, _, _ = os.getloadavg()
|
|
||||||
load_capacity = max_load_average - one_min_load_average
|
|
||||||
if load_capacity < 0:
|
|
||||||
load_capacity = 0
|
|
||||||
if load_capacity < capacity:
|
|
||||||
capacity = int(load_capacity)
|
|
||||||
if not capacity and not pending:
|
|
||||||
# Ensure there is at least one job running.
|
|
||||||
capacity = 1
|
|
||||||
|
|
||||||
pending.extend(i.start(verbose) for i in invocations[:capacity])
|
|
||||||
invocations = invocations[capacity:]
|
|
||||||
|
|
||||||
# Yield CPU.
|
|
||||||
time.sleep(0.0001)
|
|
||||||
return exit_code
|
|
||||||
|
|
||||||
|
|
||||||
def main(compilation_db_path, source_files, exclude, verbose, formatter, jobs,
|
|
||||||
max_load_average, extra_args):
|
|
||||||
""" Entry point. """
|
|
||||||
|
|
||||||
if not IWYU_EXECUTABLE:
|
|
||||||
print('error: include-what-you-use executable not found',
|
|
||||||
file=sys.stderr)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
try:
|
|
||||||
if os.path.isdir(compilation_db_path):
|
|
||||||
compilation_db_path = os.path.join(compilation_db_path,
|
|
||||||
'compile_commands.json')
|
|
||||||
|
|
||||||
# Read compilation db from disk.
|
|
||||||
compilation_db_path = os.path.realpath(compilation_db_path)
|
|
||||||
with open(compilation_db_path, 'r') as fileobj:
|
|
||||||
compilation_db = json.load(fileobj)
|
|
||||||
except IOError as why:
|
|
||||||
print('error: failed to parse compilation database: %s' % why,
|
|
||||||
file=sys.stderr)
|
|
||||||
return 1
|
|
||||||
|
|
||||||
compilation_db = fixup_compilation_db(compilation_db)
|
|
||||||
compilation_db = slice_compilation_db(compilation_db, source_files, exclude)
|
|
||||||
|
|
||||||
# Transform compilation db entries into a list of IWYU invocations.
|
|
||||||
invocations = [
|
|
||||||
Invocation.from_compile_command(e, extra_args) for e in compilation_db
|
|
||||||
]
|
|
||||||
|
|
||||||
return execute(invocations, verbose, formatter, jobs, max_load_average)
|
|
||||||
|
|
||||||
|
|
||||||
def _bootstrap(sys_argv):
|
|
||||||
""" Parse arguments and dispatch to main(). """
|
|
||||||
|
|
||||||
# This hackery is necessary to add the forwarded IWYU args to the
|
|
||||||
# usage and help strings.
|
|
||||||
def customize_usage(parser):
|
|
||||||
""" Rewrite the parser's format_usage. """
|
|
||||||
original_format_usage = parser.format_usage
|
|
||||||
parser.format_usage = lambda: original_format_usage().rstrip() + \
|
|
||||||
' -- [<IWYU args>]' + os.linesep
|
|
||||||
|
|
||||||
def customize_help(parser):
|
|
||||||
""" Rewrite the parser's format_help. """
|
|
||||||
original_format_help = parser.format_help
|
|
||||||
|
|
||||||
def custom_help():
|
|
||||||
""" Customized help string, calls the adjusted format_usage. """
|
|
||||||
helpmsg = original_format_help()
|
|
||||||
helplines = helpmsg.splitlines()
|
|
||||||
helplines[0] = parser.format_usage().rstrip()
|
|
||||||
return os.linesep.join(helplines) + os.linesep
|
|
||||||
|
|
||||||
parser.format_help = custom_help
|
|
||||||
|
|
||||||
# Parse arguments.
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description='Include-what-you-use compilation database driver.',
|
|
||||||
epilog='Assumes include-what-you-use is available on the PATH.')
|
|
||||||
customize_usage(parser)
|
|
||||||
customize_help(parser)
|
|
||||||
|
|
||||||
parser.add_argument('-v', '--verbose', action='store_true',
|
|
||||||
help='Print IWYU commands')
|
|
||||||
parser.add_argument('-o', '--output-format', type=str,
|
|
||||||
choices=FORMATTERS.keys(), default=DEFAULT_FORMAT,
|
|
||||||
help='Output format (default: %s)' % DEFAULT_FORMAT)
|
|
||||||
parser.add_argument('-j', '--jobs', type=int, default=1,
|
|
||||||
nargs='?', const=0,
|
|
||||||
help=('Number of concurrent subprocesses. If zero, '
|
|
||||||
'will try to match the logical cores of the '
|
|
||||||
'system.'))
|
|
||||||
parser.add_argument('-l', '--load', type=float, default=0,
|
|
||||||
help=('Do not start new jobs if the 1min load average '
|
|
||||||
'is greater than the provided value'))
|
|
||||||
parser.add_argument('-p', metavar='<build-path>', required=True,
|
|
||||||
help='Compilation database path', dest='dbpath')
|
|
||||||
parser.add_argument('-e', '--exclude', action='append', default=[],
|
|
||||||
help=('Do not run IWYU on source files (or directories) '
|
|
||||||
'below this path.'))
|
|
||||||
parser.add_argument('source', nargs='*',
|
|
||||||
help=('Zero or more source files (or directories) to '
|
|
||||||
'run IWYU on. Defaults to all in compilation '
|
|
||||||
'database.'))
|
|
||||||
|
|
||||||
def partition_args(argv):
|
|
||||||
""" Split around '--' into driver args and IWYU args. """
|
|
||||||
try:
|
|
||||||
double_dash = argv.index('--')
|
|
||||||
return argv[:double_dash], argv[double_dash+1:]
|
|
||||||
except ValueError:
|
|
||||||
return argv, []
|
|
||||||
argv, extra_args = partition_args(sys_argv[1:])
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
|
|
||||||
jobs = args.jobs
|
|
||||||
if jobs == 0:
|
|
||||||
jobs = os.cpu_count() or 1
|
|
||||||
|
|
||||||
return main(args.dbpath, args.source, args.exclude, args.verbose,
|
|
||||||
FORMATTERS[args.output_format], jobs, args.load, extra_args)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
sys.exit(_bootstrap(sys.argv))
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Script para ejecutar clang-tidy en múltiples directorios
|
|
||||||
# Uso: ./run_clang-tidy.sh [--fix]
|
|
||||||
# --fix: Aplica las correcciones automáticamente (opcional)
|
|
||||||
|
|
||||||
# Detectar si se pasó el parámetro --fix
|
|
||||||
FIX_FLAG=""
|
|
||||||
if [[ "$1" == "--fix" ]]; then
|
|
||||||
FIX_FLAG="--fix"
|
|
||||||
echo "Modo: Aplicando correcciones automáticamente (--fix)"
|
|
||||||
else
|
|
||||||
echo "Modo: Solo análisis (sin --fix)"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
|
|
||||||
# Lista de rutas donde ejecutar clang-tidy
|
|
||||||
PATHS=(
|
|
||||||
"/home/sergio/gitea/coffee_crisis_arcade_edition/source"
|
|
||||||
"/home/sergio/gitea/coffee_crisis_arcade_edition/source/rendering"
|
|
||||||
"/home/sergio/gitea/coffee_crisis_arcade_edition/source/rendering/opengl"
|
|
||||||
"/home/sergio/gitea/coffee_crisis_arcade_edition/source/sections"
|
|
||||||
"/home/sergio/gitea/coffee_crisis_arcade_edition/source/ui"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Ruta del directorio build (relativa desde donde se ejecuta el script)
|
|
||||||
BUILD_DIR="/home/sergio/gitea/coffee_crisis_arcade_edition/build/"
|
|
||||||
|
|
||||||
# Función para procesar un directorio
|
|
||||||
process_directory() {
|
|
||||||
local dir="$1"
|
|
||||||
|
|
||||||
echo "=== Procesando directorio: $dir ==="
|
|
||||||
|
|
||||||
# Verificar que el directorio existe
|
|
||||||
if [[ ! -d "$dir" ]]; then
|
|
||||||
echo "Error: El directorio $dir no existe"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Cambiar al directorio y ejecutar find con -maxdepth 1 para un solo nivel
|
|
||||||
cd "$dir" || return 1
|
|
||||||
|
|
||||||
# Buscar archivos .cpp, .h, .hpp solo en el nivel actual (no subdirectorios)
|
|
||||||
find . -maxdepth 1 \( -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) -print0 | \
|
|
||||||
xargs -0 -P4 -I{} bash -c 'echo "Procesando: {}"; clang-tidy {} -p '"$BUILD_DIR"' '"$FIX_FLAG"
|
|
||||||
|
|
||||||
echo "=== Completado: $dir ==="
|
|
||||||
echo
|
|
||||||
}
|
|
||||||
|
|
||||||
# Procesar cada directorio en la lista
|
|
||||||
for path in "${PATHS[@]}"; do
|
|
||||||
process_directory "$path"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "¡Proceso completado para todos los directorios!"
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Función para mostrar el uso del script
|
|
||||||
mostrar_uso() {
|
|
||||||
echo "Uso: $0 [-o opción]"
|
|
||||||
echo "Opciones:"
|
|
||||||
echo " w Ejecutar cppcheck con warning, style, performance"
|
|
||||||
echo " a Ejecutar cppcheck con todas las opciones habilitadas"
|
|
||||||
echo " u Ejecutar cppcheck para unusedFunction"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Inicializar las variables
|
|
||||||
opcion=""
|
|
||||||
|
|
||||||
# Procesar las opciones
|
|
||||||
while getopts "o:" opt; do
|
|
||||||
case $opt in
|
|
||||||
o)
|
|
||||||
opcion=$OPTARG
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
mostrar_uso
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# Ejecutar según la opción seleccionada
|
|
||||||
case $opcion in
|
|
||||||
w)
|
|
||||||
cppcheck --force --enable=warning,style,performance --std=c++20 \
|
|
||||||
--check-level=exhaustive \
|
|
||||||
--suppressions-list=./cppcheck_suppressions \
|
|
||||||
../source/ \
|
|
||||||
2>./cppcheck-result-warning-style-performance.txt
|
|
||||||
;;
|
|
||||||
a)
|
|
||||||
cppcheck --force --enable=all -I /usr/include --std=c++20 \
|
|
||||||
--suppress=missingIncludeSystem \
|
|
||||||
--suppressions-list=./cppcheck_suppressions \
|
|
||||||
../source/ \
|
|
||||||
2>./cppcheck-result-all.txt
|
|
||||||
;;
|
|
||||||
u)
|
|
||||||
cppcheck --enable=style --std=c++20 \
|
|
||||||
--suppressions-list=./cppcheck_suppressions \
|
|
||||||
../source/ \
|
|
||||||
2>./cppcheck-result-unusedFunction.txt
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
mostrar_uso
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# 🏁 Ruta base del proyecto
|
|
||||||
BASE_DIR="/home/sergio/gitea/coffee_crisis_arcade_edition"
|
|
||||||
|
|
||||||
# 📁 Ruta al build
|
|
||||||
BUILD_DIR="$BASE_DIR/build"
|
|
||||||
|
|
||||||
# 📄 Archivo de mapping personalizado
|
|
||||||
MAPPING_FILE="$BASE_DIR/linux_utils/sdl3_mapping.imp"
|
|
||||||
|
|
||||||
# 📦 Generar compile_commands.json
|
|
||||||
echo "🔧 Generando compile_commands.json..."
|
|
||||||
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S "$BASE_DIR" -B "$BUILD_DIR"
|
|
||||||
|
|
||||||
# 🛠️ Ejecutar IWYU con fix_includes.py
|
|
||||||
echo "🚀 Ejecutando IWYU..."
|
|
||||||
./iwyu_tool.py -p "$BUILD_DIR" -- -Xiwyu --mapping_file="$MAPPING_FILE" -Xiwyu --verbose=3 \
|
|
||||||
| fix_include --update_comments --reorder --nosafe_headers
|
|
||||||
|
|
||||||
# 🧹 Reemplazar // for por // Para en líneas de #include
|
|
||||||
echo "✍️ Corrigiendo comentarios en includes..."
|
|
||||||
find "$BASE_DIR" -type f \( -name "*.cpp" -o -name "*.h" \) -exec \
|
|
||||||
sed -i '/^#include .*\/\/ for/s/\/\/ for/\/\/ Para/' {} +
|
|
||||||
|
|
||||||
echo "✅ Script completado."
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# 🏁 Ruta base del proyecto
|
|
||||||
BASE_DIR="/home/sergio/gitea/coffee_crisis_arcade_edition"
|
|
||||||
|
|
||||||
# 📁 Ruta al build
|
|
||||||
BUILD_DIR="$BASE_DIR/build"
|
|
||||||
|
|
||||||
# 📄 Archivo de mapping personalizado
|
|
||||||
MAPPING_FILE="$BASE_DIR/linux_utils/sdl3_mapping.imp"
|
|
||||||
|
|
||||||
# 📦 Generar compile_commands.json
|
|
||||||
echo "🔧 Generando compile_commands.json..."
|
|
||||||
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S "$BASE_DIR" -B "$BUILD_DIR"
|
|
||||||
|
|
||||||
# 🛠️ Ejecutar IWYU con fix_includes.py
|
|
||||||
echo "🚀 Ejecutando IWYU..."
|
|
||||||
iwyu_tool.py -p "$BUILD_DIR" -- -Xiwyu --mapping_file="$MAPPING_FILE" -Xiwyu --verbose=3 \
|
|
||||||
| python3 /usr/bin/fix_includes.py --update_comments --reorder --nosafe_headers --dry_run
|
|
||||||
|
|
||||||
# 🧹 Reemplazar // for por // Para en líneas de #include
|
|
||||||
echo "✍️ Corrigiendo comentarios en includes..."
|
|
||||||
find "$BASE_DIR" -type f \( -name "*.cpp" -o -name "*.h" \) -exec \
|
|
||||||
sed -i '/^#include .*\/\/ for/s/\/\/ for/\/\/ Para/' {} +
|
|
||||||
|
|
||||||
echo "✅ Script completado."
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
valgrind --suppressions=valgrind_exceptions \
|
|
||||||
--leak-check=full \
|
|
||||||
~/gitea/coffee_crisis_arcade_edition/coffee_crisis_arcade_edition \
|
|
||||||
> ~/gitea/coffee_crisis_arcade_edition/linux_utils/valgrind_out.txt 2>&1
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
[
|
|
||||||
{ "include": ["<SDL3/SDL_stdinc.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_assert.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_asyncio.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_atomic.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_audio.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_bits.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_blendmode.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_camera.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_clipboard.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_cpuinfo.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_dialog.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_endian.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_error.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_events.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_filesystem.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_gamepad.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_gpu.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_guid.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_haptic.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_hidapi.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_hints.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_init.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_iostream.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_joystick.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_keyboard.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_keycode.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_loadso.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_locale.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_log.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_messagebox.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_metal.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_misc.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_mouse.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_mutex.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_pen.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_pixels.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_platform.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_power.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_process.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_properties.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_rect.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_render.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_scancode.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_sensor.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_storage.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_surface.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_system.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_thread.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_time.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_timer.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_tray.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_touch.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_version.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_video.h>", "private", "<SDL3/SDL.h>", "public"] },
|
|
||||||
{ "include": ["<SDL3/SDL_oldnames.h>", "private", "<SDL3/SDL.h>", "public"] }
|
|
||||||
]
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
ignore_unversioned_libs
|
|
||||||
Memcheck:Leak
|
|
||||||
...
|
|
||||||
obj:*/lib*/lib*.so
|
|
||||||
}
|
|
||||||
{
|
|
||||||
ignore_versioned_libs
|
|
||||||
Memcheck:Leak
|
|
||||||
...
|
|
||||||
obj:*/lib*/lib*.so.*
|
|
||||||
}
|
|
||||||
57
tools/shaders/compile_shader.cmake
Normal file
57
tools/shaders/compile_shader.cmake
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
# compile_shader.cmake
|
||||||
|
# Compila un shader GLSL a header C++ embebible con datos SPIR-V.
|
||||||
|
# Funciona en Windows, Linux y macOS sin bash ni herramientas Unix.
|
||||||
|
#
|
||||||
|
# Variables requeridas (pasar con -D al invocar cmake -P):
|
||||||
|
# GLSLC - ruta al ejecutable glslc
|
||||||
|
# SRC - archivo fuente GLSL
|
||||||
|
# OUT_H - archivo header de salida
|
||||||
|
# VAR - nombre base de la variable C++ (e.g. "postfx_vert_spv")
|
||||||
|
# STAGE - (opcional) stage explícito para glslc (e.g. "fragment")
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
get_filename_component(SRC_NAME "${SRC}" NAME_WE)
|
||||||
|
get_filename_component(OUT_DIR "${OUT_H}" DIRECTORY)
|
||||||
|
set(OUT_SPV "${OUT_DIR}/${SRC_NAME}.spv")
|
||||||
|
|
||||||
|
# Compilar GLSL → SPIR-V
|
||||||
|
if(DEFINED STAGE AND NOT STAGE STREQUAL "")
|
||||||
|
execute_process(
|
||||||
|
COMMAND "${GLSLC}" "-fshader-stage=${STAGE}" "${SRC}" -o "${OUT_SPV}"
|
||||||
|
RESULT_VARIABLE RESULT
|
||||||
|
ERROR_VARIABLE ERROR_MSG
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
execute_process(
|
||||||
|
COMMAND "${GLSLC}" "${SRC}" -o "${OUT_SPV}"
|
||||||
|
RESULT_VARIABLE RESULT
|
||||||
|
ERROR_VARIABLE ERROR_MSG
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT RESULT EQUAL 0)
|
||||||
|
message(FATAL_ERROR "glslc falló compilando ${SRC}:\n${ERROR_MSG}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Leer binario SPIR-V como cadena hexadecimal
|
||||||
|
file(READ "${OUT_SPV}" SPV_HEX HEX)
|
||||||
|
file(REMOVE "${OUT_SPV}")
|
||||||
|
|
||||||
|
string(LENGTH "${SPV_HEX}" HEX_LEN)
|
||||||
|
math(EXPR BYTE_COUNT "${HEX_LEN} / 2")
|
||||||
|
|
||||||
|
# Convertir a array C++ con formato " 0xXX,\n" por byte
|
||||||
|
string(REGEX REPLACE "([0-9a-f][0-9a-f])" " 0x\\1,\n" SPV_BYTES "${SPV_HEX}")
|
||||||
|
# Eliminar la última coma y sustituir por el "}" de cierre
|
||||||
|
string(REGEX REPLACE ",\n$" "}" SPV_BYTES "${SPV_BYTES}")
|
||||||
|
|
||||||
|
# Escribir el header
|
||||||
|
file(WRITE "${OUT_H}"
|
||||||
|
"#pragma once\n"
|
||||||
|
"#include <cstddef>\n"
|
||||||
|
"#include <cstdint>\n"
|
||||||
|
"static const uint8_t k${VAR}[] = {\n"
|
||||||
|
"${SPV_BYTES};\n"
|
||||||
|
"static const size_t k${VAR}_size = ${BYTE_COUNT};\n"
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user