Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 64ab08973c | |||
| 94a7a38cdd | |||
| 76165e4345 | |||
| 767a1f6af8 | |||
| 20ca024100 | |||
| 3c3857c1b2 | |||
| 523342fed9 | |||
| 217ca58b1a | |||
| ec6565bf71 | |||
| cd7f06f3a1 | |||
| 8886873ed5 | |||
| a41e696b69 | |||
| 4b7cbd88bb | |||
| 789cbbc593 | |||
| 1dd87c0707 | |||
| 330044e10f | |||
| f8c5207d5c | |||
| 2caaa29124 | |||
| cdc4d07394 | |||
| 1023cde1be | |||
| a3aeed4b7c | |||
| 3b0354da54 | |||
| 622ccd22bc | |||
| 1441134aea | |||
| 0500dce7aa | |||
| 9f0dfc4e24 | |||
| aa66dd41c1 | |||
| 69fb5f3cc1 | |||
| d6b2e97777 | |||
| 98c90e6075 | |||
| f795c86a38 | |||
| c1c5774406 | |||
| 0139da4764 | |||
| ec911979fb | |||
| e51749dbc6 | |||
| 9ceb21c04f | |||
| 76a91b4736 | |||
| df744338f1 | |||
| 8803fc3806 | |||
| c26a4774a1 | |||
| 20538af4c6 | |||
| 73f222fcb7 | |||
| 67681e0f37 |
21
.clang-format
Normal file
21
.clang-format
Normal file
@@ -0,0 +1,21 @@
|
||||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
IndentAccessModifiers: true
|
||||
ColumnLimit: 0 # Sin límite de longitud de línea
|
||||
BreakBeforeBraces: Attach # Llaves en la misma línea
|
||||
AllowShortIfStatementsOnASingleLine: true
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AlignOperands: DontAlign
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
ContinuationIndentWidth: 4
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
Cpp11BracedListStyle: true
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
AllowAllConstructorInitializersOnNextLine: false
|
||||
PackConstructorInitializers: Never
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
16
.gitignore
vendored
16
.gitignore
vendored
@@ -17,6 +17,16 @@ asteroids
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
tools/pack_resources/pack_resources
|
||||
tools/pack_resources/pack_resources.exe
|
||||
|
||||
# Releases
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.dmg
|
||||
|
||||
# Generated resources
|
||||
resources.pack
|
||||
|
||||
# Compiled Object files
|
||||
*.o
|
||||
@@ -58,9 +68,9 @@ _deps/
|
||||
*.ilk
|
||||
|
||||
# Core dumps
|
||||
core
|
||||
core.*
|
||||
*.core
|
||||
# core
|
||||
# core.*
|
||||
# *.core
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
|
||||
97
CLAUDE.md
97
CLAUDE.md
@@ -12,59 +12,98 @@ This is **Orni Attack**, an **Asteroids-style game** originally written in **Tur
|
||||
|
||||
## Build System
|
||||
|
||||
Based on `/home/sergio/gitea/pollo` project structure.
|
||||
Based on `/home/sergio/gitea/pollo` project structure, now with **CMake as build authority** and **automatic file discovery**.
|
||||
|
||||
### Build Commands
|
||||
### Basic Build Commands
|
||||
|
||||
```bash
|
||||
# Clean + compile
|
||||
make clean && make
|
||||
make # Compile (delegates to CMake)
|
||||
make debug # Debug build
|
||||
make clean # Clean artifacts
|
||||
./orni # Run
|
||||
```
|
||||
|
||||
# Run
|
||||
./orni
|
||||
### Release Packaging
|
||||
|
||||
# Individual targets
|
||||
make linux # Linux build
|
||||
make macos # macOS build
|
||||
make windows # Windows build (MinGW)
|
||||
```bash
|
||||
make macos_release # macOS .app bundle + .dmg (Apple Silicon)
|
||||
make linux_release # Linux .tar.gz
|
||||
make windows_release # Windows .zip (requires MinGW on Windows)
|
||||
make windows_cross # Cross-compile Windows from Linux/macOS
|
||||
make rpi_release # Raspberry Pi ARM64 cross-compile
|
||||
```
|
||||
|
||||
### Build Files
|
||||
|
||||
- **CMakeLists.txt** - CMake configuration (C++20, SDL3, project metadata)
|
||||
- **Makefile** - Cross-platform wrapper, extracts project info from CMakeLists.txt
|
||||
- **CMakeLists.txt** - CMake configuration (C++20, SDL3, auto-discovers .cpp files)
|
||||
- **Makefile** - Wrapper for compilation + complex packaging recipes
|
||||
- **source/project.h.in** - Template for auto-generated project.h
|
||||
- **build/project.h** - Auto-generated (by CMake) with project constants
|
||||
- **release/** - Platform-specific resources (icons, .rc, .plist)
|
||||
- **release/** - Platform-specific resources (icons, .rc, .plist, frameworks, DLLs)
|
||||
|
||||
### Architecture: Hybrid CMake + Makefile
|
||||
|
||||
**CMake handles**: Compilation (simple, standard, IDE-friendly)
|
||||
- Auto-discovers all `.cpp` files in `source/core/` and `source/game/`
|
||||
- Excludes `source/legacy/` automatically
|
||||
- Generates `build/project.h` from template
|
||||
- Links SDL3
|
||||
|
||||
**Makefile handles**: Packaging (complex bash scripts)
|
||||
- Delegates compilation to CMake (`make` → `cmake --build build`)
|
||||
- Contains 5 release packaging targets (macOS, Linux, Windows, RPI, Windows-cross)
|
||||
- Includes: code signing, framework symlinks, DMG creation, cross-compilation
|
||||
|
||||
### Project Metadata System
|
||||
|
||||
**Auto-generation with CMake**:
|
||||
**Single source of truth** in `CMakeLists.txt`:
|
||||
|
||||
CMake generates `build/project.h` from `source/project.h.in` template on every compilation:
|
||||
```cmake
|
||||
project(orni VERSION 0.3.0)
|
||||
set(PROJECT_LONG_NAME "Orni Attack")
|
||||
set(PROJECT_COPYRIGHT "© 1999 Visente i Sergi, 2025 Port")
|
||||
```
|
||||
|
||||
**Auto-generated** `build/project.h`:
|
||||
|
||||
```cpp
|
||||
// build/project.h (generated automatically)
|
||||
namespace Project {
|
||||
constexpr const char* NAME = "orni"; // From project(orni ...)
|
||||
constexpr const char* LONG_NAME = "Orni Attack"; // From PROJECT_LONG_NAME
|
||||
constexpr const char* VERSION = "0.1.0"; // From VERSION
|
||||
constexpr const char* COPYRIGHT = "© 1999..."; // From PROJECT_COPYRIGHT
|
||||
constexpr const char* GIT_HASH = "abc1234"; // From git rev-parse
|
||||
constexpr const char* NAME = "orni";
|
||||
constexpr const char* LONG_NAME = "Orni Attack";
|
||||
constexpr const char* VERSION = "0.3.0";
|
||||
constexpr const char* COPYRIGHT = "© 1999 Visente i Sergi, 2025 Port";
|
||||
constexpr const char* GIT_HASH = "abc1234"; // From git rev-parse
|
||||
}
|
||||
```
|
||||
|
||||
**Window title format** (dynamic, in sdl_manager.cpp):
|
||||
```cpp
|
||||
std::format("{} v{} ({})",
|
||||
Project::LONG_NAME, // "Orni Attack"
|
||||
Project::VERSION, // "0.1.0"
|
||||
Project::COPYRIGHT) // "© 1999 Visente i Sergi, 2025 Port"
|
||||
**Window title** (dynamic): `Orni Attack v0.3.0 (© 1999 Visente i Sergi, 2025 Port)`
|
||||
|
||||
### File Discovery
|
||||
|
||||
**Automatic** - no manual maintenance needed:
|
||||
|
||||
```cmake
|
||||
# CMakeLists.txt automatically finds:
|
||||
file(GLOB_RECURSE CORE_SOURCES "source/core/*.cpp")
|
||||
file(GLOB_RECURSE GAME_SOURCES "source/game/*.cpp")
|
||||
# + source/main.cpp
|
||||
# - source/legacy/* (excluded)
|
||||
```
|
||||
|
||||
Result: `Orni Attack v0.1.0 (© 1999 Visente i Sergi, 2025 Port)`
|
||||
**When you create a new file** like `source/game/entities/asteroide.cpp`:
|
||||
1. Just create it in the appropriate directory
|
||||
2. Run `make`
|
||||
3. CMake automatically detects and compiles it
|
||||
|
||||
**Single source of truth**: All project info in CMakeLists.txt, no hardcoded strings.
|
||||
**No need to edit** Makefile or CMakeLists.txt!
|
||||
|
||||
### Cross-Platform Notes
|
||||
|
||||
- **macOS**: Requires `create-dmg` (auto-installed via Homebrew)
|
||||
- **Windows**: Compile natively with MinGW or use `make windows_cross` on Linux/macOS
|
||||
- **Windows cross**: Requires `x86_64-w64-mingw32-g++` toolchain
|
||||
- **RPI cross**: Requires `aarch64-linux-gnu-g++` toolchain
|
||||
- **Frameworks**: macOS release includes SDL3.xcframework with symlink recreation
|
||||
|
||||
## Architecture
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# CMakeLists.txt
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(orni VERSION 0.1.0)
|
||||
project(orni VERSION 0.5.0)
|
||||
|
||||
# Info del proyecto
|
||||
set(PROJECT_LONG_NAME "Orni Attack")
|
||||
@@ -31,28 +31,23 @@ endif()
|
||||
# Configurar archivo de versión
|
||||
configure_file(${CMAKE_SOURCE_DIR}/source/project.h.in ${CMAKE_BINARY_DIR}/project.h @ONLY)
|
||||
|
||||
# --- LISTA DE FUENTES ---
|
||||
# --- LISTA DE FUENTES (AUTO-DESCUBRIMIENTO) ---
|
||||
# Buscar automáticamente todos los archivos .cpp en core/, game/ y main.cpp
|
||||
file(GLOB_RECURSE CORE_SOURCES "${CMAKE_SOURCE_DIR}/source/core/*.cpp")
|
||||
file(GLOB_RECURSE GAME_SOURCES "${CMAKE_SOURCE_DIR}/source/game/*.cpp")
|
||||
set(APP_SOURCES
|
||||
${CORE_SOURCES}
|
||||
${GAME_SOURCES}
|
||||
source/main.cpp
|
||||
source/core/system/director.cpp
|
||||
source/core/system/global_events.cpp
|
||||
source/core/rendering/sdl_manager.cpp
|
||||
source/core/rendering/line_renderer.cpp
|
||||
source/core/rendering/color_oscillator.cpp
|
||||
source/core/rendering/polygon_renderer.cpp
|
||||
source/core/rendering/primitives.cpp
|
||||
source/core/rendering/shape_renderer.cpp
|
||||
source/core/graphics/shape.cpp
|
||||
source/core/graphics/shape_loader.cpp
|
||||
source/core/graphics/vector_text.cpp
|
||||
source/game/options.cpp
|
||||
source/game/escenes/escena_logo.cpp
|
||||
source/game/escenes/escena_joc.cpp
|
||||
source/game/entities/nau.cpp
|
||||
source/game/entities/bala.cpp
|
||||
source/game/entities/enemic.cpp
|
||||
)
|
||||
|
||||
# Excluir archivos legacy (código Pascal de referencia)
|
||||
list(FILTER APP_SOURCES EXCLUDE REGEX ".*/legacy/.*")
|
||||
|
||||
# Log de archivos encontrados (útil para debug)
|
||||
list(LENGTH APP_SOURCES APP_SOURCES_COUNT)
|
||||
message(STATUS "Archivos .cpp encontrados: ${APP_SOURCES_COUNT}")
|
||||
|
||||
# Configuración de SDL3
|
||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
|
||||
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
|
||||
@@ -78,6 +73,11 @@ target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-O2 -ffunctio
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:RELEASE_BUILD>)
|
||||
|
||||
# Definir MACOS_BUNDLE si es un bundle de macOS
|
||||
if(APPLE AND MACOSX_BUNDLE)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUNDLE)
|
||||
endif()
|
||||
|
||||
# Configuración específica para cada plataforma
|
||||
if(WIN32)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
|
||||
|
||||
410
Makefile
410
Makefile
@@ -20,6 +20,15 @@ TARGET_FILE := $(DIR_BIN)$(TARGET_NAME)
|
||||
RELEASE_FOLDER := $(TARGET_NAME)_release
|
||||
RELEASE_FILE := $(RELEASE_FOLDER)/$(TARGET_NAME)
|
||||
|
||||
# Release file names
|
||||
RAW_VERSION := $(shell echo $(VERSION) | sed 's/^v//')
|
||||
WINDOWS_RELEASE := $(TARGET_NAME)-$(VERSION)-windows-x64.zip
|
||||
MACOS_ARM_RELEASE := $(TARGET_NAME)-$(VERSION)-macos-arm64.dmg
|
||||
MACOS_INTEL_RELEASE := $(TARGET_NAME)-$(VERSION)-macos-x64.dmg
|
||||
LINUX_RELEASE := $(TARGET_NAME)-$(VERSION)-linux-x64.tar.gz
|
||||
RPI_RELEASE := $(TARGET_NAME)-$(VERSION)-rpi-arm64.tar.gz
|
||||
APP_NAME := $(LONG_NAME)
|
||||
|
||||
# ==============================================================================
|
||||
# VERSION
|
||||
# ==============================================================================
|
||||
@@ -32,147 +41,307 @@ endif
|
||||
# ==============================================================================
|
||||
# SOURCE FILES
|
||||
# ==============================================================================
|
||||
APP_SOURCES := \
|
||||
source/main.cpp \
|
||||
source/core/system/director.cpp \
|
||||
source/core/system/global_events.cpp \
|
||||
source/core/rendering/sdl_manager.cpp \
|
||||
source/core/rendering/line_renderer.cpp \
|
||||
source/core/rendering/color_oscillator.cpp \
|
||||
source/core/rendering/polygon_renderer.cpp \
|
||||
source/core/rendering/primitives.cpp \
|
||||
source/core/rendering/shape_renderer.cpp \
|
||||
source/core/graphics/shape.cpp \
|
||||
source/core/graphics/shape_loader.cpp \
|
||||
source/core/graphics/vector_text.cpp \
|
||||
source/game/options.cpp \
|
||||
source/game/escenes/escena_logo.cpp \
|
||||
source/game/escenes/escena_joc.cpp \
|
||||
source/game/entities/nau.cpp \
|
||||
source/game/entities/bala.cpp \
|
||||
source/game/entities/enemic.cpp
|
||||
# Note: Source files are now auto-discovered by CMake using GLOB_RECURSE
|
||||
# No need to maintain this list manually anymore!
|
||||
|
||||
# ==============================================================================
|
||||
# INCLUDES
|
||||
# PLATFORM-SPECIFIC UTILITIES
|
||||
# ==============================================================================
|
||||
INCLUDES := -Isource -Ibuild
|
||||
|
||||
# ==============================================================================
|
||||
# COMPILER FLAGS (OS-specific)
|
||||
# ==============================================================================
|
||||
CPP_STANDARD := c++20
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
# Windows (MinGW)
|
||||
FixPath = $(subst /,\\,$1)
|
||||
CXX := g++
|
||||
CXXFLAGS := -std=$(CPP_STANDARD) -Wall -O2 -ffunction-sections -fdata-sections \
|
||||
-Wl,--gc-sections -static-libstdc++ -static-libgcc \
|
||||
-Wl,-subsystem,windows -DWINDOWS_BUILD
|
||||
CXXFLAGS_DEBUG := -std=$(CPP_STANDARD) -Wall -g -D_DEBUG -DWINDOWS_BUILD
|
||||
LDFLAGS := -lmingw32 -lSDL3
|
||||
WINDRES := windres
|
||||
RESOURCE_FILE := release/orni.res
|
||||
RM := del /Q
|
||||
RMFILE := del /Q
|
||||
RMDIR := rmdir /S /Q
|
||||
MKDIR := mkdir
|
||||
else
|
||||
# Unix-like systems (Linux/macOS)
|
||||
FixPath = $1
|
||||
CXX := g++
|
||||
CXXFLAGS := -std=$(CPP_STANDARD) -Wall -O2
|
||||
CXXFLAGS_DEBUG := -std=$(CPP_STANDARD) -Wall -g -D_DEBUG
|
||||
LDFLAGS := -lSDL3
|
||||
RMFILE := rm -f
|
||||
RMDIR := rm -rf
|
||||
MKDIR := mkdir -p
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
CXXFLAGS += -DLINUX_BUILD
|
||||
CXXFLAGS_DEBUG += -DLINUX_BUILD
|
||||
endif
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
CXXFLAGS += -arch arm64 -Wno-deprecated -DMACOS_BUILD
|
||||
CXXFLAGS_DEBUG += -arch arm64 -Wno-deprecated -DMACOS_BUILD
|
||||
endif
|
||||
endif
|
||||
|
||||
# ==============================================================================
|
||||
# PACKING TOOL
|
||||
# ==============================================================================
|
||||
PACK_TOOL := tools/pack_resources/pack_resources
|
||||
|
||||
# ==============================================================================
|
||||
# DEFAULT GOAL
|
||||
# ==============================================================================
|
||||
.DEFAULT_GOAL := all
|
||||
|
||||
.PHONY: pack_tool resources.pack
|
||||
|
||||
pack_tool:
|
||||
@$(MAKE) -C tools/pack_resources
|
||||
|
||||
resources.pack: pack_tool
|
||||
@echo "Creating resources.pack..."
|
||||
@./$(PACK_TOOL) data resources.pack
|
||||
|
||||
# ==============================================================================
|
||||
# TARGETS
|
||||
# ==============================================================================
|
||||
.PHONY: all clean debug help backup
|
||||
|
||||
# Default target
|
||||
all: $(TARGET_FILE)
|
||||
# ==============================================================================
|
||||
# BUILD TARGETS (delegate to CMake)
|
||||
# ==============================================================================
|
||||
|
||||
# Generate build/project.h from template
|
||||
.PHONY: generate_project_h
|
||||
generate_project_h:
|
||||
@$(MKDIR) build 2>/dev/null || true
|
||||
@echo "Generating build/project.h..."
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@powershell -Command \
|
||||
"$$name = '$(TARGET_NAME)'; \
|
||||
$$long = '$(LONG_NAME)'; \
|
||||
$$ver = '$(VERSION)'.TrimStart('v'); \
|
||||
$$copy = (Get-Content CMakeLists.txt | Where-Object {$$_ -match 'PROJECT_COPYRIGHT'} | ForEach-Object {if ($$_ -match '\"(.+)\"') {$$matches[1]}}); \
|
||||
$$hash = try {git rev-parse --short=7 HEAD 2>$$null} catch {'unknown'}; \
|
||||
(Get-Content source/project.h.in) -replace '@PROJECT_NAME@', $$name -replace '@PROJECT_LONG_NAME@', $$long -replace '@PROJECT_VERSION@', $$ver -replace '@PROJECT_COPYRIGHT@', $$copy -replace '@GIT_HASH@', $$hash | Set-Content build/project.h"
|
||||
else
|
||||
@PROJECT_COPYRIGHT=$$(grep 'PROJECT_COPYRIGHT' CMakeLists.txt | sed 's/.*"\(.*\)".*/\1/'); \
|
||||
PROJECT_VERSION=$$(echo $(VERSION) | sed 's/^v//'); \
|
||||
GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
|
||||
sed -e "s/@PROJECT_NAME@/$(TARGET_NAME)/g" \
|
||||
-e "s/@PROJECT_LONG_NAME@/$(LONG_NAME)/g" \
|
||||
-e "s/@PROJECT_VERSION@/$${PROJECT_VERSION}/g" \
|
||||
-e "s/@PROJECT_COPYRIGHT@/$${PROJECT_COPYRIGHT}/g" \
|
||||
-e "s/@GIT_HASH@/$${GIT_HASH}/g" \
|
||||
source/project.h.in > build/project.h
|
||||
endif
|
||||
@echo "build/project.h generated successfully"
|
||||
# Default target: build with CMake + resources
|
||||
all: resources.pack $(TARGET_FILE)
|
||||
|
||||
# Compile executable
|
||||
$(TARGET_FILE): generate_project_h $(APP_SOURCES)
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@if not exist build $(MKDIR) build
|
||||
@if not exist release\\orni.res $(WINDRES) release\\orni.rc -O coff -o release\\orni.res
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) $(APP_SOURCES) $(RESOURCE_FILE) $(LDFLAGS) -o $(TARGET_FILE).exe
|
||||
else
|
||||
@$(MKDIR) build
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) $(APP_SOURCES) $(LDFLAGS) -o $(TARGET_FILE)
|
||||
endif
|
||||
@echo Compilation successful: $(TARGET_FILE)
|
||||
$(TARGET_FILE):
|
||||
@cmake -B build -DCMAKE_BUILD_TYPE=Release
|
||||
@cmake --build build
|
||||
@echo "Build successful: $(TARGET_FILE)"
|
||||
|
||||
# Debug build
|
||||
debug: generate_project_h $(APP_SOURCES)
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@if not exist build $(MKDIR) build
|
||||
$(CXX) $(CXXFLAGS_DEBUG) $(INCLUDES) $(APP_SOURCES) $(LDFLAGS) -o $(TARGET_FILE)_debug.exe
|
||||
else
|
||||
@$(MKDIR) build
|
||||
$(CXX) $(CXXFLAGS_DEBUG) $(INCLUDES) $(APP_SOURCES) $(LDFLAGS) -o $(TARGET_FILE)_debug
|
||||
endif
|
||||
@echo Debug build successful: $(TARGET_FILE)_debug
|
||||
debug: resources.pack
|
||||
@cmake -B build -DCMAKE_BUILD_TYPE=Debug
|
||||
@cmake --build build
|
||||
@echo "Debug build successful: $(TARGET_FILE)"
|
||||
|
||||
# ==============================================================================
|
||||
# RELEASE PACKAGING TARGETS
|
||||
# ==============================================================================
|
||||
|
||||
# macOS Release (Apple Silicon)
|
||||
.PHONY: macos_release
|
||||
macos_release: pack_tool resources.pack
|
||||
@echo "Creating macOS release - Version: $(VERSION)"
|
||||
|
||||
# Check/install create-dmg
|
||||
@command -v create-dmg >/dev/null || (echo "Installing create-dmg..." && brew install create-dmg)
|
||||
|
||||
# Clean previous releases
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)" 2>/dev/null || true
|
||||
@$(RMDIR) Frameworks 2>/dev/null || true
|
||||
@$(RMFILE) "$(MACOS_ARM_RELEASE)" 2>/dev/null || true
|
||||
|
||||
# Create .app structure
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS"
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
|
||||
@$(MKDIR) Frameworks
|
||||
|
||||
# Copy resources.pack to Resources
|
||||
@cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources/"
|
||||
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework"
|
||||
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks/SDL3.framework
|
||||
|
||||
# Recreate framework symlinks (may be broken)
|
||||
@cd Frameworks/SDL3.framework && rm -f SDL3 Headers Resources && \
|
||||
ln -s Versions/Current/SDL3 SDL3 && \
|
||||
ln -s Versions/Current/Headers Headers && \
|
||||
ln -s Versions/Current/Resources Resources
|
||||
@cd Frameworks/SDL3.framework/Versions && rm -f Current && ln -s A Current
|
||||
@cd "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework" && rm -f SDL3 Headers Resources && \
|
||||
ln -s Versions/Current/SDL3 SDL3 && \
|
||||
ln -s Versions/Current/Headers Headers && \
|
||||
ln -s Versions/Current/Resources Resources
|
||||
@cd "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework/Versions" && rm -f Current && ln -s A Current
|
||||
|
||||
@cp release/icon.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources/"
|
||||
@cp release/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/"
|
||||
@cp LICENSE "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: LICENSE not found"
|
||||
@cp README.md "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: README.md not found"
|
||||
|
||||
# Update Info.plist version
|
||||
@echo "Updating Info.plist with version $(RAW_VERSION)..."
|
||||
@sed -i '' '/<key>CFBundleShortVersionString<\/key>/{n;s|<string>.*</string>|<string>$(RAW_VERSION)</string>|;}' \
|
||||
"$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Info.plist"
|
||||
@sed -i '' '/<key>CFBundleVersion<\/key>/{n;s|<string>.*</string>|<string>$(RAW_VERSION)</string>|;}' \
|
||||
"$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Info.plist"
|
||||
|
||||
# Compile for Apple Silicon using CMake
|
||||
@cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES=arm64
|
||||
@cmake --build build
|
||||
@cp $(TARGET_FILE) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)"
|
||||
|
||||
# Code sign
|
||||
@codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app" || echo "Warning: Code signing failed"
|
||||
|
||||
# Create DMG
|
||||
@echo "Creating DMG for Apple Silicon..."
|
||||
@create-dmg \
|
||||
--volname "$(APP_NAME)" \
|
||||
--window-pos 200 120 \
|
||||
--window-size 720 300 \
|
||||
--icon-size 96 \
|
||||
--text-size 12 \
|
||||
--icon "$(APP_NAME).app" 278 102 \
|
||||
--icon "LICENSE" 441 102 \
|
||||
--icon "README.md" 604 102 \
|
||||
--app-drop-link 115 102 \
|
||||
--hide-extension "$(APP_NAME).app" \
|
||||
"$(MACOS_ARM_RELEASE)" \
|
||||
"$(RELEASE_FOLDER)" || true
|
||||
@echo "✓ macOS release created: $(MACOS_ARM_RELEASE)"
|
||||
|
||||
# Cleanup
|
||||
@$(RMDIR) Frameworks
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Linux Release
|
||||
.PHONY: linux_release
|
||||
linux_release:
|
||||
@echo "Creating Linux release - Version: $(VERSION)"
|
||||
|
||||
# Clean previous
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
@$(RMFILE) "$(LINUX_RELEASE)"
|
||||
|
||||
# Create folder
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Copy resources
|
||||
@cp -r resources "$(RELEASE_FOLDER)/"
|
||||
@cp LICENSE "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: LICENSE not found"
|
||||
@cp README.md "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: README.md not found"
|
||||
|
||||
# Compile with CMake
|
||||
@cmake -B build -DCMAKE_BUILD_TYPE=Release
|
||||
@cmake --build build
|
||||
@cp $(TARGET_FILE) "$(RELEASE_FILE)"
|
||||
@strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded || strip "$(RELEASE_FILE)"
|
||||
|
||||
# Package
|
||||
@tar -czf "$(LINUX_RELEASE)" -C "$(RELEASE_FOLDER)" .
|
||||
@echo "✓ Linux release created: $(LINUX_RELEASE)"
|
||||
|
||||
# Cleanup
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Windows Release (requires MinGW on Windows or cross-compiler on Linux)
|
||||
.PHONY: windows_release
|
||||
windows_release:
|
||||
@echo "Creating Windows release - Version: $(VERSION)"
|
||||
@echo "Note: This target should be run on Windows with MinGW or use windows_cross on Linux"
|
||||
|
||||
# Clean previous
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
@$(RMFILE) "$(WINDOWS_RELEASE)"
|
||||
|
||||
# Create folder
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Copy resources
|
||||
@cp -r resources "$(RELEASE_FOLDER)/"
|
||||
@cp release/dll/*.dll "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: DLLs not found"
|
||||
@cp LICENSE "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: LICENSE not found"
|
||||
@cp README.md "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: README.md not found"
|
||||
|
||||
# Compile resource file
|
||||
@windres release/$(TARGET_NAME).rc -O coff -o release/$(TARGET_NAME).res 2>/dev/null || echo "Warning: windres failed"
|
||||
|
||||
# Compile with CMake
|
||||
@cmake -B build -DCMAKE_BUILD_TYPE=Release
|
||||
@cmake --build build
|
||||
@cp $(TARGET_FILE).exe "$(RELEASE_FILE).exe" || cp $(TARGET_FILE) "$(RELEASE_FILE).exe"
|
||||
|
||||
# Package
|
||||
@cd "$(RELEASE_FOLDER)" && zip -r ../$(WINDOWS_RELEASE) *
|
||||
@echo "✓ Windows release created: $(WINDOWS_RELEASE)"
|
||||
|
||||
# Cleanup
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Raspberry Pi Release (cross-compilation from Linux/macOS)
|
||||
.PHONY: rpi_release
|
||||
rpi_release:
|
||||
@echo "Creating Raspberry Pi ARM64 release - Version: $(VERSION)"
|
||||
@echo "Note: Requires aarch64-linux-gnu-g++ cross-compiler"
|
||||
|
||||
# Check for cross-compiler
|
||||
@command -v aarch64-linux-gnu-g++ >/dev/null || (echo "Error: aarch64-linux-gnu-g++ not found" && exit 1)
|
||||
|
||||
# Clean previous
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
@$(RMFILE) "$(RPI_RELEASE)"
|
||||
|
||||
# Create folder
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Copy resources
|
||||
@cp -r resources "$(RELEASE_FOLDER)/"
|
||||
@cp LICENSE "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: LICENSE not found"
|
||||
@cp README.md "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: README.md not found"
|
||||
|
||||
# Note: Cross-compilation with CMake is complex, would need toolchain file
|
||||
@echo "Warning: RPI cross-compilation requires manual setup with CMake toolchain file"
|
||||
@echo "Falling back to direct g++ compilation..."
|
||||
@aarch64-linux-gnu-g++ -std=c++20 -Wall -O2 -DLINUX_BUILD -DRPI_BUILD \
|
||||
-Isource -Ibuild \
|
||||
$$(find source/core source/game -name "*.cpp") source/main.cpp \
|
||||
-lSDL3 -o "$(RELEASE_FILE)" || echo "Error: Compilation failed"
|
||||
@aarch64-linux-gnu-strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded || true
|
||||
|
||||
# Package
|
||||
@tar -czf "$(RPI_RELEASE)" -C "$(RELEASE_FOLDER)" .
|
||||
@echo "✓ Raspberry Pi release created: $(RPI_RELEASE)"
|
||||
|
||||
# Cleanup
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Windows Cross-compilation (from Linux/macOS)
|
||||
.PHONY: windows_cross
|
||||
windows_cross:
|
||||
@echo "Cross-compiling for Windows from $(UNAME_S) - Version: $(VERSION)"
|
||||
|
||||
# Check for cross-compiler
|
||||
@command -v x86_64-w64-mingw32-g++ >/dev/null || (echo "Error: x86_64-w64-mingw32-g++ not found" && exit 1)
|
||||
|
||||
# Clean previous
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
@$(RMFILE) "$(WINDOWS_RELEASE)"
|
||||
|
||||
# Create folder
|
||||
@$(MKDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Copy resources
|
||||
@cp -r resources "$(RELEASE_FOLDER)/"
|
||||
@cp release/dll/*.dll "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: DLLs not found in release/dll/"
|
||||
@cp LICENSE "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: LICENSE not found"
|
||||
@cp README.md "$(RELEASE_FOLDER)/" 2>/dev/null || echo "Warning: README.md not found"
|
||||
|
||||
# Compile resource file
|
||||
@x86_64-w64-mingw32-windres release/$(TARGET_NAME).rc -O coff -o release/$(TARGET_NAME).res 2>/dev/null || echo "Warning: windres failed"
|
||||
|
||||
# Cross-compile
|
||||
@echo "Compiling with MinGW cross-compiler..."
|
||||
@x86_64-w64-mingw32-g++ -std=c++20 -Wall -O2 -DWINDOWS_BUILD -DRELEASE_BUILD \
|
||||
-static-libstdc++ -static-libgcc -Wl,-subsystem,windows \
|
||||
-Isource -Ibuild \
|
||||
$$(find source/core source/game -name "*.cpp") source/main.cpp \
|
||||
release/$(TARGET_NAME).res \
|
||||
-lmingw32 -lSDL3 -o "$(RELEASE_FILE).exe" || echo "Error: Compilation failed"
|
||||
@x86_64-w64-mingw32-strip "$(RELEASE_FILE).exe" || true
|
||||
|
||||
# Package
|
||||
@cd "$(RELEASE_FOLDER)" && zip -r ../$(WINDOWS_RELEASE) *
|
||||
@echo "✓ Windows cross-compiled release created: $(WINDOWS_RELEASE)"
|
||||
|
||||
# Cleanup
|
||||
@$(RMDIR) "$(RELEASE_FOLDER)"
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@if exist $(call FixPath,$(TARGET_FILE).exe) $(RM) $(call FixPath,$(TARGET_FILE).exe)
|
||||
@if exist $(call FixPath,$(TARGET_FILE)_debug.exe) $(RM) $(call FixPath,$(TARGET_FILE)_debug.exe)
|
||||
@if exist $(call FixPath,$(TARGET_FILE).exe) $(RMFILE) $(call FixPath,$(TARGET_FILE).exe)
|
||||
@if exist $(call FixPath,$(TARGET_FILE)_debug.exe) $(RMFILE) $(call FixPath,$(TARGET_FILE)_debug.exe)
|
||||
@if exist build $(RMDIR) build
|
||||
@if exist $(RELEASE_FOLDER) $(RMDIR) $(RELEASE_FOLDER)
|
||||
else
|
||||
@$(RMFILE) $(TARGET_FILE)
|
||||
@$(RMFILE) $(TARGET_FILE)_debug
|
||||
@$(RMDIR) build
|
||||
@$(RMDIR) $(RELEASE_FOLDER)
|
||||
@$(RMFILE) $(TARGET_FILE) $(TARGET_FILE)_debug
|
||||
@$(RMDIR) build $(RELEASE_FOLDER)
|
||||
@$(RMFILE) *.dmg *.zip *.tar.gz 2>/dev/null || true
|
||||
@$(RMFILE) resources.pack 2>/dev/null || true
|
||||
@$(MAKE) -C tools/pack_resources clean 2>/dev/null || true
|
||||
endif
|
||||
@echo Clean complete
|
||||
@echo "Clean complete"
|
||||
|
||||
# Backup to remote server
|
||||
backup:
|
||||
@echo "Backing up project to maverick:/home/sergio/git-backup/asteroids..."
|
||||
@echo "Backing up project to maverick:/home/sergio/git-backup/orni..."
|
||||
rsync -a --delete \
|
||||
--exclude='build/' \
|
||||
--exclude='*.o' \
|
||||
@@ -180,20 +349,31 @@ backup:
|
||||
--exclude='orni' \
|
||||
--exclude='orni_debug' \
|
||||
--exclude='*_release/' \
|
||||
$(DIR_ROOT) maverick:/home/sergio/git-backup/asteroids/
|
||||
$(DIR_ROOT) maverick:/home/sergio/git-backup/orni/
|
||||
@echo "Backup completed successfully"
|
||||
|
||||
# Help target
|
||||
help:
|
||||
@echo "Available targets:"
|
||||
@echo " all - Build the game (default)"
|
||||
@echo " debug - Build with debug symbols"
|
||||
@echo " clean - Remove build artifacts"
|
||||
@echo " backup - Backup project to remote server"
|
||||
@echo " help - Show this help message"
|
||||
@echo ""
|
||||
@echo "Build:"
|
||||
@echo " all - Build the game (default, delegates to CMake)"
|
||||
@echo " debug - Build with debug symbols"
|
||||
@echo " clean - Remove build artifacts and release packages"
|
||||
@echo ""
|
||||
@echo "Release Packaging:"
|
||||
@echo " macos_release - Create macOS .app bundle + .dmg (Apple Silicon)"
|
||||
@echo " linux_release - Create Linux .tar.gz"
|
||||
@echo " windows_release - Create Windows .zip (requires MinGW on Windows)"
|
||||
@echo " windows_cross - Cross-compile Windows from Linux/macOS (requires MinGW)"
|
||||
@echo " rpi_release - Cross-compile for Raspberry Pi ARM64"
|
||||
@echo ""
|
||||
@echo "Other:"
|
||||
@echo " backup - Backup project to remote server"
|
||||
@echo " help - Show this help message"
|
||||
@echo ""
|
||||
@echo "Current configuration:"
|
||||
@echo " Project: $(LONG_NAME)"
|
||||
@echo " Target: $(TARGET_NAME)"
|
||||
@echo " Version: $(VERSION)"
|
||||
@echo " Platform: $(UNAME_S)"
|
||||
@echo " C++ Standard: $(CPP_STANDARD)"
|
||||
|
||||
BIN
data/music/game.ogg
Normal file
BIN
data/music/game.ogg
Normal file
Binary file not shown.
BIN
data/music/title.ogg
Normal file
BIN
data/music/title.ogg
Normal file
Binary file not shown.
@@ -6,14 +6,18 @@ name: bullet
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Pentàgon petit radi=5 (1/4 del enemic)
|
||||
# Pentàgon regular amb 72° entre punts
|
||||
# Cercle (octàgon regular radi=3)
|
||||
# 8 punts equidistants (45° entre ells) per aproximar un cercle
|
||||
# Començant a angle=-90° (amunt), rotant sentit horari
|
||||
#
|
||||
# Conversió polar→cartesià (radi=5, SDL: Y creix cap avall):
|
||||
# angle=-90°: (0.00, -5.00)
|
||||
# angle=-18°: (4.76, -1.55)
|
||||
# angle=54°: (2.94, 4.05)
|
||||
# angle=126°: (-2.94, 4.05)
|
||||
# angle=198°: (-4.76, -1.55)
|
||||
# Conversió polar→cartesià (radi=3, SDL: Y creix cap avall):
|
||||
# angle=-90°: (0.00, -3.00)
|
||||
# angle=-45°: (2.12, -2.12)
|
||||
# angle=0°: (3.00, 0.00)
|
||||
# angle=45°: (2.12, 2.12)
|
||||
# angle=90°: (0.00, 3.00)
|
||||
# angle=135°: (-2.12, 2.12)
|
||||
# angle=180°: (-3.00, 0.00)
|
||||
# angle=225°: (-2.12, -2.12)
|
||||
|
||||
polyline: 0,-5 4.76,-1.55 2.94,4.05 -2.94,4.05 -4.76,-1.55 0,-5
|
||||
polyline: 0,-3 2.12,-2.12 3,0 2.12,2.12 0,3 -2.12,2.12 -3,0 -2.12,-2.12 0,-3
|
||||
|
||||
30
data/shapes/enemy_pinwheel.shp
Normal file
30
data/shapes/enemy_pinwheel.shp
Normal file
@@ -0,0 +1,30 @@
|
||||
# enemy_pinwheel.shp - ORNI enemic (molinillo de 4 triangles)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: enemy_pinwheel
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Molinillo: 4 triangles, un en cada quadrant
|
||||
# Cada triangle comparteix el centre (0,0) i té:
|
||||
# - Un vèrtex en un eix (±20, 0) o (0, ±20)
|
||||
# - Un vèrtex en la diagonal del quadrant (±14.14, ±14.14)
|
||||
# - El tercer vèrtex al centre (0,0)
|
||||
#
|
||||
# Geometria:
|
||||
# Triangle 1 (quadrant superior-dret): centre → eix dret → diagonal
|
||||
# Triangle 2 (quadrant superior-esq): centre → eix superior → diagonal
|
||||
# Triangle 3 (quadrant inferior-esq): centre → eix esquerre → diagonal
|
||||
# Triangle 4 (quadrant inferior-dret): centre → eix inferior → diagonal
|
||||
|
||||
# Triangle 1: quadrant superior-dret
|
||||
polyline: 0,0 20,0 14.14,-14.14 0,0
|
||||
|
||||
# Triangle 2: quadrant superior-esquerre
|
||||
polyline: 0,0 0,-20 -14.14,-14.14 0,0
|
||||
|
||||
# Triangle 3: quadrant inferior-esquerre
|
||||
polyline: 0,0 -20,0 -14.14,14.14 0,0
|
||||
|
||||
# Triangle 4: quadrant inferior-dret
|
||||
polyline: 0,0 0,20 14.14,14.14 0,0
|
||||
19
data/shapes/enemy_square.shp
Normal file
19
data/shapes/enemy_square.shp
Normal file
@@ -0,0 +1,19 @@
|
||||
# enemy_square.shp - ORNI enemic (quadrat regular)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: enemy_square
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Quadrat regular radi=20 (circumscrit)
|
||||
# 4 punts equidistants al voltant d'un cercle (90° entre ells)
|
||||
# Començant a angle=-90° (amunt), rotant sentit horari
|
||||
#
|
||||
# Angles: -90°, 0°, 90°, 180°
|
||||
# Conversió polar→cartesià (SDL: Y creix cap avall):
|
||||
# angle=-90°: (0.00, -20.00)
|
||||
# angle=0°: (20.00, 0.00)
|
||||
# angle=90°: (0.00, 20.00)
|
||||
# angle=180°: (-20.00, 0.00)
|
||||
|
||||
polyline: 0,-20 20,0 0,20 -20,0 0,-20
|
||||
12
data/shapes/font/char_copyright.shp
Normal file
12
data/shapes/font/char_copyright.shp
Normal file
@@ -0,0 +1,12 @@
|
||||
# char_copyright.shp - Símbolo © (copyright)
|
||||
# Dimensiones: 20×40 (blocky display)
|
||||
|
||||
name: char_copyright
|
||||
scale: 1.0
|
||||
center: 10, 20
|
||||
|
||||
# Círculo exterior (aproximado con 12 puntos)
|
||||
polyline: 10,8 13,9 15,11 17,14 18,17 18,23 17,26 15,29 13,31 10,32 7,31 5,29 3,26 2,23 2,17 3,14 5,11 7,9 10,8
|
||||
|
||||
# Letra C interior
|
||||
polyline: 13,16 9,14 7,16 6,20 7,24 9,26 13,24
|
||||
24
data/shapes/ship2.shp
Normal file
24
data/shapes/ship2.shp
Normal file
@@ -0,0 +1,24 @@
|
||||
# ship2.shp - Nau del jugador (triangle amb base còncava - punta de fletxa)
|
||||
# © 1999 Visente i Sergi (versió Pascal)
|
||||
# © 2025 Port a C++20 amb SDL3
|
||||
|
||||
name: ship2
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Triangle amb base còncava tipus "punta de fletxa"
|
||||
# Punts originals (polar):
|
||||
# p1: r=12, angle=270° (3π/2) → punta amunt
|
||||
# p2: r=12, angle=45° (π/4) → base dreta-darrere
|
||||
# p3: r=12, angle=135° (3π/4) → base esquerra-darrere
|
||||
#
|
||||
# MODIFICACIÓ: afegit p4 al mig de la base, desplaçat cap al centre
|
||||
# p4: (0, 4) → punt central de la base, cap endins
|
||||
#
|
||||
# Conversió polar→cartesià (angle-90° perquè origen visual és amunt):
|
||||
# p1: (0, -12) → punta
|
||||
# p2: (8.49, 8.49) → base dreta
|
||||
# p4: (0, 4) → base centre (cap endins)
|
||||
# p3: (-8.49, 8.49) → base esquerra
|
||||
|
||||
polyline: 0,-12 8.49,8.49 0,4 -8.49,8.49 0,-12
|
||||
19
data/shapes/star.shp
Normal file
19
data/shapes/star.shp
Normal file
@@ -0,0 +1,19 @@
|
||||
# star.shp - Estrella per a starfield
|
||||
# © 2025 Orni Attack
|
||||
|
||||
name: star
|
||||
scale: 1.0
|
||||
center: 0, 0
|
||||
|
||||
# Estrella de 4 puntes (diamant/creu)
|
||||
# Petita i simple per a l'efecte starfield
|
||||
#
|
||||
# Punts:
|
||||
# angle=0°: (0, -3) Dalt
|
||||
# angle=90°: (3, 0) Dreta
|
||||
# angle=180°: (0, 3) Baix
|
||||
# angle=270°: (-3, 0) Esquerra
|
||||
#
|
||||
# Forma de diamant amb línies de centre a puntes
|
||||
|
||||
polyline: 0,-3 3,0 0,3 -3,0 0,-3
|
||||
10
data/shapes/title/letra_a.shp
Normal file
10
data/shapes/title/letra_a.shp
Normal file
@@ -0,0 +1,10 @@
|
||||
# letra_a.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_a
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 0.00,100.00 0.00,75.00 37.50,0.00 100.00,0.00 137.50,75.00 137.50,100.00 100.00,100.00 100.00,87.50 37.50,87.50 37.50,100.00 0.00,100.00
|
||||
polyline: 62.50,25.00 50.00,50.00 50.00,62.50 87.50,62.50 87.50,50.00 75.00,25.00 62.50,25.00
|
||||
9
data/shapes/title/letra_c.shp
Normal file
9
data/shapes/title/letra_c.shp
Normal file
@@ -0,0 +1,9 @@
|
||||
# letra_c.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_c
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 12.50,100.00 0.00,87.50 0.00,12.50 12.50,0.00 125.00,0.00 137.50,12.50 137.50,37.50 100.00,37.50 100.00,25.00 37.50,25.00 37.50,75.00 100.00,75.00 100.00,62.50 137.50,62.50 137.50,87.50 125.00,100.00 12.50,100.00
|
||||
10
data/shapes/title/letra_exclamacion.shp
Normal file
10
data/shapes/title/letra_exclamacion.shp
Normal file
@@ -0,0 +1,10 @@
|
||||
# letra_exclamacion.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 37.51 x 100.00 px
|
||||
|
||||
name: letra_exclamacion
|
||||
scale: 1.0
|
||||
center: 18.75, 50.00
|
||||
|
||||
polyline: 0.00,62.50 0.00,0.00 37.51,0.00 37.51,62.50 0.00,62.50
|
||||
polyline: 0.00,100.00 0.00,75.00 37.51,75.00 37.51,100.00 0.00,100.00
|
||||
9
data/shapes/title/letra_i.shp
Normal file
9
data/shapes/title/letra_i.shp
Normal file
@@ -0,0 +1,9 @@
|
||||
# letra_i.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 37.50 x 100.00 px
|
||||
|
||||
name: letra_i
|
||||
scale: 1.0
|
||||
center: 18.75, 50.00
|
||||
|
||||
polyline: 0.00,0.00 37.50,0.00 37.50,100.00 0.00,100.00 0.00,0.00
|
||||
9
data/shapes/title/letra_k.shp
Normal file
9
data/shapes/title/letra_k.shp
Normal file
@@ -0,0 +1,9 @@
|
||||
# letra_k.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_k
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 0.00,100.00 0.00,0.00 37.50,0.00 37.50,37.50 50.00,37.50 100.00,0.00 137.50,0.00 137.50,25.00 87.06,50.00 137.50,75.00 137.50,100.00 100.00,100.00 50.00,62.50 37.50,62.50 37.50,100.00 0.00,100.00
|
||||
9
data/shapes/title/letra_n.shp
Normal file
9
data/shapes/title/letra_n.shp
Normal file
@@ -0,0 +1,9 @@
|
||||
# letra_n.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_n
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 0.00,100.00 0.00,0.00 50.00,0.00 100.00,50.00 100.00,0.00 137.50,0.00 137.50,100.00 87.50,100.00 37.50,50.00 37.50,100.00 0.00,100.00
|
||||
10
data/shapes/title/letra_o.shp
Normal file
10
data/shapes/title/letra_o.shp
Normal file
@@ -0,0 +1,10 @@
|
||||
# letra_o.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_o
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 12.50,100.00 0.00,87.50 0.00,12.50 12.50,0.00 125.00,0.00 137.50,12.50 137.50,87.50 125.00,100.00 12.50,100.00
|
||||
polyline: 100.00,25.00 37.50,25.00 37.50,75.00 100.00,75.00 100.00,25.00
|
||||
10
data/shapes/title/letra_r.shp
Normal file
10
data/shapes/title/letra_r.shp
Normal file
@@ -0,0 +1,10 @@
|
||||
# letra_r.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_r
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 0.00,100.00 0.00,0.00 125.00,0.00 137.50,12.50 137.50,62.50 125.00,62.50 137.50,75.00 137.50,100.00 100.00,100.00 100.00,75.00 37.50,75.00 37.50,100.00 0.00,100.00
|
||||
polyline: 37.50,50.00 100.00,50.00 100.00,25.00 37.50,25.00 37.50,50.00
|
||||
9
data/shapes/title/letra_t.shp
Normal file
9
data/shapes/title/letra_t.shp
Normal file
@@ -0,0 +1,9 @@
|
||||
# letra_t.shp
|
||||
# Generado automáticamente desde jailgames.svg
|
||||
# Dimensiones: 137.50 x 100.00 px
|
||||
|
||||
name: letra_t
|
||||
scale: 1.0
|
||||
center: 68.75, 50.00
|
||||
|
||||
polyline: 0.00,25.00 0.00,0.00 137.50,0.00 137.50,25.00 87.50,25.00 87.50,100.00 50.00,100.00 50.00,25.00 0.00,25.00
|
||||
BIN
data/sounds/explosion.wav
Normal file
BIN
data/sounds/explosion.wav
Normal file
Binary file not shown.
BIN
data/sounds/good_job_commander.wav
Normal file
BIN
data/sounds/good_job_commander.wav
Normal file
Binary file not shown.
BIN
data/sounds/laser_shoot.wav
Normal file
BIN
data/sounds/laser_shoot.wav
Normal file
Binary file not shown.
BIN
data/sounds/logo.wav
Normal file
BIN
data/sounds/logo.wav
Normal file
Binary file not shown.
168
data/stages/stages.yaml
Normal file
168
data/stages/stages.yaml
Normal file
@@ -0,0 +1,168 @@
|
||||
# stages.yaml - Configuració de les 10 etapes d'Orni Attack
|
||||
# © 2025 Orni Attack
|
||||
|
||||
metadata:
|
||||
version: "1.0"
|
||||
total_stages: 10
|
||||
description: "Progressive difficulty curve from novice to expert"
|
||||
|
||||
stages:
|
||||
# STAGE 1: Tutorial - Only pentagons, slow speed
|
||||
- stage_id: 1
|
||||
total_enemies: 5
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 2.0
|
||||
spawn_interval: 3.0
|
||||
enemy_distribution:
|
||||
pentagon: 100
|
||||
quadrat: 0
|
||||
molinillo: 0
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 0.7
|
||||
rotation_multiplier: 0.8
|
||||
tracking_strength: 0.0
|
||||
|
||||
# STAGE 2: Introduction to tracking enemies
|
||||
- stage_id: 2
|
||||
total_enemies: 7
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 1.5
|
||||
spawn_interval: 2.5
|
||||
enemy_distribution:
|
||||
pentagon: 70
|
||||
quadrat: 30
|
||||
molinillo: 0
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 0.85
|
||||
rotation_multiplier: 0.9
|
||||
tracking_strength: 0.3
|
||||
|
||||
# STAGE 3: All enemy types, normal speed
|
||||
- stage_id: 3
|
||||
total_enemies: 10
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 1.0
|
||||
spawn_interval: 2.0
|
||||
enemy_distribution:
|
||||
pentagon: 50
|
||||
quadrat: 30
|
||||
molinillo: 20
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.0
|
||||
rotation_multiplier: 1.0
|
||||
tracking_strength: 0.5
|
||||
|
||||
# STAGE 4: Increased count, faster enemies
|
||||
- stage_id: 4
|
||||
total_enemies: 12
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.8
|
||||
spawn_interval: 1.8
|
||||
enemy_distribution:
|
||||
pentagon: 40
|
||||
quadrat: 35
|
||||
molinillo: 25
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.1
|
||||
rotation_multiplier: 1.15
|
||||
tracking_strength: 0.6
|
||||
|
||||
# STAGE 5: Maximum count reached
|
||||
- stage_id: 5
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.5
|
||||
spawn_interval: 1.5
|
||||
enemy_distribution:
|
||||
pentagon: 35
|
||||
quadrat: 35
|
||||
molinillo: 30
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.2
|
||||
rotation_multiplier: 1.25
|
||||
tracking_strength: 0.7
|
||||
|
||||
# STAGE 6: Molinillo becomes dominant
|
||||
- stage_id: 6
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.3
|
||||
spawn_interval: 1.3
|
||||
enemy_distribution:
|
||||
pentagon: 30
|
||||
quadrat: 30
|
||||
molinillo: 40
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.3
|
||||
rotation_multiplier: 1.4
|
||||
tracking_strength: 0.8
|
||||
|
||||
# STAGE 7: High intensity, fast spawns
|
||||
- stage_id: 7
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.2
|
||||
spawn_interval: 1.0
|
||||
enemy_distribution:
|
||||
pentagon: 25
|
||||
quadrat: 30
|
||||
molinillo: 45
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.4
|
||||
rotation_multiplier: 1.5
|
||||
tracking_strength: 0.9
|
||||
|
||||
# STAGE 8: Expert level, 50% molinillos
|
||||
- stage_id: 8
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.1
|
||||
spawn_interval: 0.8
|
||||
enemy_distribution:
|
||||
pentagon: 20
|
||||
quadrat: 30
|
||||
molinillo: 50
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.5
|
||||
rotation_multiplier: 1.6
|
||||
tracking_strength: 1.0
|
||||
|
||||
# STAGE 9: Near-maximum difficulty
|
||||
- stage_id: 9
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.0
|
||||
spawn_interval: 0.6
|
||||
enemy_distribution:
|
||||
pentagon: 15
|
||||
quadrat: 25
|
||||
molinillo: 60
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.6
|
||||
rotation_multiplier: 1.7
|
||||
tracking_strength: 1.1
|
||||
|
||||
# STAGE 10: Final challenge, 70% molinillos
|
||||
- stage_id: 10
|
||||
total_enemies: 15
|
||||
spawn_config:
|
||||
mode: "progressive"
|
||||
initial_delay: 0.0
|
||||
spawn_interval: 0.5
|
||||
enemy_distribution:
|
||||
pentagon: 10
|
||||
quadrat: 20
|
||||
molinillo: 70
|
||||
difficulty_multipliers:
|
||||
speed_multiplier: 1.8
|
||||
rotation_multiplier: 2.0
|
||||
tracking_strength: 1.2
|
||||
BIN
data/voices/good_job_commander.wav
Normal file
BIN
data/voices/good_job_commander.wav
Normal file
Binary file not shown.
@@ -1,15 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg width="100%" height="100%" viewBox="0 0 7875 4016" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
|
||||
<g transform="matrix(0.792849,0,0,0.792849,84.327,350.707)">
|
||||
<path d="M896,1693L896,1531.23L1219.53,1531.23L1219.53,560.632L1543.07,560.632L1543.07,1531.23L1381.3,1531.23L1381.3,1693L896,1693Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M2028.37,1369.47L2028.37,1693L1704.83,1693L1704.83,722.399L1866.6,722.399L1866.6,560.632L2351.9,560.632L2351.9,722.399L2513.67,722.399L2513.67,1693L2190.14,1693L2190.14,1369.47L2028.37,1369.47ZM2028.37,722.399L2028.37,1207.7L2190.14,1207.7L2190.14,722.399L2028.37,722.399Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<rect x="2675.44" y="560.632" width="323.534" height="1132.37" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M3160.74,560.632L3484.27,560.632L3484.27,1531.23L3807.8,1531.23L3807.8,1693L3160.74,1693L3160.74,560.632Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M4131.34,560.632L4616.64,560.632L4616.64,722.399L4293.1,722.399L4293.1,1531.23L4454.87,1531.23L4454.87,1045.93L4778.4,1045.93L4778.4,1693L4131.34,1693L4131.34,1531.23L3969.57,1531.23L3969.57,722.399L4131.34,722.399L4131.34,560.632Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M5263.71,1369.47L5263.71,1693L4940.17,1693L4940.17,722.399L5101.94,722.399L5101.94,560.632L5587.24,560.632L5587.24,722.399L5749.01,722.399L5749.01,1693L5425.47,1693L5425.47,1369.47L5263.71,1369.47ZM5263.71,722.399L5263.71,1207.7L5425.47,1207.7L5425.47,722.399L5263.71,722.399Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M6719.61,1207.7L6557.84,1207.7L6557.84,1369.47L6396.07,1369.47L6396.07,1207.7L6234.31,1207.7L6234.31,1693L5910.77,1693L5910.77,560.632L6072.54,560.632L6072.54,722.399L6234.31,722.399L6234.31,884.166L6396.07,884.166L6396.07,1045.93L6557.84,1045.93L6557.84,884.166L6719.61,884.166L6719.61,722.399L6881.37,722.399L6881.37,560.632L7043.14,560.632L7043.14,1693L6719.61,1693L6719.61,1207.7Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M7851.98,884.166L7851.98,1045.93L7528.44,1045.93L7528.44,1531.23L8013.74,1531.23L8013.74,1693L7204.91,1693L7204.91,560.632L8013.74,560.632L8013.74,722.399L7528.44,722.399L7528.44,884.166L7851.98,884.166Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
<path d="M8175.51,1531.23L8499.04,1531.23L8499.04,1207.7L8337.28,1207.7L8337.28,1045.93L8175.51,1045.93L8175.51,722.399L8337.28,722.399L8337.28,560.632L8822.58,560.632L8822.58,722.399L8499.04,722.399L8499.04,1045.93L8660.81,1045.93L8660.81,1207.7L8822.58,1207.7L8822.58,1531.23L8660.81,1531.23L8660.81,1693L8175.51,1693L8175.51,1531.23Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 3.2 KiB |
Binary file not shown.
BIN
release/icon.ico
BIN
release/icon.ico
Binary file not shown.
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 111 KiB |
BIN
release/icon.png
BIN
release/icon.png
Binary file not shown.
|
Before Width: | Height: | Size: 402 KiB After Width: | Height: | Size: 174 KiB |
BIN
sample.png
BIN
sample.png
Binary file not shown.
|
Before Width: | Height: | Size: 36 KiB |
183
source/core/audio/audio.cpp
Normal file
183
source/core/audio/audio.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "audio.hpp"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_LogInfo, SDL_LogCategory, SDL_G...
|
||||
|
||||
#include <algorithm> // Para clamp
|
||||
#include <iostream> // Para std::cout
|
||||
|
||||
// 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"
|
||||
// clang-format on
|
||||
|
||||
#include "core/audio/audio_cache.hpp" // Para AudioCache
|
||||
#include "core/audio/jail_audio.hpp" // Para JA_FadeOutMusic, JA_Init, JA_PauseM...
|
||||
#include "game/options.hpp" // Para AudioOptions, audio, MusicOptions
|
||||
|
||||
// Singleton
|
||||
Audio* Audio::instance = nullptr;
|
||||
|
||||
// Inicializa la instancia única del singleton
|
||||
void Audio::init() { Audio::instance = new Audio(); }
|
||||
|
||||
// Libera la instancia
|
||||
void Audio::destroy() { delete Audio::instance; }
|
||||
|
||||
// Obtiene la instancia
|
||||
auto Audio::get() -> Audio* { return Audio::instance; }
|
||||
|
||||
// Constructor
|
||||
Audio::Audio() { initSDLAudio(); }
|
||||
|
||||
// Destructor
|
||||
Audio::~Audio() {
|
||||
JA_Quit();
|
||||
}
|
||||
|
||||
// Método principal
|
||||
void Audio::update() {
|
||||
JA_Update();
|
||||
}
|
||||
|
||||
// Reproduce la música
|
||||
void Audio::playMusic(const std::string& name, const int loop) {
|
||||
bool new_loop = (loop != 0);
|
||||
|
||||
// Si ya está sonando exactamente la misma pista y mismo modo loop, no hacemos nada
|
||||
if (music_.state == MusicState::PLAYING && music_.name == name && music_.loop == new_loop) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Intentar obtener recurso; si falla, no tocar estado
|
||||
auto* resource = AudioCache::getMusic(name);
|
||||
if (resource == nullptr) {
|
||||
// manejo de error opcional
|
||||
return;
|
||||
}
|
||||
|
||||
// Si hay algo reproduciéndose, detenerlo primero (si el backend lo requiere)
|
||||
if (music_.state == MusicState::PLAYING) {
|
||||
JA_StopMusic(); // sustituir por la función de stop real del API si tiene otro nombre
|
||||
}
|
||||
|
||||
// Llamada al motor para reproducir la nueva pista
|
||||
JA_PlayMusic(resource, loop);
|
||||
|
||||
// Actualizar estado y metadatos después de iniciar con éxito
|
||||
music_.name = name;
|
||||
music_.loop = new_loop;
|
||||
music_.state = MusicState::PLAYING;
|
||||
}
|
||||
|
||||
// Pausa la música
|
||||
void Audio::pauseMusic() {
|
||||
if (music_enabled_ && music_.state == MusicState::PLAYING) {
|
||||
JA_PauseMusic();
|
||||
music_.state = MusicState::PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
// Continua la música pausada
|
||||
void Audio::resumeMusic() {
|
||||
if (music_enabled_ && music_.state == MusicState::PAUSED) {
|
||||
JA_ResumeMusic();
|
||||
music_.state = MusicState::PLAYING;
|
||||
}
|
||||
}
|
||||
|
||||
// Detiene la música
|
||||
void Audio::stopMusic() {
|
||||
if (music_enabled_) {
|
||||
JA_StopMusic();
|
||||
music_.state = MusicState::STOPPED;
|
||||
}
|
||||
}
|
||||
|
||||
// Reproduce un sonido por nombre
|
||||
void Audio::playSound(const std::string& name, Group group) const {
|
||||
if (sound_enabled_) {
|
||||
JA_PlaySound(AudioCache::getSound(name), 0, static_cast<int>(group));
|
||||
}
|
||||
}
|
||||
|
||||
// Reproduce un sonido por puntero directo
|
||||
void Audio::playSound(JA_Sound_t* sound, Group group) const {
|
||||
if (sound_enabled_) {
|
||||
JA_PlaySound(sound, 0, static_cast<int>(group));
|
||||
}
|
||||
}
|
||||
|
||||
// Detiene todos los sonidos
|
||||
void Audio::stopAllSounds() const {
|
||||
if (sound_enabled_) {
|
||||
JA_StopChannel(-1);
|
||||
}
|
||||
}
|
||||
|
||||
// Realiza un fundido de salida de la música
|
||||
void Audio::fadeOutMusic(int milliseconds) const {
|
||||
if (music_enabled_ && getRealMusicState() == MusicState::PLAYING) {
|
||||
JA_FadeOutMusic(milliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
// Consulta directamente el estado real de la música en jailaudio
|
||||
auto Audio::getRealMusicState() -> MusicState {
|
||||
JA_Music_state ja_state = JA_GetMusicState();
|
||||
switch (ja_state) {
|
||||
case JA_MUSIC_PLAYING:
|
||||
return MusicState::PLAYING;
|
||||
case JA_MUSIC_PAUSED:
|
||||
return MusicState::PAUSED;
|
||||
case JA_MUSIC_STOPPED:
|
||||
case JA_MUSIC_INVALID:
|
||||
case JA_MUSIC_DISABLED:
|
||||
default:
|
||||
return MusicState::STOPPED;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el volumen de los sonidos
|
||||
void Audio::setSoundVolume(float sound_volume, Group group) const {
|
||||
if (sound_enabled_) {
|
||||
sound_volume = std::clamp(sound_volume, MIN_VOLUME, MAX_VOLUME);
|
||||
const float CONVERTED_VOLUME = sound_volume * Options::audio.volume;
|
||||
JA_SetSoundVolume(CONVERTED_VOLUME, static_cast<int>(group));
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el volumen de la música
|
||||
void Audio::setMusicVolume(float music_volume) const {
|
||||
if (music_enabled_) {
|
||||
music_volume = std::clamp(music_volume, MIN_VOLUME, MAX_VOLUME);
|
||||
const float CONVERTED_VOLUME = music_volume * Options::audio.volume;
|
||||
JA_SetMusicVolume(CONVERTED_VOLUME);
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica la configuración
|
||||
void Audio::applySettings() {
|
||||
enable(Options::audio.enabled);
|
||||
}
|
||||
|
||||
// Establecer estado general
|
||||
void Audio::enable(bool value) {
|
||||
enabled_ = value;
|
||||
|
||||
setSoundVolume(enabled_ ? Options::audio.sound.volume : MIN_VOLUME);
|
||||
setMusicVolume(enabled_ ? Options::audio.music.volume : MIN_VOLUME);
|
||||
}
|
||||
|
||||
// Inicializa SDL Audio
|
||||
void Audio::initSDLAudio() {
|
||||
if (!SDL_Init(SDL_INIT_AUDIO)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_AUDIO could not initialize! SDL Error: %s", SDL_GetError());
|
||||
} else {
|
||||
JA_Init(FREQUENCY, SDL_AUDIO_S16LE, 2);
|
||||
enable(Options::audio.enabled);
|
||||
|
||||
std::cout << "\n** AUDIO SYSTEM **\n";
|
||||
std::cout << "Audio system initialized successfully\n";
|
||||
}
|
||||
}
|
||||
97
source/core/audio/audio.hpp
Normal file
97
source/core/audio/audio.hpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include <string> // Para string
|
||||
#include <utility> // Para move
|
||||
|
||||
// --- Clase Audio: gestor de audio (singleton) ---
|
||||
class Audio {
|
||||
public:
|
||||
// --- Enums ---
|
||||
enum class Group : int {
|
||||
ALL = -1, // Todos los grupos
|
||||
GAME = 0, // Sonidos del juego
|
||||
INTERFACE = 1 // Sonidos de la interfaz
|
||||
};
|
||||
|
||||
enum class MusicState {
|
||||
PLAYING, // Reproduciendo música
|
||||
PAUSED, // Música pausada
|
||||
STOPPED, // Música detenida
|
||||
};
|
||||
|
||||
// --- Constantes ---
|
||||
static constexpr float MAX_VOLUME = 1.0F; // Volumen máximo
|
||||
static constexpr float MIN_VOLUME = 0.0F; // Volumen mínimo
|
||||
static constexpr int FREQUENCY = 48000; // Frecuencia de audio
|
||||
|
||||
// --- Singleton ---
|
||||
static void init(); // Inicializa el objeto Audio
|
||||
static void destroy(); // Libera el objeto Audio
|
||||
static auto get() -> Audio*; // Obtiene el puntero al objeto Audio
|
||||
Audio(const Audio&) = delete; // Evitar copia
|
||||
auto operator=(const Audio&) -> Audio& = delete; // Evitar asignación
|
||||
|
||||
static void update(); // Actualización del sistema de audio
|
||||
|
||||
// --- Control de música ---
|
||||
void playMusic(const std::string& name, int loop = -1); // Reproducir música en bucle
|
||||
void pauseMusic(); // Pausar reproducción de música
|
||||
void resumeMusic(); // Continua la música pausada
|
||||
void stopMusic(); // Detener completamente la música
|
||||
void fadeOutMusic(int milliseconds) const; // Fundido de salida de la música
|
||||
|
||||
// --- Control de sonidos ---
|
||||
void playSound(const std::string& name, Group group = Group::GAME) const; // Reproducir sonido puntual por nombre
|
||||
void playSound(struct JA_Sound_t* sound, Group group = Group::GAME) const; // Reproducir sonido puntual por puntero
|
||||
void stopAllSounds() const; // Detener todos los sonidos
|
||||
|
||||
// --- Control de volumen ---
|
||||
void setSoundVolume(float volume, Group group = Group::ALL) const; // Ajustar volumen de efectos
|
||||
void setMusicVolume(float volume) const; // Ajustar volumen de música
|
||||
|
||||
// --- Configuración general ---
|
||||
void enable(bool value); // Establecer estado general
|
||||
void toggleEnabled() { enabled_ = !enabled_; } // Alternar estado general
|
||||
void applySettings(); // Aplica la configuración
|
||||
|
||||
// --- Configuración de sonidos ---
|
||||
void enableSound() { sound_enabled_ = true; } // Habilitar sonidos
|
||||
void disableSound() { sound_enabled_ = false; } // Deshabilitar sonidos
|
||||
void enableSound(bool value) { sound_enabled_ = value; } // Establecer estado de sonidos
|
||||
void toggleSound() { sound_enabled_ = !sound_enabled_; } // Alternar estado de sonidos
|
||||
|
||||
// --- Configuración de música ---
|
||||
void enableMusic() { music_enabled_ = true; } // Habilitar música
|
||||
void disableMusic() { music_enabled_ = false; } // Deshabilitar música
|
||||
void enableMusic(bool value) { music_enabled_ = value; } // Establecer estado de música
|
||||
void toggleMusic() { music_enabled_ = !music_enabled_; } // Alternar estado de música
|
||||
|
||||
// --- Consultas de estado ---
|
||||
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }
|
||||
[[nodiscard]] auto isSoundEnabled() const -> bool { return sound_enabled_; }
|
||||
[[nodiscard]] auto isMusicEnabled() const -> bool { return music_enabled_; }
|
||||
[[nodiscard]] auto getMusicState() const -> MusicState { return music_.state; }
|
||||
[[nodiscard]] static auto getRealMusicState() -> MusicState;
|
||||
[[nodiscard]] auto getCurrentMusicName() const -> const std::string& { return music_.name; }
|
||||
|
||||
private:
|
||||
// --- Tipos anidados ---
|
||||
struct Music {
|
||||
MusicState state{MusicState::STOPPED}; // Estado actual de la música
|
||||
std::string name; // Última pista de música reproducida
|
||||
bool loop{false}; // Indica si se reproduce en bucle
|
||||
};
|
||||
|
||||
// --- Métodos ---
|
||||
Audio(); // Constructor privado
|
||||
~Audio(); // Destructor privado
|
||||
void initSDLAudio(); // Inicializa SDL Audio
|
||||
|
||||
// --- Variables miembro ---
|
||||
static Audio* instance; // Instancia única de Audio
|
||||
|
||||
Music music_; // Estado de la música
|
||||
bool enabled_{true}; // Estado general del audio
|
||||
bool sound_enabled_{true}; // Estado de los efectos de sonido
|
||||
bool music_enabled_{true}; // Estado de la música
|
||||
};
|
||||
142
source/core/audio/audio_cache.cpp
Normal file
142
source/core/audio/audio_cache.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
// audio_cache.cpp - Implementació del caché de sons i música
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/audio/audio_cache.hpp"
|
||||
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// Inicialització de variables estàtiques
|
||||
std::unordered_map<std::string, JA_Sound_t*> AudioCache::sounds_;
|
||||
std::unordered_map<std::string, JA_Music_t*> AudioCache::musics_;
|
||||
std::string AudioCache::sounds_base_path_ = "data/sounds/";
|
||||
std::string AudioCache::music_base_path_ = "data/music/";
|
||||
|
||||
JA_Sound_t* AudioCache::getSound(const std::string& name) {
|
||||
// Cache hit
|
||||
auto it = sounds_.find(name);
|
||||
if (it != sounds_.end()) {
|
||||
std::cout << "[AudioCache] Sound cache hit: " << name << std::endl;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Normalize path: "laser_shoot.wav" → "sounds/laser_shoot.wav"
|
||||
std::string normalized = name;
|
||||
if (normalized.find("sounds/") != 0 && normalized.find('/') == std::string::npos) {
|
||||
normalized = "sounds/" + normalized;
|
||||
}
|
||||
|
||||
// Load from resource system
|
||||
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||
if (data.empty()) {
|
||||
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << normalized << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Load sound from memory
|
||||
JA_Sound_t* sound = JA_LoadSound(data.data(), static_cast<uint32_t>(data.size()));
|
||||
if (sound == nullptr) {
|
||||
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
|
||||
<< std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::cout << "[AudioCache] Sound loaded: " << normalized << std::endl;
|
||||
sounds_[name] = sound;
|
||||
return sound;
|
||||
}
|
||||
|
||||
JA_Music_t* AudioCache::getMusic(const std::string& name) {
|
||||
// Cache hit
|
||||
auto it = musics_.find(name);
|
||||
if (it != musics_.end()) {
|
||||
std::cout << "[AudioCache] Music cache hit: " << name << std::endl;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Normalize path: "title.ogg" → "music/title.ogg"
|
||||
std::string normalized = name;
|
||||
if (normalized.find("music/") != 0 && normalized.find('/') == std::string::npos) {
|
||||
normalized = "music/" + normalized;
|
||||
}
|
||||
|
||||
// Load from resource system
|
||||
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||
if (data.empty()) {
|
||||
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << normalized << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Load music from memory
|
||||
JA_Music_t* music = JA_LoadMusic(data.data(), static_cast<uint32_t>(data.size()));
|
||||
if (music == nullptr) {
|
||||
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
|
||||
<< std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::cout << "[AudioCache] Music loaded: " << normalized << std::endl;
|
||||
musics_[name] = music;
|
||||
return music;
|
||||
}
|
||||
|
||||
void AudioCache::clear() {
|
||||
std::cout << "[AudioCache] Clearing cache (" << sounds_.size() << " sounds, "
|
||||
<< musics_.size() << " music)" << std::endl;
|
||||
|
||||
// Liberar memoria de sonidos
|
||||
for (auto& [name, sound] : sounds_) {
|
||||
if (sound && sound->buffer) {
|
||||
SDL_free(sound->buffer);
|
||||
}
|
||||
delete sound;
|
||||
}
|
||||
sounds_.clear();
|
||||
|
||||
// Liberar memoria de música
|
||||
for (auto& [name, music] : musics_) {
|
||||
if (music && music->buffer) {
|
||||
SDL_free(music->buffer);
|
||||
}
|
||||
if (music && music->filename) {
|
||||
free(music->filename);
|
||||
}
|
||||
delete music;
|
||||
}
|
||||
musics_.clear();
|
||||
}
|
||||
|
||||
size_t AudioCache::getSoundCacheSize() { return sounds_.size(); }
|
||||
|
||||
size_t AudioCache::getMusicCacheSize() { return musics_.size(); }
|
||||
|
||||
std::string AudioCache::resolveSoundPath(const std::string& name) {
|
||||
// Si es un path absoluto (comienza con '/'), usarlo directamente
|
||||
if (!name.empty() && name[0] == '/') {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Si ya contiene el prefix base_path, usarlo directamente
|
||||
if (name.find(sounds_base_path_) == 0) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Caso contrario, añadir base_path
|
||||
return sounds_base_path_ + name;
|
||||
}
|
||||
|
||||
std::string AudioCache::resolveMusicPath(const std::string& name) {
|
||||
// Si es un path absoluto (comienza con '/'), usarlo directamente
|
||||
if (!name.empty() && name[0] == '/') {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Si ya contiene el prefix base_path, usarlo directamente
|
||||
if (name.find(music_base_path_) == 0) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Caso contrario, añadir base_path
|
||||
return music_base_path_ + name;
|
||||
}
|
||||
42
source/core/audio/audio_cache.hpp
Normal file
42
source/core/audio/audio_cache.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// audio_cache.hpp - Caché simplificado de sonidos y música
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/audio/jail_audio.hpp"
|
||||
|
||||
// Caché estático de sonidos y música
|
||||
// Patrón inspirado en Graphics::ShapeLoader
|
||||
class AudioCache {
|
||||
public:
|
||||
// No instanciable (todo estático)
|
||||
AudioCache() = delete;
|
||||
|
||||
// Obtener sonido (carga bajo demanda)
|
||||
// Retorna puntero (nullptr si error)
|
||||
static JA_Sound_t* getSound(const std::string& name);
|
||||
|
||||
// Obtener música (carga bajo demanda)
|
||||
// Retorna puntero (nullptr si error)
|
||||
static JA_Music_t* getMusic(const std::string& name);
|
||||
|
||||
// Limpiar caché (útil para debug/recarga)
|
||||
static void clear();
|
||||
|
||||
// Estadísticas (debug)
|
||||
static size_t getSoundCacheSize();
|
||||
static size_t getMusicCacheSize();
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, JA_Sound_t*> sounds_;
|
||||
static std::unordered_map<std::string, JA_Music_t*> musics_;
|
||||
static std::string sounds_base_path_; // "data/sounds/"
|
||||
static std::string music_base_path_; // "data/music/"
|
||||
|
||||
// Helpers privados
|
||||
static std::string resolveSoundPath(const std::string& name);
|
||||
static std::string resolveMusicPath(const std::string& name);
|
||||
};
|
||||
482
source/core/audio/jail_audio.hpp
Normal file
482
source/core/audio/jail_audio.hpp
Normal file
@@ -0,0 +1,482 @@
|
||||
#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
|
||||
|
||||
#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};
|
||||
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};
|
||||
};
|
||||
|
||||
// --- 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);
|
||||
|
||||
// --- 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));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) 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;
|
||||
}
|
||||
|
||||
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) {
|
||||
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 = static_cast<Uint8*>(SDL_malloc(music->length));
|
||||
SDL_memcpy(music->buffer, output, music->length);
|
||||
free(output);
|
||||
music->pos = 0;
|
||||
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) return; // Añadida comprobación de music
|
||||
|
||||
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 (!current_music->stream) { // Comprobar creación de stream
|
||||
SDL_Log("Failed to create audio stream!");
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
return;
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
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->pos = 0;
|
||||
current_music->state = JA_MUSIC_STOPPED;
|
||||
if (current_music->stream) {
|
||||
SDL_DestroyAudioStream(current_music->stream);
|
||||
current_music->stream = nullptr;
|
||||
}
|
||||
// No liberamos filename aquí, se debería liberar 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;
|
||||
}
|
||||
SDL_free(music->buffer);
|
||||
if (music->stream) SDL_DestroyAudioStream(music->stream);
|
||||
free(music->filename); // filename se 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) {
|
||||
if (!current_music) return;
|
||||
current_music->pos = value * current_music->spec.freq;
|
||||
// Nota: Esta implementación de 'pos' no parece usarse en JA_Update para
|
||||
// el streaming. El streaming siempre parece empezar desde el principio.
|
||||
}
|
||||
|
||||
inline float JA_GetMusicPosition() {
|
||||
if (!current_music) return 0;
|
||||
return float(current_music->pos) / float(current_music->spec.freq);
|
||||
// Nota: Ver `JA_SetMusicPosition`
|
||||
}
|
||||
|
||||
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)) {
|
||||
SDL_Log("Failed to load WAV from memory: %s", SDL_GetError());
|
||||
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)) {
|
||||
SDL_Log("Failed to load WAV file: %s", SDL_GetError());
|
||||
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) {
|
||||
SDL_Log("Failed to create audio stream for sound!");
|
||||
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;
|
||||
}
|
||||
282
source/core/defaults.hpp
Normal file
282
source/core/defaults.hpp
Normal file
@@ -0,0 +1,282 @@
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace Defaults {
|
||||
// Configuración de ventana
|
||||
namespace Window {
|
||||
constexpr int WIDTH = 640;
|
||||
constexpr int HEIGHT = 480;
|
||||
constexpr int MIN_WIDTH = 320; // Mínimo: mitad del original
|
||||
constexpr int MIN_HEIGHT = 240;
|
||||
// Zoom system
|
||||
constexpr float BASE_ZOOM = 1.0f; // 640x480 baseline
|
||||
constexpr float MIN_ZOOM = 0.5f; // 320x240 minimum
|
||||
constexpr float ZOOM_INCREMENT = 0.1f; // 10% steps (F1/F2)
|
||||
} // namespace Window
|
||||
|
||||
// Dimensions base del joc (coordenades lògiques)
|
||||
namespace Game {
|
||||
constexpr int WIDTH = 640;
|
||||
constexpr int HEIGHT = 480;
|
||||
} // namespace Game
|
||||
|
||||
// Zones del joc (SDL_FRect amb càlculs automàtics)
|
||||
namespace Zones {
|
||||
// --- CONFIGURACIÓ DE PORCENTATGES ---
|
||||
// Basats en valors originals 640x480
|
||||
// Ajusta estos valors per canviar proporcions
|
||||
|
||||
constexpr float PLAYAREA_MARGIN_HORIZONTAL_PERCENT = 10.0f / Game::WIDTH;
|
||||
constexpr float PLAYAREA_MARGIN_VERTICAL_PERCENT = 10.0f / Game::HEIGHT;
|
||||
constexpr float SCOREBOARD_HEIGHT_PERCENT = 48.0f / Game::HEIGHT;
|
||||
|
||||
// --- CÀLCULS AUTOMÀTICS ---
|
||||
// Estos valors es recalculen si canvien Game::WIDTH o Game::HEIGHT
|
||||
|
||||
constexpr float PLAYAREA_MARGIN_H =
|
||||
Game::WIDTH * PLAYAREA_MARGIN_HORIZONTAL_PERCENT;
|
||||
constexpr float PLAYAREA_MARGIN_V =
|
||||
Game::HEIGHT * PLAYAREA_MARGIN_VERTICAL_PERCENT;
|
||||
constexpr float SCOREBOARD_H = Game::HEIGHT * SCOREBOARD_HEIGHT_PERCENT;
|
||||
|
||||
// --- ZONES FINALS (SDL_FRect) ---
|
||||
|
||||
// Zona de joc principal
|
||||
// Ocupa: tot menys marges (dalt, esq, dret) i scoreboard (baix)
|
||||
constexpr SDL_FRect PLAYAREA = {
|
||||
PLAYAREA_MARGIN_H, // x = 10.0
|
||||
PLAYAREA_MARGIN_V, // y = 10.0
|
||||
Game::WIDTH - 2.0f * PLAYAREA_MARGIN_H, // width = 620.0
|
||||
Game::HEIGHT - PLAYAREA_MARGIN_V - SCOREBOARD_H // height = 406.0
|
||||
};
|
||||
|
||||
// Zona de marcador
|
||||
// Ocupa: tot l'ample, 64px d'alçada en la part inferior
|
||||
constexpr SDL_FRect SCOREBOARD = {
|
||||
0.0f, // x = 0.0
|
||||
Game::HEIGHT - SCOREBOARD_H, // y = 416.0
|
||||
static_cast<float>(Game::WIDTH), // width = 640.0
|
||||
SCOREBOARD_H // height = 64.0
|
||||
};
|
||||
} // namespace Zones
|
||||
|
||||
// Objetos del juego
|
||||
namespace Entities {
|
||||
constexpr int MAX_ORNIS = 15;
|
||||
constexpr int MAX_BALES = 3;
|
||||
constexpr int MAX_IPUNTS = 30;
|
||||
|
||||
constexpr float SHIP_RADIUS = 12.0f;
|
||||
constexpr float ENEMY_RADIUS = 20.0f;
|
||||
constexpr float BULLET_RADIUS = 3.0f;
|
||||
} // namespace Entities
|
||||
|
||||
// Game rules (lives, respawn, game over)
|
||||
namespace Game {
|
||||
constexpr int STARTING_LIVES = 3; // Initial lives
|
||||
constexpr float DEATH_DURATION = 3.0f; // Seconds of death animation
|
||||
constexpr float GAME_OVER_DURATION = 5.0f; // Seconds to display game over
|
||||
constexpr float COLLISION_SHIP_ENEMY_AMPLIFIER = 0.80f; // 80% hitbox (generous)
|
||||
// Transición LEVEL_START (mensajes aleatorios PRE-level)
|
||||
constexpr float LEVEL_START_DURATION = 3.0f; // Duración total
|
||||
constexpr float LEVEL_START_TYPING_RATIO = 0.3f; // 30% escribiendo, 70% mostrando
|
||||
|
||||
// Transición LEVEL_COMPLETED (mensaje "GOOD JOB COMMANDER!")
|
||||
constexpr float LEVEL_COMPLETED_DURATION = 3.0f; // Duración total
|
||||
constexpr float LEVEL_COMPLETED_TYPING_RATIO = 0.0f; // 0.0 = sin typewriter (directo)
|
||||
} // namespace Game
|
||||
|
||||
// Física (valores actuales del juego, sincronizados con joc_asteroides.cpp)
|
||||
namespace Physics {
|
||||
constexpr float ROTATION_SPEED = 3.14f; // rad/s (~180°/s)
|
||||
constexpr float ACCELERATION = 400.0f; // px/s²
|
||||
constexpr float MAX_VELOCITY = 120.0f; // px/s
|
||||
constexpr float FRICTION = 20.0f; // px/s²
|
||||
constexpr float ENEMY_SPEED = 2.0f; // unidades/frame
|
||||
constexpr float BULLET_SPEED = 6.0f; // unidades/frame
|
||||
constexpr float VELOCITY_SCALE = 20.0f; // factor conversión frame→tiempo
|
||||
|
||||
// Explosions (debris physics)
|
||||
namespace Debris {
|
||||
constexpr float VELOCITAT_BASE = 80.0f; // Velocitat inicial (px/s)
|
||||
constexpr float VARIACIO_VELOCITAT = 40.0f; // ±variació aleatòria (px/s)
|
||||
constexpr float ACCELERACIO = -60.0f; // Fricció/desacceleració (px/s²)
|
||||
constexpr float ROTACIO_MIN = 0.1f; // Rotació mínima (rad/s ~5.7°/s)
|
||||
constexpr float ROTACIO_MAX = 0.3f; // Rotació màxima (rad/s ~17.2°/s)
|
||||
constexpr float TEMPS_VIDA = 2.0f; // Duració màxima (segons) - enemy/bullet debris
|
||||
constexpr float TEMPS_VIDA_NAU = 3.0f; // Ship debris lifetime (matches DEATH_DURATION)
|
||||
constexpr float SHRINK_RATE = 0.5f; // Reducció de mida (factor/s)
|
||||
|
||||
// Herència de velocitat angular (trayectorias curvas)
|
||||
constexpr float FACTOR_HERENCIA_MIN = 0.7f; // Mínimo 70% del drotacio heredat
|
||||
constexpr float FACTOR_HERENCIA_MAX = 1.0f; // Màxim 100% del drotacio heredat
|
||||
constexpr float FRICCIO_ANGULAR = 0.5f; // Desacceleració angular (rad/s²)
|
||||
|
||||
// Angular velocity cap for trajectory inheritance
|
||||
// Excess above this threshold is converted to tangential linear velocity
|
||||
// Prevents "vortex trap" problem with high-rotation enemies
|
||||
constexpr float VELOCITAT_ROT_MAX = 1.5f; // rad/s (~86°/s)
|
||||
} // namespace Debris
|
||||
} // namespace Physics
|
||||
|
||||
// Matemáticas
|
||||
namespace Math {
|
||||
constexpr float PI = 3.14159265359f;
|
||||
} // namespace Math
|
||||
|
||||
// Colores (oscilación para efecto CRT)
|
||||
namespace Color {
|
||||
// Frecuencia de oscilación
|
||||
constexpr float FREQUENCY = 6.0f; // 1 Hz (1 ciclo/segundo)
|
||||
|
||||
// Color de líneas (efecto fósforo verde CRT)
|
||||
constexpr uint8_t LINE_MIN_R = 100; // Verde oscuro
|
||||
constexpr uint8_t LINE_MIN_G = 200;
|
||||
constexpr uint8_t LINE_MIN_B = 100;
|
||||
|
||||
constexpr uint8_t LINE_MAX_R = 100; // Verde brillante
|
||||
constexpr uint8_t LINE_MAX_G = 255;
|
||||
constexpr uint8_t LINE_MAX_B = 100;
|
||||
|
||||
// Color de fondo (pulso sutil verde oscuro)
|
||||
constexpr uint8_t BACKGROUND_MIN_R = 0; // Negro
|
||||
constexpr uint8_t BACKGROUND_MIN_G = 5;
|
||||
constexpr uint8_t BACKGROUND_MIN_B = 0;
|
||||
|
||||
constexpr uint8_t BACKGROUND_MAX_R = 0; // Verde muy oscuro
|
||||
constexpr uint8_t BACKGROUND_MAX_G = 15;
|
||||
constexpr uint8_t BACKGROUND_MAX_B = 0;
|
||||
} // namespace Color
|
||||
|
||||
// Brillantor (control de intensitat per cada tipus d'entitat)
|
||||
namespace Brightness {
|
||||
// Brillantor estàtica per entitats de joc (0.0-1.0)
|
||||
constexpr float NAU = 1.0f; // Màxima visibilitat (jugador)
|
||||
constexpr float ENEMIC = 0.7f; // 30% més tènue (destaca menys)
|
||||
constexpr float BALA = 1.0f; // Brillo a tope (màxima visibilitat)
|
||||
|
||||
// Starfield: gradient segons distància al centre
|
||||
// distancia_centre: 0.0 (centre) → 1.0 (vora pantalla)
|
||||
// brightness = MIN + (MAX - MIN) * distancia_centre
|
||||
constexpr float STARFIELD_MIN = 0.3f; // Estrelles llunyanes (prop del centre)
|
||||
constexpr float STARFIELD_MAX = 0.8f; // Estrelles properes (vora pantalla)
|
||||
} // namespace Brightness
|
||||
|
||||
// Renderització (V-Sync i altres opcions de render)
|
||||
namespace Rendering {
|
||||
constexpr int VSYNC_DEFAULT = 1; // 0=disabled, 1=enabled
|
||||
} // namespace Rendering
|
||||
|
||||
// Audio (sistema de so i música)
|
||||
namespace Audio {
|
||||
constexpr float VOLUME = 1.0F; // Volumen maestro (0.0 a 1.0)
|
||||
constexpr bool ENABLED = true; // Audio habilitado por defecto
|
||||
} // namespace Audio
|
||||
|
||||
// Música (pistas de fondo)
|
||||
namespace Music {
|
||||
constexpr float VOLUME = 0.8F; // Volumen música
|
||||
constexpr bool ENABLED = true; // Música habilitada
|
||||
constexpr const char* GAME_TRACK = "game.ogg"; // Pista de juego
|
||||
constexpr int FADE_DURATION_MS = 1000; // Fade out duration
|
||||
} // namespace Music
|
||||
|
||||
// Efectes de so (sons puntuals)
|
||||
namespace Sound {
|
||||
constexpr float VOLUME = 1.0F; // Volumen efectos
|
||||
constexpr bool ENABLED = true; // Sonidos habilitados
|
||||
constexpr const char* EXPLOSION = "explosion.wav"; // Explosión
|
||||
constexpr const char* LASER = "laser_shoot.wav"; // Disparo
|
||||
constexpr const char* GOOD_JOB_COMMANDER = "good_job_commander.wav"; // Voz: "Good job, commander"
|
||||
} // namespace Sound
|
||||
|
||||
// Enemy type configuration (tipus d'enemics)
|
||||
namespace Enemies {
|
||||
// Pentagon (esquivador - zigzag evasion)
|
||||
namespace Pentagon {
|
||||
constexpr float VELOCITAT = 35.0f; // px/s (slightly slower)
|
||||
constexpr float CANVI_ANGLE_PROB = 0.20f; // 20% per wall hit (frequent zigzag)
|
||||
constexpr float CANVI_ANGLE_MAX = 1.0f; // Max random angle change (rad)
|
||||
constexpr float DROTACIO_MIN = 0.75f; // Min visual rotation (rad/s) [+50%]
|
||||
constexpr float DROTACIO_MAX = 3.75f; // Max visual rotation (rad/s) [+50%]
|
||||
constexpr const char* SHAPE_FILE = "enemy_pentagon.shp";
|
||||
} // namespace Pentagon
|
||||
|
||||
// Quadrat (perseguidor - tracks player)
|
||||
namespace Quadrat {
|
||||
constexpr float VELOCITAT = 40.0f; // px/s (medium speed)
|
||||
constexpr float TRACKING_STRENGTH = 0.5f; // Interpolation toward player (0.0-1.0)
|
||||
constexpr float TRACKING_INTERVAL = 1.0f; // Seconds between angle updates
|
||||
constexpr float DROTACIO_MIN = 0.3f; // Slow rotation [+50%]
|
||||
constexpr float DROTACIO_MAX = 1.5f; // [+50%]
|
||||
constexpr const char* SHAPE_FILE = "enemy_square.shp";
|
||||
} // namespace Quadrat
|
||||
|
||||
// Molinillo (agressiu - fast straight lines, proximity spin-up)
|
||||
namespace Molinillo {
|
||||
constexpr float VELOCITAT = 50.0f; // px/s (fastest)
|
||||
constexpr float CANVI_ANGLE_PROB = 0.05f; // 5% per wall hit (rare direction change)
|
||||
constexpr float CANVI_ANGLE_MAX = 0.3f; // Small angle adjustments
|
||||
constexpr float DROTACIO_MIN = 3.0f; // Base rotation (rad/s) [+50%]
|
||||
constexpr float DROTACIO_MAX = 6.0f; // [+50%]
|
||||
constexpr float DROTACIO_PROXIMITY_MULTIPLIER = 3.0f; // Spin-up multiplier when near ship
|
||||
constexpr float PROXIMITY_DISTANCE = 100.0f; // Distance threshold (px)
|
||||
constexpr const char* SHAPE_FILE = "enemy_pinwheel.shp";
|
||||
} // namespace Molinillo
|
||||
|
||||
// Animation parameters (shared)
|
||||
namespace Animation {
|
||||
// Palpitation
|
||||
constexpr float PALPITACIO_TRIGGER_PROB = 0.01f; // 1% chance per second
|
||||
constexpr float PALPITACIO_DURACIO_MIN = 1.0f; // Min duration (seconds)
|
||||
constexpr float PALPITACIO_DURACIO_MAX = 3.0f; // Max duration (seconds)
|
||||
constexpr float PALPITACIO_AMPLITUD_MIN = 0.08f; // Min scale variation
|
||||
constexpr float PALPITACIO_AMPLITUD_MAX = 0.20f; // Max scale variation
|
||||
constexpr float PALPITACIO_FREQ_MIN = 1.5f; // Min frequency (Hz)
|
||||
constexpr float PALPITACIO_FREQ_MAX = 3.0f; // Max frequency (Hz)
|
||||
|
||||
// Rotation acceleration
|
||||
constexpr float ROTACIO_ACCEL_TRIGGER_PROB = 0.02f; // 2% chance per second [4x more frequent]
|
||||
constexpr float ROTACIO_ACCEL_DURACIO_MIN = 3.0f; // Min transition time
|
||||
constexpr float ROTACIO_ACCEL_DURACIO_MAX = 8.0f; // Max transition time
|
||||
constexpr float ROTACIO_ACCEL_MULTIPLIER_MIN = 0.3f; // Min speed multiplier [more dramatic]
|
||||
constexpr float ROTACIO_ACCEL_MULTIPLIER_MAX = 4.0f; // Max speed multiplier [more dramatic]
|
||||
} // namespace Animation
|
||||
|
||||
// Spawn safety and invulnerability system
|
||||
namespace Spawn {
|
||||
// Safe spawn distance from player
|
||||
constexpr float SAFETY_DISTANCE_MULTIPLIER = 3.0f; // 3x ship radius
|
||||
constexpr float SAFETY_DISTANCE = Defaults::Entities::SHIP_RADIUS * SAFETY_DISTANCE_MULTIPLIER; // 36.0f px
|
||||
constexpr int MAX_SPAWN_ATTEMPTS = 50; // Max attempts to find safe position
|
||||
|
||||
// Invulnerability system
|
||||
constexpr float INVULNERABILITY_DURATION = 3.0f; // Seconds
|
||||
constexpr float INVULNERABILITY_BRIGHTNESS_START = 0.3f; // Dim
|
||||
constexpr float INVULNERABILITY_BRIGHTNESS_END = 0.7f; // Normal (same as Defaults::Brightness::ENEMIC)
|
||||
constexpr float INVULNERABILITY_SCALE_START = 0.0f; // Invisible
|
||||
constexpr float INVULNERABILITY_SCALE_END = 1.0f; // Full size
|
||||
} // namespace Spawn
|
||||
|
||||
// Scoring system (puntuació per tipus d'enemic)
|
||||
namespace Scoring {
|
||||
constexpr int PENTAGON_SCORE = 100; // Pentàgon (esquivador, 35 px/s)
|
||||
constexpr int QUADRAT_SCORE = 150; // Quadrat (perseguidor, 40 px/s)
|
||||
constexpr int MOLINILLO_SCORE = 200; // Molinillo (agressiu, 50 px/s)
|
||||
} // namespace Scoring
|
||||
|
||||
} // namespace Enemies
|
||||
|
||||
// Floating score numbers (números flotants de puntuació)
|
||||
namespace FloatingScore {
|
||||
constexpr float LIFETIME = 2.0f; // Duració màxima (segons)
|
||||
constexpr float VELOCITY_Y = -30.0f; // Velocitat vertical (px/s, negatiu = amunt)
|
||||
constexpr float VELOCITY_X = 0.0f; // Velocitat horizontal (px/s)
|
||||
constexpr float SCALE = 0.75f; // Escala del text (0.75 = 75% del marcador)
|
||||
constexpr float SPACING = 0.0f; // Espaiat entre caràcters
|
||||
constexpr int MAX_CONCURRENT = 15; // Pool size (= MAX_ORNIS)
|
||||
} // namespace FloatingScore
|
||||
|
||||
} // namespace Defaults
|
||||
155
source/core/graphics/shape.cpp
Normal file
155
source/core/graphics/shape.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
// shape.cpp - Implementació del sistema de formes vectorials
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
Shape::Shape(const std::string& filepath)
|
||||
: centre_({0.0f, 0.0f}),
|
||||
escala_defecte_(1.0f),
|
||||
nom_("unnamed") {
|
||||
carregar(filepath);
|
||||
}
|
||||
|
||||
bool Shape::carregar(const std::string& filepath) {
|
||||
// Llegir fitxer
|
||||
std::ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[Shape] Error: no es pot obrir " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Llegir tot el contingut
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
std::string contingut = buffer.str();
|
||||
file.close();
|
||||
|
||||
// Parsejar
|
||||
return parsejar_fitxer(contingut);
|
||||
}
|
||||
|
||||
bool Shape::parsejar_fitxer(const std::string& contingut) {
|
||||
std::istringstream iss(contingut);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
// Trim whitespace
|
||||
line = trim(line);
|
||||
|
||||
// Skip comments and blanks
|
||||
if (line.empty() || line[0] == '#')
|
||||
continue;
|
||||
|
||||
// Parse command
|
||||
if (starts_with(line, "name:")) {
|
||||
nom_ = trim(extract_value(line));
|
||||
} else if (starts_with(line, "scale:")) {
|
||||
try {
|
||||
escala_defecte_ = std::stof(extract_value(line));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: escala invàlida, usant 1.0" << std::endl;
|
||||
escala_defecte_ = 1.0f;
|
||||
}
|
||||
} else if (starts_with(line, "center:")) {
|
||||
parse_center(extract_value(line));
|
||||
} else if (starts_with(line, "polyline:")) {
|
||||
auto points = parse_points(extract_value(line));
|
||||
if (points.size() >= 2) {
|
||||
primitives_.push_back({PrimitiveType::POLYLINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: polyline amb menys de 2 punts ignorada"
|
||||
<< std::endl;
|
||||
}
|
||||
} else if (starts_with(line, "line:")) {
|
||||
auto points = parse_points(extract_value(line));
|
||||
if (points.size() == 2) {
|
||||
primitives_.push_back({PrimitiveType::LINE, points});
|
||||
} else {
|
||||
std::cerr << "[Shape] Warning: line ha de tenir exactament 2 punts"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
// Comandes desconegudes ignorades silenciosament
|
||||
}
|
||||
|
||||
if (primitives_.empty()) {
|
||||
std::cerr << "[Shape] Error: cap primitiva carregada" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper: trim whitespace
|
||||
std::string Shape::trim(const std::string& str) const {
|
||||
const char* whitespace = " \t\n\r";
|
||||
size_t start = str.find_first_not_of(whitespace);
|
||||
if (start == std::string::npos)
|
||||
return "";
|
||||
|
||||
size_t end = str.find_last_not_of(whitespace);
|
||||
return str.substr(start, end - start + 1);
|
||||
}
|
||||
|
||||
// Helper: starts_with
|
||||
bool Shape::starts_with(const std::string& str,
|
||||
const std::string& prefix) const {
|
||||
if (str.length() < prefix.length())
|
||||
return false;
|
||||
return str.compare(0, prefix.length(), prefix) == 0;
|
||||
}
|
||||
|
||||
// Helper: extract value after ':'
|
||||
std::string Shape::extract_value(const std::string& line) const {
|
||||
size_t colon = line.find(':');
|
||||
if (colon == std::string::npos)
|
||||
return "";
|
||||
return line.substr(colon + 1);
|
||||
}
|
||||
|
||||
// Helper: parse center "x, y"
|
||||
void Shape::parse_center(const std::string& value) {
|
||||
std::string val = trim(value);
|
||||
size_t comma = val.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
try {
|
||||
centre_.x = std::stof(trim(val.substr(0, comma)));
|
||||
centre_.y = std::stof(trim(val.substr(comma + 1)));
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: centre invàlid, usant (0,0)" << std::endl;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: parse points "x1,y1 x2,y2 x3,y3"
|
||||
std::vector<Punt> Shape::parse_points(const std::string& str) const {
|
||||
std::vector<Punt> points;
|
||||
std::istringstream iss(trim(str));
|
||||
std::string pair;
|
||||
|
||||
while (iss >> pair) { // Whitespace-separated
|
||||
size_t comma = pair.find(',');
|
||||
if (comma != std::string::npos) {
|
||||
try {
|
||||
float x = std::stof(pair.substr(0, comma));
|
||||
float y = std::stof(pair.substr(comma + 1));
|
||||
points.push_back({x, y});
|
||||
} catch (...) {
|
||||
std::cerr << "[Shape] Warning: punt invàlid ignorat: " << pair
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
} // namespace Graphics
|
||||
64
source/core/graphics/shape.hpp
Normal file
64
source/core/graphics/shape.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// shape.hpp - Sistema de formes vectorials
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Tipus de primitiva dins d'una forma
|
||||
enum class PrimitiveType {
|
||||
POLYLINE, // Seqüència de punts connectats
|
||||
LINE // Línia individual (2 punts)
|
||||
};
|
||||
|
||||
// Primitiva individual (polyline o line)
|
||||
struct ShapePrimitive {
|
||||
PrimitiveType type;
|
||||
std::vector<Punt> points; // 2+ punts per polyline, exactament 2 per line
|
||||
};
|
||||
|
||||
// Classe Shape - representa una forma vectorial carregada des de .shp
|
||||
class Shape {
|
||||
public:
|
||||
// Constructors
|
||||
Shape() = default;
|
||||
explicit Shape(const std::string& filepath);
|
||||
|
||||
// Carregar forma des de fitxer .shp
|
||||
bool carregar(const std::string& filepath);
|
||||
|
||||
// Parsejar forma des de buffer de memòria (per al sistema de recursos)
|
||||
bool parsejar_fitxer(const std::string& contingut);
|
||||
|
||||
// Getters
|
||||
const std::vector<ShapePrimitive>& get_primitives() const {
|
||||
return primitives_;
|
||||
}
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
float get_escala_defecte() const { return escala_defecte_; }
|
||||
bool es_valida() const { return !primitives_.empty(); }
|
||||
|
||||
// Info de depuració
|
||||
std::string get_nom() const { return nom_; }
|
||||
size_t get_num_primitives() const { return primitives_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<ShapePrimitive> primitives_;
|
||||
Punt centre_; // Centre/origen de la forma
|
||||
float escala_defecte_; // Escala per defecte (normalment 1.0)
|
||||
std::string nom_; // Nom de la forma (per depuració)
|
||||
|
||||
// Helpers privats per parsejar
|
||||
std::string trim(const std::string& str) const;
|
||||
bool starts_with(const std::string& str, const std::string& prefix) const;
|
||||
std::string extract_value(const std::string& line) const;
|
||||
void parse_center(const std::string& value);
|
||||
std::vector<Punt> parse_points(const std::string& str) const;
|
||||
};
|
||||
|
||||
} // namespace Graphics
|
||||
86
source/core/graphics/shape_loader.cpp
Normal file
86
source/core/graphics/shape_loader.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
// shape_loader.cpp - Implementació del carregador amb caché
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Inicialització de variables estàtiques
|
||||
std::unordered_map<std::string, std::shared_ptr<Shape>> ShapeLoader::cache_;
|
||||
std::string ShapeLoader::base_path_ = "data/shapes/";
|
||||
|
||||
std::shared_ptr<Shape> ShapeLoader::load(const std::string& filename) {
|
||||
// Check cache first
|
||||
auto it = cache_.find(filename);
|
||||
if (it != cache_.end()) {
|
||||
std::cout << "[ShapeLoader] Cache hit: " << filename << std::endl;
|
||||
return it->second; // Cache hit
|
||||
}
|
||||
|
||||
// Normalize path: "ship.shp" → "shapes/ship.shp"
|
||||
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
|
||||
std::string normalized = filename;
|
||||
if (normalized.find("shapes/") != 0) {
|
||||
// Doesn't start with "shapes/", so add it
|
||||
normalized = "shapes/" + normalized;
|
||||
}
|
||||
|
||||
// Load from resource system
|
||||
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||
if (data.empty()) {
|
||||
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << normalized
|
||||
<< std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Convert bytes to string and parse
|
||||
std::string file_content(data.begin(), data.end());
|
||||
auto shape = std::make_shared<Shape>();
|
||||
if (!shape->parsejar_fitxer(file_content)) {
|
||||
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
|
||||
<< std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Verify shape is valid
|
||||
if (!shape->es_valida()) {
|
||||
std::cerr << "[ShapeLoader] Error: forma invàlida " << normalized << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Cache and return
|
||||
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->get_nom()
|
||||
<< ", " << shape->get_num_primitives() << " primitives)" << std::endl;
|
||||
|
||||
cache_[filename] = shape;
|
||||
return shape;
|
||||
}
|
||||
|
||||
void ShapeLoader::clear_cache() {
|
||||
std::cout << "[ShapeLoader] Netejant caché (" << cache_.size() << " formes)"
|
||||
<< std::endl;
|
||||
cache_.clear();
|
||||
}
|
||||
|
||||
size_t ShapeLoader::get_cache_size() { return cache_.size(); }
|
||||
|
||||
std::string ShapeLoader::resolve_path(const std::string& filename) {
|
||||
// Si és un path absolut (comença amb '/'), usar-lo directament
|
||||
if (!filename.empty() && filename[0] == '/') {
|
||||
return filename;
|
||||
}
|
||||
|
||||
// Si ja conté el prefix base_path, usar-lo directament
|
||||
if (filename.find(base_path_) == 0) {
|
||||
return filename;
|
||||
}
|
||||
|
||||
// Altrament, afegir base_path (ara suporta subdirectoris)
|
||||
return base_path_ + filename;
|
||||
}
|
||||
|
||||
} // namespace Graphics
|
||||
39
source/core/graphics/shape_loader.hpp
Normal file
39
source/core/graphics/shape_loader.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// shape_loader.hpp - Carregador estàtic de formes amb caché
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Carregador estàtic de formes amb caché
|
||||
class ShapeLoader {
|
||||
public:
|
||||
// No instanciable (tot estàtic)
|
||||
ShapeLoader() = delete;
|
||||
|
||||
// Carregar forma des de fitxer (amb caché)
|
||||
// Retorna punter compartit (nullptr si error)
|
||||
// Exemple: load("ship.shp") → busca a "data/shapes/ship.shp"
|
||||
static std::shared_ptr<Shape> load(const std::string& filename);
|
||||
|
||||
// Netejar caché (útil per debug/recàrrega)
|
||||
static void clear_cache();
|
||||
|
||||
// Estadístiques (debug)
|
||||
static size_t get_cache_size();
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string, std::shared_ptr<Shape>> cache_;
|
||||
static std::string base_path_; // "data/shapes/"
|
||||
|
||||
// Helpers privats
|
||||
static std::string resolve_path(const std::string& filename);
|
||||
};
|
||||
|
||||
} // namespace Graphics
|
||||
172
source/core/graphics/starfield.cpp
Normal file
172
source/core/graphics/starfield.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
// starfield.cpp - Implementació del sistema d'estrelles de fons
|
||||
// © 2025 Orni Attack
|
||||
|
||||
#include "core/graphics/starfield.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Constructor
|
||||
Starfield::Starfield(SDL_Renderer* renderer,
|
||||
const Punt& punt_fuga,
|
||||
const SDL_FRect& area,
|
||||
int densitat)
|
||||
: renderer_(renderer),
|
||||
punt_fuga_(punt_fuga),
|
||||
area_(area),
|
||||
densitat_(densitat) {
|
||||
// Carregar forma d'estrella amb ShapeLoader
|
||||
shape_estrella_ = ShapeLoader::load("star.shp");
|
||||
|
||||
if (!shape_estrella_ || !shape_estrella_->es_valida()) {
|
||||
std::cerr << "ERROR: No s'ha pogut carregar star.shp" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Configurar 3 capes amb diferents velocitats i escales
|
||||
// Capa 0: Fons llunyà (lenta, petita)
|
||||
capes_.push_back({20.0f, 0.3f, 0.8f, densitat / 3});
|
||||
|
||||
// Capa 1: Profunditat mitjana
|
||||
capes_.push_back({40.0f, 0.5f, 1.2f, densitat / 3});
|
||||
|
||||
// Capa 2: Primer pla (ràpida, gran)
|
||||
capes_.push_back({80.0f, 0.8f, 2.0f, densitat / 3});
|
||||
|
||||
// Calcular radi màxim (distància del centre al racó més llunyà)
|
||||
float dx = std::max(punt_fuga_.x, area_.w - punt_fuga_.x);
|
||||
float dy = std::max(punt_fuga_.y, area_.h - punt_fuga_.y);
|
||||
radi_max_ = std::sqrt(dx * dx + dy * dy);
|
||||
|
||||
// Inicialitzar estrelles amb posicions distribuïdes (pre-omplir pantalla)
|
||||
for (int capa_idx = 0; capa_idx < 3; capa_idx++) {
|
||||
int num = capes_[capa_idx].num_estrelles;
|
||||
for (int i = 0; i < num; i++) {
|
||||
Estrella estrella;
|
||||
estrella.capa = capa_idx;
|
||||
|
||||
// Angle aleatori
|
||||
estrella.angle = (static_cast<float>(rand()) / RAND_MAX) * 2.0f * Defaults::Math::PI;
|
||||
|
||||
// Distància aleatòria (0.0 a 1.0) per omplir tota la pantalla
|
||||
estrella.distancia_centre = static_cast<float>(rand()) / RAND_MAX;
|
||||
|
||||
// Calcular posició des de la distància
|
||||
float radi = estrella.distancia_centre * radi_max_;
|
||||
estrella.posicio.x = punt_fuga_.x + radi * std::cos(estrella.angle);
|
||||
estrella.posicio.y = punt_fuga_.y + radi * std::sin(estrella.angle);
|
||||
|
||||
estrelles_.push_back(estrella);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inicialitzar una estrella (nova o regenerada)
|
||||
void Starfield::inicialitzar_estrella(Estrella& estrella) {
|
||||
// Angle aleatori des del punt de fuga cap a fora
|
||||
estrella.angle = (static_cast<float>(rand()) / RAND_MAX) * 2.0f * Defaults::Math::PI;
|
||||
|
||||
// Distància inicial petita (5% del radi màxim) - neix prop del centre
|
||||
estrella.distancia_centre = 0.05f;
|
||||
|
||||
// Posició inicial: molt prop del punt de fuga
|
||||
float radi = estrella.distancia_centre * radi_max_;
|
||||
estrella.posicio.x = punt_fuga_.x + radi * std::cos(estrella.angle);
|
||||
estrella.posicio.y = punt_fuga_.y + radi * std::sin(estrella.angle);
|
||||
}
|
||||
|
||||
// Verificar si una estrella està fora de l'àrea
|
||||
bool Starfield::fora_area(const Estrella& estrella) const {
|
||||
return (estrella.posicio.x < area_.x ||
|
||||
estrella.posicio.x > area_.x + area_.w ||
|
||||
estrella.posicio.y < area_.y ||
|
||||
estrella.posicio.y > area_.y + area_.h);
|
||||
}
|
||||
|
||||
// Calcular escala dinàmica segons distància del centre
|
||||
float Starfield::calcular_escala(const Estrella& estrella) const {
|
||||
const CapaConfig& capa = capes_[estrella.capa];
|
||||
|
||||
// Interpolació lineal basada en distància del centre
|
||||
// distancia_centre: 0.0 (centre) → 1.0 (vora)
|
||||
return capa.escala_min +
|
||||
(capa.escala_max - capa.escala_min) * estrella.distancia_centre;
|
||||
}
|
||||
|
||||
// Calcular brightness dinàmica segons distància del centre
|
||||
float Starfield::calcular_brightness(const Estrella& estrella) const {
|
||||
// Interpolació lineal: estrelles properes (vora) més brillants
|
||||
// distancia_centre: 0.0 (centre, llunyanes) → 1.0 (vora, properes)
|
||||
float brightness_base = Defaults::Brightness::STARFIELD_MIN +
|
||||
(Defaults::Brightness::STARFIELD_MAX - Defaults::Brightness::STARFIELD_MIN) *
|
||||
estrella.distancia_centre;
|
||||
|
||||
// Aplicar multiplicador i limitar a 1.0
|
||||
return std::min(1.0f, brightness_base * multiplicador_brightness_);
|
||||
}
|
||||
|
||||
// Actualitzar posicions de les estrelles
|
||||
void Starfield::actualitzar(float delta_time) {
|
||||
for (auto& estrella : estrelles_) {
|
||||
// Obtenir configuració de la capa
|
||||
const CapaConfig& capa = capes_[estrella.capa];
|
||||
|
||||
// Moure cap a fora des del centre
|
||||
float velocitat = capa.velocitat_base;
|
||||
float dx = velocitat * std::cos(estrella.angle) * delta_time;
|
||||
float dy = velocitat * std::sin(estrella.angle) * delta_time;
|
||||
|
||||
estrella.posicio.x += dx;
|
||||
estrella.posicio.y += dy;
|
||||
|
||||
// Actualitzar distància del centre
|
||||
float dx_centre = estrella.posicio.x - punt_fuga_.x;
|
||||
float dy_centre = estrella.posicio.y - punt_fuga_.y;
|
||||
float dist_px = std::sqrt(dx_centre * dx_centre + dy_centre * dy_centre);
|
||||
estrella.distancia_centre = dist_px / radi_max_;
|
||||
|
||||
// Si ha sortit de l'àrea, regenerar-la
|
||||
if (fora_area(estrella)) {
|
||||
inicialitzar_estrella(estrella);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establir multiplicador de brightness
|
||||
void Starfield::set_brightness(float multiplier) {
|
||||
multiplicador_brightness_ = std::max(0.0f, multiplier); // Evitar valors negatius
|
||||
}
|
||||
|
||||
// Dibuixar totes les estrelles
|
||||
void Starfield::dibuixar() {
|
||||
if (!shape_estrella_->es_valida()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& estrella : estrelles_) {
|
||||
// Calcular escala i brightness dinàmicament
|
||||
float escala = calcular_escala(estrella);
|
||||
float brightness = calcular_brightness(estrella);
|
||||
|
||||
// Renderitzar estrella sense rotació
|
||||
Rendering::render_shape(
|
||||
renderer_,
|
||||
shape_estrella_,
|
||||
estrella.posicio,
|
||||
0.0f, // angle (les estrelles no giren)
|
||||
escala, // escala dinàmica
|
||||
true, // dibuixar
|
||||
1.0f, // progress (sempre visible)
|
||||
brightness // brightness dinàmica
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Graphics
|
||||
82
source/core/graphics/starfield.hpp
Normal file
82
source/core/graphics/starfield.hpp
Normal file
@@ -0,0 +1,82 @@
|
||||
// starfield.hpp - Sistema d'estrelles de fons amb efecte de profunditat
|
||||
// © 2025 Orni Attack
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Configuració per cada capa de profunditat
|
||||
struct CapaConfig {
|
||||
float velocitat_base; // Velocitat base d'aquesta capa (px/s)
|
||||
float escala_min; // Escala mínima prop del centre
|
||||
float escala_max; // Escala màxima al límit de pantalla
|
||||
int num_estrelles; // Nombre d'estrelles en aquesta capa
|
||||
};
|
||||
|
||||
// Classe Starfield - camp d'estrelles animat amb efecte de profunditat
|
||||
class Starfield {
|
||||
public:
|
||||
// Constructor
|
||||
// - renderer: SDL renderer
|
||||
// - punt_fuga: punt d'origen/fuga des d'on surten les estrelles
|
||||
// - area: rectangle on actuen les estrelles (SDL_FRect)
|
||||
// - densitat: nombre total d'estrelles (es divideix entre capes)
|
||||
Starfield(SDL_Renderer* renderer,
|
||||
const Punt& punt_fuga,
|
||||
const SDL_FRect& area,
|
||||
int densitat = 150);
|
||||
|
||||
// Actualitzar posicions de les estrelles
|
||||
void actualitzar(float delta_time);
|
||||
|
||||
// Dibuixar totes les estrelles
|
||||
void dibuixar();
|
||||
|
||||
// Setters per ajustar paràmetres en temps real
|
||||
void set_punt_fuga(const Punt& punt) { punt_fuga_ = punt; }
|
||||
void set_brightness(float multiplier);
|
||||
|
||||
private:
|
||||
// Estructura interna per cada estrella
|
||||
struct Estrella {
|
||||
Punt posicio; // Posició actual
|
||||
float angle; // Angle de moviment (radians)
|
||||
float distancia_centre; // Distància normalitzada del centre (0.0-1.0)
|
||||
int capa; // Índex de capa (0=lluny, 1=mitjà, 2=prop)
|
||||
};
|
||||
|
||||
// Inicialitzar una estrella (nova o regenerada)
|
||||
void inicialitzar_estrella(Estrella& estrella);
|
||||
|
||||
// Verificar si una estrella està fora de l'àrea
|
||||
bool fora_area(const Estrella& estrella) const;
|
||||
|
||||
// Calcular escala dinàmica segons distància del centre
|
||||
float calcular_escala(const Estrella& estrella) const;
|
||||
|
||||
// Calcular brightness dinàmica segons distància del centre
|
||||
float calcular_brightness(const Estrella& estrella) const;
|
||||
|
||||
// Dades
|
||||
std::vector<Estrella> estrelles_;
|
||||
std::vector<CapaConfig> capes_; // Configuració de les 3 capes
|
||||
std::shared_ptr<Shape> shape_estrella_;
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// Configuració
|
||||
Punt punt_fuga_; // Punt d'origen de les estrelles
|
||||
SDL_FRect area_; // Àrea activa
|
||||
float radi_max_; // Distància màxima del centre al límit de pantalla
|
||||
int densitat_; // Nombre total d'estrelles
|
||||
float multiplicador_brightness_{1.0f}; // Multiplicador de brillantor (1.0 = default)
|
||||
};
|
||||
|
||||
} // namespace Graphics
|
||||
263
source/core/graphics/vector_text.cpp
Normal file
263
source/core/graphics/vector_text.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
// vector_text.cpp - Implementació del sistema de text vectorial
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/graphics/vector_text.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
// Constants per a mides base dels caràcters
|
||||
constexpr float char_width = 20.0f; // Amplada base del caràcter
|
||||
constexpr float char_height = 40.0f; // Altura base del caràcter
|
||||
|
||||
VectorText::VectorText(SDL_Renderer* renderer)
|
||||
: renderer_(renderer) {
|
||||
load_charset();
|
||||
}
|
||||
|
||||
void VectorText::load_charset() {
|
||||
// Cargar dígitos 0-9
|
||||
for (char c = '0'; c <= '9'; c++) {
|
||||
std::string filename = get_shape_filename(c);
|
||||
auto shape = ShapeLoader::load(filename);
|
||||
|
||||
if (shape && shape->es_valida()) {
|
||||
chars_[c] = shape;
|
||||
} else {
|
||||
std::cerr << "[VectorText] Warning: no s'ha pogut carregar " << filename
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Cargar lletres A-Z (majúscules)
|
||||
for (char c = 'A'; c <= 'Z'; c++) {
|
||||
std::string filename = get_shape_filename(c);
|
||||
auto shape = ShapeLoader::load(filename);
|
||||
|
||||
if (shape && shape->es_valida()) {
|
||||
chars_[c] = shape;
|
||||
} else {
|
||||
std::cerr << "[VectorText] Warning: no s'ha pogut carregar " << filename
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Cargar símbolos
|
||||
const std::string symbols[] = {".", ",", "-", ":", "!", "?"};
|
||||
for (const auto& sym : symbols) {
|
||||
char c = sym[0];
|
||||
std::string filename = get_shape_filename(c);
|
||||
auto shape = ShapeLoader::load(filename);
|
||||
|
||||
if (shape && shape->es_valida()) {
|
||||
chars_[c] = shape;
|
||||
} else {
|
||||
std::cerr << "[VectorText] Warning: no s'ha pogut carregar " << filename
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Cargar símbolo de copyright (©) - UTF-8 U+00A9
|
||||
// Usem el segon byte (0xA9) com a key interna
|
||||
{
|
||||
char c = '\xA9'; // 169 decimal
|
||||
std::string filename = "font/char_copyright.shp";
|
||||
auto shape = ShapeLoader::load(filename);
|
||||
|
||||
if (shape && shape->es_valida()) {
|
||||
chars_[c] = shape;
|
||||
} else {
|
||||
std::cerr << "[VectorText] Warning: no s'ha pogut carregar " << filename
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "[VectorText] Carregats " << chars_.size() << " caràcters"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::string VectorText::get_shape_filename(char c) const {
|
||||
// Mapeo carácter → nombre de archivo (amb prefix "font/")
|
||||
switch (c) {
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
return std::string("font/char_") + c + ".shp";
|
||||
|
||||
// Lletres majúscules A-Z
|
||||
case 'A':
|
||||
case 'B':
|
||||
case 'C':
|
||||
case 'D':
|
||||
case 'E':
|
||||
case 'F':
|
||||
case 'G':
|
||||
case 'H':
|
||||
case 'I':
|
||||
case 'J':
|
||||
case 'K':
|
||||
case 'L':
|
||||
case 'M':
|
||||
case 'N':
|
||||
case 'O':
|
||||
case 'P':
|
||||
case 'Q':
|
||||
case 'R':
|
||||
case 'S':
|
||||
case 'T':
|
||||
case 'U':
|
||||
case 'V':
|
||||
case 'W':
|
||||
case 'X':
|
||||
case 'Y':
|
||||
case 'Z':
|
||||
return std::string("font/char_") + c + ".shp";
|
||||
|
||||
// Lletres minúscules a-z (convertir a majúscules)
|
||||
case 'a':
|
||||
case 'b':
|
||||
case 'c':
|
||||
case 'd':
|
||||
case 'e':
|
||||
case 'f':
|
||||
case 'g':
|
||||
case 'h':
|
||||
case 'i':
|
||||
case 'j':
|
||||
case 'k':
|
||||
case 'l':
|
||||
case 'm':
|
||||
case 'n':
|
||||
case 'o':
|
||||
case 'p':
|
||||
case 'q':
|
||||
case 'r':
|
||||
case 's':
|
||||
case 't':
|
||||
case 'u':
|
||||
case 'v':
|
||||
case 'w':
|
||||
case 'x':
|
||||
case 'y':
|
||||
case 'z':
|
||||
return std::string("font/char_") + char(c - 32) + ".shp";
|
||||
|
||||
// Símbols
|
||||
case '.':
|
||||
return "font/char_dot.shp";
|
||||
case ',':
|
||||
return "font/char_comma.shp";
|
||||
case '-':
|
||||
return "font/char_minus.shp";
|
||||
case ':':
|
||||
return "font/char_colon.shp";
|
||||
case '!':
|
||||
return "font/char_exclamation.shp";
|
||||
case '?':
|
||||
return "font/char_question.shp";
|
||||
case ' ':
|
||||
return ""; // Espai es maneja sense carregar shape
|
||||
|
||||
case '\xA9': // Copyright symbol (©) - UTF-8 U+00A9
|
||||
return "font/char_copyright.shp";
|
||||
|
||||
default:
|
||||
return ""; // Caràcter no suportat
|
||||
}
|
||||
}
|
||||
|
||||
bool VectorText::is_supported(char c) const {
|
||||
return chars_.find(c) != chars_.end();
|
||||
}
|
||||
|
||||
void VectorText::render(const std::string& text, const Punt& posicio, float escala, float spacing, float brightness) {
|
||||
if (!renderer_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ancho de un carácter base (20 px a escala 1.0)
|
||||
const float char_width_scaled = char_width * escala;
|
||||
|
||||
// Spacing escalado
|
||||
const float spacing_scaled = spacing * escala;
|
||||
|
||||
// Altura de un carácter escalado (necesario para ajustar Y)
|
||||
const float char_height_scaled = char_height * escala;
|
||||
|
||||
// Posición actual del centro del carácter (ajustada desde esquina superior
|
||||
// izquierda)
|
||||
float current_x = posicio.x;
|
||||
|
||||
// Iterar sobre cada byte del string (con detecció UTF-8)
|
||||
for (size_t i = 0; i < text.length(); i++) {
|
||||
unsigned char c = static_cast<unsigned char>(text[i]);
|
||||
|
||||
// Detectar copyright UTF-8 (0xC2 0xA9)
|
||||
if (c == 0xC2 && i + 1 < text.length() &&
|
||||
static_cast<unsigned char>(text[i + 1]) == 0xA9) {
|
||||
c = 0xA9; // Usar segon byte com a key
|
||||
i++; // Saltar el següent byte
|
||||
}
|
||||
|
||||
// Manejar espacios (avanzar sin dibujar)
|
||||
if (c == ' ') {
|
||||
current_x += char_width_scaled + spacing_scaled;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Verificar si el carácter está soportado
|
||||
auto it = chars_.find(c);
|
||||
if (it != chars_.end()) {
|
||||
// Renderizar carácter
|
||||
// Ajustar Y para que posicio represente esquina superior izquierda
|
||||
// (render_shape espera el centro, así que sumamos la mitad de la altura)
|
||||
Punt char_pos = {current_x, posicio.y + char_height_scaled / 2.0f};
|
||||
Rendering::render_shape(renderer_, it->second, char_pos, 0.0f, escala, true, 1.0f, brightness);
|
||||
|
||||
// Avanzar posición
|
||||
current_x += char_width_scaled + spacing_scaled;
|
||||
} else {
|
||||
// Carácter no soportado: saltar (o renderizar '?' en el futuro)
|
||||
std::cerr << "[VectorText] Warning: caràcter no suportat '" << c << "'"
|
||||
<< std::endl;
|
||||
current_x += char_width_scaled + spacing_scaled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float VectorText::get_text_width(const std::string& text, float escala, float spacing) const {
|
||||
if (text.empty()) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
const float char_width_scaled = char_width * escala;
|
||||
const float spacing_scaled = spacing * escala;
|
||||
|
||||
// Ancho total = (número de caracteres × char_width) + (espacios entre
|
||||
// caracteres)
|
||||
float width = text.length() * char_width_scaled;
|
||||
|
||||
// Añadir spacing entre caracteres (n-1 espacios para n caracteres)
|
||||
if (text.length() > 1) {
|
||||
width += (text.length() - 1) * spacing_scaled;
|
||||
}
|
||||
|
||||
return width;
|
||||
}
|
||||
|
||||
float VectorText::get_text_height(float escala) const {
|
||||
return char_height * escala;
|
||||
}
|
||||
|
||||
} // namespace Graphics
|
||||
47
source/core/graphics/vector_text.hpp
Normal file
47
source/core/graphics/vector_text.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
// vector_text.hpp - Sistema de texto vectorial con display de 7-segmentos
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Graphics {
|
||||
|
||||
class VectorText {
|
||||
public:
|
||||
VectorText(SDL_Renderer* renderer);
|
||||
|
||||
// Renderizar string completo
|
||||
// - text: cadena a renderizar (soporta: A-Z, a-z, 0-9, '.', ',', '-', ':',
|
||||
// '!', '?', ' ')
|
||||
// - posicio: posición inicial (esquina superior izquierda)
|
||||
// - escala: factor de escala (1.0 = 20×40 px por carácter)
|
||||
// - spacing: espacio entre caracteres en píxeles (a escala 1.0)
|
||||
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
|
||||
void render(const std::string& text, const Punt& posicio, float escala = 1.0f, float spacing = 2.0f, float brightness = 1.0f);
|
||||
|
||||
// Calcular ancho total de un string (útil para centrado)
|
||||
float get_text_width(const std::string& text, float escala = 1.0f, float spacing = 2.0f) const;
|
||||
|
||||
// Calcular altura del texto (útil para centrado vertical)
|
||||
float get_text_height(float escala = 1.0f) const;
|
||||
|
||||
// Verificar si un carácter está soportado
|
||||
bool is_supported(char c) const;
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
std::unordered_map<char, std::shared_ptr<Shape>> chars_;
|
||||
|
||||
void load_charset();
|
||||
std::string get_shape_filename(char c) const;
|
||||
};
|
||||
|
||||
} // namespace Graphics
|
||||
63
source/core/input/mouse.cpp
Normal file
63
source/core/input/mouse.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "core/input/mouse.hpp"
|
||||
|
||||
namespace Mouse {
|
||||
Uint32 cursor_hide_time = 3000; // Tiempo en milisegundos para ocultar el cursor
|
||||
Uint32 last_mouse_move_time = 0; // Última vez que el ratón se movió
|
||||
bool cursor_visible = true; // Estado del cursor
|
||||
|
||||
// Modo forzado: Usado cuando SDLManager entra en pantalla completa.
|
||||
// Cuando está activado, el cursor permanece oculto independientemente del movimiento del ratón.
|
||||
// SDLManager controla esto mediante llamadas a setForceHidden().
|
||||
bool force_hidden = false;
|
||||
|
||||
void setForceHidden(bool force) {
|
||||
force_hidden = force;
|
||||
|
||||
if (force) {
|
||||
// Entrando en modo oculto forzado: ocultar cursor inmediatamente
|
||||
if (cursor_visible) {
|
||||
SDL_HideCursor();
|
||||
cursor_visible = false;
|
||||
}
|
||||
} else {
|
||||
// Saliendo de modo oculto forzado: mostrar cursor y resetear temporizador
|
||||
SDL_ShowCursor();
|
||||
cursor_visible = true;
|
||||
last_mouse_move_time = SDL_GetTicks(); // Resetear temporizador
|
||||
}
|
||||
}
|
||||
|
||||
bool isForceHidden() {
|
||||
return force_hidden;
|
||||
}
|
||||
|
||||
void handleEvent(const SDL_Event& event) {
|
||||
// CRÍTICO: Si estamos en modo forzado, ignorar todos los eventos del ratón
|
||||
if (force_hidden) {
|
||||
return; // Salir temprano - no procesar ningún evento
|
||||
}
|
||||
|
||||
// MODO NORMAL: Mostrar cursor al mover el ratón
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
last_mouse_move_time = SDL_GetTicks();
|
||||
if (!cursor_visible) {
|
||||
SDL_ShowCursor();
|
||||
cursor_visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateCursorVisibility() {
|
||||
// CRÍTICO: Si estamos en modo forzado, no aplicar lógica de timeout
|
||||
if (force_hidden) {
|
||||
return; // Salir temprano - el cursor permanece oculto
|
||||
}
|
||||
|
||||
// MODO NORMAL: Auto-ocultar basado en timeout
|
||||
Uint32 current_time = SDL_GetTicks();
|
||||
if (cursor_visible && (current_time - last_mouse_move_time > cursor_hide_time)) {
|
||||
SDL_HideCursor();
|
||||
cursor_visible = false;
|
||||
}
|
||||
}
|
||||
} // namespace Mouse
|
||||
16
source/core/input/mouse.hpp
Normal file
16
source/core/input/mouse.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace Mouse {
|
||||
extern Uint32 cursor_hide_time; // Tiempo en milisegundos para ocultar el cursor
|
||||
extern Uint32 last_mouse_move_time; // Última vez que el ratón se movió
|
||||
extern bool cursor_visible; // Estado del cursor
|
||||
|
||||
void handleEvent(const SDL_Event& event);
|
||||
void updateCursorVisibility();
|
||||
|
||||
// Control de visibilidad forzada (para modo pantalla completa)
|
||||
void setForceHidden(bool force); // Activar/desactivar ocultación forzada
|
||||
bool isForceHidden(); // Consultar estado actual
|
||||
} // namespace Mouse
|
||||
68
source/core/rendering/color_oscillator.cpp
Normal file
68
source/core/rendering/color_oscillator.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// color_oscillator.cpp - Implementació d'oscil·lació de color
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/rendering/color_oscillator.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
ColorOscillator::ColorOscillator()
|
||||
: accumulated_time_(0.0f) {
|
||||
// Inicialitzar amb el color mínim
|
||||
current_line_color_ = {Defaults::Color::LINE_MIN_R,
|
||||
Defaults::Color::LINE_MIN_G,
|
||||
Defaults::Color::LINE_MIN_B,
|
||||
255};
|
||||
current_background_color_ = {Defaults::Color::BACKGROUND_MIN_R,
|
||||
Defaults::Color::BACKGROUND_MIN_G,
|
||||
Defaults::Color::BACKGROUND_MIN_B,
|
||||
255};
|
||||
}
|
||||
|
||||
void ColorOscillator::update(float delta_time) {
|
||||
accumulated_time_ += delta_time;
|
||||
|
||||
float factor =
|
||||
calculateOscillationFactor(accumulated_time_, Defaults::Color::FREQUENCY);
|
||||
|
||||
// Interpolar colors de línies
|
||||
SDL_Color line_min = {Defaults::Color::LINE_MIN_R,
|
||||
Defaults::Color::LINE_MIN_G,
|
||||
Defaults::Color::LINE_MIN_B,
|
||||
255};
|
||||
SDL_Color line_max = {Defaults::Color::LINE_MAX_R,
|
||||
Defaults::Color::LINE_MAX_G,
|
||||
Defaults::Color::LINE_MAX_B,
|
||||
255};
|
||||
current_line_color_ = interpolateColor(line_min, line_max, factor);
|
||||
|
||||
// Interpolar colors de fons
|
||||
SDL_Color bg_min = {Defaults::Color::BACKGROUND_MIN_R,
|
||||
Defaults::Color::BACKGROUND_MIN_G,
|
||||
Defaults::Color::BACKGROUND_MIN_B,
|
||||
255};
|
||||
SDL_Color bg_max = {Defaults::Color::BACKGROUND_MAX_R,
|
||||
Defaults::Color::BACKGROUND_MAX_G,
|
||||
Defaults::Color::BACKGROUND_MAX_B,
|
||||
255};
|
||||
current_background_color_ = interpolateColor(bg_min, bg_max, factor);
|
||||
}
|
||||
|
||||
float ColorOscillator::calculateOscillationFactor(float time, float frequency) {
|
||||
// Oscil·lació senoïdal: sin(t * freq * 2π)
|
||||
// Mapejar de [-1, 1] a [0, 1]
|
||||
float radians = time * frequency * 2.0f * Defaults::Math::PI;
|
||||
return (std::sin(radians) + 1.0f) / 2.0f;
|
||||
}
|
||||
|
||||
SDL_Color ColorOscillator::interpolateColor(SDL_Color min, SDL_Color max, float factor) {
|
||||
return {static_cast<uint8_t>(min.r + (max.r - min.r) * factor),
|
||||
static_cast<uint8_t>(min.g + (max.g - min.g) * factor),
|
||||
static_cast<uint8_t>(min.b + (max.b - min.b) * factor),
|
||||
255};
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
29
source/core/rendering/color_oscillator.hpp
Normal file
29
source/core/rendering/color_oscillator.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
// color_oscillator.hpp - Sistema d'oscil·lació de color per efecte CRT
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
class ColorOscillator {
|
||||
public:
|
||||
ColorOscillator();
|
||||
|
||||
void update(float delta_time);
|
||||
|
||||
SDL_Color getCurrentLineColor() const { return current_line_color_; }
|
||||
SDL_Color getCurrentBackgroundColor() const {
|
||||
return current_background_color_;
|
||||
}
|
||||
|
||||
private:
|
||||
float accumulated_time_;
|
||||
SDL_Color current_line_color_;
|
||||
SDL_Color current_background_color_;
|
||||
|
||||
static float calculateOscillationFactor(float time, float frequency);
|
||||
static SDL_Color interpolateColor(SDL_Color min, SDL_Color max, float factor);
|
||||
};
|
||||
|
||||
} // namespace Rendering
|
||||
11
source/core/rendering/coordinate_transform.cpp
Normal file
11
source/core/rendering/coordinate_transform.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
// coordinate_transform.cpp - Inicialització de variables globals
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/rendering/coordinate_transform.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
// Factor d'escala global (inicialitzat a 1.0 per defecte)
|
||||
float g_current_scale_factor = 1.0f;
|
||||
|
||||
} // namespace Rendering
|
||||
31
source/core/rendering/coordinate_transform.hpp
Normal file
31
source/core/rendering/coordinate_transform.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// coordinate_transform.hpp - Transformació de coordenades lògiques a físiques
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
// Factor d'escala global (actualitzat cada frame per SDLManager)
|
||||
extern float g_current_scale_factor;
|
||||
|
||||
// Transforma coordenada lògica a física amb arrodoniment
|
||||
inline int transform_x(int logical_x, float scale) {
|
||||
return static_cast<int>(std::round(logical_x * scale));
|
||||
}
|
||||
|
||||
inline int transform_y(int logical_y, float scale) {
|
||||
return static_cast<int>(std::round(logical_y * scale));
|
||||
}
|
||||
|
||||
// Variant que usa el factor d'escala global
|
||||
inline int transform_x(int logical_x) {
|
||||
return transform_x(logical_x, g_current_scale_factor);
|
||||
}
|
||||
|
||||
inline int transform_y(int logical_y) {
|
||||
return transform_y(logical_y, g_current_scale_factor);
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
100
source/core/rendering/line_renderer.cpp
Normal file
100
source/core/rendering/line_renderer.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
// line_renderer.cpp - Implementació de renderitzat de línies
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "core/rendering/coordinate_transform.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
// [NUEVO] Color global compartit (actualitzat per ColorOscillator via
|
||||
// SDLManager)
|
||||
SDL_Color g_current_line_color = {255, 255, 255, 255}; // Blanc inicial
|
||||
|
||||
bool linea(SDL_Renderer* renderer, int x1, int y1, int x2, int y2, bool dibuixar, float brightness) {
|
||||
// Algorisme de Bresenham per dibuixar línies
|
||||
// Basat en el codi Pascal original
|
||||
|
||||
// Helper function: retorna el signe d'un nombre
|
||||
auto sign = [](int x) -> int {
|
||||
if (x < 0)
|
||||
return -1;
|
||||
if (x > 0)
|
||||
return 1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Variables per a l'algorisme (no utilitzades fins Fase 10 - detecció de
|
||||
// col·lisions) int x = x1, y = y1; int xs = x2 - x1; int ys = y2 - y1; int
|
||||
// xm = sign(xs); int ym = sign(ys); xs = std::abs(xs); ys = std::abs(ys);
|
||||
|
||||
// Suprimir warning de variable no usada
|
||||
(void)sign;
|
||||
|
||||
// Detecció de col·lisió (TODO per Fase 10)
|
||||
// El codi Pascal original llegia pixels del framebuffer bit-packed
|
||||
// i comptava col·lisions. Per ara, usem SDL_RenderDrawLine i retornem false.
|
||||
bool colisio = false;
|
||||
|
||||
// Dibuixar amb SDL3 (més eficient que Bresenham píxel a píxel)
|
||||
if (dibuixar && renderer) {
|
||||
// Transformar coordenades lògiques (640x480) a físiques (resolució real)
|
||||
float scale = g_current_scale_factor;
|
||||
int px1 = transform_x(x1, scale);
|
||||
int py1 = transform_y(y1, scale);
|
||||
int px2 = transform_x(x2, scale);
|
||||
int py2 = transform_y(y2, scale);
|
||||
|
||||
// Aplicar brightness al color oscil·lat global
|
||||
SDL_Color color_final;
|
||||
color_final.r = static_cast<uint8_t>(g_current_line_color.r * brightness);
|
||||
color_final.g = static_cast<uint8_t>(g_current_line_color.g * brightness);
|
||||
color_final.b = static_cast<uint8_t>(g_current_line_color.b * brightness);
|
||||
color_final.a = 255;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, color_final.r, color_final.g, color_final.b, 255);
|
||||
|
||||
// Renderitzar amb coordenades físiques
|
||||
SDL_RenderLine(renderer, static_cast<float>(px1), static_cast<float>(py1), static_cast<float>(px2), static_cast<float>(py2));
|
||||
}
|
||||
|
||||
// Algorisme de Bresenham original (conservat per a futura detecció de
|
||||
// col·lisió)
|
||||
/*
|
||||
if (xs > ys) {
|
||||
// Línia plana (<45 graus)
|
||||
int count = -(xs / 2);
|
||||
while (x != x2) {
|
||||
count = count + ys;
|
||||
x = x + xm;
|
||||
if (count > 0) {
|
||||
y = y + ym;
|
||||
count = count - xs;
|
||||
}
|
||||
// Aquí aniria la detecció de col·lisió píxel a píxel
|
||||
}
|
||||
} else {
|
||||
// Línia pronunciada (>=45 graus)
|
||||
int count = -(ys / 2);
|
||||
while (y != y2) {
|
||||
count = count + xs;
|
||||
y = y + ym;
|
||||
if (count > 0) {
|
||||
x = x + xm;
|
||||
count = count - ys;
|
||||
}
|
||||
// Aquí aniria la detecció de col·lisió píxel a píxel
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
return colisio;
|
||||
}
|
||||
|
||||
// [NUEVO] Establir el color global de les línies
|
||||
void setLineColor(SDL_Color color) { g_current_line_color = color; }
|
||||
|
||||
} // namespace Rendering
|
||||
16
source/core/rendering/line_renderer.hpp
Normal file
16
source/core/rendering/line_renderer.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
// line_renderer.hpp - Renderitzat de línies
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace Rendering {
|
||||
// Algorisme de Bresenham per dibuixar línies
|
||||
// Retorna true si hi ha col·lisió (per Fase 10)
|
||||
// brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
|
||||
bool linea(SDL_Renderer* renderer, int x1, int y1, int x2, int y2, bool dibuixar, float brightness = 1.0f);
|
||||
|
||||
// [NUEVO] Establir el color global de les línies (oscil·lació)
|
||||
void setLineColor(SDL_Color color);
|
||||
} // namespace Rendering
|
||||
86
source/core/rendering/polygon_renderer.cpp
Normal file
86
source/core/rendering/polygon_renderer.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
// polygon_renderer.cpp - Implementació de renderitzat de polígons
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
//
|
||||
// ==============================================================================
|
||||
// DEPRECATED: Use core/rendering/shape_renderer.cpp instead
|
||||
// ==============================================================================
|
||||
|
||||
#include "core/rendering/polygon_renderer.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
void rota_tri(SDL_Renderer* renderer, const Triangle& tri, float angul, float velocitat, bool dibuixar) {
|
||||
// Rotar i dibuixar triangle (nau)
|
||||
// Conversió de coordenades polars a cartesianes amb rotació
|
||||
// Basat en el codi Pascal original: lines 271-284
|
||||
|
||||
// Convertir cada punt polar a cartesià
|
||||
// x = (r + velocitat) * cos(angle_punt + angle_nau) + centre.x
|
||||
// y = (r + velocitat) * sin(angle_punt + angle_nau) + centre.y
|
||||
|
||||
int x1 = static_cast<int>(std::round((tri.p1.r + velocitat) *
|
||||
std::cos(tri.p1.angle + angul))) +
|
||||
tri.centre.x;
|
||||
|
||||
int y1 = static_cast<int>(std::round((tri.p1.r + velocitat) *
|
||||
std::sin(tri.p1.angle + angul))) +
|
||||
tri.centre.y;
|
||||
|
||||
int x2 = static_cast<int>(std::round((tri.p2.r + velocitat) *
|
||||
std::cos(tri.p2.angle + angul))) +
|
||||
tri.centre.x;
|
||||
|
||||
int y2 = static_cast<int>(std::round((tri.p2.r + velocitat) *
|
||||
std::sin(tri.p2.angle + angul))) +
|
||||
tri.centre.y;
|
||||
|
||||
int x3 = static_cast<int>(std::round((tri.p3.r + velocitat) *
|
||||
std::cos(tri.p3.angle + angul))) +
|
||||
tri.centre.x;
|
||||
|
||||
int y3 = static_cast<int>(std::round((tri.p3.r + velocitat) *
|
||||
std::sin(tri.p3.angle + angul))) +
|
||||
tri.centre.y;
|
||||
|
||||
// Dibuixar les 3 línies que formen el triangle
|
||||
linea(renderer, x1, y1, x2, y2, dibuixar);
|
||||
linea(renderer, x1, y1, x3, y3, dibuixar);
|
||||
linea(renderer, x3, y3, x2, y2, dibuixar);
|
||||
}
|
||||
|
||||
void rota_pol(SDL_Renderer* renderer, const Poligon& pol, float angul, bool dibuixar) {
|
||||
// Rotar i dibuixar polígon (enemics i bales)
|
||||
// Conversió de coordenades polars a cartesianes amb rotació
|
||||
// Basat en el codi Pascal original: lines 286-296
|
||||
|
||||
// Array temporal per emmagatzemar punts convertits a cartesianes
|
||||
std::array<Punt, Defaults::Entities::MAX_IPUNTS> xy;
|
||||
|
||||
// Convertir cada punt polar a cartesià
|
||||
for (uint8_t i = 0; i < pol.n; i++) {
|
||||
xy[i].x = static_cast<int>(std::round(
|
||||
pol.ipuntx[i].r * std::cos(pol.ipuntx[i].angle + angul))) +
|
||||
pol.centre.x;
|
||||
|
||||
xy[i].y = static_cast<int>(std::round(
|
||||
pol.ipuntx[i].r * std::sin(pol.ipuntx[i].angle + angul))) +
|
||||
pol.centre.y;
|
||||
}
|
||||
|
||||
// Dibuixar línies entre punts consecutius
|
||||
for (uint8_t i = 0; i < pol.n - 1; i++) {
|
||||
linea(renderer, xy[i].x, xy[i].y, xy[i + 1].x, xy[i + 1].y, dibuixar);
|
||||
}
|
||||
|
||||
// Tancar el polígon (últim punt → primer punt)
|
||||
linea(renderer, xy[pol.n - 1].x, xy[pol.n - 1].y, xy[0].x, xy[0].y, dibuixar);
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
22
source/core/rendering/polygon_renderer.hpp
Normal file
22
source/core/rendering/polygon_renderer.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// polygon_renderer.hpp - Renderitzat de polígons polars
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
//
|
||||
// ==============================================================================
|
||||
// DEPRECATED: Use core/rendering/shape_renderer.hpp instead
|
||||
// ==============================================================================
|
||||
// This file is kept temporarily for chatarra_cosmica_ (Phase 10: explosions)
|
||||
// TODO Phase 10: Replace with particle system or remove completely
|
||||
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
// Rotar i dibuixar triangle (nau)
|
||||
void rota_tri(SDL_Renderer* renderer, const Triangle& tri, float angul, float velocitat, bool dibuixar);
|
||||
|
||||
// Rotar i dibuixar polígon (enemics i bales)
|
||||
void rota_pol(SDL_Renderer* renderer, const Poligon& pol, float angul, bool dibuixar);
|
||||
} // namespace Rendering
|
||||
66
source/core/rendering/primitives.cpp
Normal file
66
source/core/rendering/primitives.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// primitives.cpp - Implementació de funcions geomètriques
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
//
|
||||
// ==============================================================================
|
||||
// DEPRECATED: Use Shape system instead (.shp files + ShapeLoader)
|
||||
// ==============================================================================
|
||||
|
||||
#include "primitives.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
|
||||
float modul(const Punt& p) {
|
||||
// Càlcul de la magnitud d'un vector: sqrt(x² + y²)
|
||||
return std::sqrt(p.x * p.x + p.y * p.y);
|
||||
}
|
||||
|
||||
void diferencia(const Punt& o, const Punt& d, Punt& p) {
|
||||
// Resta de vectors (origen - destí)
|
||||
p.x = o.x - d.x;
|
||||
p.y = o.y - d.y;
|
||||
}
|
||||
|
||||
int distancia(const Punt& o, const Punt& d) {
|
||||
// Distància entre dos punts
|
||||
Punt p;
|
||||
diferencia(o, d, p);
|
||||
return static_cast<int>(std::round(modul(p)));
|
||||
}
|
||||
|
||||
float angle_punt(const Punt& p) {
|
||||
// Càlcul de l'angle d'un punt (arctan)
|
||||
if (p.y != 0) {
|
||||
return std::atan(p.x / p.y);
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
void crear_poligon_regular(Poligon& pol, uint8_t n, float r) {
|
||||
// Crear un polígon regular amb n costats i radi r
|
||||
// Distribueix els punts uniformement al voltant d'un cercle
|
||||
|
||||
float interval = 2.0f * Defaults::Math::PI / n;
|
||||
float act = 0.0f;
|
||||
|
||||
for (uint8_t i = 0; i < n; i++) {
|
||||
pol.ipuntx[i].r = r;
|
||||
pol.ipuntx[i].angle = act;
|
||||
act += interval;
|
||||
}
|
||||
|
||||
// Inicialitzar propietats del polígon
|
||||
pol.centre.x = 320.0f;
|
||||
pol.centre.y = 200.0f;
|
||||
pol.angle = 0.0f;
|
||||
// Convertir velocitat de px/frame a px/s: 2 px/frame × 20 FPS = 40 px/s
|
||||
pol.velocitat = Defaults::Physics::ENEMY_SPEED * 20.0f;
|
||||
pol.n = n;
|
||||
// Convertir rotació de rad/frame a rad/s: 0.0785 rad/frame × 20 FPS = 1.57
|
||||
// rad/s (~90°/s)
|
||||
pol.drotacio = 0.078539816f * 20.0f;
|
||||
pol.rotacio = 0.0f;
|
||||
pol.esta = true;
|
||||
}
|
||||
32
source/core/rendering/primitives.hpp
Normal file
32
source/core/rendering/primitives.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// primitives.hpp - Funcions geomètriques bàsiques
|
||||
// © 1999 Visente i Sergi (versió Pascal)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
//
|
||||
// ==============================================================================
|
||||
// DEPRECATED: Use Shape system instead (.shp files + ShapeLoader)
|
||||
// ==============================================================================
|
||||
// This file is kept temporarily for chatarra_cosmica_ (Phase 10: explosions)
|
||||
// TODO Phase 10: Replace with particle system or remove completely
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "core/types.hpp"
|
||||
|
||||
// Funcions matemàtiques geomètriques pures (sense dependències d'estat)
|
||||
|
||||
// Càlcul de la magnitud d'un vector
|
||||
float modul(const Punt& p);
|
||||
|
||||
// Diferència entre dos punts (vector origen - destí)
|
||||
void diferencia(const Punt& o, const Punt& d, Punt& p);
|
||||
|
||||
// Distància entre dos punts
|
||||
int distancia(const Punt& o, const Punt& d);
|
||||
|
||||
// Càlcul de l'angle d'un punt
|
||||
float angle_punt(const Punt& p);
|
||||
|
||||
// Creació de polígons regulars
|
||||
void crear_poligon_regular(Poligon& pol, uint8_t n, float r);
|
||||
@@ -2,302 +2,472 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "sdl_manager.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
#include "game/options.hpp"
|
||||
#include "project.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/rendering/coordinate_transform.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
#include "game/options.hpp"
|
||||
#include "project.h"
|
||||
|
||||
SDLManager::SDLManager()
|
||||
: finestra_(nullptr), renderer_(nullptr),
|
||||
: finestra_(nullptr),
|
||||
renderer_(nullptr),
|
||||
fps_accumulator_(0.0f),
|
||||
fps_frame_count_(0),
|
||||
fps_display_(0),
|
||||
current_width_(Defaults::Window::WIDTH),
|
||||
current_height_(Defaults::Window::HEIGHT), is_fullscreen_(false),
|
||||
max_width_(1920), max_height_(1080) {
|
||||
// Inicialitzar SDL3
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
std::cerr << "Error inicialitzant SDL3: " << SDL_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
current_height_(Defaults::Window::HEIGHT),
|
||||
is_fullscreen_(false),
|
||||
max_width_(1920),
|
||||
max_height_(1080),
|
||||
zoom_factor_(Defaults::Window::BASE_ZOOM),
|
||||
windowed_width_(Defaults::Window::WIDTH),
|
||||
windowed_height_(Defaults::Window::HEIGHT),
|
||||
max_zoom_(1.0f) {
|
||||
// Inicialitzar SDL3
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
std::cerr << "Error inicialitzant SDL3: " << SDL_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcular mida màxima des del display
|
||||
calculateMaxWindowSize();
|
||||
// Calcular mida màxima des del display
|
||||
calculateMaxWindowSize();
|
||||
|
||||
// Construir títol dinàmic igual que en pollo
|
||||
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME,
|
||||
Project::VERSION, Project::COPYRIGHT);
|
||||
// Construir títol dinàmic
|
||||
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT);
|
||||
|
||||
// Crear finestra CENTRADA (SDL ho fa automàticament amb CENTERED)
|
||||
finestra_ =
|
||||
SDL_CreateWindow(window_title.c_str(), current_width_, current_height_,
|
||||
SDL_WINDOW_RESIZABLE // Permetre resize manual també
|
||||
);
|
||||
// Crear finestra CENTRADA (SDL ho fa automàticament amb CENTERED)
|
||||
finestra_ =
|
||||
SDL_CreateWindow(window_title.c_str(), current_width_, current_height_,
|
||||
SDL_WINDOW_RESIZABLE // Permetre resize manual també
|
||||
);
|
||||
|
||||
if (!finestra_) {
|
||||
std::cerr << "Error creant finestra: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
if (!finestra_) {
|
||||
std::cerr << "Error creant finestra: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
|
||||
// IMPORTANT: Centrar explícitament la finestra
|
||||
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED);
|
||||
// IMPORTANT: Centrar explícitament la finestra
|
||||
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
|
||||
// Crear renderer amb acceleració
|
||||
renderer_ = SDL_CreateRenderer(finestra_, nullptr);
|
||||
// Crear renderer amb acceleració
|
||||
renderer_ = SDL_CreateRenderer(finestra_, nullptr);
|
||||
|
||||
if (!renderer_) {
|
||||
std::cerr << "Error creant renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(finestra_);
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
if (!renderer_) {
|
||||
std::cerr << "Error creant renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(finestra_);
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
|
||||
// CRÍTIC: Configurar viewport scaling
|
||||
updateLogicalPresentation();
|
||||
// Aplicar configuració de V-Sync
|
||||
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
|
||||
|
||||
std::cout << "SDL3 inicialitzat: " << current_width_ << "x" << current_height_
|
||||
<< " (logic: " << Defaults::Game::WIDTH << "x"
|
||||
<< Defaults::Game::HEIGHT << ")" << std::endl;
|
||||
// CRÍTIC: Configurar viewport scaling
|
||||
updateLogicalPresentation();
|
||||
|
||||
std::cout << "SDL3 inicialitzat: " << current_width_ << "x" << current_height_
|
||||
<< " (logic: " << Defaults::Game::WIDTH << "x"
|
||||
<< Defaults::Game::HEIGHT << ")" << std::endl;
|
||||
}
|
||||
|
||||
// Constructor amb configuració
|
||||
SDLManager::SDLManager(int width, int height, bool fullscreen)
|
||||
: finestra_(nullptr), renderer_(nullptr), current_width_(width),
|
||||
current_height_(height), is_fullscreen_(fullscreen), max_width_(1920),
|
||||
max_height_(1080) {
|
||||
// Inicialitzar SDL3
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
std::cerr << "Error inicialitzant SDL3: " << SDL_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
: finestra_(nullptr),
|
||||
renderer_(nullptr),
|
||||
fps_accumulator_(0.0f),
|
||||
fps_frame_count_(0),
|
||||
fps_display_(0),
|
||||
current_width_(width),
|
||||
current_height_(height),
|
||||
is_fullscreen_(fullscreen),
|
||||
max_width_(1920),
|
||||
max_height_(1080),
|
||||
zoom_factor_(static_cast<float>(width) / Defaults::Window::WIDTH),
|
||||
windowed_width_(width),
|
||||
windowed_height_(height),
|
||||
max_zoom_(1.0f) {
|
||||
// Inicialitzar SDL3
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
std::cerr << "Error inicialitzant SDL3: " << SDL_GetError() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcular mida màxima des del display
|
||||
calculateMaxWindowSize();
|
||||
// Calcular mida màxima des del display
|
||||
calculateMaxWindowSize();
|
||||
|
||||
// Construir títol dinàmic
|
||||
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME,
|
||||
Project::VERSION, Project::COPYRIGHT);
|
||||
// Construir títol dinàmic
|
||||
std::string window_title = std::format("{} v{} ({})", Project::LONG_NAME, Project::VERSION, Project::COPYRIGHT);
|
||||
|
||||
// Configurar flags de la finestra
|
||||
SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
|
||||
if (is_fullscreen_) {
|
||||
flags = static_cast<SDL_WindowFlags>(flags | SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
// Configurar flags de la finestra
|
||||
SDL_WindowFlags flags = SDL_WINDOW_RESIZABLE;
|
||||
if (is_fullscreen_) {
|
||||
flags = static_cast<SDL_WindowFlags>(flags | SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
|
||||
// Crear finestra
|
||||
finestra_ = SDL_CreateWindow(window_title.c_str(), current_width_,
|
||||
current_height_, flags);
|
||||
// Crear finestra
|
||||
finestra_ = SDL_CreateWindow(window_title.c_str(), current_width_, current_height_, flags);
|
||||
|
||||
if (!finestra_) {
|
||||
std::cerr << "Error creant finestra: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
if (!finestra_) {
|
||||
std::cerr << "Error creant finestra: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
|
||||
// Centrar explícitament la finestra (si no és fullscreen)
|
||||
if (!is_fullscreen_) {
|
||||
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED);
|
||||
}
|
||||
// Centrar explícitament la finestra (si no és fullscreen)
|
||||
if (!is_fullscreen_) {
|
||||
SDL_SetWindowPosition(finestra_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
}
|
||||
|
||||
// Crear renderer amb acceleració
|
||||
renderer_ = SDL_CreateRenderer(finestra_, nullptr);
|
||||
// Crear renderer amb acceleració
|
||||
renderer_ = SDL_CreateRenderer(finestra_, nullptr);
|
||||
|
||||
if (!renderer_) {
|
||||
std::cerr << "Error creant renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(finestra_);
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
if (!renderer_) {
|
||||
std::cerr << "Error creant renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(finestra_);
|
||||
SDL_Quit();
|
||||
return;
|
||||
}
|
||||
|
||||
// Configurar viewport scaling
|
||||
updateLogicalPresentation();
|
||||
// Aplicar configuració de V-Sync
|
||||
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
|
||||
|
||||
std::cout << "SDL3 inicialitzat: " << current_width_ << "x" << current_height_
|
||||
<< " (logic: " << Defaults::Game::WIDTH << "x"
|
||||
<< Defaults::Game::HEIGHT << ")";
|
||||
if (is_fullscreen_) {
|
||||
std::cout << " [FULLSCREEN]";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// Configurar viewport scaling
|
||||
updateLogicalPresentation();
|
||||
|
||||
std::cout << "SDL3 inicialitzat: " << current_width_ << "x" << current_height_
|
||||
<< " (logic: " << Defaults::Game::WIDTH << "x"
|
||||
<< Defaults::Game::HEIGHT << ")";
|
||||
if (is_fullscreen_) {
|
||||
std::cout << " [FULLSCREEN]";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Inicialitzar mòdul Mouse amb l'estat actual de fullscreen
|
||||
Mouse::setForceHidden(is_fullscreen_);
|
||||
}
|
||||
|
||||
SDLManager::~SDLManager() {
|
||||
if (renderer_) {
|
||||
SDL_DestroyRenderer(renderer_);
|
||||
renderer_ = nullptr;
|
||||
}
|
||||
if (renderer_) {
|
||||
SDL_DestroyRenderer(renderer_);
|
||||
renderer_ = nullptr;
|
||||
}
|
||||
|
||||
if (finestra_) {
|
||||
SDL_DestroyWindow(finestra_);
|
||||
finestra_ = nullptr;
|
||||
}
|
||||
if (finestra_) {
|
||||
SDL_DestroyWindow(finestra_);
|
||||
finestra_ = nullptr;
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
std::cout << "SDL3 netejat correctament" << std::endl;
|
||||
SDL_Quit();
|
||||
std::cout << "SDL3 netejat correctament" << std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::calculateMaxWindowSize() {
|
||||
SDL_DisplayID display = SDL_GetPrimaryDisplay();
|
||||
const SDL_DisplayMode *mode = SDL_GetCurrentDisplayMode(display);
|
||||
SDL_DisplayID display = SDL_GetPrimaryDisplay();
|
||||
const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(display);
|
||||
|
||||
if (mode) {
|
||||
// Deixar marge de 100px per a decoracions de l'OS
|
||||
max_width_ = mode->w - 100;
|
||||
max_height_ = mode->h - 100;
|
||||
std::cout << "Display detectat: " << mode->w << "x" << mode->h
|
||||
<< " (max finestra: " << max_width_ << "x" << max_height_ << ")"
|
||||
<< std::endl;
|
||||
} else {
|
||||
// Fallback conservador
|
||||
max_width_ = 1920;
|
||||
max_height_ = 1080;
|
||||
std::cerr << "No s'ha pogut detectar el display, usant fallback: "
|
||||
<< max_width_ << "x" << max_height_ << std::endl;
|
||||
}
|
||||
if (mode) {
|
||||
// Deixar marge de 100px per a decoracions de l'OS
|
||||
max_width_ = mode->w - 100;
|
||||
max_height_ = mode->h - 100;
|
||||
std::cout << "Display detectat: " << mode->w << "x" << mode->h
|
||||
<< " (max finestra: " << max_width_ << "x" << max_height_ << ")"
|
||||
<< std::endl;
|
||||
} else {
|
||||
// Fallback conservador
|
||||
max_width_ = 1920;
|
||||
max_height_ = 1080;
|
||||
std::cerr << "No s'ha pogut detectar el display, usant fallback: "
|
||||
<< max_width_ << "x" << max_height_ << std::endl;
|
||||
}
|
||||
|
||||
// Calculate max zoom immediately after determining max size
|
||||
calculateMaxZoom();
|
||||
}
|
||||
|
||||
void SDLManager::calculateMaxZoom() {
|
||||
// Maximum zoom limited by BOTH width and height (preserves 4:3)
|
||||
float max_zoom_width = static_cast<float>(max_width_) / Defaults::Window::WIDTH;
|
||||
float max_zoom_height = static_cast<float>(max_height_) / Defaults::Window::HEIGHT;
|
||||
|
||||
// Take smaller constraint
|
||||
float max_zoom_unrounded = std::min(max_zoom_width, max_zoom_height);
|
||||
|
||||
// Round DOWN to nearest 0.1 increment (user preference)
|
||||
max_zoom_ = std::floor(max_zoom_unrounded / Defaults::Window::ZOOM_INCREMENT) * Defaults::Window::ZOOM_INCREMENT;
|
||||
|
||||
// Safety clamp
|
||||
max_zoom_ = std::max(max_zoom_, Defaults::Window::MIN_ZOOM);
|
||||
|
||||
std::cout << "Max zoom: " << max_zoom_ << "x (display: "
|
||||
<< max_width_ << "x" << max_height_ << ")" << std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::applyZoom(float new_zoom) {
|
||||
// Clamp to valid range
|
||||
new_zoom = std::max(Defaults::Window::MIN_ZOOM,
|
||||
std::min(new_zoom, max_zoom_));
|
||||
|
||||
// Round to nearest 0.1 increment
|
||||
new_zoom = std::round(new_zoom / Defaults::Window::ZOOM_INCREMENT) * Defaults::Window::ZOOM_INCREMENT;
|
||||
|
||||
// No change?
|
||||
if (std::abs(new_zoom - zoom_factor_) < 0.01f) {
|
||||
return;
|
||||
}
|
||||
|
||||
zoom_factor_ = new_zoom;
|
||||
|
||||
// Calculate physical dimensions (4:3 maintained automatically)
|
||||
int new_width = static_cast<int>(std::round(
|
||||
Defaults::Window::WIDTH * zoom_factor_));
|
||||
int new_height = static_cast<int>(std::round(
|
||||
Defaults::Window::HEIGHT * zoom_factor_));
|
||||
|
||||
// Apply to window (centers via applyWindowSize)
|
||||
applyWindowSize(new_width, new_height);
|
||||
|
||||
// Update viewport for new zoom
|
||||
updateViewport();
|
||||
|
||||
// Update windowed size cache
|
||||
windowed_width_ = new_width;
|
||||
windowed_height_ = new_height;
|
||||
|
||||
// Persist
|
||||
Options::window.width = new_width;
|
||||
Options::window.height = new_height;
|
||||
Options::window.zoom_factor = zoom_factor_;
|
||||
|
||||
std::cout << "Zoom: " << zoom_factor_ << "x ("
|
||||
<< new_width << "x" << new_height << ")" << std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::updateLogicalPresentation() {
|
||||
// AIXÒ ÉS LA MÀGIA: El joc SEMPRE dibuixa en 640x480,
|
||||
// SDL escala automàticament a la mida física de la finestra
|
||||
SDL_SetRenderLogicalPresentation(
|
||||
renderer_,
|
||||
Defaults::Game::WIDTH, // 640 (lògic)
|
||||
Defaults::Game::HEIGHT, // 480 (lògic)
|
||||
SDL_LOGICAL_PRESENTATION_LETTERBOX // Mantenir aspect ratio 4:3
|
||||
);
|
||||
// CANVIAT: Ja no usem SDL_SetRenderLogicalPresentation
|
||||
// Ara renderitzem directament a resolució física per evitar pixelació irregular
|
||||
// El viewport amb letterbox es configura a updateViewport()
|
||||
updateViewport();
|
||||
}
|
||||
|
||||
void SDLManager::updateViewport() {
|
||||
// Calcular dimensions físiques basades en el zoom
|
||||
float scale = zoom_factor_;
|
||||
int scaled_width = static_cast<int>(std::round(Defaults::Game::WIDTH * scale));
|
||||
int scaled_height = static_cast<int>(std::round(Defaults::Game::HEIGHT * scale));
|
||||
|
||||
// Càlcul de letterbox (centrar l'àrea escalada)
|
||||
int offset_x = (current_width_ - scaled_width) / 2;
|
||||
int offset_y = (current_height_ - scaled_height) / 2;
|
||||
|
||||
// Evitar offsets negatius
|
||||
offset_x = std::max(offset_x, 0);
|
||||
offset_y = std::max(offset_y, 0);
|
||||
|
||||
// Configurar viewport per al renderitzat
|
||||
SDL_Rect viewport = {offset_x, offset_y, scaled_width, scaled_height};
|
||||
SDL_SetRenderViewport(renderer_, &viewport);
|
||||
|
||||
std::cout << "Viewport: " << scaled_width << "x" << scaled_height
|
||||
<< " @ (" << offset_x << "," << offset_y << ") [scale=" << scale << "]"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::updateRenderingContext() {
|
||||
// Actualitzar el factor d'escala global per a totes les funcions de renderitzat
|
||||
Rendering::g_current_scale_factor = zoom_factor_;
|
||||
}
|
||||
|
||||
void SDLManager::increaseWindowSize() {
|
||||
if (is_fullscreen_)
|
||||
return; // No operar en fullscreen
|
||||
if (is_fullscreen_)
|
||||
return;
|
||||
|
||||
int new_width = current_width_ + Defaults::Window::SIZE_INCREMENT;
|
||||
int new_height = current_height_ + Defaults::Window::SIZE_INCREMENT;
|
||||
float new_zoom = zoom_factor_ + Defaults::Window::ZOOM_INCREMENT;
|
||||
applyZoom(new_zoom);
|
||||
|
||||
// Clamp a màxim
|
||||
new_width = std::min(new_width, max_width_);
|
||||
new_height = std::min(new_height, max_height_);
|
||||
|
||||
if (new_width != current_width_ || new_height != current_height_) {
|
||||
applyWindowSize(new_width, new_height);
|
||||
|
||||
// Persistir canvis a Options (es guardarà a config.yaml al tancar)
|
||||
Options::window.width = current_width_;
|
||||
Options::window.height = current_height_;
|
||||
|
||||
std::cout << "F2: Finestra augmentada a " << new_width << "x" << new_height
|
||||
<< std::endl;
|
||||
}
|
||||
std::cout << "F2: Zoom aumentat a " << zoom_factor_ << "x" << std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::decreaseWindowSize() {
|
||||
if (is_fullscreen_)
|
||||
return;
|
||||
if (is_fullscreen_)
|
||||
return;
|
||||
|
||||
int new_width = current_width_ - Defaults::Window::SIZE_INCREMENT;
|
||||
int new_height = current_height_ - Defaults::Window::SIZE_INCREMENT;
|
||||
float new_zoom = zoom_factor_ - Defaults::Window::ZOOM_INCREMENT;
|
||||
applyZoom(new_zoom);
|
||||
|
||||
// Clamp a mínim
|
||||
new_width = std::max(new_width, Defaults::Window::MIN_WIDTH);
|
||||
new_height = std::max(new_height, Defaults::Window::MIN_HEIGHT);
|
||||
|
||||
if (new_width != current_width_ || new_height != current_height_) {
|
||||
applyWindowSize(new_width, new_height);
|
||||
|
||||
// Persistir canvis a Options (es guardarà a config.yaml al tancar)
|
||||
Options::window.width = current_width_;
|
||||
Options::window.height = current_height_;
|
||||
|
||||
std::cout << "F1: Finestra reduïda a " << new_width << "x" << new_height
|
||||
<< std::endl;
|
||||
}
|
||||
std::cout << "F1: Zoom reduït a " << zoom_factor_ << "x" << std::endl;
|
||||
}
|
||||
|
||||
void SDLManager::applyWindowSize(int new_width, int new_height) {
|
||||
// Obtenir posició actual ABANS del resize
|
||||
int old_x, old_y;
|
||||
SDL_GetWindowPosition(finestra_, &old_x, &old_y);
|
||||
// Obtenir posició actual ABANS del resize
|
||||
int old_x, old_y;
|
||||
SDL_GetWindowPosition(finestra_, &old_x, &old_y);
|
||||
|
||||
int old_width = current_width_;
|
||||
int old_height = current_height_;
|
||||
int old_width = current_width_;
|
||||
int old_height = current_height_;
|
||||
|
||||
// Actualitzar mida
|
||||
SDL_SetWindowSize(finestra_, new_width, new_height);
|
||||
current_width_ = new_width;
|
||||
current_height_ = new_height;
|
||||
// Actualitzar mida
|
||||
SDL_SetWindowSize(finestra_, new_width, new_height);
|
||||
current_width_ = new_width;
|
||||
current_height_ = new_height;
|
||||
|
||||
// CENTRADO INTEL·LIGENT (algoritme de pollo)
|
||||
// Calcular nova posició per mantenir la finestra centrada sobre si mateixa
|
||||
int delta_width = old_width - new_width;
|
||||
int delta_height = old_height - new_height;
|
||||
// CENTRADO INTEL·LIGENT (algoritme de pollo)
|
||||
// Calcular nova posició per mantenir la finestra centrada sobre si mateixa
|
||||
int delta_width = old_width - new_width;
|
||||
int delta_height = old_height - new_height;
|
||||
|
||||
int new_x = old_x + (delta_width / 2);
|
||||
int new_y = old_y + (delta_height / 2);
|
||||
int new_x = old_x + (delta_width / 2);
|
||||
int new_y = old_y + (delta_height / 2);
|
||||
|
||||
// Evitar que la finestra surti de la pantalla
|
||||
constexpr int TITLEBAR_HEIGHT = 35; // Alçada aproximada de la barra de títol
|
||||
new_x = std::max(new_x, 0);
|
||||
new_y = std::max(new_y, TITLEBAR_HEIGHT);
|
||||
// Evitar que la finestra surti de la pantalla
|
||||
constexpr int TITLEBAR_HEIGHT = 35; // Alçada aproximada de la barra de títol
|
||||
new_x = std::max(new_x, 0);
|
||||
new_y = std::max(new_y, TITLEBAR_HEIGHT);
|
||||
|
||||
SDL_SetWindowPosition(finestra_, new_x, new_y);
|
||||
SDL_SetWindowPosition(finestra_, new_x, new_y);
|
||||
|
||||
// NO cal actualitzar el logical presentation aquí,
|
||||
// SDL ho maneja automàticament
|
||||
// Actualitzar viewport després del resize
|
||||
updateViewport();
|
||||
}
|
||||
|
||||
void SDLManager::toggleFullscreen() {
|
||||
is_fullscreen_ = !is_fullscreen_;
|
||||
SDL_SetWindowFullscreen(finestra_, is_fullscreen_);
|
||||
if (!is_fullscreen_) {
|
||||
// ENTERING FULLSCREEN
|
||||
windowed_width_ = current_width_;
|
||||
windowed_height_ = current_height_;
|
||||
|
||||
// Persistir canvis a Options (es guardarà a config.yaml al tancar)
|
||||
Options::window.fullscreen = is_fullscreen_;
|
||||
is_fullscreen_ = true;
|
||||
SDL_SetWindowFullscreen(finestra_, true);
|
||||
|
||||
std::cout << "F3: Fullscreen " << (is_fullscreen_ ? "activat" : "desactivat")
|
||||
<< std::endl;
|
||||
std::cout << "F3: Fullscreen activat (guardada: "
|
||||
<< windowed_width_ << "x" << windowed_height_ << ")" << std::endl;
|
||||
} else {
|
||||
// EXITING FULLSCREEN
|
||||
is_fullscreen_ = false;
|
||||
SDL_SetWindowFullscreen(finestra_, false);
|
||||
|
||||
// En fullscreen, SDL gestiona tot automàticament
|
||||
// En sortir, restaura la mida anterior
|
||||
// CRITICAL: Explicitly restore windowed size
|
||||
applyWindowSize(windowed_width_, windowed_height_);
|
||||
|
||||
std::cout << "F3: Fullscreen desactivat (restaurada: "
|
||||
<< windowed_width_ << "x" << windowed_height_ << ")" << std::endl;
|
||||
}
|
||||
|
||||
Options::window.fullscreen = is_fullscreen_;
|
||||
|
||||
// Notificar al mòdul Mouse: Fullscreen requereix ocultació permanent del cursor.
|
||||
// Quan es surt de fullscreen, restaurar el comportament normal d'auto-ocultació.
|
||||
Mouse::setForceHidden(is_fullscreen_);
|
||||
}
|
||||
|
||||
bool SDLManager::handleWindowEvent(const SDL_Event &event) {
|
||||
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
|
||||
// Usuari ha redimensionat manualment (arrossegar vora)
|
||||
// Actualitzar el nostre tracking
|
||||
SDL_GetWindowSize(finestra_, ¤t_width_, ¤t_height_);
|
||||
std::cout << "Finestra redimensionada manualment a " << current_width_
|
||||
<< "x" << current_height_ << std::endl;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
bool SDLManager::handleWindowEvent(const SDL_Event& event) {
|
||||
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
|
||||
SDL_GetWindowSize(finestra_, ¤t_width_, ¤t_height_);
|
||||
|
||||
// Calculate zoom from actual size (may not align to 0.1 increments)
|
||||
float new_zoom = static_cast<float>(current_width_) / Defaults::Window::WIDTH;
|
||||
zoom_factor_ = std::max(Defaults::Window::MIN_ZOOM,
|
||||
std::min(new_zoom, max_zoom_));
|
||||
|
||||
// Update windowed cache (if not in fullscreen)
|
||||
if (!is_fullscreen_) {
|
||||
windowed_width_ = current_width_;
|
||||
windowed_height_ = current_height_;
|
||||
}
|
||||
|
||||
// Actualitzar viewport després del resize manual
|
||||
updateViewport();
|
||||
|
||||
std::cout << "Finestra redimensionada: " << current_width_
|
||||
<< "x" << current_height_ << " (zoom ≈" << zoom_factor_ << "x)"
|
||||
<< std::endl;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SDLManager::neteja(uint8_t r, uint8_t g, uint8_t b) {
|
||||
if (!renderer_)
|
||||
return;
|
||||
if (!renderer_)
|
||||
return;
|
||||
|
||||
// [MODIFICAT] Usar color oscil·lat del fons en lloc dels paràmetres
|
||||
(void)r;
|
||||
(void)g;
|
||||
(void)b; // Suprimir warnings
|
||||
SDL_Color bg = color_oscillator_.getCurrentBackgroundColor();
|
||||
SDL_SetRenderDrawColor(renderer_, bg.r, bg.g, bg.b, 255);
|
||||
SDL_RenderClear(renderer_);
|
||||
// [MODIFICAT] Usar color oscil·lat del fons en lloc dels paràmetres
|
||||
(void)r;
|
||||
(void)g;
|
||||
(void)b; // Suprimir warnings
|
||||
SDL_Color bg = color_oscillator_.getCurrentBackgroundColor();
|
||||
SDL_SetRenderDrawColor(renderer_, bg.r, bg.g, bg.b, 255);
|
||||
SDL_RenderClear(renderer_);
|
||||
}
|
||||
|
||||
void SDLManager::presenta() {
|
||||
if (!renderer_)
|
||||
return;
|
||||
if (!renderer_)
|
||||
return;
|
||||
|
||||
SDL_RenderPresent(renderer_);
|
||||
SDL_RenderPresent(renderer_);
|
||||
}
|
||||
|
||||
// [NUEVO] Actualitzar colors amb oscil·lació
|
||||
void SDLManager::updateColors(float delta_time) {
|
||||
color_oscillator_.update(delta_time);
|
||||
color_oscillator_.update(delta_time);
|
||||
|
||||
// Actualitzar color global de línies
|
||||
Rendering::setLineColor(color_oscillator_.getCurrentLineColor());
|
||||
// Actualitzar color global de línies
|
||||
Rendering::setLineColor(color_oscillator_.getCurrentLineColor());
|
||||
}
|
||||
|
||||
// [NUEVO] Actualitzar comptador de FPS
|
||||
void SDLManager::updateFPS(float delta_time) {
|
||||
// Acumular temps i frames
|
||||
fps_accumulator_ += delta_time;
|
||||
fps_frame_count_++;
|
||||
|
||||
// Actualitzar display cada 0.5 segons
|
||||
if (fps_accumulator_ >= 0.5f) {
|
||||
fps_display_ = static_cast<int>(fps_frame_count_ / fps_accumulator_);
|
||||
fps_frame_count_ = 0;
|
||||
fps_accumulator_ = 0.0f;
|
||||
|
||||
// Actualitzar títol de la finestra
|
||||
std::string vsync_state = (Options::rendering.vsync == 1) ? "ON" : "OFF";
|
||||
std::string title = std::format("{} v{} ({}) - {} FPS - VSync: {}",
|
||||
Project::LONG_NAME,
|
||||
Project::VERSION,
|
||||
Project::COPYRIGHT,
|
||||
fps_display_,
|
||||
vsync_state);
|
||||
|
||||
if (finestra_) {
|
||||
SDL_SetWindowTitle(finestra_, title.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [NUEVO] Actualitzar títol de la finestra
|
||||
void SDLManager::setWindowTitle(const std::string& title) {
|
||||
if (finestra_) {
|
||||
SDL_SetWindowTitle(finestra_, title.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// [NUEVO] Toggle V-Sync (F4)
|
||||
void SDLManager::toggleVSync() {
|
||||
// Toggle: 1 → 0 → 1
|
||||
Options::rendering.vsync = (Options::rendering.vsync == 1) ? 0 : 1;
|
||||
|
||||
// Aplicar a SDL
|
||||
if (renderer_) {
|
||||
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
|
||||
}
|
||||
|
||||
// Reset FPS counter para evitar valores mixtos entre regímenes
|
||||
fps_accumulator_ = 0.0f;
|
||||
fps_frame_count_ = 0;
|
||||
|
||||
// Guardar configuració
|
||||
Options::saveToFile();
|
||||
}
|
||||
|
||||
@@ -5,55 +5,81 @@
|
||||
#define SDL_MANAGER_HPP
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "core/rendering/color_oscillator.hpp"
|
||||
|
||||
class SDLManager {
|
||||
public:
|
||||
SDLManager(); // Constructor per defecte (usa Defaults::)
|
||||
SDLManager(int width, int height,
|
||||
bool fullscreen); // Constructor amb configuració
|
||||
~SDLManager();
|
||||
public:
|
||||
SDLManager(); // Constructor per defecte (usa Defaults::)
|
||||
SDLManager(int width, int height, bool fullscreen); // Constructor amb configuració
|
||||
~SDLManager();
|
||||
|
||||
// No permetre còpia ni assignació
|
||||
SDLManager(const SDLManager &) = delete;
|
||||
SDLManager &operator=(const SDLManager &) = delete;
|
||||
// No permetre còpia ni assignació
|
||||
SDLManager(const SDLManager&) = delete;
|
||||
SDLManager& operator=(const SDLManager&) = delete;
|
||||
|
||||
// [NUEVO] Gestió de finestra dinàmica
|
||||
void increaseWindowSize(); // F2: +100px
|
||||
void decreaseWindowSize(); // F1: -100px
|
||||
void toggleFullscreen(); // F3
|
||||
bool
|
||||
handleWindowEvent(const SDL_Event &event); // Per a SDL_EVENT_WINDOW_RESIZED
|
||||
// [NUEVO] Gestió de finestra dinàmica
|
||||
void increaseWindowSize(); // F2: +100px
|
||||
void decreaseWindowSize(); // F1: -100px
|
||||
void toggleFullscreen(); // F3
|
||||
void toggleVSync(); // F4
|
||||
bool handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED
|
||||
|
||||
// Funcions principals (renderitzat)
|
||||
void neteja(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0);
|
||||
void presenta();
|
||||
// Funcions principals (renderitzat)
|
||||
void neteja(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0);
|
||||
void presenta();
|
||||
|
||||
// [NUEVO] Actualització de colors (oscil·lació)
|
||||
void updateColors(float delta_time);
|
||||
// [NUEVO] Actualització de colors (oscil·lació)
|
||||
void updateColors(float delta_time);
|
||||
|
||||
// Getters
|
||||
SDL_Renderer *obte_renderer() { return renderer_; }
|
||||
// [NUEVO] Actualitzar comptador de FPS
|
||||
void updateFPS(float delta_time);
|
||||
|
||||
private:
|
||||
SDL_Window *finestra_;
|
||||
SDL_Renderer *renderer_;
|
||||
// Getters
|
||||
SDL_Renderer* obte_renderer() { return renderer_; }
|
||||
float getScaleFactor() const { return zoom_factor_; }
|
||||
|
||||
// [NUEVO] Estat de la finestra
|
||||
int current_width_; // Mida física actual
|
||||
int current_height_;
|
||||
bool is_fullscreen_;
|
||||
int max_width_; // Calculat des del display
|
||||
int max_height_;
|
||||
// [NUEVO] Actualitzar títol de la finestra
|
||||
void setWindowTitle(const std::string& title);
|
||||
|
||||
// [NUEVO] Funcions internes
|
||||
void calculateMaxWindowSize(); // Llegir resolució del display
|
||||
void applyWindowSize(int width, int height); // Canviar mida + centrar
|
||||
void updateLogicalPresentation(); // Actualitzar viewport
|
||||
// [NUEVO] Actualitzar context de renderitzat (factor d'escala global)
|
||||
void updateRenderingContext();
|
||||
|
||||
// [NUEVO] Oscil·lador de colors
|
||||
Rendering::ColorOscillator color_oscillator_;
|
||||
private:
|
||||
SDL_Window* finestra_;
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Variables FPS
|
||||
float fps_accumulator_;
|
||||
int fps_frame_count_;
|
||||
int fps_display_;
|
||||
|
||||
// [NUEVO] Estat de la finestra
|
||||
int current_width_; // Mida física actual
|
||||
int current_height_;
|
||||
bool is_fullscreen_;
|
||||
int max_width_; // Calculat des del display
|
||||
int max_height_;
|
||||
|
||||
// [ZOOM SYSTEM]
|
||||
float zoom_factor_; // Current zoom (0.5x to max_zoom_)
|
||||
int windowed_width_; // Saved size before fullscreen
|
||||
int windowed_height_; // Saved size before fullscreen
|
||||
float max_zoom_; // Maximum zoom (calculated from display)
|
||||
|
||||
// [NUEVO] Funcions internes
|
||||
void calculateMaxWindowSize(); // Llegir resolució del display
|
||||
void calculateMaxZoom(); // Calculate max zoom from display
|
||||
void applyZoom(float new_zoom); // Apply zoom and resize window
|
||||
void applyWindowSize(int width, int height); // Canviar mida + centrar
|
||||
void updateLogicalPresentation(); // Actualitzar viewport
|
||||
void updateViewport(); // Configurar viewport amb letterbox
|
||||
|
||||
// [NUEVO] Oscil·lador de colors
|
||||
Rendering::ColorOscillator color_oscillator_;
|
||||
};
|
||||
|
||||
#endif // SDL_MANAGER_HPP
|
||||
#endif // SDL_MANAGER_HPP
|
||||
|
||||
80
source/core/rendering/shape_renderer.cpp
Normal file
80
source/core/rendering/shape_renderer.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
// shape_renderer.cpp - Implementació del renderitzat de formes
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
// Helper: transformar un punt amb rotació, escala i trasllació
|
||||
static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala) {
|
||||
// 1. Centrar el punt respecte al centre de la forma
|
||||
float centered_x = point.x - shape_centre.x;
|
||||
float centered_y = point.y - shape_centre.y;
|
||||
|
||||
// 2. Aplicar escala al punt centrat
|
||||
float scaled_x = centered_x * escala;
|
||||
float scaled_y = centered_y * escala;
|
||||
|
||||
// 3. Aplicar rotació
|
||||
// IMPORTANT: En el sistema original, angle=0 apunta AMUNT (no dreta)
|
||||
// Per això usem (angle - PI/2) per compensar
|
||||
// Però aquí angle ja ve en el sistema correcte del joc
|
||||
float cos_a = std::cos(angle);
|
||||
float sin_a = std::sin(angle);
|
||||
|
||||
float rotated_x = scaled_x * cos_a - scaled_y * sin_a;
|
||||
float rotated_y = scaled_x * sin_a + scaled_y * cos_a;
|
||||
|
||||
// 4. Aplicar trasllació a posició mundial
|
||||
return {rotated_x + posicio.x, rotated_y + posicio.y};
|
||||
}
|
||||
|
||||
void render_shape(SDL_Renderer* renderer,
|
||||
const std::shared_ptr<Graphics::Shape>& shape,
|
||||
const Punt& posicio,
|
||||
float angle,
|
||||
float escala,
|
||||
bool dibuixar,
|
||||
float progress,
|
||||
float brightness) {
|
||||
// Verificar que la forma és vàlida
|
||||
if (!shape || !shape->es_valida()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Si progress < 1.0, no dibuixar (tot o res)
|
||||
if (progress < 1.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Obtenir el centre de la forma per a transformacions
|
||||
const Punt& shape_centre = shape->get_centre();
|
||||
|
||||
// Iterar sobre totes les primitives
|
||||
for (const auto& primitive : shape->get_primitives()) {
|
||||
if (primitive.type == Graphics::PrimitiveType::POLYLINE) {
|
||||
// POLYLINE: connectar punts consecutius
|
||||
for (size_t i = 0; i < primitive.points.size() - 1; i++) {
|
||||
Punt p1 = transform_point(primitive.points[i], shape_centre, posicio, angle, escala);
|
||||
Punt p2 = transform_point(primitive.points[i + 1], shape_centre, posicio, angle, escala);
|
||||
|
||||
linea(renderer, static_cast<int>(p1.x), static_cast<int>(p1.y), static_cast<int>(p2.x), static_cast<int>(p2.y), dibuixar, brightness);
|
||||
}
|
||||
} else { // PrimitiveType::LINE
|
||||
// LINE: exactament 2 punts
|
||||
if (primitive.points.size() >= 2) {
|
||||
Punt p1 = transform_point(primitive.points[0], shape_centre, posicio, angle, escala);
|
||||
Punt p2 = transform_point(primitive.points[1], shape_centre, posicio, angle, escala);
|
||||
|
||||
linea(renderer, static_cast<int>(p1.x), static_cast<int>(p1.y), static_cast<int>(p2.x), static_cast<int>(p2.y), dibuixar, brightness);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Rendering
|
||||
33
source/core/rendering/shape_renderer.hpp
Normal file
33
source/core/rendering/shape_renderer.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// shape_renderer.hpp - Renderitzat de formes vectorials
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Rendering {
|
||||
|
||||
// Renderitzar forma amb transformacions
|
||||
// - renderer: SDL renderer
|
||||
// - shape: forma vectorial a dibuixar
|
||||
// - posicio: posició del centre en coordenades mundials
|
||||
// - angle: rotació en radians (0 = amunt, sentit horari)
|
||||
// - escala: factor d'escala (1.0 = mida original)
|
||||
// - dibuixar: flag per dibuixar (false per col·lisions futures)
|
||||
// - progress: progrés de l'animació (0.0-1.0, default 1.0 = tot visible)
|
||||
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
|
||||
void render_shape(SDL_Renderer* renderer,
|
||||
const std::shared_ptr<Graphics::Shape>& shape,
|
||||
const Punt& posicio,
|
||||
float angle,
|
||||
float escala = 1.0f,
|
||||
bool dibuixar = true,
|
||||
float progress = 1.0f,
|
||||
float brightness = 1.0f);
|
||||
|
||||
} // namespace Rendering
|
||||
83
source/core/resources/resource_helper.cpp
Normal file
83
source/core/resources/resource_helper.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
// resource_helper.cpp - Implementació de funcions d'ajuda
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "resource_helper.hpp"
|
||||
|
||||
#include "resource_loader.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace Resource {
|
||||
namespace Helper {
|
||||
|
||||
// Inicialitzar el sistema de recursos
|
||||
bool initializeResourceSystem(const std::string& pack_file, bool fallback) {
|
||||
return Loader::get().initialize(pack_file, fallback);
|
||||
}
|
||||
|
||||
// Carregar un fitxer
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath) {
|
||||
// Normalitzar la ruta
|
||||
std::string normalized = normalizePath(filepath);
|
||||
|
||||
// Carregar del sistema de recursos
|
||||
return Loader::get().loadResource(normalized);
|
||||
}
|
||||
|
||||
// Comprovar si existeix un fitxer
|
||||
bool fileExists(const std::string& filepath) {
|
||||
std::string normalized = normalizePath(filepath);
|
||||
return Loader::get().resourceExists(normalized);
|
||||
}
|
||||
|
||||
// Obtenir ruta normalitzada per al paquet
|
||||
// Elimina prefixos "data/", rutes absolutes, etc.
|
||||
std::string getPackPath(const std::string& asset_path) {
|
||||
std::string path = asset_path;
|
||||
|
||||
// Eliminar rutes absolutes (detectar / o C:\ al principi)
|
||||
if (!path.empty() && path[0] == '/') {
|
||||
// Buscar "data/" i agafar el que ve després
|
||||
size_t data_pos = path.find("/data/");
|
||||
if (data_pos != std::string::npos) {
|
||||
path = path.substr(data_pos + 6); // Saltar "/data/"
|
||||
}
|
||||
}
|
||||
|
||||
// Eliminar "./" i "../" del principi
|
||||
while (path.starts_with("./")) {
|
||||
path = path.substr(2);
|
||||
}
|
||||
while (path.starts_with("../")) {
|
||||
path = path.substr(3);
|
||||
}
|
||||
|
||||
// Eliminar "data/" del principi
|
||||
if (path.starts_with("data/")) {
|
||||
path = path.substr(5);
|
||||
}
|
||||
|
||||
// Eliminar "Resources/" (macOS bundles)
|
||||
if (path.starts_with("Resources/")) {
|
||||
path = path.substr(10);
|
||||
}
|
||||
|
||||
// Convertir barres invertides a normals
|
||||
std::ranges::replace(path, '\\', '/');
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
// Normalitzar ruta (alias de getPackPath)
|
||||
std::string normalizePath(const std::string& path) {
|
||||
return getPackPath(path);
|
||||
}
|
||||
|
||||
// Comprovar si hi ha paquet carregat
|
||||
bool isPackLoaded() {
|
||||
return Loader::get().isPackLoaded();
|
||||
}
|
||||
|
||||
} // namespace Helper
|
||||
} // namespace Resource
|
||||
28
source/core/resources/resource_helper.hpp
Normal file
28
source/core/resources/resource_helper.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
// resource_helper.hpp - Funcions d'ajuda per gestió de recursos
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
// API simplificada i normalització de rutes
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Resource {
|
||||
namespace Helper {
|
||||
|
||||
// Inicialització del sistema
|
||||
bool initializeResourceSystem(const std::string& pack_file, bool fallback);
|
||||
|
||||
// Càrrega de fitxers
|
||||
std::vector<uint8_t> loadFile(const std::string& filepath);
|
||||
bool fileExists(const std::string& filepath);
|
||||
|
||||
// Normalització de rutes
|
||||
std::string getPackPath(const std::string& asset_path);
|
||||
std::string normalizePath(const std::string& path);
|
||||
|
||||
// Estat
|
||||
bool isPackLoaded();
|
||||
|
||||
} // namespace Helper
|
||||
} // namespace Resource
|
||||
143
source/core/resources/resource_loader.cpp
Normal file
143
source/core/resources/resource_loader.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
// resource_loader.cpp - Implementació del carregador de recursos
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "resource_loader.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace Resource {
|
||||
|
||||
// Singleton
|
||||
Loader& Loader::get() {
|
||||
static Loader instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Inicialitzar el sistema de recursos
|
||||
bool Loader::initialize(const std::string& pack_file, bool enable_fallback) {
|
||||
fallback_enabled_ = enable_fallback;
|
||||
|
||||
// Intentar carregar el paquet
|
||||
pack_ = std::make_unique<Pack>();
|
||||
|
||||
if (!pack_->loadPack(pack_file)) {
|
||||
if (!fallback_enabled_) {
|
||||
std::cerr << "[ResourceLoader] ERROR FATAL: No es pot carregar " << pack_file
|
||||
<< " i el fallback està desactivat\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "[ResourceLoader] Paquet no trobat, usant fallback al sistema de fitxers\n";
|
||||
pack_.reset(); // No hi ha paquet
|
||||
return true;
|
||||
}
|
||||
|
||||
std::cout << "[ResourceLoader] Paquet carregat: " << pack_file << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carregar un recurs
|
||||
std::vector<uint8_t> Loader::loadResource(const std::string& filename) {
|
||||
// Intentar carregar del paquet primer
|
||||
if (pack_) {
|
||||
if (pack_->hasResource(filename)) {
|
||||
auto data = pack_->getResource(filename);
|
||||
if (!data.empty()) {
|
||||
return data;
|
||||
}
|
||||
std::cerr << "[ResourceLoader] Advertència: recurs buit al paquet: " << filename
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
// Si no està al paquet i no hi ha fallback, falla
|
||||
if (!fallback_enabled_) {
|
||||
std::cerr << "[ResourceLoader] ERROR: Recurs no trobat al paquet i fallback desactivat: "
|
||||
<< filename << "\n";
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback al sistema de fitxers
|
||||
if (fallback_enabled_) {
|
||||
return loadFromFilesystem(filename);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// Comprovar si existeix un recurs
|
||||
bool Loader::resourceExists(const std::string& filename) {
|
||||
// Comprovar al paquet
|
||||
if (pack_ && pack_->hasResource(filename)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Comprovar al sistema de fitxers si està activat el fallback
|
||||
if (fallback_enabled_) {
|
||||
std::string fullpath = base_path_.empty() ? "data/" + filename : base_path_ + "/data/" + filename;
|
||||
return std::filesystem::exists(fullpath);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validar el paquet
|
||||
bool Loader::validatePack() {
|
||||
if (!pack_) {
|
||||
std::cerr << "[ResourceLoader] Advertència: no hi ha paquet carregat per validar\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
return pack_->validatePack();
|
||||
}
|
||||
|
||||
// Comprovar si hi ha paquet carregat
|
||||
bool Loader::isPackLoaded() const {
|
||||
return pack_ != nullptr;
|
||||
}
|
||||
|
||||
// Establir la ruta base
|
||||
void Loader::setBasePath(const std::string& path) {
|
||||
base_path_ = path;
|
||||
std::cout << "[ResourceLoader] Ruta base establerta: " << base_path_ << "\n";
|
||||
}
|
||||
|
||||
// Obtenir la ruta base
|
||||
std::string Loader::getBasePath() const {
|
||||
return base_path_;
|
||||
}
|
||||
|
||||
// Carregar des del sistema de fitxers (fallback)
|
||||
std::vector<uint8_t> Loader::loadFromFilesystem(const std::string& filename) {
|
||||
// The filename is already normalized (e.g., "shapes/logo/letra_j.shp")
|
||||
// We need to prepend base_path + "data/"
|
||||
std::string fullpath;
|
||||
|
||||
if (base_path_.empty()) {
|
||||
fullpath = "data/" + filename;
|
||||
} else {
|
||||
fullpath = base_path_ + "/data/" + filename;
|
||||
}
|
||||
|
||||
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
std::cerr << "[ResourceLoader] Error: no es pot obrir " << fullpath << "\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::streamsize file_size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> data(file_size);
|
||||
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
|
||||
std::cerr << "[ResourceLoader] Error: no es pot llegir " << fullpath << "\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::cout << "[ResourceLoader] Carregat des del sistema de fitxers: " << fullpath << "\n";
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace Resource
|
||||
53
source/core/resources/resource_loader.hpp
Normal file
53
source/core/resources/resource_loader.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
// resource_loader.hpp - Carregador de recursos (Singleton)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
// Coordina càrrega des del paquet i/o sistema de fitxers
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "resource_pack.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Resource {
|
||||
|
||||
// Singleton per gestionar la càrrega de recursos
|
||||
class Loader {
|
||||
public:
|
||||
// Singleton
|
||||
static Loader& get();
|
||||
|
||||
// Inicialització
|
||||
bool initialize(const std::string& pack_file, bool enable_fallback);
|
||||
|
||||
// Càrrega de recursos
|
||||
std::vector<uint8_t> loadResource(const std::string& filename);
|
||||
bool resourceExists(const std::string& filename);
|
||||
|
||||
// Validació
|
||||
bool validatePack();
|
||||
bool isPackLoaded() const;
|
||||
|
||||
// Estat
|
||||
void setBasePath(const std::string& path);
|
||||
std::string getBasePath() const;
|
||||
|
||||
private:
|
||||
Loader() = default;
|
||||
~Loader() = default;
|
||||
|
||||
// No es pot copiar ni moure
|
||||
Loader(const Loader&) = delete;
|
||||
Loader& operator=(const Loader&) = delete;
|
||||
|
||||
// Dades
|
||||
std::unique_ptr<Pack> pack_;
|
||||
bool fallback_enabled_ = false;
|
||||
std::string base_path_;
|
||||
|
||||
// Funcions auxiliars
|
||||
std::vector<uint8_t> loadFromFilesystem(const std::string& filename);
|
||||
};
|
||||
|
||||
} // namespace Resource
|
||||
309
source/core/resources/resource_pack.cpp
Normal file
309
source/core/resources/resource_pack.cpp
Normal file
@@ -0,0 +1,309 @@
|
||||
// resource_pack.cpp - Implementació del sistema d'empaquetament
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "resource_pack.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace Resource {
|
||||
|
||||
// Calcular checksum CRC32 simplificat
|
||||
uint32_t Pack::calculateChecksum(const std::vector<uint8_t>& data) const {
|
||||
uint32_t checksum = 0x12345678;
|
||||
for (unsigned char byte : data) {
|
||||
checksum = ((checksum << 5) + checksum) + byte;
|
||||
}
|
||||
return checksum;
|
||||
}
|
||||
|
||||
// Encriptació XOR (simètrica)
|
||||
void Pack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
if (key.empty()) {
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
data[i] ^= key[i % key.length()];
|
||||
}
|
||||
}
|
||||
|
||||
void Pack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
|
||||
// XOR és simètric
|
||||
encryptData(data, key);
|
||||
}
|
||||
|
||||
// Llegir fitxer complet a memòria
|
||||
std::vector<uint8_t> Pack::readFile(const std::string& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!file) {
|
||||
std::cerr << "[ResourcePack] Error: no es pot obrir " << filepath << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
std::streamsize file_size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> data(file_size);
|
||||
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
|
||||
std::cerr << "[ResourcePack] Error: no es pot llegir " << filepath << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Afegir un fitxer individual al paquet
|
||||
bool Pack::addFile(const std::string& filepath, const std::string& pack_name) {
|
||||
auto file_data = readFile(filepath);
|
||||
if (file_data.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ResourceEntry entry{
|
||||
.filename = pack_name,
|
||||
.offset = data_.size(),
|
||||
.size = file_data.size(),
|
||||
.checksum = calculateChecksum(file_data)};
|
||||
|
||||
// Afegir dades al bloc de dades
|
||||
data_.insert(data_.end(), file_data.begin(), file_data.end());
|
||||
|
||||
resources_[pack_name] = entry;
|
||||
|
||||
std::cout << "[ResourcePack] Afegit: " << pack_name << " (" << file_data.size()
|
||||
<< " bytes)\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
// Afegir tots els fitxers d'un directori recursivament
|
||||
bool Pack::addDirectory(const std::string& dir_path,
|
||||
const std::string& base_path) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
|
||||
std::cerr << "[ResourcePack] Error: directori no trobat: " << dir_path << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string current_base = base_path.empty() ? "" : base_path + "/";
|
||||
|
||||
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
|
||||
if (!entry.is_regular_file()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string full_path = entry.path().string();
|
||||
std::string relative_path = entry.path().lexically_relative(dir_path).string();
|
||||
|
||||
// Convertir barres invertides a normals (Windows)
|
||||
std::ranges::replace(relative_path, '\\', '/');
|
||||
|
||||
// Saltar fitxers de desenvolupament
|
||||
if (relative_path.find(".world") != std::string::npos ||
|
||||
relative_path.find(".tsx") != std::string::npos ||
|
||||
relative_path.find(".DS_Store") != std::string::npos ||
|
||||
relative_path.find(".git") != std::string::npos) {
|
||||
std::cout << "[ResourcePack] Saltant: " << relative_path << '\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string pack_name = current_base + relative_path;
|
||||
addFile(full_path, pack_name);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Guardar paquet a disc
|
||||
bool Pack::savePack(const std::string& pack_file) {
|
||||
std::ofstream file(pack_file, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "[ResourcePack] Error: no es pot crear " << pack_file << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
// Escriure capçalera
|
||||
file.write(MAGIC_HEADER, 4);
|
||||
file.write(reinterpret_cast<const char*>(&VERSION), sizeof(VERSION));
|
||||
|
||||
// Escriure nombre de recursos
|
||||
auto resource_count = static_cast<uint32_t>(resources_.size());
|
||||
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
|
||||
|
||||
// Escriure metadades de recursos
|
||||
for (const auto& [name, entry] : resources_) {
|
||||
// Nom del fitxer
|
||||
auto name_len = static_cast<uint32_t>(entry.filename.length());
|
||||
file.write(reinterpret_cast<const char*>(&name_len), sizeof(name_len));
|
||||
file.write(entry.filename.c_str(), name_len);
|
||||
|
||||
// Offset, mida, checksum
|
||||
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
|
||||
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
|
||||
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
|
||||
}
|
||||
|
||||
// Encriptar dades
|
||||
std::vector<uint8_t> encrypted_data = data_;
|
||||
encryptData(encrypted_data, DEFAULT_ENCRYPT_KEY);
|
||||
|
||||
// Escriure mida de dades i dades encriptades
|
||||
auto data_size = static_cast<uint64_t>(encrypted_data.size());
|
||||
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
|
||||
file.write(reinterpret_cast<const char*>(encrypted_data.data()), encrypted_data.size());
|
||||
|
||||
std::cout << "[ResourcePack] Guardat: " << pack_file << " (" << resources_.size()
|
||||
<< " recursos, " << data_size << " bytes)\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carregar paquet des de disc
|
||||
bool Pack::loadPack(const std::string& pack_file) {
|
||||
std::ifstream file(pack_file, std::ios::binary);
|
||||
if (!file) {
|
||||
std::cerr << "[ResourcePack] Error: no es pot obrir " << pack_file << '\n';
|
||||
return false;
|
||||
}
|
||||
|
||||
// Llegir capçalera
|
||||
char magic[4];
|
||||
file.read(magic, 4);
|
||||
if (std::string(magic, 4) != MAGIC_HEADER) {
|
||||
std::cerr << "[ResourcePack] Error: capçalera invàlida (esperava " << MAGIC_HEADER
|
||||
<< ")\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t version;
|
||||
file.read(reinterpret_cast<char*>(&version), sizeof(version));
|
||||
if (version != VERSION) {
|
||||
std::cerr << "[ResourcePack] Error: versió incompatible (esperava " << VERSION
|
||||
<< ", trobat " << version << ")\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Llegir nombre de recursos
|
||||
uint32_t resource_count;
|
||||
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
|
||||
|
||||
// Llegir metadades de recursos
|
||||
resources_.clear();
|
||||
for (uint32_t i = 0; i < resource_count; ++i) {
|
||||
// Nom del fitxer
|
||||
uint32_t name_len;
|
||||
file.read(reinterpret_cast<char*>(&name_len), sizeof(name_len));
|
||||
|
||||
std::string filename(name_len, '\0');
|
||||
file.read(&filename[0], name_len);
|
||||
|
||||
// Offset, mida, checksum
|
||||
ResourceEntry entry;
|
||||
entry.filename = filename;
|
||||
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
|
||||
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
|
||||
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
|
||||
|
||||
resources_[filename] = entry;
|
||||
}
|
||||
|
||||
// Llegir dades encriptades
|
||||
uint64_t data_size;
|
||||
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
|
||||
|
||||
data_.resize(data_size);
|
||||
file.read(reinterpret_cast<char*>(data_.data()), data_size);
|
||||
|
||||
// Desencriptar
|
||||
decryptData(data_, DEFAULT_ENCRYPT_KEY);
|
||||
|
||||
std::cout << "[ResourcePack] Carregat: " << pack_file << " (" << resources_.size()
|
||||
<< " recursos)\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Obtenir un recurs del paquet
|
||||
std::vector<uint8_t> Pack::getResource(const std::string& filename) {
|
||||
auto it = resources_.find(filename);
|
||||
if (it == resources_.end()) {
|
||||
std::cerr << "[ResourcePack] Error: recurs no trobat: " << filename << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto& entry = it->second;
|
||||
|
||||
// Extreure dades
|
||||
if (entry.offset + entry.size > data_.size()) {
|
||||
std::cerr << "[ResourcePack] Error: offset/mida invàlid per " << filename << '\n';
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<uint8_t> resource_data(data_.begin() + entry.offset,
|
||||
data_.begin() + entry.offset + entry.size);
|
||||
|
||||
// Verificar checksum
|
||||
uint32_t computed_checksum = calculateChecksum(resource_data);
|
||||
if (computed_checksum != entry.checksum) {
|
||||
std::cerr << "[ResourcePack] ADVERTÈNCIA: checksum invàlid per " << filename
|
||||
<< " (esperat " << entry.checksum << ", calculat " << computed_checksum
|
||||
<< ")\n";
|
||||
// No falla, però adverteix
|
||||
}
|
||||
|
||||
return resource_data;
|
||||
}
|
||||
|
||||
// Comprovar si existeix un recurs
|
||||
bool Pack::hasResource(const std::string& filename) const {
|
||||
return resources_.find(filename) != resources_.end();
|
||||
}
|
||||
|
||||
// Obtenir llista de tots els recursos
|
||||
std::vector<std::string> Pack::getResourceList() const {
|
||||
std::vector<std::string> list;
|
||||
list.reserve(resources_.size());
|
||||
|
||||
for (const auto& [name, entry] : resources_) {
|
||||
list.push_back(name);
|
||||
}
|
||||
|
||||
std::ranges::sort(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
// Validar integritat del paquet
|
||||
bool Pack::validatePack() const {
|
||||
bool valid = true;
|
||||
|
||||
for (const auto& [name, entry] : resources_) {
|
||||
// Verificar offset i mida
|
||||
if (entry.offset + entry.size > data_.size()) {
|
||||
std::cerr << "[ResourcePack] Error de validació: " << name
|
||||
<< " té offset/mida invàlid\n";
|
||||
valid = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extreure i verificar checksum
|
||||
std::vector<uint8_t> resource_data(data_.begin() + entry.offset,
|
||||
data_.begin() + entry.offset + entry.size);
|
||||
|
||||
uint32_t computed_checksum = calculateChecksum(resource_data);
|
||||
if (computed_checksum != entry.checksum) {
|
||||
std::cerr << "[ResourcePack] Error de validació: " << name
|
||||
<< " té checksum invàlid\n";
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
std::cout << "[ResourcePack] Validació OK (" << resources_.size() << " recursos)\n";
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
} // namespace Resource
|
||||
67
source/core/resources/resource_pack.hpp
Normal file
67
source/core/resources/resource_pack.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
// resource_pack.hpp - Sistema d'empaquetament de recursos
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
// Basat en el sistema de "pollo" amb adaptacions per Orni Attack
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Resource {
|
||||
|
||||
// Capçalera del fitxer de paquet
|
||||
struct PackHeader {
|
||||
char magic[4]; // "ORNI"
|
||||
uint32_t version; // Versió del format (1)
|
||||
};
|
||||
|
||||
// Entrada de recurs dins el paquet
|
||||
struct ResourceEntry {
|
||||
std::string filename; // Nom del recurs (amb barres normals)
|
||||
uint64_t offset; // Posició dins el bloc de dades
|
||||
uint64_t size; // Mida en bytes
|
||||
uint32_t checksum; // Checksum CRC32 per verificació
|
||||
};
|
||||
|
||||
// Classe principal per gestionar paquets de recursos
|
||||
class Pack {
|
||||
public:
|
||||
Pack() = default;
|
||||
~Pack() = default;
|
||||
|
||||
// Afegir fitxers al paquet
|
||||
bool addFile(const std::string& filepath, const std::string& pack_name);
|
||||
bool addDirectory(const std::string& dir_path, const std::string& base_path = "");
|
||||
|
||||
// Guardar i carregar paquets
|
||||
bool savePack(const std::string& pack_file);
|
||||
bool loadPack(const std::string& pack_file);
|
||||
|
||||
// Accés a recursos
|
||||
std::vector<uint8_t> getResource(const std::string& filename);
|
||||
bool hasResource(const std::string& filename) const;
|
||||
std::vector<std::string> getResourceList() const;
|
||||
|
||||
// Validació
|
||||
bool validatePack() const;
|
||||
|
||||
private:
|
||||
// Constants
|
||||
static constexpr const char* MAGIC_HEADER = "ORNI";
|
||||
static constexpr uint32_t VERSION = 1;
|
||||
static constexpr const char* DEFAULT_ENCRYPT_KEY = "ORNI_RESOURCES_2025";
|
||||
|
||||
// Dades del paquet
|
||||
std::unordered_map<std::string, ResourceEntry> resources_;
|
||||
std::vector<uint8_t> data_;
|
||||
|
||||
// Funcions auxiliars
|
||||
std::vector<uint8_t> readFile(const std::string& filepath);
|
||||
uint32_t calculateChecksum(const std::vector<uint8_t>& data) const;
|
||||
void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||
};
|
||||
|
||||
} // namespace Resource
|
||||
70
source/core/system/context_escenes.hpp
Normal file
70
source/core/system/context_escenes.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
// context_escenes.hpp - Sistema de gestió d'escenes i context de transicions
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace GestorEscenes {
|
||||
|
||||
// Context de transició entre escenes
|
||||
// Conté l'escena destinació i opcions específiques per aquella escena
|
||||
class ContextEscenes {
|
||||
public:
|
||||
// Tipus d'escena del joc
|
||||
enum class Escena {
|
||||
LOGO, // Pantalla d'inici (logo JAILGAMES)
|
||||
TITOL, // Pantalla de títol amb menú
|
||||
JOC, // Joc principal (Asteroids)
|
||||
EIXIR // Sortir del programa
|
||||
};
|
||||
|
||||
// Opcions específiques per a cada escena
|
||||
enum class Opcio {
|
||||
NONE, // Sense opcions especials (comportament per defecte)
|
||||
JUMP_TO_TITLE_MAIN, // TITOL: Saltar directament a MAIN (starfield instantani)
|
||||
// MODE_DEMO, // JOC: Mode demostració amb IA (futur)
|
||||
};
|
||||
|
||||
// Constructor inicial amb escena LOGO i sense opcions
|
||||
ContextEscenes()
|
||||
: escena_desti_(Escena::LOGO),
|
||||
opcio_(Opcio::NONE) {}
|
||||
|
||||
// Canviar escena amb opció específica
|
||||
void canviar_escena(Escena nova_escena, Opcio opcio = Opcio::NONE) {
|
||||
escena_desti_ = nova_escena;
|
||||
opcio_ = opcio;
|
||||
}
|
||||
|
||||
// Consultar escena destinació
|
||||
[[nodiscard]] auto escena_desti() const -> Escena {
|
||||
return escena_desti_;
|
||||
}
|
||||
|
||||
// Consultar opció actual
|
||||
[[nodiscard]] auto opcio() const -> Opcio {
|
||||
return opcio_;
|
||||
}
|
||||
|
||||
// Consumir opció (retorna valor i reseteja a NONE)
|
||||
// Utilitzar quan l'escena processa l'opció
|
||||
[[nodiscard]] auto consumir_opcio() -> Opcio {
|
||||
Opcio valor = opcio_;
|
||||
opcio_ = Opcio::NONE;
|
||||
return valor;
|
||||
}
|
||||
|
||||
// Reset opció a NONE (sense retornar valor)
|
||||
void reset_opcio() {
|
||||
opcio_ = Opcio::NONE;
|
||||
}
|
||||
|
||||
private:
|
||||
Escena escena_desti_; // Escena a la qual transicionar
|
||||
Opcio opcio_; // Opció específica per l'escena
|
||||
};
|
||||
|
||||
// Variable global inline per gestionar l'escena actual (backward compatibility)
|
||||
// Sincronitzada amb context.escena_desti() pel Director
|
||||
inline ContextEscenes::Escena actual = ContextEscenes::Escena::LOGO;
|
||||
|
||||
} // namespace GestorEscenes
|
||||
261
source/core/system/director.cpp
Normal file
261
source/core/system/director.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
#include "director.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/audio/audio_cache.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/sdl_manager.hpp"
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
#include "core/resources/resource_loader.hpp"
|
||||
#include "core/utils/path_utils.hpp"
|
||||
#include "game/escenes/escena_joc.hpp"
|
||||
#include "game/escenes/escena_logo.hpp"
|
||||
#include "game/escenes/escena_titol.hpp"
|
||||
#include "game/options.hpp"
|
||||
#include "context_escenes.hpp"
|
||||
#include "project.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Using declarations per simplificar el codi
|
||||
using GestorEscenes::ContextEscenes;
|
||||
using Escena = ContextEscenes::Escena;
|
||||
|
||||
// Constructor
|
||||
Director::Director(std::vector<std::string> const& args) {
|
||||
std::cout << "Orni Attack - Inici\n";
|
||||
|
||||
// Inicialitzar opcions amb valors per defecte
|
||||
Options::init();
|
||||
|
||||
// Comprovar arguments del programa
|
||||
executable_path_ = checkProgramArguments(args);
|
||||
|
||||
// Inicialitzar sistema de rutes
|
||||
Utils::initializePathSystem(args[0].c_str());
|
||||
|
||||
// Obtenir ruta base dels recursos
|
||||
std::string resource_base = Utils::getResourceBasePath();
|
||||
|
||||
// Inicialitzar sistema de recursos
|
||||
#ifdef RELEASE_BUILD
|
||||
// Mode release: paquet obligatori, sense fallback
|
||||
std::string pack_path = resource_base + "/resources.pack";
|
||||
if (!Resource::Helper::initializeResourceSystem(pack_path, false)) {
|
||||
std::cerr << "ERROR FATAL: No es pot carregar " << pack_path << "\n";
|
||||
std::cerr << "El joc no pot continuar sense els recursos.\n";
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
// Validar integritat del paquet
|
||||
if (!Resource::Loader::get().validatePack()) {
|
||||
std::cerr << "ERROR FATAL: El paquet de recursos està corromput\n";
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
std::cout << "Sistema de recursos inicialitzat (mode release)\n";
|
||||
#else
|
||||
// Mode desenvolupament: intentar paquet amb fallback a data/
|
||||
std::string pack_path = resource_base + "/resources.pack";
|
||||
Resource::Helper::initializeResourceSystem(pack_path, true);
|
||||
|
||||
if (Resource::Helper::isPackLoaded()) {
|
||||
std::cout << "Sistema de recursos inicialitzat (mode dev amb paquet)\n";
|
||||
} else {
|
||||
std::cout << "Sistema de recursos inicialitzat (mode dev, fallback a data/)\n";
|
||||
}
|
||||
|
||||
// Establir ruta base per al fallback
|
||||
Resource::Loader::get().setBasePath(resource_base);
|
||||
#endif
|
||||
|
||||
// Crear carpetes del sistema
|
||||
createSystemFolder("jailgames");
|
||||
createSystemFolder(std::string("jailgames/") + Project::NAME);
|
||||
|
||||
// Establir ruta del fitxer de configuració
|
||||
Options::setConfigFile(system_folder_ + "/config.yaml");
|
||||
|
||||
// Carregar o crear configuració
|
||||
Options::loadFromFile();
|
||||
|
||||
if (Options::console) {
|
||||
std::cout << "Configuració carregada\n";
|
||||
std::cout << " Finestra: " << Options::window.width << "×"
|
||||
<< Options::window.height << '\n';
|
||||
std::cout << " Física: rotation=" << Options::physics.rotation_speed
|
||||
<< " rad/s\n";
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
Director::~Director() {
|
||||
// Guardar opcions
|
||||
Options::saveToFile();
|
||||
|
||||
// Cleanup audio
|
||||
Audio::destroy();
|
||||
|
||||
// Cleanup SDL
|
||||
SDL_Quit();
|
||||
|
||||
std::cout << "\nAdéu!\n";
|
||||
}
|
||||
|
||||
// Comprovar arguments del programa
|
||||
auto Director::checkProgramArguments(std::vector<std::string> const& args)
|
||||
-> std::string {
|
||||
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||
const std::string& argument = args[i];
|
||||
|
||||
if (argument == "--console") {
|
||||
Options::console = true;
|
||||
std::cout << "Mode consola activat\n";
|
||||
} else if (argument == "--reset-config") {
|
||||
Options::init();
|
||||
Options::saveToFile();
|
||||
std::cout << "Configuració restablida als valors per defecte\n";
|
||||
}
|
||||
}
|
||||
|
||||
return args[0]; // Retornar ruta de l'executable
|
||||
}
|
||||
|
||||
// Crear carpeta del sistema (específic per plataforma)
|
||||
void Director::createSystemFolder(const std::string& folder) {
|
||||
#ifdef _WIN32
|
||||
system_folder_ = std::string(getenv("APPDATA")) + "/" + folder;
|
||||
#elif __APPLE__
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
const char* homedir = pw->pw_dir;
|
||||
system_folder_ =
|
||||
std::string(homedir) + "/Library/Application Support/" + folder;
|
||||
#elif __linux__
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
const char* homedir = pw->pw_dir;
|
||||
system_folder_ = std::string(homedir) + "/.config/" + folder;
|
||||
|
||||
// CRÍTIC: Crear ~/.config si no existeix
|
||||
{
|
||||
std::string config_base_folder = std::string(homedir) + "/.config";
|
||||
int ret = mkdir(config_base_folder.c_str(), S_IRWXU);
|
||||
if (ret == -1 && errno != EEXIST) {
|
||||
printf("ERROR: No es pot crear la carpeta ~/.config\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Comprovar si la carpeta existeix
|
||||
struct stat st = {.st_dev = 0};
|
||||
if (stat(system_folder_.c_str(), &st) == -1) {
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
int ret = mkdir(system_folder_.c_str());
|
||||
#else
|
||||
int ret = mkdir(system_folder_.c_str(), S_IRWXU);
|
||||
#endif
|
||||
|
||||
if (ret == -1) {
|
||||
switch (errno) {
|
||||
case EACCES:
|
||||
printf("ERROR: Permisos denegats creant %s\n", system_folder_.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
case EEXIST:
|
||||
// La carpeta ja existeix (race condition), continuar
|
||||
break;
|
||||
|
||||
case ENAMETOOLONG:
|
||||
printf("ERROR: Ruta massa llarga: %s\n", system_folder_.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
default:
|
||||
perror("mkdir");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Options::console) {
|
||||
std::cout << "Carpeta del sistema: " << system_folder_ << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle principal del joc
|
||||
auto Director::run() -> int {
|
||||
// Calculate initial size from saved zoom_factor
|
||||
int initial_width = static_cast<int>(std::round(
|
||||
Defaults::Window::WIDTH * Options::window.zoom_factor));
|
||||
int initial_height = static_cast<int>(std::round(
|
||||
Defaults::Window::HEIGHT * Options::window.zoom_factor));
|
||||
|
||||
// Crear gestor SDL amb configuració de Options
|
||||
SDLManager sdl(initial_width, initial_height, Options::window.fullscreen);
|
||||
|
||||
// Inicialitzar sistema d'audio
|
||||
Audio::init();
|
||||
Audio::get()->setMusicVolume(1.0);
|
||||
Audio::get()->setSoundVolume(0.4);
|
||||
|
||||
// Precachejar música per evitar lag al començar
|
||||
AudioCache::getMusic("title.ogg");
|
||||
if (Options::console) {
|
||||
std::cout << "Música precachejada: "
|
||||
<< AudioCache::getMusicCacheSize() << " fitxers\n";
|
||||
}
|
||||
|
||||
// Crear context d'escenes
|
||||
ContextEscenes context;
|
||||
#ifdef _DEBUG
|
||||
context.canviar_escena(Escena::JOC);
|
||||
#else
|
||||
context.canviar_escena(Escena::LOGO);
|
||||
#endif
|
||||
|
||||
// Bucle principal de gestió d'escenes
|
||||
while (context.escena_desti() != Escena::EIXIR) {
|
||||
// Sincronitzar GestorEscenes::actual amb context
|
||||
// (altres sistemes encara poden llegir GestorEscenes::actual)
|
||||
GestorEscenes::actual = context.escena_desti();
|
||||
|
||||
switch (context.escena_desti()) {
|
||||
case Escena::LOGO: {
|
||||
EscenaLogo logo(sdl, context);
|
||||
logo.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
case Escena::TITOL: {
|
||||
EscenaTitol titol(sdl, context);
|
||||
titol.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
case Escena::JOC: {
|
||||
EscenaJoc joc(sdl, context);
|
||||
joc.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sincronitzar final amb GestorEscenes::actual
|
||||
GestorEscenes::actual = Escena::EIXIR;
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
source/core/system/director.hpp
Normal file
20
source/core/system/director.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Director {
|
||||
public:
|
||||
explicit Director(std::vector<std::string> const& args);
|
||||
~Director();
|
||||
|
||||
auto run() -> int; // Main game loop
|
||||
|
||||
private:
|
||||
std::string executable_path_;
|
||||
std::string system_folder_;
|
||||
|
||||
static auto checkProgramArguments(std::vector<std::string> const& args)
|
||||
-> std::string;
|
||||
void createSystemFolder(const std::string& folder);
|
||||
};
|
||||
54
source/core/system/global_events.cpp
Normal file
54
source/core/system/global_events.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// global_events.cpp - Implementació dels events globals
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#include "global_events.hpp"
|
||||
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/rendering/sdl_manager.hpp"
|
||||
#include "context_escenes.hpp"
|
||||
|
||||
// Using declarations per simplificar el codi
|
||||
using GestorEscenes::ContextEscenes;
|
||||
using Escena = ContextEscenes::Escena;
|
||||
|
||||
namespace GlobalEvents {
|
||||
|
||||
bool handle(const SDL_Event& event, SDLManager& sdl, ContextEscenes& context) {
|
||||
// Tecles globals de finestra (F1/F2/F3)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_F1:
|
||||
sdl.decreaseWindowSize();
|
||||
return true;
|
||||
case SDLK_F2:
|
||||
sdl.increaseWindowSize();
|
||||
return true;
|
||||
case SDLK_F3:
|
||||
sdl.toggleFullscreen();
|
||||
return true;
|
||||
case SDLK_F4:
|
||||
sdl.toggleVSync();
|
||||
return true;
|
||||
case SDLK_ESCAPE:
|
||||
context.canviar_escena(Escena::EIXIR);
|
||||
GestorEscenes::actual = Escena::EIXIR;
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Tancar finestra
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
context.canviar_escena(Escena::EIXIR);
|
||||
GestorEscenes::actual = Escena::EIXIR;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gestió del ratolí (auto-ocultar)
|
||||
Mouse::handleEvent(event);
|
||||
|
||||
return false; // Event no processat
|
||||
}
|
||||
|
||||
} // namespace GlobalEvents
|
||||
17
source/core/system/global_events.hpp
Normal file
17
source/core/system/global_events.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// global_events.hpp - Events globals del joc
|
||||
// Basat en el patró del projecte "pollo"
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
// Forward declarations
|
||||
class SDLManager;
|
||||
namespace GestorEscenes { class ContextEscenes; }
|
||||
|
||||
namespace GlobalEvents {
|
||||
// Processa events globals (F1/F2/F3/ESC/QUIT)
|
||||
// Retorna true si l'event ha estat processat i no cal seguir processant-lo
|
||||
bool handle(const SDL_Event& event, SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||
} // namespace GlobalEvents
|
||||
43
source/core/types.hpp
Normal file
43
source/core/types.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
|
||||
// Punt polar (coordenades polars)
|
||||
struct IPunt {
|
||||
float r; // Radi
|
||||
float angle; // Angle en radians
|
||||
};
|
||||
|
||||
// Punt cartesià
|
||||
struct Punt {
|
||||
float x, y;
|
||||
};
|
||||
|
||||
// ==============================================================================
|
||||
// DEPRECATED: Legacy types (replaced by Shape system)
|
||||
// ==============================================================================
|
||||
// These types are kept temporarily for chatarra_cosmica_ (Phase 10: explosions)
|
||||
// TODO Phase 10: Replace with particle system or remove completely
|
||||
|
||||
// Nau (triangle) - DEPRECATED: Now using Shape system (ship.shp)
|
||||
struct Triangle {
|
||||
IPunt p1, p2, p3;
|
||||
Punt centre;
|
||||
float angle;
|
||||
float velocitat;
|
||||
};
|
||||
|
||||
// Polígon (enemics i bales) - DEPRECATED: Now using Shape system (.shp files)
|
||||
struct Poligon {
|
||||
std::array<IPunt, Defaults::Entities::MAX_IPUNTS> ipuntx;
|
||||
Punt centre;
|
||||
float angle;
|
||||
float velocitat;
|
||||
uint8_t n;
|
||||
float drotacio;
|
||||
float rotacio;
|
||||
bool esta;
|
||||
};
|
||||
92
source/core/utils/path_utils.cpp
Normal file
92
source/core/utils/path_utils.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// path_utils.cpp - Implementació de utilitats de rutes
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "path_utils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
// Variables globals per guardar argv[0]
|
||||
static std::string executable_path_;
|
||||
static std::string executable_directory_;
|
||||
|
||||
// Inicialitzar el sistema de rutes amb argv[0]
|
||||
void initializePathSystem(const char* argv0) {
|
||||
if (!argv0) {
|
||||
std::cerr << "[PathUtils] ADVERTÈNCIA: argv[0] és nullptr\n";
|
||||
executable_path_ = "";
|
||||
executable_directory_ = ".";
|
||||
return;
|
||||
}
|
||||
|
||||
executable_path_ = argv0;
|
||||
|
||||
// Extreure el directori
|
||||
std::filesystem::path path(argv0);
|
||||
executable_directory_ = path.parent_path().string();
|
||||
|
||||
if (executable_directory_.empty()) {
|
||||
executable_directory_ = ".";
|
||||
}
|
||||
|
||||
std::cout << "[PathUtils] Executable: " << executable_path_ << "\n";
|
||||
std::cout << "[PathUtils] Directori: " << executable_directory_ << "\n";
|
||||
}
|
||||
|
||||
// Obtenir el directori de l'executable
|
||||
std::string getExecutableDirectory() {
|
||||
if (executable_directory_.empty()) {
|
||||
std::cerr << "[PathUtils] ADVERTÈNCIA: Sistema de rutes no inicialitzat\n";
|
||||
return ".";
|
||||
}
|
||||
return executable_directory_;
|
||||
}
|
||||
|
||||
// Detectar si estem dins un bundle de macOS
|
||||
bool isMacOSBundle() {
|
||||
#ifdef MACOS_BUNDLE
|
||||
return true;
|
||||
#else
|
||||
// Detecció en temps d'execució
|
||||
// Cercar ".app/Contents/MacOS" a la ruta de l'executable
|
||||
std::string exe_dir = getExecutableDirectory();
|
||||
return exe_dir.find(".app/Contents/MacOS") != std::string::npos;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Obtenir la ruta base dels recursos
|
||||
std::string getResourceBasePath() {
|
||||
std::string exe_dir = getExecutableDirectory();
|
||||
|
||||
if (isMacOSBundle()) {
|
||||
// Bundle de macOS: recursos a ../Resources des de MacOS/
|
||||
std::cout << "[PathUtils] Detectat bundle de macOS\n";
|
||||
return exe_dir + "/../Resources";
|
||||
} else {
|
||||
// Executable normal: recursos al mateix directori
|
||||
return exe_dir;
|
||||
}
|
||||
}
|
||||
|
||||
// Normalitzar ruta (convertir barres, etc.)
|
||||
std::string normalizePath(const std::string& path) {
|
||||
std::string normalized = path;
|
||||
|
||||
// Convertir barres invertides a normals
|
||||
std::ranges::replace(normalized, '\\', '/');
|
||||
|
||||
// Simplificar rutes amb filesystem
|
||||
try {
|
||||
std::filesystem::path fs_path(normalized);
|
||||
normalized = fs_path.lexically_normal().string();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[PathUtils] Error normalitzant ruta: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
} // namespace Utils
|
||||
24
source/core/utils/path_utils.hpp
Normal file
24
source/core/utils/path_utils.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
// path_utils.hpp - Utilitats de gestió de rutes
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
// Detecció de directoris i bundles multiplataforma
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Utils {
|
||||
|
||||
// Inicialització amb argv[0]
|
||||
void initializePathSystem(const char* argv0);
|
||||
|
||||
// Obtenció de rutes
|
||||
std::string getExecutableDirectory();
|
||||
std::string getResourceBasePath();
|
||||
|
||||
// Detecció de plataforma
|
||||
bool isMacOSBundle();
|
||||
|
||||
// Normalització
|
||||
std::string normalizePath(const std::string& path);
|
||||
|
||||
} // namespace Utils
|
||||
2
source/external/.clang-format
vendored
Normal file
2
source/external/.clang-format
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DisableFormat: true
|
||||
SortIncludes: Never
|
||||
5565
source/external/stb_vorbis.h
vendored
Normal file
5565
source/external/stb_vorbis.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -7,10 +7,12 @@
|
||||
|
||||
namespace Constants {
|
||||
// Marges de l'àrea de joc (derivats de Defaults::Zones::GAME)
|
||||
constexpr int MARGE_ESQ = static_cast<int>(Defaults::Zones::GAME.x);
|
||||
constexpr int MARGE_DRET = static_cast<int>(Defaults::Zones::GAME.x + Defaults::Zones::GAME.w);
|
||||
constexpr int MARGE_DALT = static_cast<int>(Defaults::Zones::GAME.y);
|
||||
constexpr int MARGE_BAIX = static_cast<int>(Defaults::Zones::GAME.y + Defaults::Zones::GAME.h);
|
||||
constexpr int MARGE_ESQ = static_cast<int>(Defaults::Zones::PLAYAREA.x);
|
||||
constexpr int MARGE_DRET =
|
||||
static_cast<int>(Defaults::Zones::PLAYAREA.x + Defaults::Zones::PLAYAREA.w);
|
||||
constexpr int MARGE_DALT = static_cast<int>(Defaults::Zones::PLAYAREA.y);
|
||||
constexpr int MARGE_BAIX =
|
||||
static_cast<int>(Defaults::Zones::PLAYAREA.y + Defaults::Zones::PLAYAREA.h);
|
||||
|
||||
// Límits de polígons i objectes
|
||||
constexpr int MAX_IPUNTS = Defaults::Entities::MAX_IPUNTS;
|
||||
@@ -26,15 +28,33 @@ constexpr float PI = Defaults::Math::PI;
|
||||
|
||||
// Helpers per comprovar límits de zona
|
||||
inline bool dins_zona_joc(float x, float y) {
|
||||
const SDL_FPoint punt = {x, y};
|
||||
return SDL_PointInRectFloat(&punt, &Defaults::Zones::GAME);
|
||||
const SDL_FPoint punt = {x, y};
|
||||
return SDL_PointInRectFloat(&punt, &Defaults::Zones::PLAYAREA);
|
||||
}
|
||||
|
||||
inline void obtenir_limits_zona(float& min_x, float& max_x, float& min_y, float& max_y) {
|
||||
const auto& zona = Defaults::Zones::GAME;
|
||||
min_x = zona.x;
|
||||
max_x = zona.x + zona.w;
|
||||
min_y = zona.y;
|
||||
max_y = zona.y + zona.h;
|
||||
const auto& zona = Defaults::Zones::PLAYAREA;
|
||||
min_x = zona.x;
|
||||
max_x = zona.x + zona.w;
|
||||
min_y = zona.y;
|
||||
max_y = zona.y + zona.h;
|
||||
}
|
||||
} // namespace Constants
|
||||
|
||||
// Obtenir límits segurs (compensant radi de l'entitat)
|
||||
inline void obtenir_limits_zona_segurs(float radi, float& min_x, float& max_x, float& min_y, float& max_y) {
|
||||
const auto& zona = Defaults::Zones::PLAYAREA;
|
||||
constexpr float MARGE_SEGURETAT = 10.0f; // Safety margin
|
||||
|
||||
min_x = zona.x + radi + MARGE_SEGURETAT;
|
||||
max_x = zona.x + zona.w - radi - MARGE_SEGURETAT;
|
||||
min_y = zona.y + radi + MARGE_SEGURETAT;
|
||||
max_y = zona.y + zona.h - radi - MARGE_SEGURETAT;
|
||||
}
|
||||
|
||||
// Obtenir centre de l'àrea de joc
|
||||
inline void obtenir_centre_zona(float& centre_x, float& centre_y) {
|
||||
const auto& zona = Defaults::Zones::PLAYAREA;
|
||||
centre_x = zona.x + zona.w / 2.0f;
|
||||
centre_y = zona.y + zona.h / 2.0f;
|
||||
}
|
||||
} // namespace Constants
|
||||
|
||||
37
source/game/effects/debris.hpp
Normal file
37
source/game/effects/debris.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// debris.hpp - Fragment de línia volant (explosió de formes)
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// Debris: un segment de línia que vola perpendicular a sí mateix
|
||||
// Representa un fragment d'una forma destruïda (nau, enemic, bala)
|
||||
struct Debris {
|
||||
// Geometria del segment (2 punts en coordenades mundials)
|
||||
Punt p1; // Punt inicial del segment
|
||||
Punt p2; // Punt final del segment
|
||||
|
||||
// Física
|
||||
Punt velocitat; // Velocitat en px/s (components x, y)
|
||||
float acceleracio; // Acceleració negativa (fricció) en px/s²
|
||||
|
||||
// Rotació
|
||||
float angle_rotacio; // Angle de rotació acumulat (radians)
|
||||
float velocitat_rot; // Velocitat de rotació de TRAYECTORIA (rad/s)
|
||||
float velocitat_rot_visual; // Velocitat de rotació VISUAL del segment (rad/s)
|
||||
|
||||
// Estat de vida
|
||||
float temps_vida; // Temps transcorregut (segons)
|
||||
float temps_max; // Temps de vida màxim (segons)
|
||||
bool actiu; // Està actiu?
|
||||
|
||||
// Shrinking (reducció de distància entre punts)
|
||||
float factor_shrink; // Factor de reducció per segon (0.0-1.0)
|
||||
|
||||
// Rendering
|
||||
float brightness; // Factor de brillantor (0.0-1.0, heretat de l'objecte original)
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
380
source/game/effects/debris_manager.cpp
Normal file
380
source/game/effects/debris_manager.cpp
Normal file
@@ -0,0 +1,380 @@
|
||||
// debris_manager.cpp - Implementació del gestor de fragments
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "debris_manager.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// Helper: transformar punt amb rotació, escala i trasllació
|
||||
// (Copiat de shape_renderer.cpp:12-34)
|
||||
static Punt transform_point(const Punt& point, const Punt& shape_centre, const Punt& posicio, float angle, float escala) {
|
||||
// 1. Centrar el punt respecte al centre de la forma
|
||||
float centered_x = point.x - shape_centre.x;
|
||||
float centered_y = point.y - shape_centre.y;
|
||||
|
||||
// 2. Aplicar escala al punt centrat
|
||||
float scaled_x = centered_x * escala;
|
||||
float scaled_y = centered_y * escala;
|
||||
|
||||
// 3. Aplicar rotació
|
||||
float cos_a = std::cos(angle);
|
||||
float sin_a = std::sin(angle);
|
||||
|
||||
float rotated_x = scaled_x * cos_a - scaled_y * sin_a;
|
||||
float rotated_y = scaled_x * sin_a + scaled_y * cos_a;
|
||||
|
||||
// 4. Aplicar trasllació a posició mundial
|
||||
return {rotated_x + posicio.x, rotated_y + posicio.y};
|
||||
}
|
||||
|
||||
DebrisManager::DebrisManager(SDL_Renderer* renderer)
|
||||
: renderer_(renderer) {
|
||||
// Inicialitzar tots els debris com inactius
|
||||
for (auto& debris : debris_pool_) {
|
||||
debris.actiu = false;
|
||||
}
|
||||
}
|
||||
|
||||
void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
||||
const Punt& centre,
|
||||
float angle,
|
||||
float escala,
|
||||
float velocitat_base,
|
||||
float brightness,
|
||||
const Punt& velocitat_objecte,
|
||||
float velocitat_angular,
|
||||
float factor_herencia_visual) {
|
||||
if (!shape || !shape->es_valida()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reproducir sonido de explosión
|
||||
Audio::get()->playSound(Defaults::Sound::EXPLOSION, Audio::Group::GAME);
|
||||
|
||||
// Obtenir centre de la forma per a transformacions
|
||||
const Punt& shape_centre = shape->get_centre();
|
||||
|
||||
// Iterar sobre totes les primitives de la forma
|
||||
for (const auto& primitive : shape->get_primitives()) {
|
||||
// Processar cada segment de línia
|
||||
std::vector<std::pair<Punt, Punt>> segments;
|
||||
|
||||
if (primitive.type == Graphics::PrimitiveType::POLYLINE) {
|
||||
// Polyline: extreure segments consecutius
|
||||
for (size_t i = 0; i < primitive.points.size() - 1; i++) {
|
||||
segments.push_back({primitive.points[i], primitive.points[i + 1]});
|
||||
}
|
||||
} else { // PrimitiveType::LINE
|
||||
// Line: un únic segment
|
||||
if (primitive.points.size() >= 2) {
|
||||
segments.push_back({primitive.points[0], primitive.points[1]});
|
||||
}
|
||||
}
|
||||
|
||||
// Crear debris per a cada segment
|
||||
for (const auto& [local_p1, local_p2] : segments) {
|
||||
// 1. Transformar punts locals → coordenades mundials
|
||||
Punt world_p1 =
|
||||
transform_point(local_p1, shape_centre, centre, angle, escala);
|
||||
Punt world_p2 =
|
||||
transform_point(local_p2, shape_centre, centre, angle, escala);
|
||||
|
||||
// 2. Trobar slot lliure
|
||||
Debris* debris = trobar_slot_lliure();
|
||||
if (!debris) {
|
||||
std::cerr << "[DebrisManager] Warning: no debris slots disponibles\n";
|
||||
return; // Pool ple
|
||||
}
|
||||
|
||||
// 3. Inicialitzar geometria
|
||||
debris->p1 = world_p1;
|
||||
debris->p2 = world_p2;
|
||||
|
||||
// 4. Calcular direcció d'explosió (radial, des del centre cap a fora)
|
||||
Punt direccio = calcular_direccio_explosio(world_p1, world_p2, centre);
|
||||
|
||||
// 5. Velocitat inicial (base ± variació aleatòria + velocitat heretada)
|
||||
float speed =
|
||||
velocitat_base +
|
||||
((std::rand() / static_cast<float>(RAND_MAX)) * 2.0f - 1.0f) *
|
||||
Defaults::Physics::Debris::VARIACIO_VELOCITAT;
|
||||
|
||||
// Heredar velocitat de l'objecte original (suma vectorial)
|
||||
debris->velocitat.x = direccio.x * speed + velocitat_objecte.x;
|
||||
debris->velocitat.y = direccio.y * speed + velocitat_objecte.y;
|
||||
debris->acceleracio = Defaults::Physics::Debris::ACCELERACIO;
|
||||
|
||||
// 6. Herència de velocitat angular amb cap + conversió d'excés
|
||||
|
||||
// 6a. Rotació de TRAYECTORIA amb cap + conversió tangencial
|
||||
if (std::abs(velocitat_angular) > 0.01f) {
|
||||
// FASE 1: Aplicar herència i variació (igual que abans)
|
||||
float factor_herencia =
|
||||
Defaults::Physics::Debris::FACTOR_HERENCIA_MIN +
|
||||
(std::rand() / static_cast<float>(RAND_MAX)) *
|
||||
(Defaults::Physics::Debris::FACTOR_HERENCIA_MAX -
|
||||
Defaults::Physics::Debris::FACTOR_HERENCIA_MIN);
|
||||
|
||||
float velocitat_ang_heretada = velocitat_angular * factor_herencia;
|
||||
|
||||
float variacio =
|
||||
(std::rand() / static_cast<float>(RAND_MAX)) * 0.2f - 0.1f;
|
||||
velocitat_ang_heretada *= (1.0f + variacio);
|
||||
|
||||
// FASE 2: Aplicar cap i calcular excés
|
||||
constexpr float CAP = Defaults::Physics::Debris::VELOCITAT_ROT_MAX;
|
||||
float abs_ang = std::abs(velocitat_ang_heretada);
|
||||
float sign_ang = (velocitat_ang_heretada >= 0.0f) ? 1.0f : -1.0f;
|
||||
|
||||
if (abs_ang > CAP) {
|
||||
// Excés: convertir a velocitat tangencial
|
||||
float excess = abs_ang - CAP;
|
||||
|
||||
// Radi de la forma (enemics = 20 px)
|
||||
float radius = 20.0f;
|
||||
|
||||
// Velocitat tangencial = ω_excés × radi
|
||||
float v_tangential = excess * radius;
|
||||
|
||||
// Direcció tangencial: perpendicular a la radial (90° CCW)
|
||||
// Si direccio = (dx, dy), tangent = (-dy, dx)
|
||||
float tangent_x = -direccio.y;
|
||||
float tangent_y = direccio.x;
|
||||
|
||||
// Afegir velocitat tangencial (suma vectorial)
|
||||
debris->velocitat.x += tangent_x * v_tangential;
|
||||
debris->velocitat.y += tangent_y * v_tangential;
|
||||
|
||||
// Aplicar cap a velocitat angular (preservar signe)
|
||||
debris->velocitat_rot = sign_ang * CAP;
|
||||
} else {
|
||||
// Per sota del cap: comportament normal
|
||||
debris->velocitat_rot = velocitat_ang_heretada;
|
||||
}
|
||||
} else {
|
||||
debris->velocitat_rot = 0.0f; // Nave: sin curvas
|
||||
}
|
||||
|
||||
// 6b. Rotació VISUAL (proporcional según factor_herencia_visual)
|
||||
if (factor_herencia_visual > 0.01f && std::abs(velocitat_angular) > 0.01f) {
|
||||
// Heredar rotación visual con factor proporcional
|
||||
debris->velocitat_rot_visual = debris->velocitat_rot * factor_herencia_visual;
|
||||
|
||||
// Variació aleatòria petita (±5%) per naturalitat
|
||||
float variacio_visual =
|
||||
(std::rand() / static_cast<float>(RAND_MAX)) * 0.1f - 0.05f;
|
||||
debris->velocitat_rot_visual *= (1.0f + variacio_visual);
|
||||
} else {
|
||||
// Rotació visual aleatòria (factor = 0.0 o sin velocidad angular)
|
||||
debris->velocitat_rot_visual =
|
||||
Defaults::Physics::Debris::ROTACIO_MIN +
|
||||
(std::rand() / static_cast<float>(RAND_MAX)) *
|
||||
(Defaults::Physics::Debris::ROTACIO_MAX -
|
||||
Defaults::Physics::Debris::ROTACIO_MIN);
|
||||
|
||||
// 50% probabilitat de rotació en sentit contrari
|
||||
if (std::rand() % 2 == 0) {
|
||||
debris->velocitat_rot_visual = -debris->velocitat_rot_visual;
|
||||
}
|
||||
}
|
||||
|
||||
debris->angle_rotacio = 0.0f;
|
||||
|
||||
// 7. Configurar vida i shrinking
|
||||
debris->temps_vida = 0.0f;
|
||||
debris->temps_max = Defaults::Physics::Debris::TEMPS_VIDA;
|
||||
debris->factor_shrink = Defaults::Physics::Debris::SHRINK_RATE;
|
||||
|
||||
// 8. Heredar brightness
|
||||
debris->brightness = brightness;
|
||||
|
||||
// 9. Activar
|
||||
debris->actiu = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DebrisManager::actualitzar(float delta_time) {
|
||||
for (auto& debris : debris_pool_) {
|
||||
if (!debris.actiu)
|
||||
continue;
|
||||
|
||||
// 1. Actualitzar temps de vida
|
||||
debris.temps_vida += delta_time;
|
||||
|
||||
// Desactivar si ha superat temps màxim
|
||||
if (debris.temps_vida >= debris.temps_max) {
|
||||
debris.actiu = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2. Actualitzar velocitat (desacceleració)
|
||||
// Aplicar fricció en la direcció del moviment
|
||||
float speed = std::sqrt(debris.velocitat.x * debris.velocitat.x +
|
||||
debris.velocitat.y * debris.velocitat.y);
|
||||
|
||||
if (speed > 1.0f) {
|
||||
// Calcular direcció normalitzada
|
||||
float dir_x = debris.velocitat.x / speed;
|
||||
float dir_y = debris.velocitat.y / speed;
|
||||
|
||||
// Aplicar acceleració negativa (fricció)
|
||||
float nova_speed = speed + debris.acceleracio * delta_time;
|
||||
if (nova_speed < 0.0f)
|
||||
nova_speed = 0.0f;
|
||||
|
||||
debris.velocitat.x = dir_x * nova_speed;
|
||||
debris.velocitat.y = dir_y * nova_speed;
|
||||
} else {
|
||||
// Velocitat molt baixa, aturar
|
||||
debris.velocitat.x = 0.0f;
|
||||
debris.velocitat.y = 0.0f;
|
||||
}
|
||||
|
||||
// 2b. Rotar vector de velocitat (trayectoria curva)
|
||||
if (std::abs(debris.velocitat_rot) > 0.01f) {
|
||||
// Calcular angle de rotació aquest frame
|
||||
float dangle = debris.velocitat_rot * delta_time;
|
||||
|
||||
// Rotar vector de velocitat usant matriu de rotació 2D
|
||||
float vel_x_old = debris.velocitat.x;
|
||||
float vel_y_old = debris.velocitat.y;
|
||||
|
||||
float cos_a = std::cos(dangle);
|
||||
float sin_a = std::sin(dangle);
|
||||
|
||||
debris.velocitat.x = vel_x_old * cos_a - vel_y_old * sin_a;
|
||||
debris.velocitat.y = vel_x_old * sin_a + vel_y_old * cos_a;
|
||||
}
|
||||
|
||||
// 2c. Aplicar fricció angular (desacceleració gradual)
|
||||
if (std::abs(debris.velocitat_rot) > 0.01f) {
|
||||
float sign = (debris.velocitat_rot > 0) ? 1.0f : -1.0f;
|
||||
float reduccion =
|
||||
Defaults::Physics::Debris::FRICCIO_ANGULAR * delta_time;
|
||||
debris.velocitat_rot -= sign * reduccion;
|
||||
|
||||
// Evitar canvi de signe (no pot passar de CW a CCW)
|
||||
if ((debris.velocitat_rot > 0) != (sign > 0)) {
|
||||
debris.velocitat_rot = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Calcular centre del segment
|
||||
Punt centre = {(debris.p1.x + debris.p2.x) / 2.0f,
|
||||
(debris.p1.y + debris.p2.y) / 2.0f};
|
||||
|
||||
// 4. Actualitzar posició del centre
|
||||
centre.x += debris.velocitat.x * delta_time;
|
||||
centre.y += debris.velocitat.y * delta_time;
|
||||
|
||||
// 5. Actualitzar rotació VISUAL
|
||||
debris.angle_rotacio += debris.velocitat_rot_visual * delta_time;
|
||||
|
||||
// 6. Aplicar shrinking (reducció de distància entre punts)
|
||||
float shrink_factor =
|
||||
1.0f - (debris.factor_shrink * debris.temps_vida / debris.temps_max);
|
||||
shrink_factor = std::max(0.0f, shrink_factor); // No negatiu
|
||||
|
||||
// Calcular distància original entre punts
|
||||
float dx = debris.p2.x - debris.p1.x;
|
||||
float dy = debris.p2.y - debris.p1.y;
|
||||
|
||||
// 7. Reconstruir segment amb nova mida i rotació
|
||||
float half_length = std::sqrt(dx * dx + dy * dy) * shrink_factor / 2.0f;
|
||||
float original_angle = std::atan2(dy, dx);
|
||||
float new_angle = original_angle + debris.angle_rotacio;
|
||||
|
||||
debris.p1.x = centre.x - half_length * std::cos(new_angle);
|
||||
debris.p1.y = centre.y - half_length * std::sin(new_angle);
|
||||
debris.p2.x = centre.x + half_length * std::cos(new_angle);
|
||||
debris.p2.y = centre.y + half_length * std::sin(new_angle);
|
||||
}
|
||||
}
|
||||
|
||||
void DebrisManager::dibuixar() const {
|
||||
for (const auto& debris : debris_pool_) {
|
||||
if (!debris.actiu)
|
||||
continue;
|
||||
|
||||
// Dibuixar segment de línia amb brightness heretat
|
||||
Rendering::linea(renderer_,
|
||||
static_cast<int>(debris.p1.x),
|
||||
static_cast<int>(debris.p1.y),
|
||||
static_cast<int>(debris.p2.x),
|
||||
static_cast<int>(debris.p2.y),
|
||||
true,
|
||||
debris.brightness);
|
||||
}
|
||||
}
|
||||
|
||||
Debris* DebrisManager::trobar_slot_lliure() {
|
||||
for (auto& debris : debris_pool_) {
|
||||
if (!debris.actiu) {
|
||||
return &debris;
|
||||
}
|
||||
}
|
||||
return nullptr; // Pool ple
|
||||
}
|
||||
|
||||
Punt DebrisManager::calcular_direccio_explosio(const Punt& p1,
|
||||
const Punt& p2,
|
||||
const Punt& centre_objecte) const {
|
||||
// 1. Calcular centre del segment
|
||||
float centro_seg_x = (p1.x + p2.x) / 2.0f;
|
||||
float centro_seg_y = (p1.y + p2.y) / 2.0f;
|
||||
|
||||
// 2. Calcular vector des del centre de l'objecte cap al centre del segment
|
||||
// Això garanteix que la direcció sempre apunte cap a fora (direcció radial)
|
||||
float dx = centro_seg_x - centre_objecte.x;
|
||||
float dy = centro_seg_y - centre_objecte.y;
|
||||
|
||||
// 3. Normalitzar (obtenir vector unitari)
|
||||
float length = std::sqrt(dx * dx + dy * dy);
|
||||
if (length < 0.001f) {
|
||||
// Segment al centre (cas extrem molt improbable), retornar direcció aleatòria
|
||||
float angle_rand =
|
||||
(std::rand() / static_cast<float>(RAND_MAX)) * 2.0f * Defaults::Math::PI;
|
||||
return {std::cos(angle_rand), std::sin(angle_rand)};
|
||||
}
|
||||
|
||||
dx /= length;
|
||||
dy /= length;
|
||||
|
||||
// 4. Afegir variació aleatòria petita (±15°) per varietat visual
|
||||
float angle_variacio =
|
||||
((std::rand() % 30) - 15) * Defaults::Math::PI / 180.0f;
|
||||
|
||||
float cos_v = std::cos(angle_variacio);
|
||||
float sin_v = std::sin(angle_variacio);
|
||||
|
||||
float final_x = dx * cos_v - dy * sin_v;
|
||||
float final_y = dx * sin_v + dy * cos_v;
|
||||
|
||||
return {final_x, final_y};
|
||||
}
|
||||
|
||||
void DebrisManager::reiniciar() {
|
||||
for (auto& debris : debris_pool_) {
|
||||
debris.actiu = false;
|
||||
}
|
||||
}
|
||||
|
||||
int DebrisManager::get_num_actius() const {
|
||||
int count = 0;
|
||||
for (const auto& debris : debris_pool_) {
|
||||
if (debris.actiu)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
} // namespace Effects
|
||||
71
source/game/effects/debris_manager.hpp
Normal file
71
source/game/effects/debris_manager.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// debris_manager.hpp - Gestor de fragments d'explosions
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "debris.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// Gestor de fragments d'explosions
|
||||
// Manté un pool d'objectes Debris i gestiona el seu cicle de vida
|
||||
class DebrisManager {
|
||||
public:
|
||||
explicit DebrisManager(SDL_Renderer* renderer);
|
||||
|
||||
// Crear explosió a partir d'una forma
|
||||
// - shape: forma vectorial a explotar
|
||||
// - centre: posició del centre de l'objecte
|
||||
// - angle: orientació de l'objecte (radians)
|
||||
// - escala: escala de l'objecte (1.0 = normal)
|
||||
// - velocitat_base: velocitat inicial dels fragments (px/s)
|
||||
// - brightness: factor de brillantor heretat (0.0-1.0, per defecte 1.0)
|
||||
// - velocitat_objecte: velocitat de l'objecte que explota (px/s, per defecte 0)
|
||||
// - velocitat_angular: velocitat angular heretada (rad/s, per defecte 0)
|
||||
// - factor_herencia_visual: factor de herència rotació visual (0.0-1.0, per defecte 0.0)
|
||||
void explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
||||
const Punt& centre,
|
||||
float angle,
|
||||
float escala,
|
||||
float velocitat_base,
|
||||
float brightness = 1.0f,
|
||||
const Punt& velocitat_objecte = {0.0f, 0.0f},
|
||||
float velocitat_angular = 0.0f,
|
||||
float factor_herencia_visual = 0.0f);
|
||||
|
||||
// Actualitzar tots els fragments actius
|
||||
void actualitzar(float delta_time);
|
||||
|
||||
// Dibuixar tots els fragments actius
|
||||
void dibuixar() const;
|
||||
|
||||
// Reiniciar tots els fragments (neteja)
|
||||
void reiniciar();
|
||||
|
||||
// Obtenir número de fragments actius
|
||||
int get_num_actius() const;
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// Pool de fragments (màxim concurrent)
|
||||
// Un pentàgon té 5 línies, 15 enemics = 75 línies
|
||||
// + nau (3 línies) + bales (5 línies * 3) = 93 línies màxim
|
||||
// Arrodonit a 100 per seguretat
|
||||
static constexpr int MAX_DEBRIS = 100;
|
||||
std::array<Debris, MAX_DEBRIS> debris_pool_;
|
||||
|
||||
// Trobar primer slot inactiu
|
||||
Debris* trobar_slot_lliure();
|
||||
|
||||
// Calcular direcció d'explosió (radial, des del centre cap al segment)
|
||||
Punt calcular_direccio_explosio(const Punt& p1, const Punt& p2, const Punt& centre_objecte) const;
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
// gestor_puntuacio_flotant.cpp - Implementació del gestor de números flotants
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "gestor_puntuacio_flotant.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Effects {
|
||||
|
||||
GestorPuntuacioFlotant::GestorPuntuacioFlotant(SDL_Renderer* renderer)
|
||||
: renderer_(renderer), text_(renderer) {
|
||||
// Inicialitzar tots els slots com inactius
|
||||
for (auto& pf : pool_) {
|
||||
pf.actiu = false;
|
||||
}
|
||||
}
|
||||
|
||||
void GestorPuntuacioFlotant::crear(int punts, const Punt& posicio) {
|
||||
// 1. Trobar slot lliure
|
||||
PuntuacioFlotant* pf = trobar_slot_lliure();
|
||||
if (!pf)
|
||||
return; // Pool ple (improbable)
|
||||
|
||||
// 2. Inicialitzar puntuació flotant
|
||||
pf->text = std::to_string(punts);
|
||||
pf->posicio = posicio;
|
||||
pf->velocitat = {Defaults::FloatingScore::VELOCITY_X,
|
||||
Defaults::FloatingScore::VELOCITY_Y};
|
||||
pf->temps_vida = 0.0f;
|
||||
pf->temps_max = Defaults::FloatingScore::LIFETIME;
|
||||
pf->brightness = 1.0f;
|
||||
pf->actiu = true;
|
||||
}
|
||||
|
||||
void GestorPuntuacioFlotant::actualitzar(float delta_time) {
|
||||
for (auto& pf : pool_) {
|
||||
if (!pf.actiu)
|
||||
continue;
|
||||
|
||||
// 1. Actualitzar posició (deriva cap amunt)
|
||||
pf.posicio.x += pf.velocitat.x * delta_time;
|
||||
pf.posicio.y += pf.velocitat.y * delta_time;
|
||||
|
||||
// 2. Actualitzar temps de vida
|
||||
pf.temps_vida += delta_time;
|
||||
|
||||
// 3. Calcular brightness (fade lineal)
|
||||
float progress = pf.temps_vida / pf.temps_max; // 0.0 → 1.0
|
||||
pf.brightness = 1.0f - progress; // 1.0 → 0.0
|
||||
|
||||
// 4. Desactivar quan acaba el temps
|
||||
if (pf.temps_vida >= pf.temps_max) {
|
||||
pf.actiu = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GestorPuntuacioFlotant::dibuixar() {
|
||||
for (const auto& pf : pool_) {
|
||||
if (!pf.actiu)
|
||||
continue;
|
||||
|
||||
// 1. Calcular dimensions del text per centrar-lo
|
||||
constexpr float escala = Defaults::FloatingScore::SCALE;
|
||||
constexpr float spacing = Defaults::FloatingScore::SPACING;
|
||||
float text_width = text_.get_text_width(pf.text, escala, spacing);
|
||||
|
||||
// 2. Centrar text sobre la posició
|
||||
Punt render_pos = {pf.posicio.x - text_width / 2.0f, pf.posicio.y};
|
||||
|
||||
// 3. Renderitzar amb brightness (fade)
|
||||
text_.render(pf.text, render_pos, escala, spacing, pf.brightness);
|
||||
}
|
||||
}
|
||||
|
||||
void GestorPuntuacioFlotant::reiniciar() {
|
||||
for (auto& pf : pool_) {
|
||||
pf.actiu = false;
|
||||
}
|
||||
}
|
||||
|
||||
int GestorPuntuacioFlotant::get_num_actius() const {
|
||||
int count = 0;
|
||||
for (const auto& pf : pool_) {
|
||||
if (pf.actiu)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
PuntuacioFlotant* GestorPuntuacioFlotant::trobar_slot_lliure() {
|
||||
for (auto& pf : pool_) {
|
||||
if (!pf.actiu)
|
||||
return &pf;
|
||||
}
|
||||
return nullptr; // Pool ple
|
||||
}
|
||||
|
||||
} // namespace Effects
|
||||
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// gestor_puntuacio_flotant.hpp - Gestor de números de puntuació flotants
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/vector_text.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "puntuacio_flotant.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// Gestor de números de puntuació flotants
|
||||
// Manté un pool de PuntuacioFlotant i gestiona el seu cicle de vida
|
||||
class GestorPuntuacioFlotant {
|
||||
public:
|
||||
explicit GestorPuntuacioFlotant(SDL_Renderer* renderer);
|
||||
|
||||
// Crear número flotant
|
||||
// - punts: valor numèric (100, 150, 200)
|
||||
// - posicio: on apareix (normalment centre d'enemic destruït)
|
||||
void crear(int punts, const Punt& posicio);
|
||||
|
||||
// Actualitzar tots els números actius
|
||||
void actualitzar(float delta_time);
|
||||
|
||||
// Dibuixar tots els números actius
|
||||
void dibuixar();
|
||||
|
||||
// Reiniciar tots (neteja)
|
||||
void reiniciar();
|
||||
|
||||
// Obtenir número actius (debug)
|
||||
int get_num_actius() const;
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
Graphics::VectorText text_; // Sistema de text vectorial
|
||||
|
||||
// Pool de números flotants (màxim concurrent)
|
||||
// Màxim 15 enemics simultanis = màxim 15 números
|
||||
static constexpr int MAX_PUNTUACIONS =
|
||||
Defaults::FloatingScore::MAX_CONCURRENT;
|
||||
std::array<PuntuacioFlotant, MAX_PUNTUACIONS> pool_;
|
||||
|
||||
// Trobar primer slot inactiu
|
||||
PuntuacioFlotant* trobar_slot_lliure();
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
33
source/game/effects/puntuacio_flotant.hpp
Normal file
33
source/game/effects/puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// puntuacio_flotant.hpp - Número de puntuació que apareix i desapareix
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "core/types.hpp"
|
||||
|
||||
namespace Effects {
|
||||
|
||||
// PuntuacioFlotant: text animat que mostra punts guanyats
|
||||
// S'activa quan es destrueix un enemic i s'esvaeix després d'un temps
|
||||
struct PuntuacioFlotant {
|
||||
// Text a mostrar (e.g., "100", "150", "200")
|
||||
std::string text;
|
||||
|
||||
// Posició actual (coordenades mundials)
|
||||
Punt posicio;
|
||||
|
||||
// Animació de moviment
|
||||
Punt velocitat; // px/s (normalment cap amunt: {0.0f, -30.0f})
|
||||
|
||||
// Animació de fade
|
||||
float temps_vida; // Temps transcorregut (segons)
|
||||
float temps_max; // Temps de vida màxim (segons)
|
||||
float brightness; // Brillantor calculada (0.0-1.0)
|
||||
|
||||
// Estat
|
||||
bool actiu;
|
||||
};
|
||||
|
||||
} // namespace Effects
|
||||
@@ -3,84 +3,103 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/bala.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
Bala::Bala(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), esta_(false) {
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("bullet.shp");
|
||||
Bala::Bala(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
esta_(false),
|
||||
brightness_(Defaults::Brightness::BALA) {
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("bullet.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Bala] Error: no s'ha pogut carregar bullet.shp" << std::endl;
|
||||
}
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Bala] Error: no s'ha pogut carregar bullet.shp" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::inicialitzar() {
|
||||
// Inicialment inactiva
|
||||
esta_ = false;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
// Inicialment inactiva
|
||||
esta_ = false;
|
||||
centre_ = {0.0f, 0.0f};
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
|
||||
void Bala::disparar(const Punt &posicio, float angle) {
|
||||
// Activar bala i posicionar-la a la nau
|
||||
// Basat en joc_asteroides.cpp línies 188-200
|
||||
void Bala::disparar(const Punt& posicio, float angle) {
|
||||
// Activar bala i posicionar-la a la nau
|
||||
// Basat en joc_asteroides.cpp línies 188-200
|
||||
|
||||
// Activar bala
|
||||
esta_ = true;
|
||||
// Activar bala
|
||||
esta_ = true;
|
||||
|
||||
// Posició inicial = centre de la nau
|
||||
centre_.x = posicio.x;
|
||||
centre_.y = posicio.y;
|
||||
// Posició inicial = centre de la nau
|
||||
centre_.x = posicio.x;
|
||||
centre_.y = posicio.y;
|
||||
|
||||
// Angle = angle de la nau (dispara en la direcció que apunta)
|
||||
angle_ = angle;
|
||||
// Angle = angle de la nau (dispara en la direcció que apunta)
|
||||
angle_ = angle;
|
||||
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
velocitat_ = 140.0f;
|
||||
// Velocitat alta (el joc Pascal original usava 7 px/frame)
|
||||
// 7 px/frame × 20 FPS = 140 px/s
|
||||
velocitat_ = 140.0f;
|
||||
|
||||
// Reproducir sonido de disparo láser
|
||||
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
|
||||
}
|
||||
|
||||
void Bala::actualitzar(float delta_time) {
|
||||
if (esta_) {
|
||||
mou(delta_time);
|
||||
}
|
||||
if (esta_) {
|
||||
mou(delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::dibuixar() const {
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
// Les bales no roten visualment (angle sempre 0.0f)
|
||||
Rendering::render_shape(renderer_, forma_, centre_, 0.0f, 1.0f, true);
|
||||
}
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
// Les bales roten segons l'angle de trajectòria
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, 1.0f, true, 1.0f, brightness_);
|
||||
}
|
||||
}
|
||||
|
||||
void Bala::mou(float delta_time) {
|
||||
// Moviment rectilini de la bala
|
||||
// Basat en el codi Pascal original: procedure mou_bales
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 396-419
|
||||
// Moviment rectilini de la bala
|
||||
// Basat en el codi Pascal original: procedure mou_bales
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 396-419
|
||||
|
||||
// Calcular nova posició (moviment polar time-based)
|
||||
// velocitat ja està en px/s (140 px/s), només cal multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
// Calcular nova posició (moviment polar time-based)
|
||||
// velocitat ja està en px/s (140 px/s), només cal multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
centre_.y += dy;
|
||||
centre_.x += dx;
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
centre_.y += dy;
|
||||
centre_.x += dx;
|
||||
|
||||
// Desactivar si surt de la zona de joc (no rebota com els ORNIs)
|
||||
if (!Constants::dins_zona_joc(centre_.x, centre_.y)) {
|
||||
esta_ = false;
|
||||
}
|
||||
// Desactivar si surt de la zona de joc (no rebota com els ORNIs)
|
||||
// CORRECCIÓ: Usar límits segurs amb radi de la bala
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::BULLET_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
if (centre_.x < min_x || centre_.x > max_x ||
|
||||
centre_.y < min_y || centre_.y > max_y) {
|
||||
esta_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,37 +3,41 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class Bala {
|
||||
public:
|
||||
Bala() : renderer_(nullptr) {}
|
||||
Bala(SDL_Renderer *renderer);
|
||||
public:
|
||||
Bala()
|
||||
: renderer_(nullptr) {}
|
||||
Bala(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar();
|
||||
void disparar(const Punt &posicio, float angle);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
void inicialitzar();
|
||||
void disparar(const Punt& posicio, float angle);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_activa() const { return esta_; }
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
void desactivar() { esta_ = false; }
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_activa() const { return esta_; }
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
void desactivar() { esta_ = false; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida entre totes les bales)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
// [NUEVO] Forma vectorial (compartida entre totes les bales)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_;
|
||||
float velocitat_;
|
||||
bool esta_;
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_;
|
||||
float velocitat_;
|
||||
bool esta_;
|
||||
float brightness_; // Factor de brillantor (0.0-1.0)
|
||||
|
||||
void mou(float delta_time);
|
||||
void mou(float delta_time);
|
||||
};
|
||||
|
||||
@@ -3,117 +3,512 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/enemic.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
Enemic::Enemic(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), drotacio_(0.0f), rotacio_(0.0f), esta_(false) {
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("enemy_pentagon.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar enemy_pentagon.shp"
|
||||
<< std::endl;
|
||||
}
|
||||
Enemic::Enemic(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
drotacio_(0.0f),
|
||||
rotacio_(0.0f),
|
||||
esta_(false),
|
||||
brightness_(Defaults::Brightness::ENEMIC),
|
||||
tipus_(TipusEnemic::PENTAGON),
|
||||
tracking_timer_(0.0f),
|
||||
ship_position_(nullptr),
|
||||
tracking_strength_(0.5f), // Default tracking strength
|
||||
timer_invulnerabilitat_(0.0f) { // Start vulnerable
|
||||
// [NUEVO] Forma es carrega a inicialitzar() segons el tipus
|
||||
// Constructor no carrega forma per permetre tipus diferents
|
||||
}
|
||||
|
||||
void Enemic::inicialitzar() {
|
||||
// Inicialitzar enemic (pentàgon)
|
||||
// Copiat de joc_asteroides.cpp línies 41-54
|
||||
void Enemic::inicialitzar(TipusEnemic tipus, const Punt* ship_pos) {
|
||||
// Guardar tipus
|
||||
tipus_ = tipus;
|
||||
|
||||
// [NUEVO] Ja no cal crear_poligon_regular - la geometria es carrega del fitxer
|
||||
// Només inicialitzem l'estat de la instància
|
||||
// Carregar forma segons el tipus
|
||||
const char* shape_file;
|
||||
float drotacio_min, drotacio_max;
|
||||
|
||||
// Posició aleatòria dins de l'àrea de joc
|
||||
centre_.x = static_cast<float>((std::rand() % 580) + 30); // 30-610
|
||||
centre_.y = static_cast<float>((std::rand() % 420) + 30); // 30-450
|
||||
switch (tipus_) {
|
||||
case TipusEnemic::PENTAGON:
|
||||
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
|
||||
velocitat_ = Defaults::Enemies::Pentagon::VELOCITAT;
|
||||
drotacio_min = Defaults::Enemies::Pentagon::DROTACIO_MIN;
|
||||
drotacio_max = Defaults::Enemies::Pentagon::DROTACIO_MAX;
|
||||
break;
|
||||
|
||||
// Angle aleatori de moviment
|
||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
case TipusEnemic::QUADRAT:
|
||||
shape_file = Defaults::Enemies::Quadrat::SHAPE_FILE;
|
||||
velocitat_ = Defaults::Enemies::Quadrat::VELOCITAT;
|
||||
drotacio_min = Defaults::Enemies::Quadrat::DROTACIO_MIN;
|
||||
drotacio_max = Defaults::Enemies::Quadrat::DROTACIO_MAX;
|
||||
tracking_timer_ = 0.0f;
|
||||
break;
|
||||
|
||||
// Velocitat (2 px/frame original * 20 FPS = 40 px/s)
|
||||
velocitat_ = 40.0f;
|
||||
case TipusEnemic::MOLINILLO:
|
||||
shape_file = Defaults::Enemies::Molinillo::SHAPE_FILE;
|
||||
velocitat_ = Defaults::Enemies::Molinillo::VELOCITAT;
|
||||
drotacio_min = Defaults::Enemies::Molinillo::DROTACIO_MIN;
|
||||
drotacio_max = Defaults::Enemies::Molinillo::DROTACIO_MAX;
|
||||
break;
|
||||
}
|
||||
|
||||
// Rotació visual aleatòria (rad/s)
|
||||
// Original Pascal: random * 0.1 rad/frame * 20 FPS ≈ 2 rad/s
|
||||
drotacio_ = (static_cast<float>(std::rand()) / RAND_MAX) * 2.0f;
|
||||
rotacio_ = 0.0f;
|
||||
// Carregar forma
|
||||
forma_ = Graphics::ShapeLoader::load(shape_file);
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar " << shape_file << std::endl;
|
||||
}
|
||||
|
||||
// Activar
|
||||
esta_ = true;
|
||||
// [MODIFIED] Posició aleatòria amb comprovació de seguretat
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
if (ship_pos != nullptr) {
|
||||
// [NEW] Safe spawn: attempt to find position away from ship
|
||||
bool found_safe_position = false;
|
||||
|
||||
for (int attempt = 0; attempt < Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS; attempt++) {
|
||||
float candidate_x, candidate_y;
|
||||
|
||||
if (intent_spawn_safe(*ship_pos, candidate_x, candidate_y)) {
|
||||
centre_.x = candidate_x;
|
||||
centre_.y = candidate_y;
|
||||
found_safe_position = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found_safe_position) {
|
||||
// Fallback: spawn anywhere (user's preference)
|
||||
int range_x = static_cast<int>(max_x - min_x);
|
||||
int range_y = static_cast<int>(max_y - min_y);
|
||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||
|
||||
std::cout << "[Enemic] Advertència: spawn sense zona segura després de "
|
||||
<< Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS << " intents" << std::endl;
|
||||
}
|
||||
} else {
|
||||
// [EXISTING] No ship position: spawn anywhere (backward compatibility)
|
||||
int range_x = static_cast<int>(max_x - min_x);
|
||||
int range_y = static_cast<int>(max_y - min_y);
|
||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||
}
|
||||
|
||||
// Angle aleatori de moviment
|
||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||
|
||||
// Rotació visual aleatòria (rad/s) dins del rang del tipus
|
||||
float drotacio_range = drotacio_max - drotacio_min;
|
||||
drotacio_ = drotacio_min + (static_cast<float>(std::rand()) / RAND_MAX) * drotacio_range;
|
||||
rotacio_ = 0.0f;
|
||||
|
||||
// Inicialitzar estat d'animació
|
||||
animacio_ = AnimacioEnemic(); // Reset to defaults
|
||||
animacio_.drotacio_base = drotacio_;
|
||||
animacio_.drotacio_objetivo = drotacio_;
|
||||
animacio_.drotacio_t = 1.0f; // Start without interpolating
|
||||
|
||||
// [NEW] Inicialitzar invulnerabilitat
|
||||
timer_invulnerabilitat_ = Defaults::Enemies::Spawn::INVULNERABILITY_DURATION; // 3.0s
|
||||
brightness_ = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START; // 0.3f
|
||||
|
||||
// Activar
|
||||
esta_ = true;
|
||||
}
|
||||
|
||||
void Enemic::actualitzar(float delta_time) {
|
||||
if (esta_) {
|
||||
// Moviment autònom
|
||||
mou(delta_time);
|
||||
if (esta_) {
|
||||
// [NEW] Update invulnerability timer and brightness
|
||||
if (timer_invulnerabilitat_ > 0.0f) {
|
||||
timer_invulnerabilitat_ -= delta_time;
|
||||
|
||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||
rotacio_ += drotacio_ * delta_time;
|
||||
}
|
||||
if (timer_invulnerabilitat_ < 0.0f) {
|
||||
timer_invulnerabilitat_ = 0.0f;
|
||||
}
|
||||
|
||||
// [NEW] Update brightness with LERP during invulnerability
|
||||
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
||||
float t = 1.0f - t_inv; // 0.0 → 1.0
|
||||
float smooth_t = t * t * (3.0f - 2.0f * t); // smoothstep
|
||||
|
||||
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START;
|
||||
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_END;
|
||||
brightness_ = START + (END - START) * smooth_t;
|
||||
}
|
||||
|
||||
// Moviment autònom
|
||||
mou(delta_time);
|
||||
|
||||
// Actualitzar animacions (palpitació, rotació accelerada)
|
||||
actualitzar_animacio(delta_time);
|
||||
|
||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||
rotacio_ += drotacio_ * delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::dibuixar() const {
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, 1.0f, true);
|
||||
}
|
||||
if (esta_ && forma_) {
|
||||
// Calculate animated scale (includes invulnerability LERP)
|
||||
float escala = calcular_escala_actual();
|
||||
|
||||
// brightness_ is already updated in actualitzar()
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, escala, true, 1.0f, brightness_);
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::mou(float delta_time) {
|
||||
// Moviment autònom d'ORNI (enemic pentàgon)
|
||||
// Basat EXACTAMENT en el codi Pascal original: ASTEROID.PAS lines 279-293
|
||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 348-394
|
||||
//
|
||||
// IMPORTANT: El Pascal original NO té canvi aleatori continu!
|
||||
// Només ajusta l'angle quan toca una paret.
|
||||
|
||||
// Calcular nova posició PROPUESTA (time-based, però lògica Pascal)
|
||||
// velocitat_ ja està en px/s (40 px/s), multiplicar per delta_time
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// Obtenir límits de la zona de joc
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
|
||||
// Lògica Pascal: Actualitza Y si dins, sinó ajusta angle aleatòriament
|
||||
// if (dy>marge_dalt) and (dy<marge_baix) then orni.centre.y:=round(Dy)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_y > min_y && new_y < max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
|
||||
// random(256) = 0..255, /512 = 0..0.498
|
||||
// random(3) = 0,1,2, -1 = -1,0,1
|
||||
// Resultado: ±0.5 rad aprox
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1; // -1, 0, o 1
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
|
||||
// Lògica Pascal: Actualitza X si dins, sinó ajusta angle aleatòriament
|
||||
// if (dx>marge_esq) and (dx<marge_dret) then orni.centre.x:=round(Dx)
|
||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
||||
if (new_x > min_x && new_x < max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
||||
int rand2 = (std::rand() % 3) - 1;
|
||||
angle_ += rand1 * static_cast<float>(rand2);
|
||||
}
|
||||
|
||||
// Nota: La rotació visual (rotacio_ += drotacio_) ja es fa a actualitzar()
|
||||
// Dispatcher: crida el comportament específic segons el tipus
|
||||
switch (tipus_) {
|
||||
case TipusEnemic::PENTAGON:
|
||||
comportament_pentagon(delta_time);
|
||||
break;
|
||||
case TipusEnemic::QUADRAT:
|
||||
comportament_quadrat(delta_time);
|
||||
break;
|
||||
case TipusEnemic::MOLINILLO:
|
||||
comportament_molinillo(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::comportament_pentagon(float delta_time) {
|
||||
// Pentagon: zigzag esquivador (frequent direction changes)
|
||||
// Similar a comportament original però amb probabilitat més alta
|
||||
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
|
||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// Obtenir límits segurs
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
// Zigzag: canvi d'angle més freqüent en tocar límits
|
||||
if (new_y >= min_y && new_y <= max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
// Probabilitat més alta de canvi d'angle
|
||||
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
||||
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
||||
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||
}
|
||||
}
|
||||
|
||||
if (new_x >= min_x && new_x <= max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
||||
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
||||
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::comportament_quadrat(float delta_time) {
|
||||
// Quadrat: perseguidor (tracks player position)
|
||||
|
||||
// Update tracking timer
|
||||
tracking_timer_ += delta_time;
|
||||
|
||||
// Periodically update angle toward ship
|
||||
if (tracking_timer_ >= Defaults::Enemies::Quadrat::TRACKING_INTERVAL) {
|
||||
tracking_timer_ = 0.0f;
|
||||
|
||||
if (ship_position_) {
|
||||
// Calculate angle to ship
|
||||
float dx = ship_position_->x - centre_.x;
|
||||
float dy = ship_position_->y - centre_.y;
|
||||
float target_angle = std::atan2(dy, dx) + Constants::PI / 2.0f;
|
||||
|
||||
// Interpolate toward target angle
|
||||
float angle_diff = target_angle - angle_;
|
||||
|
||||
// Normalize angle difference to [-π, π]
|
||||
while (angle_diff > Constants::PI) angle_diff -= 2.0f * Constants::PI;
|
||||
while (angle_diff < -Constants::PI) angle_diff += 2.0f * Constants::PI;
|
||||
|
||||
// Apply tracking strength (uses member variable, defaults to 0.5)
|
||||
angle_ += angle_diff * tracking_strength_;
|
||||
}
|
||||
}
|
||||
|
||||
// Move in current direction
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// Obtenir límits segurs
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
// Bounce on walls (simple reflection)
|
||||
if (new_y >= min_y && new_y <= max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
angle_ = -angle_; // Vertical reflection
|
||||
}
|
||||
|
||||
if (new_x >= min_x && new_x <= max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
angle_ = Constants::PI - angle_; // Horizontal reflection
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::comportament_molinillo(float delta_time) {
|
||||
// Molinillo: agressiu (fast, straight lines, proximity spin-up)
|
||||
|
||||
// Check proximity to ship for spin-up effect
|
||||
if (ship_position_) {
|
||||
float dx = ship_position_->x - centre_.x;
|
||||
float dy = ship_position_->y - centre_.y;
|
||||
float distance = std::sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (distance < Defaults::Enemies::Molinillo::PROXIMITY_DISTANCE) {
|
||||
// Temporarily boost rotation speed when near ship
|
||||
float boost = Defaults::Enemies::Molinillo::DROTACIO_PROXIMITY_MULTIPLIER;
|
||||
drotacio_ = animacio_.drotacio_base * boost;
|
||||
} else {
|
||||
// Normal rotation speed
|
||||
drotacio_ = animacio_.drotacio_base;
|
||||
}
|
||||
}
|
||||
|
||||
// Fast straight-line movement
|
||||
float velocitat_efectiva = velocitat_ * delta_time;
|
||||
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||
|
||||
float new_y = centre_.y + dy;
|
||||
float new_x = centre_.x + dx;
|
||||
|
||||
// Obtenir límits segurs
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
// Rare angle changes on wall hits
|
||||
if (new_y >= min_y && new_y <= max_y) {
|
||||
centre_.y = new_y;
|
||||
} else {
|
||||
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
||||
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
||||
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||
}
|
||||
}
|
||||
|
||||
if (new_x >= min_x && new_x <= max_x) {
|
||||
centre_.x = new_x;
|
||||
} else {
|
||||
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
||||
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
||||
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::actualitzar_animacio(float delta_time) {
|
||||
actualitzar_palpitacio(delta_time);
|
||||
actualitzar_rotacio_accelerada(delta_time);
|
||||
}
|
||||
|
||||
void Enemic::actualitzar_palpitacio(float delta_time) {
|
||||
if (animacio_.palpitacio_activa) {
|
||||
// Advance phase (2π * frequency * dt)
|
||||
animacio_.palpitacio_fase += 2.0f * Constants::PI * animacio_.palpitacio_frequencia * delta_time;
|
||||
|
||||
// Decrement timer
|
||||
animacio_.palpitacio_temps_restant -= delta_time;
|
||||
|
||||
// Deactivate when timer expires
|
||||
if (animacio_.palpitacio_temps_restant <= 0.0f) {
|
||||
animacio_.palpitacio_activa = false;
|
||||
}
|
||||
} else {
|
||||
// Random trigger (probability per second)
|
||||
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
||||
float trigger_prob = Defaults::Enemies::Animation::PALPITACIO_TRIGGER_PROB * delta_time;
|
||||
|
||||
if (rand_val < trigger_prob) {
|
||||
// Activate palpitation
|
||||
animacio_.palpitacio_activa = true;
|
||||
animacio_.palpitacio_fase = 0.0f;
|
||||
|
||||
// Randomize parameters
|
||||
float freq_range = Defaults::Enemies::Animation::PALPITACIO_FREQ_MAX -
|
||||
Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN;
|
||||
animacio_.palpitacio_frequencia = Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN +
|
||||
(static_cast<float>(std::rand()) / RAND_MAX) * freq_range;
|
||||
|
||||
float amp_range = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MAX -
|
||||
Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN;
|
||||
animacio_.palpitacio_amplitud = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN +
|
||||
(static_cast<float>(std::rand()) / RAND_MAX) * amp_range;
|
||||
|
||||
float dur_range = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MAX -
|
||||
Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN;
|
||||
animacio_.palpitacio_temps_restant = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN +
|
||||
(static_cast<float>(std::rand()) / RAND_MAX) * dur_range;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Enemic::actualitzar_rotacio_accelerada(float delta_time) {
|
||||
if (animacio_.drotacio_t < 1.0f) {
|
||||
// Transitioning to new target
|
||||
animacio_.drotacio_t += delta_time / animacio_.drotacio_duracio;
|
||||
|
||||
if (animacio_.drotacio_t >= 1.0f) {
|
||||
animacio_.drotacio_t = 1.0f;
|
||||
animacio_.drotacio_base = animacio_.drotacio_objetivo; // Reached target
|
||||
drotacio_ = animacio_.drotacio_base;
|
||||
} else {
|
||||
// Smoothstep interpolation: t² * (3 - 2t)
|
||||
float t = animacio_.drotacio_t;
|
||||
float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||
|
||||
// Interpolate between base and target
|
||||
float initial = animacio_.drotacio_base;
|
||||
float target = animacio_.drotacio_objetivo;
|
||||
drotacio_ = initial + (target - initial) * smooth_t;
|
||||
}
|
||||
} else {
|
||||
// Random trigger for new acceleration
|
||||
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
||||
float trigger_prob = Defaults::Enemies::Animation::ROTACIO_ACCEL_TRIGGER_PROB * delta_time;
|
||||
|
||||
if (rand_val < trigger_prob) {
|
||||
// Start new transition
|
||||
animacio_.drotacio_t = 0.0f;
|
||||
|
||||
// Randomize target speed (multiplier * base)
|
||||
float mult_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MAX -
|
||||
Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN;
|
||||
float multiplier = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN +
|
||||
(static_cast<float>(std::rand()) / RAND_MAX) * mult_range;
|
||||
|
||||
animacio_.drotacio_objetivo = animacio_.drotacio_base * multiplier;
|
||||
|
||||
// Randomize duration
|
||||
float dur_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MAX -
|
||||
Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN;
|
||||
animacio_.drotacio_duracio = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN +
|
||||
(static_cast<float>(std::rand()) / RAND_MAX) * dur_range;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float Enemic::calcular_escala_actual() const {
|
||||
float escala = 1.0f;
|
||||
|
||||
// [NEW] Invulnerability LERP prioritza sobre palpitació
|
||||
if (timer_invulnerabilitat_ > 0.0f) {
|
||||
// Calculate t: 0.0 at spawn → 1.0 at end
|
||||
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
||||
float t = 1.0f - t_inv; // 0.0 → 1.0
|
||||
|
||||
// Apply smoothstep: t² * (3 - 2t)
|
||||
float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||
|
||||
// LERP scale from 0.0 to 1.0
|
||||
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_START;
|
||||
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_END;
|
||||
escala = START + (END - START) * smooth_t;
|
||||
} else if (animacio_.palpitacio_activa) {
|
||||
// [EXISTING] Palpitació només quan no invulnerable
|
||||
escala += animacio_.palpitacio_amplitud * std::sin(animacio_.palpitacio_fase);
|
||||
}
|
||||
|
||||
return escala;
|
||||
}
|
||||
|
||||
// [NEW] Stage system API implementations
|
||||
|
||||
float Enemic::get_base_velocity() const {
|
||||
switch (tipus_) {
|
||||
case TipusEnemic::PENTAGON:
|
||||
return Defaults::Enemies::Pentagon::VELOCITAT;
|
||||
case TipusEnemic::QUADRAT:
|
||||
return Defaults::Enemies::Quadrat::VELOCITAT;
|
||||
case TipusEnemic::MOLINILLO:
|
||||
return Defaults::Enemies::Molinillo::VELOCITAT;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
float Enemic::get_base_rotation() const {
|
||||
// Return the base rotation speed (drotacio_base if available, otherwise current drotacio_)
|
||||
return animacio_.drotacio_base != 0.0f ? animacio_.drotacio_base : drotacio_;
|
||||
}
|
||||
|
||||
void Enemic::set_tracking_strength(float strength) {
|
||||
// Only applies to QUADRAT type
|
||||
if (tipus_ == TipusEnemic::QUADRAT) {
|
||||
tracking_strength_ = strength;
|
||||
}
|
||||
}
|
||||
|
||||
// [NEW] Safe spawn helper - checks if position is away from ship
|
||||
bool Enemic::intent_spawn_safe(const Punt& ship_pos, float& out_x, float& out_y) {
|
||||
// Generate random position within safe bounds
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
int range_x = static_cast<int>(max_x - min_x);
|
||||
int range_y = static_cast<int>(max_y - min_y);
|
||||
|
||||
out_x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
out_y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||
|
||||
// Check Euclidean distance to ship
|
||||
float dx = out_x - ship_pos.x;
|
||||
float dy = out_y - ship_pos.y;
|
||||
float distancia = std::sqrt(dx * dx + dy * dy);
|
||||
|
||||
// Return true if position is safe (>= 36px from ship)
|
||||
return distancia >= Defaults::Enemies::Spawn::SAFETY_DISTANCE;
|
||||
}
|
||||
|
||||
@@ -3,38 +3,117 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Enemic {
|
||||
public:
|
||||
Enemic() : renderer_(nullptr) {}
|
||||
Enemic(SDL_Renderer *renderer);
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
void inicialitzar();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_actiu() const { return esta_; }
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
void destruir() { esta_ = false; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida entre tots els enemics)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle de moviment
|
||||
float velocitat_;
|
||||
float drotacio_; // Delta rotació visual (rad/s)
|
||||
float rotacio_; // Rotació visual acumulada
|
||||
bool esta_;
|
||||
|
||||
void mou(float delta_time);
|
||||
// Tipus d'enemic
|
||||
enum class TipusEnemic : uint8_t {
|
||||
PENTAGON = 0, // Pentàgon esquivador (zigzag)
|
||||
QUADRAT = 1, // Quadrat perseguidor (tracks ship)
|
||||
MOLINILLO = 2 // Molinillo agressiu (fast, spinning)
|
||||
};
|
||||
|
||||
// Estat d'animació (palpitació i rotació accelerada)
|
||||
struct AnimacioEnemic {
|
||||
// Palpitation (breathing effect)
|
||||
bool palpitacio_activa = false;
|
||||
float palpitacio_fase = 0.0f; // Phase in cycle (0.0-2π)
|
||||
float palpitacio_frequencia = 2.0f; // Hz (cycles per second)
|
||||
float palpitacio_amplitud = 0.15f; // Scale variation (±15%)
|
||||
float palpitacio_temps_restant = 0.0f; // Time remaining (seconds)
|
||||
|
||||
// Rotation acceleration (long-term spin modulation)
|
||||
float drotacio_base = 0.0f; // Base rotation speed (rad/s)
|
||||
float drotacio_objetivo = 0.0f; // Target rotation speed (rad/s)
|
||||
float drotacio_t = 0.0f; // Interpolation progress (0.0-1.0)
|
||||
float drotacio_duracio = 0.0f; // Duration of transition (seconds)
|
||||
};
|
||||
|
||||
class Enemic {
|
||||
public:
|
||||
Enemic()
|
||||
: renderer_(nullptr) {}
|
||||
Enemic(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar(TipusEnemic tipus = TipusEnemic::PENTAGON, const Punt* ship_pos = nullptr);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
bool esta_actiu() const { return esta_; }
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
||||
void destruir() { esta_ = false; }
|
||||
float get_brightness() const { return brightness_; }
|
||||
float get_drotacio() const { return drotacio_; }
|
||||
Punt get_velocitat_vector() const {
|
||||
return {
|
||||
velocitat_ * std::cos(angle_ - Constants::PI / 2.0f),
|
||||
velocitat_ * std::sin(angle_ - Constants::PI / 2.0f)
|
||||
};
|
||||
}
|
||||
|
||||
// Set ship position reference for tracking behavior
|
||||
void set_ship_position(const Punt* ship_pos) { ship_position_ = ship_pos; }
|
||||
|
||||
// [NEW] Getters for stage system (base stats)
|
||||
float get_base_velocity() const;
|
||||
float get_base_rotation() const;
|
||||
TipusEnemic get_tipus() const { return tipus_; }
|
||||
|
||||
// [NEW] Setters for difficulty multipliers (stage system)
|
||||
void set_velocity(float vel) { velocitat_ = vel; }
|
||||
void set_rotation(float rot) { drotacio_ = rot; animacio_.drotacio_base = rot; }
|
||||
void set_tracking_strength(float strength);
|
||||
|
||||
// [NEW] Invulnerability queries
|
||||
bool es_invulnerable() const { return timer_invulnerabilitat_ > 0.0f; }
|
||||
float get_temps_invulnerabilitat() const { return timer_invulnerabilitat_; }
|
||||
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida entre tots els enemics)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle de moviment
|
||||
float velocitat_;
|
||||
float drotacio_; // Delta rotació visual (rad/s)
|
||||
float rotacio_; // Rotació visual acumulada
|
||||
bool esta_;
|
||||
float brightness_; // Factor de brillantor (0.0-1.0)
|
||||
|
||||
// [NEW] Enemy type and configuration
|
||||
TipusEnemic tipus_;
|
||||
|
||||
// [NEW] Animation state
|
||||
AnimacioEnemic animacio_;
|
||||
|
||||
// [NEW] Behavior state (type-specific)
|
||||
float tracking_timer_; // For Quadrat: time since last angle update
|
||||
const Punt* ship_position_; // Pointer to ship position (for tracking)
|
||||
float tracking_strength_; // For Quadrat: tracking intensity (0.0-1.5), default 0.5
|
||||
|
||||
// [NEW] Invulnerability state
|
||||
float timer_invulnerabilitat_; // Countdown timer (seconds), 0.0f = vulnerable
|
||||
|
||||
// [EXISTING] Private methods
|
||||
void mou(float delta_time);
|
||||
|
||||
// [NEW] Private methods
|
||||
void actualitzar_animacio(float delta_time);
|
||||
void actualitzar_palpitacio(float delta_time);
|
||||
void actualitzar_rotacio_accelerada(float delta_time);
|
||||
void comportament_pentagon(float delta_time);
|
||||
void comportament_quadrat(float delta_time);
|
||||
void comportament_molinillo(float delta_time);
|
||||
float calcular_escala_actual() const; // Returns scale with palpitation applied
|
||||
bool intent_spawn_safe(const Punt& ship_pos, float& out_x, float& out_y);
|
||||
};
|
||||
|
||||
@@ -3,137 +3,156 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "game/entities/nau.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "game/constants.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
Nau::Nau(SDL_Renderer *renderer)
|
||||
: renderer_(renderer), centre_({0.0f, 0.0f}), angle_(0.0f),
|
||||
velocitat_(0.0f), esta_tocada_(false) {
|
||||
Nau::Nau(SDL_Renderer* renderer)
|
||||
: renderer_(renderer),
|
||||
centre_({0.0f, 0.0f}),
|
||||
angle_(0.0f),
|
||||
velocitat_(0.0f),
|
||||
esta_tocada_(false),
|
||||
brightness_(Defaults::Brightness::NAU) {
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("ship2.shp");
|
||||
|
||||
// [NUEVO] Carregar forma compartida des de fitxer
|
||||
forma_ = Graphics::ShapeLoader::load("ship.shp");
|
||||
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
||||
}
|
||||
if (!forma_ || !forma_->es_valida()) {
|
||||
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Nau::inicialitzar() {
|
||||
// Inicialització de la nau (triangle)
|
||||
// Basat en el codi Pascal original: lines 380-384
|
||||
// Copiat de joc_asteroides.cpp línies 30-44
|
||||
void Nau::inicialitzar(const Punt* spawn_point) {
|
||||
// Inicialització de la nau (triangle)
|
||||
// Basat en el codi Pascal original: lines 380-384
|
||||
// Copiat de joc_asteroides.cpp línies 30-44
|
||||
|
||||
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del fitxer
|
||||
// Només inicialitzem l'estat de la instància
|
||||
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del
|
||||
// fitxer Només inicialitzem l'estat de la instància
|
||||
|
||||
// Posició inicial al centre de la pantalla
|
||||
centre_.x = 320.0f;
|
||||
centre_.y = 240.0f;
|
||||
// Use custom spawn point if provided, otherwise use center
|
||||
if (spawn_point) {
|
||||
centre_.x = spawn_point->x;
|
||||
centre_.y = spawn_point->y;
|
||||
} else {
|
||||
// Default: center of play area
|
||||
float centre_x, centre_y;
|
||||
Constants::obtenir_centre_zona(centre_x, centre_y);
|
||||
centre_.x = static_cast<int>(centre_x);
|
||||
centre_.y = static_cast<int>(centre_y);
|
||||
}
|
||||
|
||||
// Estat inicial
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
esta_tocada_ = false;
|
||||
// Estat inicial
|
||||
angle_ = 0.0f;
|
||||
velocitat_ = 0.0f;
|
||||
esta_tocada_ = false;
|
||||
}
|
||||
|
||||
void Nau::processar_input(float delta_time) {
|
||||
// Processar input continu (com teclapuls() del Pascal original)
|
||||
// Basat en joc_asteroides.cpp línies 66-85
|
||||
// Només processa input si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Processar input continu (com teclapuls() del Pascal original)
|
||||
// Basat en joc_asteroides.cpp línies 66-85
|
||||
// Només processa input si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
// Obtenir estat actual del teclat (no events, sinó estat continu)
|
||||
const bool *keyboard_state = SDL_GetKeyboardState(nullptr);
|
||||
// Obtenir estat actual del teclat (no events, sinó estat continu)
|
||||
const bool* keyboard_state = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
// Rotació
|
||||
if (keyboard_state[SDL_SCANCODE_RIGHT]) {
|
||||
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_LEFT]) {
|
||||
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
// Acceleració
|
||||
if (keyboard_state[SDL_SCANCODE_UP]) {
|
||||
if (velocitat_ < Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (velocitat_ > Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
// Rotació
|
||||
if (keyboard_state[SDL_SCANCODE_RIGHT]) {
|
||||
angle_ += Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_LEFT]) {
|
||||
angle_ -= Defaults::Physics::ROTATION_SPEED * delta_time;
|
||||
}
|
||||
|
||||
// Acceleració
|
||||
if (keyboard_state[SDL_SCANCODE_UP]) {
|
||||
if (velocitat_ < Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ += Defaults::Physics::ACCELERATION * delta_time;
|
||||
if (velocitat_ > Defaults::Physics::MAX_VELOCITY) {
|
||||
velocitat_ = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Nau::actualitzar(float delta_time) {
|
||||
// Només actualitzar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Només actualitzar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
// Aplicar física (moviment + fricció)
|
||||
aplicar_fisica(delta_time);
|
||||
// Aplicar física (moviment + fricció)
|
||||
aplicar_fisica(delta_time);
|
||||
}
|
||||
|
||||
void Nau::dibuixar() const {
|
||||
// Només dibuixar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
// Només dibuixar si la nau està viva
|
||||
if (esta_tocada_)
|
||||
return;
|
||||
|
||||
if (!forma_)
|
||||
return;
|
||||
if (!forma_)
|
||||
return;
|
||||
|
||||
// Escalar velocitat per l'efecte visual (200 px/s → ~6 px d'efecte)
|
||||
// El codi Pascal original sumava velocitat (0-6) al radi per donar
|
||||
// sensació de "empenta". Ara velocitat està en px/s (0-200).
|
||||
// Basat en joc_asteroides.cpp línies 127-134
|
||||
//
|
||||
// [NUEVO] Convertir suma de velocitat_visual a escala multiplicativa
|
||||
// Radio base del ship = 12 px
|
||||
// velocitat_visual = 0-6 → r = 12-18 → escala = 1.0-1.5
|
||||
float velocitat_visual = velocitat_ / 33.33f;
|
||||
float escala = 1.0f + (velocitat_visual / 12.0f);
|
||||
// Escalar velocitat per l'efecte visual (200 px/s → ~6 px d'efecte)
|
||||
// El codi Pascal original sumava velocitat (0-6) al radi per donar
|
||||
// sensació de "empenta". Ara velocitat està en px/s (0-200).
|
||||
// Basat en joc_asteroides.cpp línies 127-134
|
||||
//
|
||||
// [NUEVO] Convertir suma de velocitat_visual a escala multiplicativa
|
||||
// Radio base del ship = 12 px
|
||||
// velocitat_visual = 0-6 → r = 12-18 → escala = 1.0-1.5
|
||||
float velocitat_visual = velocitat_ / 33.33f;
|
||||
float escala = 1.0f + (velocitat_visual / 12.0f);
|
||||
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, escala, true);
|
||||
Rendering::render_shape(renderer_, forma_, centre_, angle_, escala, true, 1.0f, brightness_);
|
||||
}
|
||||
|
||||
void Nau::aplicar_fisica(float delta_time) {
|
||||
// Aplicar física de moviment
|
||||
// Basat en joc_asteroides.cpp línies 87-113
|
||||
// Aplicar física de moviment
|
||||
// Basat en joc_asteroides.cpp línies 87-113
|
||||
|
||||
// Calcular nova posició basada en velocitat i angle
|
||||
// S'usa (angle - PI/2) perquè angle=0 apunta cap amunt, no cap a la dreta
|
||||
// velocitat_ està en px/s, així que multipliquem per delta_time
|
||||
float dy =
|
||||
(velocitat_ * delta_time) * std::sin(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.y;
|
||||
float dx =
|
||||
(velocitat_ * delta_time) * std::cos(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.x;
|
||||
// Calcular nova posició basada en velocitat i angle
|
||||
// S'usa (angle - PI/2) perquè angle=0 apunta cap amunt, no cap a la dreta
|
||||
// velocitat_ està en px/s, així que multipliquem per delta_time
|
||||
float dy =
|
||||
(velocitat_ * delta_time) * std::sin(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.y;
|
||||
float dx =
|
||||
(velocitat_ * delta_time) * std::cos(angle_ - Constants::PI / 2.0f) +
|
||||
centre_.x;
|
||||
|
||||
// Boundary checking - només actualitzar si dins de la zona de joc
|
||||
// Acumulació directa amb precisió subpíxel
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona(min_x, max_x, min_y, max_y);
|
||||
// Boundary checking amb radi de la nau
|
||||
// CORRECCIÓ: Usar límits segurs i inequalitats inclusives
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::SHIP_RADIUS,
|
||||
min_x,
|
||||
max_x,
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
if (dy > min_y && dy < max_y) {
|
||||
centre_.y = dy;
|
||||
}
|
||||
|
||||
if (dx > min_x && dx < max_x) {
|
||||
centre_.x = dx;
|
||||
}
|
||||
|
||||
// Fricció - desacceleració gradual (time-based)
|
||||
if (velocitat_ > 0.1f) {
|
||||
velocitat_ -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (velocitat_ < 0.0f) {
|
||||
velocitat_ = 0.0f;
|
||||
// Inequalitats inclusives (>= i <=)
|
||||
if (dy >= min_y && dy <= max_y) {
|
||||
centre_.y = dy;
|
||||
}
|
||||
|
||||
if (dx >= min_x && dx <= max_x) {
|
||||
centre_.x = dx;
|
||||
}
|
||||
|
||||
// Fricció - desacceleració gradual (time-based)
|
||||
if (velocitat_ > 0.1f) {
|
||||
velocitat_ -= Defaults::Physics::FRICTION * delta_time;
|
||||
if (velocitat_ < 0.0f) {
|
||||
velocitat_ = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,40 +3,54 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#pragma once
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/types.hpp"
|
||||
#include "game/constants.hpp"
|
||||
|
||||
class Nau {
|
||||
public:
|
||||
Nau() : renderer_(nullptr) {}
|
||||
Nau(SDL_Renderer *renderer);
|
||||
public:
|
||||
Nau()
|
||||
: renderer_(nullptr) {}
|
||||
Nau(SDL_Renderer* renderer);
|
||||
|
||||
void inicialitzar();
|
||||
void processar_input(float delta_time);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
void inicialitzar(const Punt* spawn_point = nullptr);
|
||||
void processar_input(float delta_time);
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar() const;
|
||||
|
||||
// Getters (API pública sense canvis)
|
||||
const Punt &get_centre() const { return centre_; }
|
||||
float get_angle() const { return angle_; }
|
||||
bool esta_viva() const { return !esta_tocada_; }
|
||||
// Getters (API pública sense canvis)
|
||||
const Punt& get_centre() const { return centre_; }
|
||||
float get_angle() const { return angle_; }
|
||||
bool esta_viva() const { return !esta_tocada_; }
|
||||
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
||||
float get_brightness() const { return brightness_; }
|
||||
Punt get_velocitat_vector() const {
|
||||
return {
|
||||
velocitat_ * std::cos(angle_ - Constants::PI / 2.0f),
|
||||
velocitat_ * std::sin(angle_ - Constants::PI / 2.0f)
|
||||
};
|
||||
}
|
||||
|
||||
// Col·lisions (Fase 10)
|
||||
void marcar_tocada() { esta_tocada_ = true; }
|
||||
// Col·lisions (Fase 10)
|
||||
void marcar_tocada() { esta_tocada_ = true; }
|
||||
|
||||
private:
|
||||
SDL_Renderer *renderer_;
|
||||
private:
|
||||
SDL_Renderer* renderer_;
|
||||
|
||||
// [NUEVO] Forma vectorial (compartida, només 1 instància de Nau però preparat per reutilització)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
// [NUEVO] Forma vectorial (compartida, només 1 instància de Nau però preparat
|
||||
// per reutilització)
|
||||
std::shared_ptr<Graphics::Shape> forma_;
|
||||
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle d'orientació
|
||||
float velocitat_; // Velocitat (px/s)
|
||||
bool esta_tocada_;
|
||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||
Punt centre_;
|
||||
float angle_; // Angle d'orientació
|
||||
float velocitat_; // Velocitat (px/s)
|
||||
bool esta_tocada_;
|
||||
float brightness_; // Factor de brillantor (0.0-1.0)
|
||||
|
||||
void aplicar_fisica(float delta_time);
|
||||
void aplicar_fisica(float delta_time);
|
||||
};
|
||||
|
||||
@@ -3,211 +3,753 @@
|
||||
// © 2025 Port a C++20 amb SDL3
|
||||
|
||||
#include "escena_joc.hpp"
|
||||
#include "../../core/rendering/line_renderer.hpp"
|
||||
#include "../../core/system/gestor_escenes.hpp"
|
||||
#include "../../core/system/global_events.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
EscenaJoc::EscenaJoc(SDLManager& sdl)
|
||||
: sdl_(sdl), nau_(sdl.obte_renderer()), itocado_(0), text_(sdl.obte_renderer()) {
|
||||
// Inicialitzar bales amb renderer
|
||||
for (auto &bala : bales_) {
|
||||
bala = Bala(sdl.obte_renderer());
|
||||
}
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/rendering/line_renderer.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/system/global_events.hpp"
|
||||
#include "game/stage_system/stage_loader.hpp"
|
||||
|
||||
// Inicialitzar enemics amb renderer
|
||||
for (auto &enemy : orni_) {
|
||||
enemy = Enemic(sdl.obte_renderer());
|
||||
}
|
||||
// Using declarations per simplificar el codi
|
||||
using GestorEscenes::ContextEscenes;
|
||||
using Escena = ContextEscenes::Escena;
|
||||
using Opcio = ContextEscenes::Opcio;
|
||||
|
||||
EscenaJoc::EscenaJoc(SDLManager& sdl, ContextEscenes& context)
|
||||
: sdl_(sdl),
|
||||
context_(context),
|
||||
debris_manager_(sdl.obte_renderer()),
|
||||
gestor_puntuacio_(sdl.obte_renderer()),
|
||||
nau_(sdl.obte_renderer()),
|
||||
itocado_(0),
|
||||
puntuacio_total_(0),
|
||||
text_(sdl.obte_renderer()) {
|
||||
// Consumir opcions (preparació per MODE_DEMO futur)
|
||||
auto opcio = context_.consumir_opcio();
|
||||
(void)opcio; // Suprimir warning de variable no usada
|
||||
|
||||
// Inicialitzar bales amb renderer
|
||||
for (auto& bala : bales_) {
|
||||
bala = Bala(sdl.obte_renderer());
|
||||
}
|
||||
|
||||
// Inicialitzar enemics amb renderer
|
||||
for (auto& enemy : orni_) {
|
||||
enemy = Enemic(sdl.obte_renderer());
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::executar() {
|
||||
std::cout << "Escena Joc: Inicialitzant...\n";
|
||||
std::cout << "Escena Joc: Inicialitzant...\n";
|
||||
|
||||
// Inicialitzar estat del joc
|
||||
inicialitzar();
|
||||
// Inicialitzar estat del joc
|
||||
inicialitzar();
|
||||
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
|
||||
while (GestorEscenes::actual == GestorEscenes::Escena::JOC) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
while (GestorEscenes::actual == Escena::JOC) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
}
|
||||
|
||||
// Actualitzar comptador de FPS
|
||||
sdl_.updateFPS(delta_time);
|
||||
|
||||
// Actualitzar visibilitat del cursor (auto-ocultar)
|
||||
Mouse::updateCursorVisibility();
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processament específic del joc (SPACE per disparar)
|
||||
processar_input(event);
|
||||
}
|
||||
|
||||
// Actualitzar física del joc amb delta_time real
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar sistema d'audio
|
||||
Audio::update();
|
||||
|
||||
// Actualitzar colors oscil·lats
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Netejar pantalla (usa color oscil·lat)
|
||||
sdl_.neteja(0, 0, 0);
|
||||
|
||||
// Actualitzar context de renderitzat (factor d'escala global)
|
||||
sdl_.updateRenderingContext();
|
||||
|
||||
// Dibuixar joc
|
||||
dibuixar();
|
||||
|
||||
// Presentar renderer (swap buffers)
|
||||
sdl_.presenta();
|
||||
}
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processament específic del joc (SPACE per disparar)
|
||||
processar_input(event);
|
||||
}
|
||||
|
||||
// Actualitzar física del joc amb delta_time real
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar colors oscil·lats
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Netejar pantalla (usa color oscil·lat)
|
||||
sdl_.neteja(0, 0, 0);
|
||||
|
||||
// Dibuixar joc
|
||||
dibuixar();
|
||||
|
||||
// Presentar renderer (swap buffers)
|
||||
sdl_.presenta();
|
||||
}
|
||||
|
||||
std::cout << "Escena Joc: Finalitzant...\n";
|
||||
std::cout << "Escena Joc: Finalitzant...\n";
|
||||
}
|
||||
|
||||
void EscenaJoc::inicialitzar() {
|
||||
// Inicialitzar generador de números aleatoris
|
||||
// Basat en el codi Pascal original: line 376
|
||||
std::srand(static_cast<unsigned>(std::time(nullptr)));
|
||||
// Inicialitzar generador de números aleatoris
|
||||
// Basat en el codi Pascal original: line 376
|
||||
std::srand(static_cast<unsigned>(std::time(nullptr)));
|
||||
|
||||
// Inicialitzar estat de col·lisió
|
||||
itocado_ = 0;
|
||||
// [NEW] Load stage configuration (only once)
|
||||
if (!stage_config_) {
|
||||
stage_config_ = StageSystem::StageLoader::carregar("data/stages/stages.yaml");
|
||||
if (!stage_config_) {
|
||||
std::cerr << "[EscenaJoc] Error: no s'ha pogut carregar stages.yaml" << std::endl;
|
||||
// Continue without stage system (will crash, but helps debugging)
|
||||
}
|
||||
}
|
||||
|
||||
// Inicialitzar nau
|
||||
nau_.inicialitzar();
|
||||
// [NEW] Initialize stage manager
|
||||
stage_manager_ = std::make_unique<StageSystem::StageManager>(stage_config_.get());
|
||||
stage_manager_->inicialitzar();
|
||||
|
||||
// Inicialitzar enemics (ORNIs)
|
||||
for (auto &enemy : orni_) {
|
||||
enemy.inicialitzar();
|
||||
}
|
||||
// [NEW] Set ship position reference for safe spawn
|
||||
stage_manager_->get_spawn_controller().set_ship_position(&nau_.get_centre());
|
||||
|
||||
// Inicialitzar bales
|
||||
for (auto &bala : bales_) {
|
||||
bala.inicialitzar();
|
||||
}
|
||||
// Inicialitzar estat de col·lisió
|
||||
itocado_ = 0;
|
||||
|
||||
// Initialize lives and game over state
|
||||
num_vides_ = Defaults::Game::STARTING_LIVES;
|
||||
game_over_ = false;
|
||||
game_over_timer_ = 0.0f;
|
||||
|
||||
// Initialize score
|
||||
puntuacio_total_ = 0;
|
||||
gestor_puntuacio_.reiniciar();
|
||||
|
||||
// Set spawn point to center of play area
|
||||
Constants::obtenir_centre_zona(punt_spawn_.x, punt_spawn_.y);
|
||||
|
||||
// Inicialitzar nau
|
||||
nau_.inicialitzar();
|
||||
|
||||
// [MODIFIED] Initialize enemies as inactive (stage system will spawn them)
|
||||
for (auto& enemy : orni_) {
|
||||
enemy = Enemic(sdl_.obte_renderer());
|
||||
enemy.set_ship_position(&nau_.get_centre()); // Set ship reference for tracking
|
||||
// DON'T call enemy.inicialitzar() here - stage system handles spawning
|
||||
}
|
||||
|
||||
// Inicialitzar bales
|
||||
for (auto& bala : bales_) {
|
||||
bala.inicialitzar();
|
||||
}
|
||||
|
||||
// Iniciar música de joc (sense stopMusic, ja s'ha parat en destructor de TITOL)
|
||||
Audio::get()->playMusic("game.ogg");
|
||||
}
|
||||
|
||||
void EscenaJoc::actualitzar(float delta_time) {
|
||||
// Actualitzar nau (input + física)
|
||||
nau_.processar_input(delta_time);
|
||||
nau_.actualitzar(delta_time);
|
||||
// Check game over state first
|
||||
if (game_over_) {
|
||||
// Game over: only update timer, enemies, bullets, and debris
|
||||
game_over_timer_ -= delta_time;
|
||||
|
||||
// Actualitzar moviment i rotació dels enemics (ORNIs)
|
||||
for (auto &enemy : orni_) {
|
||||
enemy.actualitzar(delta_time);
|
||||
}
|
||||
if (game_over_timer_ <= 0.0f) {
|
||||
// Aturar música de joc abans de tornar al títol
|
||||
Audio::get()->stopMusic();
|
||||
// Transició a pantalla de títol
|
||||
context_.canviar_escena(Escena::TITOL);
|
||||
GestorEscenes::actual = Escena::TITOL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Actualitzar moviment de bales (Fase 9)
|
||||
for (auto &bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
// Enemies and bullets continue moving during game over
|
||||
for (auto& enemy : orni_) {
|
||||
enemy.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
for (auto& bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
gestor_puntuacio_.actualitzar(delta_time);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check death sequence state
|
||||
if (itocado_ > 0.0f) {
|
||||
// Death sequence active: update timer
|
||||
itocado_ += delta_time;
|
||||
|
||||
// Check if death duration completed
|
||||
if (itocado_ >= Defaults::Game::DEATH_DURATION) {
|
||||
// *** PHASE 3: RESPAWN OR GAME OVER ***
|
||||
|
||||
// Decrement lives
|
||||
num_vides_--;
|
||||
|
||||
if (num_vides_ > 0) {
|
||||
// Respawn ship
|
||||
nau_.inicialitzar(&punt_spawn_);
|
||||
itocado_ = 0.0f;
|
||||
} else {
|
||||
// Game over
|
||||
game_over_ = true;
|
||||
game_over_timer_ = Defaults::Game::GAME_OVER_DURATION;
|
||||
itocado_ = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Enemies and bullets continue moving during death sequence
|
||||
for (auto& enemy : orni_) {
|
||||
enemy.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
for (auto& bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
gestor_puntuacio_.actualitzar(delta_time);
|
||||
return;
|
||||
}
|
||||
|
||||
// *** STAGE SYSTEM STATE MACHINE ***
|
||||
|
||||
StageSystem::EstatStage estat = stage_manager_->get_estat();
|
||||
|
||||
switch (estat) {
|
||||
case StageSystem::EstatStage::LEVEL_START:
|
||||
// Update countdown timer
|
||||
stage_manager_->actualitzar(delta_time);
|
||||
|
||||
// [NEW] Allow ship movement and shooting during intro
|
||||
nau_.processar_input(delta_time);
|
||||
nau_.actualitzar(delta_time);
|
||||
|
||||
// [NEW] Update bullets
|
||||
for (auto& bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
// [NEW] Update debris
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
break;
|
||||
|
||||
case StageSystem::EstatStage::PLAYING: {
|
||||
// [NEW] Update stage manager (spawns enemies, pass pause flag)
|
||||
bool pausar_spawn = (itocado_ > 0.0f); // Pause during death animation
|
||||
stage_manager_->get_spawn_controller().actualitzar(delta_time, orni_, pausar_spawn);
|
||||
|
||||
// [NEW] Check stage completion (only when not in death sequence)
|
||||
if (itocado_ == 0.0f) {
|
||||
auto& spawn_ctrl = stage_manager_->get_spawn_controller();
|
||||
if (spawn_ctrl.tots_enemics_destruits(orni_)) {
|
||||
stage_manager_->stage_completat();
|
||||
Audio::get()->playSound(Defaults::Sound::GOOD_JOB_COMMANDER, Audio::Group::GAME);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// [EXISTING] Normal gameplay
|
||||
nau_.processar_input(delta_time);
|
||||
nau_.actualitzar(delta_time);
|
||||
|
||||
for (auto& enemy : orni_) {
|
||||
enemy.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
for (auto& bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
detectar_col·lisions_bales_enemics();
|
||||
detectar_col·lisio_nau_enemics();
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
gestor_puntuacio_.actualitzar(delta_time);
|
||||
break;
|
||||
}
|
||||
|
||||
case StageSystem::EstatStage::LEVEL_COMPLETED:
|
||||
// Update countdown timer
|
||||
stage_manager_->actualitzar(delta_time);
|
||||
|
||||
// [NEW] Allow ship movement and shooting during outro
|
||||
nau_.processar_input(delta_time);
|
||||
nau_.actualitzar(delta_time);
|
||||
|
||||
// [NEW] Update bullets (allow last shots to continue)
|
||||
for (auto& bala : bales_) {
|
||||
bala.actualitzar(delta_time);
|
||||
}
|
||||
|
||||
// [NEW] Update debris (from last destroyed enemies)
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
gestor_puntuacio_.actualitzar(delta_time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::dibuixar() {
|
||||
// Dibuixar marges de la zona de joc
|
||||
dibuixar_marges();
|
||||
// Draw borders (always visible)
|
||||
dibuixar_marges();
|
||||
|
||||
// Dibuixar nau
|
||||
nau_.dibuixar();
|
||||
// Check game over state
|
||||
if (game_over_) {
|
||||
// Game over: draw enemies, bullets, debris, and "GAME OVER" text
|
||||
|
||||
// Dibuixar ORNIs (enemics)
|
||||
for (const auto &enemy : orni_) {
|
||||
enemy.dibuixar();
|
||||
}
|
||||
for (const auto& enemy : orni_) {
|
||||
enemy.dibuixar();
|
||||
}
|
||||
|
||||
// Dibuixar bales (Fase 9)
|
||||
for (const auto &bala : bales_) {
|
||||
bala.dibuixar();
|
||||
}
|
||||
for (const auto& bala : bales_) {
|
||||
bala.dibuixar();
|
||||
}
|
||||
|
||||
// Dibuixar marcador
|
||||
dibuixar_marcador();
|
||||
debris_manager_.dibuixar();
|
||||
gestor_puntuacio_.dibuixar();
|
||||
|
||||
// Draw centered "GAME OVER" text
|
||||
const std::string game_over_text = "GAME OVER";
|
||||
constexpr float escala = 2.0f;
|
||||
constexpr float spacing = 4.0f;
|
||||
|
||||
float text_width = text_.get_text_width(game_over_text, escala, spacing);
|
||||
float text_height = text_.get_text_height(escala);
|
||||
|
||||
const SDL_FRect& play_area = Defaults::Zones::PLAYAREA;
|
||||
float x = play_area.x + (play_area.w - text_width) / 2.0f;
|
||||
float y = play_area.y + (play_area.h - text_height) / 2.0f;
|
||||
|
||||
text_.render(game_over_text, {x, y}, escala, spacing);
|
||||
|
||||
dibuixar_marcador();
|
||||
return;
|
||||
}
|
||||
|
||||
// [NEW] Stage state rendering
|
||||
StageSystem::EstatStage estat = stage_manager_->get_estat();
|
||||
|
||||
switch (estat) {
|
||||
case StageSystem::EstatStage::LEVEL_START:
|
||||
// [NEW] Draw ship if alive
|
||||
if (itocado_ == 0.0f) {
|
||||
nau_.dibuixar();
|
||||
}
|
||||
|
||||
// [NEW] Draw bullets
|
||||
for (const auto& bala : bales_) {
|
||||
bala.dibuixar();
|
||||
}
|
||||
|
||||
// [NEW] Draw debris
|
||||
debris_manager_.dibuixar();
|
||||
gestor_puntuacio_.dibuixar();
|
||||
|
||||
// [EXISTING] Draw intro message and score
|
||||
dibuixar_missatge_stage(stage_manager_->get_missatge_level_start());
|
||||
dibuixar_marcador();
|
||||
break;
|
||||
|
||||
case StageSystem::EstatStage::PLAYING:
|
||||
// [EXISTING] Normal rendering
|
||||
if (itocado_ == 0.0f) {
|
||||
nau_.dibuixar();
|
||||
}
|
||||
|
||||
for (const auto& enemy : orni_) {
|
||||
enemy.dibuixar();
|
||||
}
|
||||
|
||||
for (const auto& bala : bales_) {
|
||||
bala.dibuixar();
|
||||
}
|
||||
|
||||
debris_manager_.dibuixar();
|
||||
gestor_puntuacio_.dibuixar();
|
||||
dibuixar_marcador();
|
||||
break;
|
||||
|
||||
case StageSystem::EstatStage::LEVEL_COMPLETED:
|
||||
// [NEW] Draw ship if alive
|
||||
if (itocado_ == 0.0f) {
|
||||
nau_.dibuixar();
|
||||
}
|
||||
|
||||
// [NEW] Draw bullets (allow last shots to be visible)
|
||||
for (const auto& bala : bales_) {
|
||||
bala.dibuixar();
|
||||
}
|
||||
|
||||
// [NEW] Draw debris (from last destroyed enemies)
|
||||
debris_manager_.dibuixar();
|
||||
gestor_puntuacio_.dibuixar();
|
||||
|
||||
// [EXISTING] Draw completion message and score
|
||||
dibuixar_missatge_stage(StageSystem::Constants::MISSATGE_LEVEL_COMPLETED);
|
||||
dibuixar_marcador();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::processar_input(const SDL_Event &event) {
|
||||
// Processament d'input per events puntuals (no continus)
|
||||
// L'input continu (fletxes) es processa en actualitzar() amb
|
||||
// SDL_GetKeyboardState()
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_SPACE:
|
||||
// Disparar (Fase 9)
|
||||
// Basat en el codi Pascal original: crear bala en posició de la nau
|
||||
// El joc original només permetia 1 bala activa alhora
|
||||
|
||||
// Buscar primera bala inactiva
|
||||
for (auto &bala : bales_) {
|
||||
if (!bala.esta_activa()) {
|
||||
bala.disparar(nau_.get_centre(), nau_.get_angle());
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
void EscenaJoc::processar_input(const SDL_Event& event) {
|
||||
// Ignore ship controls during game over
|
||||
if (game_over_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore ship controls during death sequence
|
||||
if (itocado_ > 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Processament d'input per events puntuals (no continus)
|
||||
// L'input continu (fletxes) es processa en actualitzar() amb
|
||||
// SDL_GetKeyboardState()
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_SPACE: {
|
||||
// No disparar si la nau està morta
|
||||
if (!nau_.esta_viva()) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Disparar bala des del front de la nau
|
||||
// El ship.shp té el front a (0, -12) en coordenades locals
|
||||
|
||||
// 1. Calcular posició del front de la nau
|
||||
constexpr float LOCAL_TIP_X = 0.0f;
|
||||
constexpr float LOCAL_TIP_Y = -12.0f;
|
||||
|
||||
const Punt& ship_centre = nau_.get_centre();
|
||||
float ship_angle = nau_.get_angle();
|
||||
|
||||
// Aplicar transformació: rotació + trasllació
|
||||
float cos_a = std::cos(ship_angle);
|
||||
float sin_a = std::sin(ship_angle);
|
||||
|
||||
float tip_x = LOCAL_TIP_X * cos_a - LOCAL_TIP_Y * sin_a + ship_centre.x;
|
||||
float tip_y = LOCAL_TIP_X * sin_a + LOCAL_TIP_Y * cos_a + ship_centre.y;
|
||||
|
||||
Punt posicio_dispar = {tip_x, tip_y};
|
||||
|
||||
// 2. Buscar primera bala inactiva i disparar
|
||||
for (auto& bala : bales_) {
|
||||
if (!bala.esta_activa()) {
|
||||
bala.disparar(posicio_dispar, ship_angle);
|
||||
break; // Només una bala per polsació
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::tocado() {
|
||||
// TODO: Implementar seqüència de mort
|
||||
// Death sequence: 3 phases
|
||||
// Phase 1: First call (itocado_ == 0) - trigger explosion
|
||||
// Phase 2: Animation (0 < itocado_ < 3.0s) - debris animation
|
||||
// Phase 3: Respawn or game over (itocado_ >= 3.0s) - handled in actualitzar()
|
||||
|
||||
if (itocado_ == 0.0f) {
|
||||
// *** PHASE 1: TRIGGER DEATH ***
|
||||
|
||||
// Mark ship as dead (stops rendering and input)
|
||||
nau_.marcar_tocada();
|
||||
|
||||
// Create ship explosion
|
||||
const Punt& ship_pos = nau_.get_centre();
|
||||
float ship_angle = nau_.get_angle();
|
||||
Punt vel_nau = nau_.get_velocitat_vector();
|
||||
// Reduir a 80% la velocitat heretada per la nau (més realista)
|
||||
Punt vel_nau_80 = {vel_nau.x * 0.8f, vel_nau.y * 0.8f};
|
||||
|
||||
debris_manager_.explotar(
|
||||
nau_.get_forma(), // Ship shape (3 lines)
|
||||
ship_pos, // Center position
|
||||
ship_angle, // Ship orientation
|
||||
1.0f, // Normal scale
|
||||
Defaults::Physics::Debris::VELOCITAT_BASE, // 80 px/s
|
||||
nau_.get_brightness(), // Heredar brightness
|
||||
vel_nau_80, // Heredar 80% velocitat
|
||||
0.0f, // Nave: trayectorias rectas (sin drotacio)
|
||||
0.0f // Sin herencia visual (rotación aleatoria)
|
||||
);
|
||||
|
||||
// Start death timer (non-zero to avoid re-triggering)
|
||||
itocado_ = 0.001f;
|
||||
}
|
||||
// Phase 2 is automatic (debris updates in actualitzar())
|
||||
// Phase 3 is handled in actualitzar() when itocado_ >= DEATH_DURATION
|
||||
}
|
||||
|
||||
void EscenaJoc::dibuixar_marges() const {
|
||||
// Dibuixar rectangle de la zona de joc
|
||||
const SDL_FRect& zona = Defaults::Zones::GAME;
|
||||
// Dibuixar rectangle de la zona de joc
|
||||
const SDL_FRect& zona = Defaults::Zones::PLAYAREA;
|
||||
|
||||
// Coordenades dels cantons
|
||||
int x1 = static_cast<int>(zona.x);
|
||||
int y1 = static_cast<int>(zona.y);
|
||||
int x2 = static_cast<int>(zona.x + zona.w);
|
||||
int y2 = static_cast<int>(zona.y + zona.h);
|
||||
// Coordenades dels cantons
|
||||
int x1 = static_cast<int>(zona.x);
|
||||
int y1 = static_cast<int>(zona.y);
|
||||
int x2 = static_cast<int>(zona.x + zona.w);
|
||||
int y2 = static_cast<int>(zona.y + zona.h);
|
||||
|
||||
// 4 línies per formar el rectangle
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y1, x2, y1, true); // Top
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y2, x2, y2, true); // Bottom
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y1, x1, y2, true); // Left
|
||||
Rendering::linea(sdl_.obte_renderer(), x2, y1, x2, y2, true); // Right
|
||||
// 4 línies per formar el rectangle
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y1, x2, y1, true); // Top
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y2, x2, y2, true); // Bottom
|
||||
Rendering::linea(sdl_.obte_renderer(), x1, y1, x1, y2, true); // Left
|
||||
Rendering::linea(sdl_.obte_renderer(), x2, y1, x2, y2, true); // Right
|
||||
}
|
||||
|
||||
void EscenaJoc::dibuixar_marcador() {
|
||||
// Text estàtic (hardcoded)
|
||||
const std::string text = "SCORE: 00000 LIFE: 3 LEVEL: 01";
|
||||
// [MODIFIED] Display current stage number from stage manager
|
||||
uint8_t stage_num = stage_manager_->get_stage_actual();
|
||||
std::string stage_str = (stage_num < 10) ? "0" + std::to_string(stage_num)
|
||||
: std::to_string(stage_num);
|
||||
|
||||
// Escala ajustada per cabre en 640px d'amplada
|
||||
// Zona marcador: width = 640 px, height = 64 px
|
||||
// Text: 34 caràcters → necessitem ~487 px amb escala 1.2
|
||||
// Altura caràcter: 20 * 1.2 = 24 px (37.5% de 64px)
|
||||
const float escala = 1.2f;
|
||||
const float spacing = 2.0f;
|
||||
// Format score with padding to 5 digits (e.g., 150 → "00150")
|
||||
std::string score_str = std::to_string(puntuacio_total_);
|
||||
score_str = std::string(5 - std::min(5, static_cast<int>(score_str.length())), '0') + score_str;
|
||||
|
||||
// Calcular amplada total del text
|
||||
float text_width = text_.get_text_width(text, escala, spacing);
|
||||
std::string text = "SCORE: " + score_str + " LIFES: " + std::to_string(num_vides_) +
|
||||
" LEVEL: " + stage_str;
|
||||
|
||||
// Centrat horitzontal
|
||||
float x = (Defaults::Zones::SCOREBOARD.w - text_width) / 2.0f;
|
||||
// Paràmetres de renderització
|
||||
const float escala = 0.85f;
|
||||
const float spacing = 0.0f;
|
||||
|
||||
// Centrat vertical
|
||||
// Altura del caràcter escalat: 20 * 1.2 = 24 px
|
||||
// Marge superior: (64 - 24) / 2 = 20 px
|
||||
float y = Defaults::Zones::SCOREBOARD.y + 20.0f;
|
||||
// Calcular dimensions del text
|
||||
float text_width = text_.get_text_width(text, escala, spacing);
|
||||
float text_height = text_.get_text_height(escala);
|
||||
|
||||
// Renderitzar
|
||||
text_.render(text, {x, y}, escala, spacing);
|
||||
// Centrat horitzontal dins de la zona del marcador
|
||||
float x = (Defaults::Zones::SCOREBOARD.w - text_width) / 2.0f;
|
||||
|
||||
// Centrat vertical dins de la zona del marcador
|
||||
float y = Defaults::Zones::SCOREBOARD.y +
|
||||
(Defaults::Zones::SCOREBOARD.h - text_height) / 2.0f;
|
||||
|
||||
// Renderitzar
|
||||
text_.render(text, {x, y}, escala, spacing);
|
||||
}
|
||||
|
||||
void EscenaJoc::detectar_col·lisions_bales_enemics() {
|
||||
// Constants amplificades per hitbox més generós (115%)
|
||||
constexpr float RADI_BALA = Defaults::Entities::BULLET_RADIUS;
|
||||
constexpr float RADI_ENEMIC = Defaults::Entities::ENEMY_RADIUS;
|
||||
constexpr float SUMA_RADIS = (RADI_BALA + RADI_ENEMIC) * 1.15f; // 28.75 px
|
||||
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS; // 826.56
|
||||
|
||||
// Velocitat d'explosió reduïda per efecte suau
|
||||
constexpr float VELOCITAT_EXPLOSIO = 80.0f; // px/s (en lloc de 80.0f per defecte)
|
||||
|
||||
// Iterar per totes les bales actives
|
||||
for (auto& bala : bales_) {
|
||||
if (!bala.esta_activa()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Punt& pos_bala = bala.get_centre();
|
||||
|
||||
// Comprovar col·lisió amb tots els enemics actius
|
||||
for (auto& enemic : orni_) {
|
||||
if (!enemic.esta_actiu()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// [NEW] Skip collision if enemy is invulnerable
|
||||
if (enemic.es_invulnerable()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Punt& pos_enemic = enemic.get_centre();
|
||||
|
||||
// Calcular distància quadrada (evita sqrt)
|
||||
float dx = pos_bala.x - pos_enemic.x;
|
||||
float dy = pos_bala.y - pos_enemic.y;
|
||||
float distancia_quadrada = dx * dx + dy * dy;
|
||||
|
||||
// Comprovar col·lisió
|
||||
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
||||
// *** COL·LISIÓ DETECTADA ***
|
||||
|
||||
// 1. Calculate score for enemy type
|
||||
int punts = 0;
|
||||
switch (enemic.get_tipus()) {
|
||||
case TipusEnemic::PENTAGON:
|
||||
punts = Defaults::Enemies::Scoring::PENTAGON_SCORE;
|
||||
break;
|
||||
case TipusEnemic::QUADRAT:
|
||||
punts = Defaults::Enemies::Scoring::QUADRAT_SCORE;
|
||||
break;
|
||||
case TipusEnemic::MOLINILLO:
|
||||
punts = Defaults::Enemies::Scoring::MOLINILLO_SCORE;
|
||||
break;
|
||||
}
|
||||
|
||||
// 2. Add to total score
|
||||
puntuacio_total_ += punts;
|
||||
|
||||
// 3. Create floating score number
|
||||
gestor_puntuacio_.crear(punts, pos_enemic);
|
||||
|
||||
// 4. Destruir enemic (marca com inactiu)
|
||||
enemic.destruir();
|
||||
|
||||
// 2. Crear explosió de fragments
|
||||
Punt vel_enemic = enemic.get_velocitat_vector();
|
||||
debris_manager_.explotar(
|
||||
enemic.get_forma(), // Forma vectorial del pentàgon
|
||||
pos_enemic, // Posició central
|
||||
0.0f, // Angle (enemic té rotació interna)
|
||||
1.0f, // Escala normal
|
||||
VELOCITAT_EXPLOSIO, // 50 px/s (explosió suau)
|
||||
enemic.get_brightness(), // Heredar brightness
|
||||
vel_enemic, // Heredar velocitat
|
||||
enemic.get_drotacio(), // Heredar velocitat angular (trayectorias curvas)
|
||||
0.0f // Sin herencia visual (rotación aleatoria)
|
||||
);
|
||||
|
||||
// 3. Desactivar bala
|
||||
bala.desactivar();
|
||||
|
||||
// 4. Eixir del bucle intern (bala només destrueix 1 enemic)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::detectar_col·lisio_nau_enemics() {
|
||||
// Only check collisions if ship is alive
|
||||
if (!nau_.esta_viva()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Generous collision detection (80% hitbox)
|
||||
constexpr float RADI_NAU = Defaults::Entities::SHIP_RADIUS;
|
||||
constexpr float RADI_ENEMIC = Defaults::Entities::ENEMY_RADIUS;
|
||||
constexpr float SUMA_RADIS =
|
||||
(RADI_NAU + RADI_ENEMIC) * Defaults::Game::COLLISION_SHIP_ENEMY_AMPLIFIER;
|
||||
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS;
|
||||
|
||||
const Punt& pos_nau = nau_.get_centre();
|
||||
|
||||
// Check collision with all active enemies
|
||||
for (const auto& enemic : orni_) {
|
||||
if (!enemic.esta_actiu()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// [NEW] Skip collision if enemy is invulnerable
|
||||
if (enemic.es_invulnerable()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Punt& pos_enemic = enemic.get_centre();
|
||||
|
||||
// Calculate squared distance (avoid sqrt)
|
||||
float dx = static_cast<float>(pos_nau.x - pos_enemic.x);
|
||||
float dy = static_cast<float>(pos_nau.y - pos_enemic.y);
|
||||
float distancia_quadrada = dx * dx + dy * dy;
|
||||
|
||||
// Check collision
|
||||
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
||||
tocado(); // Trigger death sequence
|
||||
return; // Only one collision per frame
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [NEW] Stage system helper methods
|
||||
|
||||
void EscenaJoc::dibuixar_missatge_stage(const std::string& missatge) {
|
||||
constexpr float escala_base = 1.0f;
|
||||
constexpr float spacing = 2.0f;
|
||||
constexpr float max_width_ratio = 0.9f; // 90% del ancho disponible
|
||||
|
||||
const SDL_FRect& play_area = Defaults::Zones::PLAYAREA;
|
||||
const float max_width = play_area.w * max_width_ratio; // 558px
|
||||
|
||||
// ========== TYPEWRITER EFFECT (PARAMETRIZED) ==========
|
||||
// Get state-specific timing configuration
|
||||
float total_time;
|
||||
float typing_ratio;
|
||||
|
||||
if (stage_manager_->get_estat() == StageSystem::EstatStage::LEVEL_START) {
|
||||
total_time = Defaults::Game::LEVEL_START_DURATION;
|
||||
typing_ratio = Defaults::Game::LEVEL_START_TYPING_RATIO;
|
||||
} else { // LEVEL_COMPLETED
|
||||
total_time = Defaults::Game::LEVEL_COMPLETED_DURATION;
|
||||
typing_ratio = Defaults::Game::LEVEL_COMPLETED_TYPING_RATIO;
|
||||
}
|
||||
|
||||
// Calculate progress from timer (0.0 at start → 1.0 at end)
|
||||
float remaining_time = stage_manager_->get_timer_transicio();
|
||||
float progress = 1.0f - (remaining_time / total_time);
|
||||
|
||||
// Determine how many characters to show
|
||||
size_t visible_chars;
|
||||
|
||||
if (typing_ratio > 0.0f && progress < typing_ratio) {
|
||||
// Typewriter phase: show partial text
|
||||
float typing_progress = progress / typing_ratio; // Normalize to 0.0-1.0
|
||||
visible_chars = static_cast<size_t>(missatge.length() * typing_progress);
|
||||
if (visible_chars == 0 && progress > 0.0f) {
|
||||
visible_chars = 1; // Show at least 1 character after first frame
|
||||
}
|
||||
} else {
|
||||
// Display phase: show complete text
|
||||
// (Either after typing phase, or immediately if typing_ratio == 0.0)
|
||||
visible_chars = missatge.length();
|
||||
}
|
||||
|
||||
// Create partial message (substring for typewriter)
|
||||
std::string partial_message = missatge.substr(0, visible_chars);
|
||||
// ===================================================
|
||||
|
||||
// Calculate text width at base scale (using FULL message for position calculation)
|
||||
float text_width_at_base = text_.get_text_width(missatge, escala_base, spacing);
|
||||
|
||||
// Auto-scale if text exceeds max width
|
||||
float escala = (text_width_at_base <= max_width)
|
||||
? escala_base
|
||||
: max_width / text_width_at_base;
|
||||
|
||||
// Recalculate dimensions with final scale (using FULL message for centering)
|
||||
float full_text_width = text_.get_text_width(missatge, escala, spacing);
|
||||
float text_height = text_.get_text_height(escala);
|
||||
|
||||
// Calculate position as if FULL text was there (for fixed position typewriter)
|
||||
float x = play_area.x + (play_area.w - full_text_width) / 2.0f;
|
||||
float y = play_area.y + (play_area.h * 0.25f) - (text_height / 2.0f);
|
||||
|
||||
// Render only the partial message (typewriter effect)
|
||||
Punt pos = {x, y};
|
||||
text_.render(partial_message, pos, escala, spacing);
|
||||
}
|
||||
|
||||
@@ -5,46 +5,75 @@
|
||||
#ifndef ESCENA_JOC_HPP
|
||||
#define ESCENA_JOC_HPP
|
||||
|
||||
#include "../../core/graphics/vector_text.hpp"
|
||||
#include "../../core/rendering/sdl_manager.hpp"
|
||||
#include "../../core/types.hpp"
|
||||
#include "../constants.hpp"
|
||||
#include "../entities/bala.hpp"
|
||||
#include "../entities/enemic.hpp"
|
||||
#include "../entities/nau.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../constants.hpp"
|
||||
#include "../effects/debris_manager.hpp"
|
||||
#include "../effects/gestor_puntuacio_flotant.hpp"
|
||||
#include "../entities/bala.hpp"
|
||||
#include "../entities/enemic.hpp"
|
||||
#include "../entities/nau.hpp"
|
||||
#include "../stage_system/stage_manager.hpp"
|
||||
#include "core/graphics/vector_text.hpp"
|
||||
#include "core/rendering/sdl_manager.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
// Classe principal del joc (escena)
|
||||
class EscenaJoc {
|
||||
public:
|
||||
explicit EscenaJoc(SDLManager& sdl);
|
||||
~EscenaJoc() = default;
|
||||
public:
|
||||
explicit EscenaJoc(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||
~EscenaJoc() = default;
|
||||
|
||||
void executar(); // Bucle principal de l'escena
|
||||
void inicialitzar();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar();
|
||||
void processar_input(const SDL_Event &event);
|
||||
void executar(); // Bucle principal de l'escena
|
||||
void inicialitzar();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar();
|
||||
void processar_input(const SDL_Event& event);
|
||||
|
||||
private:
|
||||
SDLManager& sdl_;
|
||||
private:
|
||||
SDLManager& sdl_;
|
||||
GestorEscenes::ContextEscenes& context_;
|
||||
|
||||
// Estat del joc
|
||||
Nau nau_;
|
||||
std::array<Enemic, Constants::MAX_ORNIS> orni_;
|
||||
std::array<Bala, Constants::MAX_BALES> bales_;
|
||||
Poligon chatarra_cosmica_;
|
||||
uint16_t itocado_;
|
||||
// Efectes visuals
|
||||
Effects::DebrisManager debris_manager_;
|
||||
Effects::GestorPuntuacioFlotant gestor_puntuacio_;
|
||||
|
||||
// Text vectorial
|
||||
Graphics::VectorText text_;
|
||||
// Estat del joc
|
||||
Nau nau_;
|
||||
std::array<Enemic, Constants::MAX_ORNIS> orni_;
|
||||
std::array<Bala, Constants::MAX_BALES> bales_;
|
||||
Poligon chatarra_cosmica_;
|
||||
float itocado_; // Death timer (seconds)
|
||||
|
||||
// Funcions privades
|
||||
void tocado();
|
||||
void dibuixar_marges() const; // Dibuixar vores de la zona de joc
|
||||
void dibuixar_marcador(); // Dibuixar marcador de puntuació
|
||||
// Lives and game over system
|
||||
int num_vides_; // Current lives count
|
||||
bool game_over_; // Game over state flag
|
||||
float game_over_timer_; // Countdown timer for auto-return (seconds)
|
||||
Punt punt_spawn_; // Configurable spawn point
|
||||
int puntuacio_total_; // Current score
|
||||
|
||||
// Text vectorial
|
||||
Graphics::VectorText text_;
|
||||
|
||||
// [NEW] Stage system
|
||||
std::unique_ptr<StageSystem::ConfigSistemaStages> stage_config_;
|
||||
std::unique_ptr<StageSystem::StageManager> stage_manager_;
|
||||
|
||||
// Funcions privades
|
||||
void tocado();
|
||||
void detectar_col·lisions_bales_enemics(); // Col·lisions bala-enemic
|
||||
void detectar_col·lisio_nau_enemics(); // Ship-enemy collision detection
|
||||
void dibuixar_marges() const; // Dibuixar vores de la zona de joc
|
||||
void dibuixar_marcador(); // Dibuixar marcador de puntuació
|
||||
|
||||
// [NEW] Stage system helpers
|
||||
void dibuixar_missatge_stage(const std::string& missatge);
|
||||
};
|
||||
|
||||
#endif // ESCENA_JOC_HPP
|
||||
#endif // ESCENA_JOC_HPP
|
||||
|
||||
@@ -2,262 +2,418 @@
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#include "escena_logo.hpp"
|
||||
#include "../../core/system/gestor_escenes.hpp"
|
||||
#include "../../core/system/global_events.hpp"
|
||||
#include "../../core/graphics/shape_loader.hpp"
|
||||
#include "../../core/rendering/shape_renderer.hpp"
|
||||
#include <iostream>
|
||||
#include <cfloat>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cfloat>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <set>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/system/global_events.hpp"
|
||||
|
||||
// Using declarations per simplificar el codi
|
||||
using GestorEscenes::ContextEscenes;
|
||||
using Escena = ContextEscenes::Escena;
|
||||
using Opcio = ContextEscenes::Opcio;
|
||||
|
||||
// Helper: calcular el progrés individual d'una lletra
|
||||
// en funció del progrés global (efecte seqüencial)
|
||||
static float calcular_progress_letra(size_t letra_index,
|
||||
size_t num_letras,
|
||||
float global_progress,
|
||||
float threshold) {
|
||||
if (num_letras == 0)
|
||||
return 1.0f;
|
||||
static float calcular_progress_letra(size_t letra_index, size_t num_letras, float global_progress, float threshold) {
|
||||
if (num_letras == 0)
|
||||
return 1.0f;
|
||||
|
||||
// Calcular temps per lletra
|
||||
float duration_per_letra = 1.0f / static_cast<float>(num_letras);
|
||||
float step = threshold * duration_per_letra;
|
||||
float start = static_cast<float>(letra_index) * step;
|
||||
float end = start + duration_per_letra;
|
||||
// Calcular temps per lletra
|
||||
float duration_per_letra = 1.0f / static_cast<float>(num_letras);
|
||||
float step = threshold * duration_per_letra;
|
||||
float start = static_cast<float>(letra_index) * step;
|
||||
float end = start + duration_per_letra;
|
||||
|
||||
// Interpolar progrés
|
||||
if (global_progress < start) {
|
||||
return 0.0f; // Encara no ha començat
|
||||
} else if (global_progress >= end) {
|
||||
return 1.0f; // Completament apareguda
|
||||
} else {
|
||||
return (global_progress - start) / (end - start);
|
||||
}
|
||||
// Interpolar progrés
|
||||
if (global_progress < start) {
|
||||
return 0.0f; // Encara no ha començat
|
||||
} else if (global_progress >= end) {
|
||||
return 1.0f; // Completament apareguda
|
||||
} else {
|
||||
return (global_progress - start) / (end - start);
|
||||
}
|
||||
}
|
||||
|
||||
EscenaLogo::EscenaLogo(SDLManager& sdl)
|
||||
: sdl_(sdl),
|
||||
estat_actual_(EstatAnimacio::PRE_ANIMATION),
|
||||
temps_estat_actual_(0.0f) {
|
||||
std::cout << "Escena Logo: Inicialitzant...\n";
|
||||
inicialitzar_lletres();
|
||||
EscenaLogo::EscenaLogo(SDLManager& sdl, ContextEscenes& context)
|
||||
: sdl_(sdl),
|
||||
context_(context),
|
||||
estat_actual_(EstatAnimacio::PRE_ANIMATION),
|
||||
temps_estat_actual_(0.0f),
|
||||
debris_manager_(std::make_unique<Effects::DebrisManager>(sdl.obte_renderer())),
|
||||
lletra_explosio_index_(0),
|
||||
temps_des_ultima_explosio_(0.0f) {
|
||||
std::cout << "Escena Logo: Inicialitzant...\n";
|
||||
|
||||
// Consumir opcions (LOGO no processa opcions actualment)
|
||||
auto opcio = context_.consumir_opcio();
|
||||
(void)opcio; // Suprimir warning
|
||||
|
||||
so_reproduit_.fill(false); // Inicialitzar seguiment de sons
|
||||
inicialitzar_lletres();
|
||||
}
|
||||
|
||||
void EscenaLogo::executar() {
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
|
||||
while (GestorEscenes::actual == GestorEscenes::Escena::LOGO) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
while (GestorEscenes::actual == Escena::LOGO) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
}
|
||||
|
||||
// Actualitzar comptador de FPS
|
||||
sdl_.updateFPS(delta_time);
|
||||
|
||||
// Actualitzar visibilitat del cursor (auto-ocultar)
|
||||
Mouse::updateCursorVisibility();
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processar events de l'escena (qualsevol tecla/clic salta al joc)
|
||||
processar_events(event);
|
||||
}
|
||||
|
||||
// Actualitzar lògica
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar colors oscil·lats (efecte verd global)
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Actualitzar context de renderitzat (factor d'escala global)
|
||||
sdl_.updateRenderingContext();
|
||||
|
||||
// Dibuixar
|
||||
dibuixar();
|
||||
}
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processar events de l'escena (qualsevol tecla/clic salta al joc)
|
||||
processar_events(event);
|
||||
}
|
||||
|
||||
// Actualitzar lògica
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar colors oscil·lats (efecte verd global)
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Dibuixar
|
||||
dibuixar();
|
||||
}
|
||||
|
||||
std::cout << "Escena Logo: Finalitzant...\n";
|
||||
std::cout << "Escena Logo: Finalitzant...\n";
|
||||
}
|
||||
|
||||
void EscenaLogo::inicialitzar_lletres() {
|
||||
using namespace Graphics;
|
||||
using namespace Graphics;
|
||||
|
||||
// Llista de fitxers .shp (A repetida per a les dues A's)
|
||||
std::vector<std::string> fitxers = {
|
||||
"logo/letra_j.shp", "logo/letra_a.shp", "logo/letra_i.shp", "logo/letra_l.shp",
|
||||
"logo/letra_g.shp", "logo/letra_a.shp", "logo/letra_m.shp", "logo/letra_e.shp", "logo/letra_s.shp"
|
||||
};
|
||||
// Llista de fitxers .shp (A repetida per a les dues A's)
|
||||
std::vector<std::string> fitxers = {
|
||||
"logo/letra_j.shp",
|
||||
"logo/letra_a.shp",
|
||||
"logo/letra_i.shp",
|
||||
"logo/letra_l.shp",
|
||||
"logo/letra_g.shp",
|
||||
"logo/letra_a.shp",
|
||||
"logo/letra_m.shp",
|
||||
"logo/letra_e.shp",
|
||||
"logo/letra_s.shp"};
|
||||
|
||||
// Pas 1: Carregar totes les formes i calcular amplades
|
||||
float ancho_total = 0.0f;
|
||||
// Pas 1: Carregar totes les formes i calcular amplades
|
||||
float ancho_total = 0.0f;
|
||||
|
||||
for (const auto& fitxer : fitxers) {
|
||||
auto forma = ShapeLoader::load(fitxer);
|
||||
if (!forma || !forma->es_valida()) {
|
||||
std::cerr << "[EscenaLogo] Error carregant " << fitxer << std::endl;
|
||||
continue;
|
||||
for (const auto& fitxer : fitxers) {
|
||||
auto forma = ShapeLoader::load(fitxer);
|
||||
if (!forma || !forma->es_valida()) {
|
||||
std::cerr << "[EscenaLogo] Error carregant " << fitxer << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calcular bounding box de la forma (trobar ancho)
|
||||
float min_x = FLT_MAX;
|
||||
float max_x = -FLT_MAX;
|
||||
|
||||
for (const auto& prim : forma->get_primitives()) {
|
||||
for (const auto& punt : prim.points) {
|
||||
min_x = std::min(min_x, punt.x);
|
||||
max_x = std::max(max_x, punt.x);
|
||||
}
|
||||
}
|
||||
|
||||
float ancho_sin_escalar = max_x - min_x;
|
||||
|
||||
// IMPORTANT: Escalar ancho i offset amb ESCALA_FINAL
|
||||
// per que les posicions finals coincideixin amb la mida real de les lletres
|
||||
float ancho = ancho_sin_escalar * ESCALA_FINAL;
|
||||
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_FINAL;
|
||||
|
||||
lletres_.push_back({forma,
|
||||
{0.0f, 0.0f}, // Posició es calcularà després
|
||||
ancho,
|
||||
offset_centre});
|
||||
|
||||
ancho_total += ancho;
|
||||
}
|
||||
|
||||
// Calcular bounding box de la forma (trobar ancho)
|
||||
float min_x = FLT_MAX;
|
||||
float max_x = -FLT_MAX;
|
||||
// Pas 2: Afegir espaiat entre lletres
|
||||
ancho_total += ESPAI_ENTRE_LLETRES * (lletres_.size() - 1);
|
||||
|
||||
for (const auto& prim : forma->get_primitives()) {
|
||||
for (const auto& punt : prim.points) {
|
||||
min_x = std::min(min_x, punt.x);
|
||||
max_x = std::max(max_x, punt.x);
|
||||
}
|
||||
// Pas 3: Calcular posició inicial (centrat horitzontal)
|
||||
constexpr float PANTALLA_ANCHO = 640.0f;
|
||||
constexpr float PANTALLA_ALTO = 480.0f;
|
||||
|
||||
float x_inicial = (PANTALLA_ANCHO - ancho_total) / 2.0f;
|
||||
float y_centre = PANTALLA_ALTO / 2.0f;
|
||||
|
||||
// Pas 4: Assignar posicions a cada lletra
|
||||
float x_actual = x_inicial;
|
||||
|
||||
for (auto& lletra : lletres_) {
|
||||
// Posicionar el centre de la forma (shape_centre) en pantalla
|
||||
// Usar offset_centre en lloc de ancho/2 perquè shape_centre
|
||||
// pot no estar exactament al mig del bounding box
|
||||
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||
lletra.posicio.y = y_centre;
|
||||
|
||||
// Avançar per a següent lletra
|
||||
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||
}
|
||||
|
||||
float ancho_sin_escalar = max_x - min_x;
|
||||
|
||||
// IMPORTANT: Escalar ancho i offset amb ESCALA_FINAL
|
||||
// per que les posicions finals coincideixin amb la mida real de les lletres
|
||||
float ancho = ancho_sin_escalar * ESCALA_FINAL;
|
||||
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_FINAL;
|
||||
|
||||
lletres_.push_back({
|
||||
forma,
|
||||
{0.0f, 0.0f}, // Posició es calcularà després
|
||||
ancho,
|
||||
offset_centre
|
||||
});
|
||||
|
||||
ancho_total += ancho;
|
||||
}
|
||||
|
||||
// Pas 2: Afegir espaiat entre lletres
|
||||
ancho_total += ESPAI_ENTRE_LLETRES * (lletres_.size() - 1);
|
||||
|
||||
// Pas 3: Calcular posició inicial (centrat horitzontal)
|
||||
constexpr float PANTALLA_ANCHO = 640.0f;
|
||||
constexpr float PANTALLA_ALTO = 480.0f;
|
||||
|
||||
float x_inicial = (PANTALLA_ANCHO - ancho_total) / 2.0f;
|
||||
float y_centre = PANTALLA_ALTO / 2.0f;
|
||||
|
||||
// Pas 4: Assignar posicions a cada lletra
|
||||
float x_actual = x_inicial;
|
||||
|
||||
for (auto& lletra : lletres_) {
|
||||
// Posicionar el centre de la forma (shape_centre) en pantalla
|
||||
// Usar offset_centre en lloc de ancho/2 perquè shape_centre
|
||||
// pot no estar exactament al mig del bounding box
|
||||
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||
lletra.posicio.y = y_centre;
|
||||
|
||||
// Avançar per a següent lletra
|
||||
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||
}
|
||||
|
||||
std::cout << "[EscenaLogo] " << lletres_.size()
|
||||
<< " lletres carregades, ancho total: " << ancho_total << " px\n";
|
||||
std::cout << "[EscenaLogo] " << lletres_.size()
|
||||
<< " lletres carregades, ancho total: " << ancho_total << " px\n";
|
||||
}
|
||||
|
||||
void EscenaLogo::canviar_estat(EstatAnimacio nou_estat) {
|
||||
estat_actual_ = nou_estat;
|
||||
temps_estat_actual_ = 0.0f; // Reset temps
|
||||
std::cout << "[EscenaLogo] Canvi a estat: " << static_cast<int>(nou_estat) << "\n";
|
||||
estat_actual_ = nou_estat;
|
||||
temps_estat_actual_ = 0.0f; // Reset temps
|
||||
|
||||
// Inicialitzar estat d'explosió
|
||||
if (nou_estat == EstatAnimacio::EXPLOSION) {
|
||||
lletra_explosio_index_ = 0;
|
||||
temps_des_ultima_explosio_ = 0.0f;
|
||||
|
||||
// Generar ordre aleatori d'explosions
|
||||
ordre_explosio_.clear();
|
||||
for (size_t i = 0; i < lletres_.size(); i++) {
|
||||
ordre_explosio_.push_back(i);
|
||||
}
|
||||
std::random_device rd;
|
||||
std::mt19937 g(rd());
|
||||
std::shuffle(ordre_explosio_.begin(), ordre_explosio_.end(), g);
|
||||
}
|
||||
else if (nou_estat == EstatAnimacio::POST_EXPLOSION)
|
||||
{
|
||||
Audio::get()->playMusic("title.ogg");
|
||||
}
|
||||
|
||||
std::cout << "[EscenaLogo] Canvi a estat: " << static_cast<int>(nou_estat)
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
bool EscenaLogo::totes_lletres_completes() const {
|
||||
// Quan global_progress = 1.0, totes les lletres tenen letra_progress = 1.0
|
||||
return temps_estat_actual_ >= DURACIO_ZOOM;
|
||||
// Quan global_progress = 1.0, totes les lletres tenen letra_progress = 1.0
|
||||
return temps_estat_actual_ >= DURACIO_ZOOM;
|
||||
}
|
||||
|
||||
void EscenaLogo::actualitzar_explosions(float delta_time) {
|
||||
temps_des_ultima_explosio_ += delta_time;
|
||||
|
||||
// Comprovar si és el moment d'explotar la següent lletra
|
||||
if (temps_des_ultima_explosio_ >= DELAY_ENTRE_EXPLOSIONS) {
|
||||
if (lletra_explosio_index_ < lletres_.size()) {
|
||||
// Explotar lletra actual (en ordre aleatori)
|
||||
size_t index_actual = ordre_explosio_[lletra_explosio_index_];
|
||||
const auto& lletra = lletres_[index_actual];
|
||||
|
||||
debris_manager_->explotar(
|
||||
lletra.forma, // Forma a explotar
|
||||
lletra.posicio, // Posició
|
||||
0.0f, // Angle (sense rotació)
|
||||
ESCALA_FINAL, // Escala (lletres a escala final)
|
||||
VELOCITAT_EXPLOSIO, // Velocitat base
|
||||
1.0f, // Brightness màxim (per defecte)
|
||||
{0.0f, 0.0f} // Sense velocitat (per defecte)
|
||||
);
|
||||
|
||||
std::cout << "[EscenaLogo] Explota lletra " << lletra_explosio_index_ << "\n";
|
||||
|
||||
// Passar a la següent lletra
|
||||
lletra_explosio_index_++;
|
||||
temps_des_ultima_explosio_ = 0.0f;
|
||||
} else {
|
||||
// Totes les lletres han explotat, transició a POST_EXPLOSION
|
||||
canviar_estat(EstatAnimacio::POST_EXPLOSION);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaLogo::actualitzar(float delta_time) {
|
||||
temps_estat_actual_ += delta_time;
|
||||
temps_estat_actual_ += delta_time;
|
||||
|
||||
switch (estat_actual_) {
|
||||
case EstatAnimacio::PRE_ANIMATION:
|
||||
if (temps_estat_actual_ >= DURACIO_PRE) {
|
||||
canviar_estat(EstatAnimacio::ANIMATION);
|
||||
}
|
||||
break;
|
||||
switch (estat_actual_) {
|
||||
case EstatAnimacio::PRE_ANIMATION:
|
||||
if (temps_estat_actual_ >= DURACIO_PRE) {
|
||||
canviar_estat(EstatAnimacio::ANIMATION);
|
||||
}
|
||||
break;
|
||||
|
||||
case EstatAnimacio::ANIMATION:
|
||||
if (totes_lletres_completes()) {
|
||||
canviar_estat(EstatAnimacio::POST_ANIMATION);
|
||||
}
|
||||
break;
|
||||
case EstatAnimacio::ANIMATION: {
|
||||
// Reproduir so per cada lletra quan comença a aparèixer
|
||||
float global_progress = std::min(temps_estat_actual_ / DURACIO_ZOOM, 1.0f);
|
||||
|
||||
case EstatAnimacio::POST_ANIMATION:
|
||||
if (temps_estat_actual_ >= DURACIO_POST) {
|
||||
GestorEscenes::actual = GestorEscenes::Escena::JOC;
|
||||
}
|
||||
break;
|
||||
}
|
||||
for (size_t i = 0; i < lletres_.size() && i < so_reproduit_.size(); i++) {
|
||||
if (!so_reproduit_[i]) {
|
||||
float letra_progress = calcular_progress_letra(
|
||||
i,
|
||||
lletres_.size(),
|
||||
global_progress,
|
||||
THRESHOLD_LETRA);
|
||||
|
||||
// Reproduir so quan la lletra comença a aparèixer (progress > 0)
|
||||
if (letra_progress > 0.0f) {
|
||||
Audio::get()->playSound("logo.wav", Audio::Group::INTERFACE);
|
||||
so_reproduit_[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totes_lletres_completes()) {
|
||||
canviar_estat(EstatAnimacio::POST_ANIMATION);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case EstatAnimacio::POST_ANIMATION:
|
||||
if (temps_estat_actual_ >= DURACIO_POST_ANIMATION) {
|
||||
canviar_estat(EstatAnimacio::EXPLOSION);
|
||||
}
|
||||
break;
|
||||
|
||||
case EstatAnimacio::EXPLOSION:
|
||||
actualitzar_explosions(delta_time);
|
||||
break;
|
||||
|
||||
case EstatAnimacio::POST_EXPLOSION:
|
||||
if (temps_estat_actual_ >= DURACIO_POST_EXPLOSION) {
|
||||
// Transició a pantalla de títol
|
||||
context_.canviar_escena(Escena::TITOL);
|
||||
GestorEscenes::actual = Escena::TITOL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Actualitzar animacions de debris
|
||||
debris_manager_->actualitzar(delta_time);
|
||||
}
|
||||
|
||||
void EscenaLogo::dibuixar() {
|
||||
// Fons negre
|
||||
sdl_.neteja(0, 0, 0);
|
||||
// Fons negre
|
||||
sdl_.neteja(0, 0, 0);
|
||||
|
||||
// PRE_ANIMATION: Només pantalla negra
|
||||
if (estat_actual_ == EstatAnimacio::PRE_ANIMATION) {
|
||||
sdl_.presenta();
|
||||
return; // No renderitzar lletres
|
||||
}
|
||||
|
||||
// ANIMATION o POST_ANIMATION: Calcular progrés
|
||||
float global_progress = (estat_actual_ == EstatAnimacio::ANIMATION)
|
||||
? std::min(temps_estat_actual_ / DURACIO_ZOOM, 1.0f)
|
||||
: 1.0f; // POST: mantenir al 100%
|
||||
|
||||
// Punt inicial del zoom (configurable amb ORIGEN_ZOOM_X/Y)
|
||||
const Punt ORIGEN_ZOOM = {ORIGEN_ZOOM_X, ORIGEN_ZOOM_Y};
|
||||
|
||||
// Dibuixar cada lletra amb animació seqüencial
|
||||
for (size_t i = 0; i < lletres_.size(); i++) {
|
||||
const auto& lletra = lletres_[i];
|
||||
|
||||
// Calcular progrés individual d'aquesta lletra (0.0 → 1.0)
|
||||
float letra_progress = calcular_progress_letra(i, lletres_.size(), global_progress, THRESHOLD_LETRA);
|
||||
|
||||
// Si la lletra encara no ha començat, saltar-la
|
||||
if (letra_progress <= 0.0f) {
|
||||
continue;
|
||||
// PRE_ANIMATION: Només pantalla negra
|
||||
if (estat_actual_ == EstatAnimacio::PRE_ANIMATION) {
|
||||
sdl_.presenta();
|
||||
return; // No renderitzar lletres
|
||||
}
|
||||
|
||||
// Interpolar posició: des del origen zoom cap a posició final
|
||||
Punt pos_actual;
|
||||
pos_actual.x = ORIGEN_ZOOM.x + (lletra.posicio.x - ORIGEN_ZOOM.x) * letra_progress;
|
||||
pos_actual.y = ORIGEN_ZOOM.y + (lletra.posicio.y - ORIGEN_ZOOM.y) * letra_progress;
|
||||
// ANIMATION o POST_ANIMATION: Dibuixar lletres amb animació
|
||||
if (estat_actual_ == EstatAnimacio::ANIMATION ||
|
||||
estat_actual_ == EstatAnimacio::POST_ANIMATION) {
|
||||
float global_progress =
|
||||
(estat_actual_ == EstatAnimacio::ANIMATION)
|
||||
? std::min(temps_estat_actual_ / DURACIO_ZOOM, 1.0f)
|
||||
: 1.0f; // POST: mantenir al 100%
|
||||
|
||||
// Aplicar ease-out quadràtic per suavitat
|
||||
float t = letra_progress;
|
||||
float ease_factor = 1.0f - (1.0f - t) * (1.0f - t);
|
||||
const Punt ORIGEN_ZOOM = {ORIGEN_ZOOM_X, ORIGEN_ZOOM_Y};
|
||||
|
||||
// Interpolar escala amb ease-out: des de ESCALA_INICIAL cap a ESCALA_FINAL
|
||||
float escala_actual = ESCALA_INICIAL + (ESCALA_FINAL - ESCALA_INICIAL) * ease_factor;
|
||||
for (size_t i = 0; i < lletres_.size(); i++) {
|
||||
const auto& lletra = lletres_[i];
|
||||
|
||||
// Renderitzar la lletra
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletra.forma,
|
||||
pos_actual, // Posició interpolada
|
||||
0.0f, // Sense rotació
|
||||
escala_actual, // Escala interpolada amb ease-out
|
||||
true, // Dibuixar
|
||||
1.0f // Progress = 1.0 (lletra completa, sense animació de primitives)
|
||||
);
|
||||
}
|
||||
float letra_progress = calcular_progress_letra(
|
||||
i,
|
||||
lletres_.size(),
|
||||
global_progress,
|
||||
THRESHOLD_LETRA);
|
||||
|
||||
sdl_.presenta();
|
||||
if (letra_progress <= 0.0f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Punt pos_actual;
|
||||
pos_actual.x =
|
||||
ORIGEN_ZOOM.x + (lletra.posicio.x - ORIGEN_ZOOM.x) * letra_progress;
|
||||
pos_actual.y =
|
||||
ORIGEN_ZOOM.y + (lletra.posicio.y - ORIGEN_ZOOM.y) * letra_progress;
|
||||
|
||||
float t = letra_progress;
|
||||
float ease_factor = 1.0f - (1.0f - t) * (1.0f - t);
|
||||
float escala_actual =
|
||||
ESCALA_INICIAL + (ESCALA_FINAL - ESCALA_INICIAL) * ease_factor;
|
||||
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletra.forma,
|
||||
pos_actual,
|
||||
0.0f,
|
||||
escala_actual,
|
||||
true,
|
||||
1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
// EXPLOSION: Dibuixar només lletres que encara no han explotat
|
||||
if (estat_actual_ == EstatAnimacio::EXPLOSION) {
|
||||
// Crear conjunt de lletres ja explotades
|
||||
std::set<size_t> explotades;
|
||||
for (size_t i = 0; i < lletra_explosio_index_; i++) {
|
||||
explotades.insert(ordre_explosio_[i]);
|
||||
}
|
||||
|
||||
// Dibuixar només lletres que NO han explotat
|
||||
for (size_t i = 0; i < lletres_.size(); i++) {
|
||||
if (explotades.find(i) == explotades.end()) {
|
||||
const auto& lletra = lletres_[i];
|
||||
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletra.forma,
|
||||
lletra.posicio,
|
||||
0.0f,
|
||||
ESCALA_FINAL,
|
||||
true,
|
||||
1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// POST_EXPLOSION: No dibuixar lletres, només debris (a baix)
|
||||
|
||||
// Sempre dibuixar debris (si n'hi ha d'actius)
|
||||
debris_manager_->dibuixar();
|
||||
|
||||
sdl_.presenta();
|
||||
}
|
||||
|
||||
void EscenaLogo::processar_events(const SDL_Event& event) {
|
||||
// Qualsevol tecla o clic de ratolí salta al joc
|
||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
GestorEscenes::actual = GestorEscenes::Escena::JOC;
|
||||
}
|
||||
// Qualsevol tecla o clic de ratolí salta a la pantalla de títol
|
||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
// Utilitzar context per especificar escena i opció
|
||||
context_.canviar_escena(
|
||||
Escena::TITOL,
|
||||
Opcio::JUMP_TO_TITLE_MAIN
|
||||
);
|
||||
// Backward compatibility: També actualitzar GestorEscenes::actual
|
||||
GestorEscenes::actual = Escena::TITOL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,60 +4,84 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/rendering/sdl_manager.hpp"
|
||||
#include "../../core/graphics/shape.hpp"
|
||||
#include "../../core/types.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <vector>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "game/effects/debris_manager.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/rendering/sdl_manager.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class EscenaLogo {
|
||||
public:
|
||||
explicit EscenaLogo(SDLManager& sdl);
|
||||
void executar(); // Bucle principal de l'escena
|
||||
public:
|
||||
explicit EscenaLogo(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||
void executar(); // Bucle principal de l'escena
|
||||
|
||||
private:
|
||||
// Màquina d'estats per l'animació
|
||||
enum class EstatAnimacio {
|
||||
PRE_ANIMATION, // Pantalla negra inicial
|
||||
ANIMATION, // Animació de zoom de lletres
|
||||
POST_ANIMATION // Logo complet visible
|
||||
};
|
||||
private:
|
||||
// Màquina d'estats per l'animació
|
||||
enum class EstatAnimacio {
|
||||
PRE_ANIMATION, // Pantalla negra inicial
|
||||
ANIMATION, // Animació de zoom de lletres
|
||||
POST_ANIMATION, // Logo complet visible
|
||||
EXPLOSION, // Explosió seqüencial de lletres
|
||||
POST_EXPLOSION // Espera després de l'última explosió
|
||||
};
|
||||
|
||||
SDLManager& sdl_;
|
||||
EstatAnimacio estat_actual_; // Estat actual de la màquina
|
||||
float temps_estat_actual_; // Temps en l'estat actual (reset en cada transició)
|
||||
SDLManager& sdl_;
|
||||
GestorEscenes::ContextEscenes& context_;
|
||||
EstatAnimacio estat_actual_; // Estat actual de la màquina
|
||||
float
|
||||
temps_estat_actual_; // Temps en l'estat actual (reset en cada transició)
|
||||
|
||||
// Estructura per a cada lletra del logo
|
||||
struct LetraLogo {
|
||||
std::shared_ptr<Graphics::Shape> forma;
|
||||
Punt posicio; // Posició final en pantalla
|
||||
float ancho; // Ancho del bounding box
|
||||
float offset_centre; // Distància de min_x a shape_centre.x
|
||||
};
|
||||
// Gestor de fragments d'explosions
|
||||
std::unique_ptr<Effects::DebrisManager> debris_manager_;
|
||||
|
||||
std::vector<LetraLogo> lletres_; // 9 lletres: J-A-I-L-G-A-M-E-S
|
||||
// Seguiment d'explosions seqüencials
|
||||
size_t lletra_explosio_index_; // Índex de la següent lletra a explotar
|
||||
float temps_des_ultima_explosio_; // Temps des de l'última explosió
|
||||
std::vector<size_t> ordre_explosio_; // Ordre aleatori d'índexs de lletres
|
||||
|
||||
// Constants d'animació
|
||||
static constexpr float DURACIO_PRE = 1.5f; // Duració PRE_ANIMATION (pantalla negra)
|
||||
static constexpr float DURACIO_ZOOM = 4.0f; // Duració del zoom (segons)
|
||||
static constexpr float DURACIO_POST = 4.0f; // Duració POST_ANIMATION (logo complet)
|
||||
static constexpr float ESCALA_INICIAL = 0.1f; // Escala inicial (10%)
|
||||
static constexpr float ESCALA_FINAL = 0.8f; // Escala final (80%)
|
||||
static constexpr float ESPAI_ENTRE_LLETRES = 10.0f; // Espaiat entre lletres
|
||||
// Estructura per a cada lletra del logo
|
||||
struct LetraLogo {
|
||||
std::shared_ptr<Graphics::Shape> forma;
|
||||
Punt posicio; // Posició final en pantalla
|
||||
float ancho; // Ancho del bounding box
|
||||
float offset_centre; // Distància de min_x a shape_centre.x
|
||||
};
|
||||
|
||||
// Constants d'animació seqüencial
|
||||
static constexpr float THRESHOLD_LETRA = 0.6f; // Umbral per activar següent lletra (0.0-1.0)
|
||||
static constexpr float ORIGEN_ZOOM_X = 640.0f / 2.0f; // Punt inicial X del zoom (320)
|
||||
static constexpr float ORIGEN_ZOOM_Y = 480.0f * 0.4f; // Punt inicial Y del zoom (240)
|
||||
std::vector<LetraLogo> lletres_; // 9 lletres: J-A-I-L-G-A-M-E-S
|
||||
|
||||
// Mètodes privats
|
||||
void inicialitzar_lletres();
|
||||
void actualitzar(float delta_time);
|
||||
void dibuixar();
|
||||
void processar_events(const SDL_Event& event);
|
||||
// Seguiment de sons de lletres (evitar reproduccions repetides)
|
||||
std::array<bool, 9> so_reproduit_; // Track si cada lletra ja ha reproduit el so
|
||||
|
||||
// Mètodes de gestió d'estats
|
||||
void canviar_estat(EstatAnimacio nou_estat);
|
||||
bool totes_lletres_completes() const;
|
||||
// Constants d'animació
|
||||
static constexpr float DURACIO_PRE = 1.5f; // Duració PRE_ANIMATION (pantalla negra)
|
||||
static constexpr float DURACIO_ZOOM = 4.0f; // Duració del zoom (segons)
|
||||
static constexpr float DURACIO_POST_ANIMATION = 3.0f; // Duració POST_ANIMATION (logo complet)
|
||||
static constexpr float DURACIO_POST_EXPLOSION = 3.0f; // Duració POST_EXPLOSION (espera final)
|
||||
static constexpr float DELAY_ENTRE_EXPLOSIONS = 0.1f; // Temps entre explosions de lletres
|
||||
static constexpr float VELOCITAT_EXPLOSIO = 240.0f; // Velocitat base fragments (px/s)
|
||||
static constexpr float ESCALA_INICIAL = 0.1f; // Escala inicial (10%)
|
||||
static constexpr float ESCALA_FINAL = 0.8f; // Escala final (80%)
|
||||
static constexpr float ESPAI_ENTRE_LLETRES = 10.0f; // Espaiat entre lletres
|
||||
|
||||
// Constants d'animació seqüencial
|
||||
static constexpr float THRESHOLD_LETRA = 0.6f; // Umbral per activar següent lletra (0.0-1.0)
|
||||
static constexpr float ORIGEN_ZOOM_X = Defaults::Game::WIDTH * 0.5f; // Punt inicial X del zoom
|
||||
static constexpr float ORIGEN_ZOOM_Y = Defaults::Game::HEIGHT * 0.4f; // Punt inicial Y del zoom
|
||||
|
||||
// Mètodes privats
|
||||
void inicialitzar_lletres();
|
||||
void actualitzar(float delta_time);
|
||||
void actualitzar_explosions(float delta_time);
|
||||
void dibuixar();
|
||||
void processar_events(const SDL_Event& event);
|
||||
|
||||
// Mètodes de gestió d'estats
|
||||
void canviar_estat(EstatAnimacio nou_estat);
|
||||
bool totes_lletres_completes() const;
|
||||
};
|
||||
|
||||
579
source/game/escenes/escena_titol.cpp
Normal file
579
source/game/escenes/escena_titol.cpp
Normal file
@@ -0,0 +1,579 @@
|
||||
// escena_titol.cpp - Implementació de l'escena de títol
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#include "escena_titol.hpp"
|
||||
|
||||
#include <cfloat>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/rendering/shape_renderer.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/system/global_events.hpp"
|
||||
#include "project.h"
|
||||
|
||||
// Using declarations per simplificar el codi
|
||||
using GestorEscenes::ContextEscenes;
|
||||
using Escena = ContextEscenes::Escena;
|
||||
using Opcio = ContextEscenes::Opcio;
|
||||
|
||||
EscenaTitol::EscenaTitol(SDLManager& sdl, ContextEscenes& context)
|
||||
: sdl_(sdl),
|
||||
context_(context),
|
||||
text_(sdl.obte_renderer()),
|
||||
estat_actual_(EstatTitol::STARFIELD_FADE_IN),
|
||||
temps_acumulat_(0.0f),
|
||||
temps_animacio_(0.0f),
|
||||
temps_estat_main_(0.0f),
|
||||
animacio_activa_(false),
|
||||
factor_lerp_(0.0f) {
|
||||
std::cout << "Escena Titol: Inicialitzant...\n";
|
||||
|
||||
// Processar opció del context
|
||||
auto opcio = context_.consumir_opcio();
|
||||
|
||||
if (opcio == Opcio::JUMP_TO_TITLE_MAIN) {
|
||||
std::cout << "Escena Titol: Opció JUMP_TO_TITLE_MAIN activada\n";
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
temps_estat_main_ = 0.0f;
|
||||
}
|
||||
|
||||
// Crear starfield de fons
|
||||
Punt centre_pantalla{
|
||||
Defaults::Game::WIDTH / 2.0f,
|
||||
Defaults::Game::HEIGHT / 2.0f};
|
||||
|
||||
SDL_FRect area_completa{
|
||||
0,
|
||||
0,
|
||||
static_cast<float>(Defaults::Game::WIDTH),
|
||||
static_cast<float>(Defaults::Game::HEIGHT)};
|
||||
|
||||
starfield_ = std::make_unique<Graphics::Starfield>(
|
||||
sdl_.obte_renderer(),
|
||||
centre_pantalla,
|
||||
area_completa,
|
||||
150 // densitat: 150 estrelles (50 per capa)
|
||||
);
|
||||
|
||||
// Brightness depèn de l'opció
|
||||
if (estat_actual_ == EstatTitol::MAIN) {
|
||||
// Si saltem a MAIN, starfield instantàniament brillant
|
||||
starfield_->set_brightness(BRIGHTNESS_STARFIELD);
|
||||
} else {
|
||||
// Flux normal: comença amb brightness 0.0 per fade-in
|
||||
starfield_->set_brightness(0.0f);
|
||||
}
|
||||
|
||||
// Inicialitzar lletres del títol "ORNI ATTACK!"
|
||||
inicialitzar_titol();
|
||||
|
||||
// Iniciar música de títol si no està sonant
|
||||
if (Audio::get()->getMusicState() != Audio::MusicState::PLAYING) {
|
||||
Audio::get()->playMusic("title.ogg");
|
||||
}
|
||||
}
|
||||
|
||||
EscenaTitol::~EscenaTitol() {
|
||||
// Aturar música de títol quan es destrueix l'escena
|
||||
Audio::get()->stopMusic();
|
||||
}
|
||||
|
||||
void EscenaTitol::inicialitzar_titol() {
|
||||
using namespace Graphics;
|
||||
|
||||
// === LÍNIA 1: "ORNI" ===
|
||||
std::vector<std::string> fitxers_orni = {
|
||||
"title/letra_o.shp",
|
||||
"title/letra_r.shp",
|
||||
"title/letra_n.shp",
|
||||
"title/letra_i.shp"};
|
||||
|
||||
// Pas 1: Carregar formes i calcular amplades per "ORNI"
|
||||
float ancho_total_orni = 0.0f;
|
||||
|
||||
for (const auto& fitxer : fitxers_orni) {
|
||||
auto forma = ShapeLoader::load(fitxer);
|
||||
if (!forma || !forma->es_valida()) {
|
||||
std::cerr << "[EscenaTitol] Error carregant " << fitxer << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calcular bounding box de la forma (trobar ancho i altura)
|
||||
float min_x = FLT_MAX;
|
||||
float max_x = -FLT_MAX;
|
||||
float min_y = FLT_MAX;
|
||||
float max_y = -FLT_MAX;
|
||||
|
||||
for (const auto& prim : forma->get_primitives()) {
|
||||
for (const auto& punt : prim.points) {
|
||||
min_x = std::min(min_x, punt.x);
|
||||
max_x = std::max(max_x, punt.x);
|
||||
min_y = std::min(min_y, punt.y);
|
||||
max_y = std::max(max_y, punt.y);
|
||||
}
|
||||
}
|
||||
|
||||
float ancho_sin_escalar = max_x - min_x;
|
||||
float altura_sin_escalar = max_y - min_y;
|
||||
|
||||
// Escalar ancho, altura i offset amb ESCALA_TITULO
|
||||
float ancho = ancho_sin_escalar * ESCALA_TITULO;
|
||||
float altura = altura_sin_escalar * ESCALA_TITULO;
|
||||
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_TITULO;
|
||||
|
||||
lletres_orni_.push_back({forma, {0.0f, 0.0f}, ancho, altura, offset_centre});
|
||||
|
||||
ancho_total_orni += ancho;
|
||||
}
|
||||
|
||||
// Afegir espaiat entre lletres
|
||||
ancho_total_orni += ESPAI_ENTRE_LLETRES * (lletres_orni_.size() - 1);
|
||||
|
||||
// Calcular posició inicial (centrat horitzontal) per "ORNI"
|
||||
float x_inicial_orni = (Defaults::Game::WIDTH - ancho_total_orni) / 2.0f;
|
||||
float x_actual = x_inicial_orni;
|
||||
|
||||
for (auto& lletra : lletres_orni_) {
|
||||
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||
lletra.posicio.y = Y_ORNI;
|
||||
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||
}
|
||||
|
||||
std::cout << "[EscenaTitol] Línia 1 (ORNI): " << lletres_orni_.size()
|
||||
<< " lletres, ancho total: " << ancho_total_orni << " px\n";
|
||||
|
||||
// === Calcular posició Y dinàmica per "ATTACK!" ===
|
||||
// Totes les lletres ORNI tenen la mateixa altura, utilitzem la primera
|
||||
float altura_orni = lletres_orni_.empty() ? 50.0f : lletres_orni_[0].altura;
|
||||
y_attack_dinamica_ = Y_ORNI + altura_orni + SEPARACION_LINEAS;
|
||||
|
||||
std::cout << "[EscenaTitol] Altura ORNI: " << altura_orni
|
||||
<< " px, Y_ATTACK dinàmica: " << y_attack_dinamica_ << " px\n";
|
||||
|
||||
// === LÍNIA 2: "ATTACK!" ===
|
||||
std::vector<std::string> fitxers_attack = {
|
||||
"title/letra_a.shp",
|
||||
"title/letra_t.shp",
|
||||
"title/letra_t.shp", // T repetida
|
||||
"title/letra_a.shp", // A repetida
|
||||
"title/letra_c.shp",
|
||||
"title/letra_k.shp",
|
||||
"title/letra_exclamacion.shp"};
|
||||
|
||||
// Pas 1: Carregar formes i calcular amplades per "ATTACK!"
|
||||
float ancho_total_attack = 0.0f;
|
||||
|
||||
for (const auto& fitxer : fitxers_attack) {
|
||||
auto forma = ShapeLoader::load(fitxer);
|
||||
if (!forma || !forma->es_valida()) {
|
||||
std::cerr << "[EscenaTitol] Error carregant " << fitxer << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calcular bounding box de la forma (trobar ancho i altura)
|
||||
float min_x = FLT_MAX;
|
||||
float max_x = -FLT_MAX;
|
||||
float min_y = FLT_MAX;
|
||||
float max_y = -FLT_MAX;
|
||||
|
||||
for (const auto& prim : forma->get_primitives()) {
|
||||
for (const auto& punt : prim.points) {
|
||||
min_x = std::min(min_x, punt.x);
|
||||
max_x = std::max(max_x, punt.x);
|
||||
min_y = std::min(min_y, punt.y);
|
||||
max_y = std::max(max_y, punt.y);
|
||||
}
|
||||
}
|
||||
|
||||
float ancho_sin_escalar = max_x - min_x;
|
||||
float altura_sin_escalar = max_y - min_y;
|
||||
|
||||
// Escalar ancho, altura i offset amb ESCALA_TITULO
|
||||
float ancho = ancho_sin_escalar * ESCALA_TITULO;
|
||||
float altura = altura_sin_escalar * ESCALA_TITULO;
|
||||
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_TITULO;
|
||||
|
||||
lletres_attack_.push_back({forma, {0.0f, 0.0f}, ancho, altura, offset_centre});
|
||||
|
||||
ancho_total_attack += ancho;
|
||||
}
|
||||
|
||||
// Afegir espaiat entre lletres
|
||||
ancho_total_attack += ESPAI_ENTRE_LLETRES * (lletres_attack_.size() - 1);
|
||||
|
||||
// Calcular posició inicial (centrat horitzontal) per "ATTACK!"
|
||||
float x_inicial_attack = (Defaults::Game::WIDTH - ancho_total_attack) / 2.0f;
|
||||
x_actual = x_inicial_attack;
|
||||
|
||||
for (auto& lletra : lletres_attack_) {
|
||||
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||
lletra.posicio.y = y_attack_dinamica_; // Usar posició dinàmica
|
||||
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||
}
|
||||
|
||||
std::cout << "[EscenaTitol] Línia 2 (ATTACK!): " << lletres_attack_.size()
|
||||
<< " lletres, ancho total: " << ancho_total_attack << " px\n";
|
||||
|
||||
// Guardar posicions originals per l'animació orbital
|
||||
posicions_originals_orni_.clear();
|
||||
for (const auto& lletra : lletres_orni_) {
|
||||
posicions_originals_orni_.push_back(lletra.posicio);
|
||||
}
|
||||
|
||||
posicions_originals_attack_.clear();
|
||||
for (const auto& lletra : lletres_attack_) {
|
||||
posicions_originals_attack_.push_back(lletra.posicio);
|
||||
}
|
||||
|
||||
std::cout << "[EscenaTitol] Animació: Posicions originals guardades\n";
|
||||
}
|
||||
|
||||
void EscenaTitol::executar() {
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
|
||||
while (GestorEscenes::actual == Escena::TITOL) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
}
|
||||
|
||||
// Actualitzar comptador de FPS
|
||||
sdl_.updateFPS(delta_time);
|
||||
|
||||
// Actualitzar visibilitat del cursor (auto-ocultar)
|
||||
Mouse::updateCursorVisibility();
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/F4/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processar events de l'escena
|
||||
processar_events(event);
|
||||
}
|
||||
|
||||
// Actualitzar lògica
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar sistema d'audio
|
||||
Audio::update();
|
||||
|
||||
// Actualitzar colors oscil·lats
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Netejar pantalla
|
||||
sdl_.neteja(0, 0, 0);
|
||||
|
||||
// Actualitzar context de renderitzat (factor d'escala global)
|
||||
sdl_.updateRenderingContext();
|
||||
|
||||
// Dibuixar
|
||||
dibuixar();
|
||||
|
||||
// Presentar renderer (swap buffers)
|
||||
sdl_.presenta();
|
||||
}
|
||||
|
||||
std::cout << "Escena Titol: Finalitzant...\n";
|
||||
}
|
||||
|
||||
void EscenaTitol::actualitzar(float delta_time) {
|
||||
// Actualitzar starfield (sempre actiu)
|
||||
if (starfield_) {
|
||||
starfield_->actualitzar(delta_time);
|
||||
}
|
||||
|
||||
switch (estat_actual_) {
|
||||
case EstatTitol::STARFIELD_FADE_IN: {
|
||||
temps_acumulat_ += delta_time;
|
||||
|
||||
// Calcular progrés del fade (0.0 → 1.0)
|
||||
float progress = std::min(1.0f, temps_acumulat_ / DURACIO_FADE_IN);
|
||||
|
||||
// Lerp brightness de 0.0 a BRIGHTNESS_STARFIELD
|
||||
float brightness_actual = progress * BRIGHTNESS_STARFIELD;
|
||||
starfield_->set_brightness(brightness_actual);
|
||||
|
||||
// Transició a STARFIELD quan el fade es completa
|
||||
if (temps_acumulat_ >= DURACIO_FADE_IN) {
|
||||
estat_actual_ = EstatTitol::STARFIELD;
|
||||
temps_acumulat_ = 0.0f; // Reset timer per al següent estat
|
||||
starfield_->set_brightness(BRIGHTNESS_STARFIELD); // Assegurar valor final
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case EstatTitol::STARFIELD:
|
||||
temps_acumulat_ += delta_time;
|
||||
if (temps_acumulat_ >= DURACIO_INIT) {
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
temps_estat_main_ = 0.0f; // Reset timer al entrar a MAIN
|
||||
animacio_activa_ = false; // Comença estàtic
|
||||
factor_lerp_ = 0.0f; // Sense animació encara
|
||||
}
|
||||
break;
|
||||
|
||||
case EstatTitol::MAIN: {
|
||||
temps_estat_main_ += delta_time;
|
||||
|
||||
// Fase 1: Estàtic (0-10s)
|
||||
if (temps_estat_main_ < DELAY_INICI_ANIMACIO) {
|
||||
factor_lerp_ = 0.0f;
|
||||
animacio_activa_ = false;
|
||||
}
|
||||
// Fase 2: Lerp (10-12s)
|
||||
else if (temps_estat_main_ < DELAY_INICI_ANIMACIO + DURACIO_LERP) {
|
||||
float temps_lerp = temps_estat_main_ - DELAY_INICI_ANIMACIO;
|
||||
factor_lerp_ = temps_lerp / DURACIO_LERP; // 0.0 → 1.0 linealment
|
||||
animacio_activa_ = true;
|
||||
}
|
||||
// Fase 3: Animació completa (12s+)
|
||||
else {
|
||||
factor_lerp_ = 1.0f;
|
||||
animacio_activa_ = true;
|
||||
}
|
||||
|
||||
// Actualitzar animació del logo
|
||||
actualitzar_animacio_logo(delta_time);
|
||||
break;
|
||||
}
|
||||
|
||||
case EstatTitol::TRANSITION_TO_GAME:
|
||||
temps_acumulat_ += delta_time;
|
||||
|
||||
// Continuar animació orbital durant la transició
|
||||
actualitzar_animacio_logo(delta_time);
|
||||
|
||||
if (temps_acumulat_ >= DURACIO_TRANSITION) {
|
||||
// Transició a JOC (la música ja s'ha parat en el fade)
|
||||
GestorEscenes::actual = Escena::JOC;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaTitol::actualitzar_animacio_logo(float delta_time) {
|
||||
// Només calcular i aplicar offsets si l'animació està activa
|
||||
if (animacio_activa_) {
|
||||
// Acumular temps escalat
|
||||
temps_animacio_ += delta_time * factor_lerp_;
|
||||
|
||||
// Usar amplituds i freqüències completes
|
||||
float amplitude_x_actual = ORBIT_AMPLITUDE_X;
|
||||
float amplitude_y_actual = ORBIT_AMPLITUDE_Y;
|
||||
float frequency_x_actual = ORBIT_FREQUENCY_X;
|
||||
float frequency_y_actual = ORBIT_FREQUENCY_Y;
|
||||
|
||||
// Calcular offset orbital
|
||||
float offset_x = amplitude_x_actual * std::sin(2.0f * Defaults::Math::PI * frequency_x_actual * temps_animacio_);
|
||||
float offset_y = amplitude_y_actual * std::sin(2.0f * Defaults::Math::PI * frequency_y_actual * temps_animacio_ + ORBIT_PHASE_OFFSET);
|
||||
|
||||
// Aplicar offset a totes les lletres de "ORNI"
|
||||
for (size_t i = 0; i < lletres_orni_.size(); ++i) {
|
||||
lletres_orni_[i].posicio.x = posicions_originals_orni_[i].x + static_cast<int>(std::round(offset_x));
|
||||
lletres_orni_[i].posicio.y = posicions_originals_orni_[i].y + static_cast<int>(std::round(offset_y));
|
||||
}
|
||||
|
||||
// Aplicar offset a totes les lletres de "ATTACK!"
|
||||
for (size_t i = 0; i < lletres_attack_.size(); ++i) {
|
||||
lletres_attack_[i].posicio.x = posicions_originals_attack_[i].x + static_cast<int>(std::round(offset_x));
|
||||
lletres_attack_[i].posicio.y = posicions_originals_attack_[i].y + static_cast<int>(std::round(offset_y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaTitol::dibuixar() {
|
||||
// Dibuixar starfield de fons (sempre, en tots els estats)
|
||||
if (starfield_) {
|
||||
starfield_->dibuixar();
|
||||
}
|
||||
|
||||
// En els estats STARFIELD_FADE_IN i STARFIELD, només mostrar starfield (sense text)
|
||||
if (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || estat_actual_ == EstatTitol::STARFIELD) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Estat MAIN i TRANSITION_TO_GAME: Dibuixar títol i text (sobre el starfield)
|
||||
if (estat_actual_ == EstatTitol::MAIN || estat_actual_ == EstatTitol::TRANSITION_TO_GAME) {
|
||||
// === Calcular i renderitzar ombra (només si animació activa) ===
|
||||
if (animacio_activa_) {
|
||||
float temps_shadow = temps_animacio_ - SHADOW_DELAY;
|
||||
if (temps_shadow < 0.0f) temps_shadow = 0.0f; // Evitar temps negatiu
|
||||
|
||||
// Usar amplituds i freqüències completes per l'ombra
|
||||
float amplitude_x_shadow = ORBIT_AMPLITUDE_X;
|
||||
float amplitude_y_shadow = ORBIT_AMPLITUDE_Y;
|
||||
float frequency_x_shadow = ORBIT_FREQUENCY_X;
|
||||
float frequency_y_shadow = ORBIT_FREQUENCY_Y;
|
||||
|
||||
// Calcular offset de l'ombra
|
||||
float shadow_offset_x = amplitude_x_shadow * std::sin(2.0f * Defaults::Math::PI * frequency_x_shadow * temps_shadow) + SHADOW_OFFSET_X;
|
||||
float shadow_offset_y = amplitude_y_shadow * std::sin(2.0f * Defaults::Math::PI * frequency_y_shadow * temps_shadow + ORBIT_PHASE_OFFSET) + SHADOW_OFFSET_Y;
|
||||
|
||||
// === RENDERITZAR OMBRA PRIMER (darrera del logo principal) ===
|
||||
|
||||
// Ombra "ORNI"
|
||||
for (size_t i = 0; i < lletres_orni_.size(); ++i) {
|
||||
Punt pos_shadow;
|
||||
pos_shadow.x = posicions_originals_orni_[i].x + static_cast<int>(std::round(shadow_offset_x));
|
||||
pos_shadow.y = posicions_originals_orni_[i].y + static_cast<int>(std::round(shadow_offset_y));
|
||||
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletres_orni_[i].forma,
|
||||
pos_shadow,
|
||||
0.0f,
|
||||
ESCALA_TITULO,
|
||||
true,
|
||||
1.0f, // progress = 1.0 (totalment visible)
|
||||
SHADOW_BRIGHTNESS // brightness = 0.4 (brillantor reduïda)
|
||||
);
|
||||
}
|
||||
|
||||
// Ombra "ATTACK!"
|
||||
for (size_t i = 0; i < lletres_attack_.size(); ++i) {
|
||||
Punt pos_shadow;
|
||||
pos_shadow.x = posicions_originals_attack_[i].x + static_cast<int>(std::round(shadow_offset_x));
|
||||
pos_shadow.y = posicions_originals_attack_[i].y + static_cast<int>(std::round(shadow_offset_y));
|
||||
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletres_attack_[i].forma,
|
||||
pos_shadow,
|
||||
0.0f,
|
||||
ESCALA_TITULO,
|
||||
true,
|
||||
1.0f, // progress = 1.0 (totalment visible)
|
||||
SHADOW_BRIGHTNESS);
|
||||
}
|
||||
}
|
||||
|
||||
// === RENDERITZAR LOGO PRINCIPAL (damunt) ===
|
||||
|
||||
// Dibuixar "ORNI" (línia 1)
|
||||
for (const auto& lletra : lletres_orni_) {
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletra.forma,
|
||||
lletra.posicio,
|
||||
0.0f,
|
||||
ESCALA_TITULO,
|
||||
true,
|
||||
1.0f // Brillantor completa
|
||||
);
|
||||
}
|
||||
|
||||
// Dibuixar "ATTACK!" (línia 2)
|
||||
for (const auto& lletra : lletres_attack_) {
|
||||
Rendering::render_shape(
|
||||
sdl_.obte_renderer(),
|
||||
lletra.forma,
|
||||
lletra.posicio,
|
||||
0.0f,
|
||||
ESCALA_TITULO,
|
||||
true,
|
||||
1.0f // Brillantor completa
|
||||
);
|
||||
}
|
||||
|
||||
// === Text "PRESS BUTTON TO PLAY" ===
|
||||
// En estat MAIN: sempre visible
|
||||
// En estat TRANSITION: parpellejant (blink amb sinusoide)
|
||||
|
||||
const float spacing = 2.0f; // Espai entre caràcters (usat també per copyright)
|
||||
|
||||
bool mostrar_text = true;
|
||||
if (estat_actual_ == EstatTitol::TRANSITION_TO_GAME) {
|
||||
// Parpelleig: sin oscil·la entre -1 i 1, volem ON quan > 0
|
||||
float fase = temps_acumulat_ * BLINK_FREQUENCY * 2.0f * 3.14159f; // 2π × freq × temps
|
||||
mostrar_text = (std::sin(fase) > 0.0f);
|
||||
}
|
||||
|
||||
if (mostrar_text) {
|
||||
const std::string main_text = "PRESS BUTTON TO PLAY";
|
||||
const float escala_main = 1.0f;
|
||||
|
||||
float text_width = text_.get_text_width(main_text, escala_main, spacing);
|
||||
|
||||
float x_center = (Defaults::Game::WIDTH - text_width) / 2.0f;
|
||||
float altura_attack = lletres_attack_.empty() ? 50.0f : lletres_attack_[0].altura;
|
||||
float y_center = y_attack_dinamica_ + altura_attack + 70.0f;
|
||||
|
||||
text_.render(main_text, Punt{x_center, y_center}, escala_main, spacing);
|
||||
}
|
||||
|
||||
// === Copyright a la part inferior (centrat horitzontalment) ===
|
||||
// Convert to uppercase since VectorText only supports A-Z
|
||||
std::string copyright = Project::COPYRIGHT;
|
||||
for (char& c : copyright) {
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
c = c - 32; // Convert to uppercase
|
||||
}
|
||||
}
|
||||
const float escala_copy = 0.6f;
|
||||
|
||||
float copy_width = text_.get_text_width(copyright, escala_copy, spacing);
|
||||
float copy_height = text_.get_text_height(escala_copy);
|
||||
|
||||
float x_copy = (Defaults::Game::WIDTH - copy_width) / 2.0f;
|
||||
float y_copy = Defaults::Game::HEIGHT - copy_height - 20.0f; // 20px des del fons
|
||||
|
||||
text_.render(copyright, Punt{x_copy, y_copy}, escala_copy, spacing);
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaTitol::processar_events(const SDL_Event& event) {
|
||||
// Qualsevol tecla o clic de ratolí
|
||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
switch (estat_actual_) {
|
||||
case EstatTitol::STARFIELD_FADE_IN:
|
||||
// Saltar directament a MAIN (ometre fade-in i starfield)
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
starfield_->set_brightness(BRIGHTNESS_STARFIELD); // Assegurar brightness final
|
||||
temps_estat_main_ = 0.0f; // Reset timer per animació de títol
|
||||
break;
|
||||
|
||||
case EstatTitol::STARFIELD:
|
||||
// Saltar a MAIN
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
temps_estat_main_ = 0.0f; // Reset timer
|
||||
break;
|
||||
|
||||
case EstatTitol::MAIN:
|
||||
// Utilitzar context per transició a JOC
|
||||
context_.canviar_escena(Escena::JOC);
|
||||
// NO actualitzar GestorEscenes::actual aquí!
|
||||
// La transició es fa en l'estat TRANSITION_TO_GAME
|
||||
|
||||
// Iniciar transició amb fade-out de música
|
||||
estat_actual_ = EstatTitol::TRANSITION_TO_GAME;
|
||||
temps_acumulat_ = 0.0f; // Reset del comptador
|
||||
Audio::get()->fadeOutMusic(MUSIC_FADE); // Fade
|
||||
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
|
||||
break;
|
||||
|
||||
case EstatTitol::TRANSITION_TO_GAME:
|
||||
// Ignorar inputs durant la transició
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
101
source/game/escenes/escena_titol.hpp
Normal file
101
source/game/escenes/escena_titol.hpp
Normal file
@@ -0,0 +1,101 @@
|
||||
// escena_titol.hpp - Pantalla de títol del joc
|
||||
// Mostra missatge "PRESS BUTTON TO PLAY" i copyright
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/graphics/shape.hpp"
|
||||
#include "core/graphics/starfield.hpp"
|
||||
#include "core/graphics/vector_text.hpp"
|
||||
#include "core/rendering/sdl_manager.hpp"
|
||||
#include "core/system/context_escenes.hpp"
|
||||
#include "core/types.hpp"
|
||||
|
||||
class EscenaTitol {
|
||||
public:
|
||||
explicit EscenaTitol(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||
~EscenaTitol(); // Destructor per aturar música
|
||||
void executar(); // Bucle principal de l'escena
|
||||
|
||||
private:
|
||||
// Màquina d'estats per la pantalla de títol
|
||||
enum class EstatTitol {
|
||||
STARFIELD_FADE_IN, // Fade-in del starfield (1.5s)
|
||||
STARFIELD, // Pantalla con el campo de estrellas
|
||||
MAIN, // Pantalla de títol amb text
|
||||
TRANSITION_TO_GAME // Transició amb fade-out de música i text parpellejant
|
||||
};
|
||||
|
||||
// Estructura per emmagatzemar informació de cada lletra del títol
|
||||
struct LetraLogo {
|
||||
std::shared_ptr<Graphics::Shape> forma; // Forma vectorial de la lletra
|
||||
Punt posicio; // Posició en pantalla
|
||||
float ancho; // Amplada escalada
|
||||
float altura; // Altura escalada
|
||||
float offset_centre; // Offset del centre per posicionament
|
||||
};
|
||||
|
||||
SDLManager& sdl_;
|
||||
GestorEscenes::ContextEscenes& context_;
|
||||
Graphics::VectorText text_; // Sistema de text vectorial
|
||||
std::unique_ptr<Graphics::Starfield> starfield_; // Camp d'estrelles de fons
|
||||
EstatTitol estat_actual_; // Estat actual de la màquina
|
||||
float temps_acumulat_; // Temps acumulat per l'estat INIT
|
||||
|
||||
// Lletres del títol "ORNI ATTACK!"
|
||||
std::vector<LetraLogo> lletres_orni_; // Lletres de "ORNI" (línia 1)
|
||||
std::vector<LetraLogo> lletres_attack_; // Lletres de "ATTACK!" (línia 2)
|
||||
float y_attack_dinamica_; // Posició Y calculada dinàmicament per "ATTACK!"
|
||||
|
||||
// Estat d'animació del logo
|
||||
float temps_animacio_; // Temps acumulat per animació orbital
|
||||
std::vector<Punt> posicions_originals_orni_; // Posicions originals de "ORNI"
|
||||
std::vector<Punt> posicions_originals_attack_; // Posicions originals de "ATTACK!"
|
||||
|
||||
// Estat d'arrencada de l'animació
|
||||
float temps_estat_main_; // Temps acumulat en estat MAIN
|
||||
bool animacio_activa_; // Flag: true quan animació està activa
|
||||
float factor_lerp_; // Factor de lerp actual (0.0 → 1.0)
|
||||
|
||||
// Constants
|
||||
static constexpr float BRIGHTNESS_STARFIELD = 1.2f; // Brightness del starfield (>1.0 = més brillant)
|
||||
static constexpr float DURACIO_FADE_IN = 3.0f; // Duració del fade-in del starfield (1.5 segons)
|
||||
static constexpr float DURACIO_INIT = 4.0f; // Duració de l'estat INIT (2 segons)
|
||||
static constexpr float DURACIO_TRANSITION = 1.5f; // Duració de la transició (1.5 segons)
|
||||
static constexpr float ESCALA_TITULO = 0.6f; // Escala per les lletres del títol (50%)
|
||||
static constexpr float ESPAI_ENTRE_LLETRES = 10.0f; // Espai entre lletres
|
||||
static constexpr float Y_ORNI = 150.0f; // Posició Y de "ORNI"
|
||||
static constexpr float SEPARACION_LINEAS = 10.0f; // Separació entre "ORNI" i "ATTACK!" (0.0f = pegades)
|
||||
static constexpr float BLINK_FREQUENCY = 3.0f; // Freqüència de parpelleig (3 Hz)
|
||||
static constexpr int MUSIC_FADE = 1000; // Duracio del fade de la musica del titol al començar a jugar
|
||||
|
||||
// Constants d'animació del logo
|
||||
static constexpr float ORBIT_AMPLITUDE_X = 4.0f; // Amplitud oscil·lació horitzontal (píxels)
|
||||
static constexpr float ORBIT_AMPLITUDE_Y = 3.0f; // Amplitud oscil·lació vertical (píxels)
|
||||
static constexpr float ORBIT_FREQUENCY_X = 0.8f; // Velocitat oscil·lació horitzontal (Hz)
|
||||
static constexpr float ORBIT_FREQUENCY_Y = 1.2f; // Velocitat oscil·lació vertical (Hz)
|
||||
static constexpr float ORBIT_PHASE_OFFSET = 1.57f; // Desfasament entre X i Y (90° per circular)
|
||||
|
||||
// Constants d'ombra del logo
|
||||
static constexpr float SHADOW_DELAY = 0.5f; // Retard temporal de l'ombra (segons)
|
||||
static constexpr float SHADOW_BRIGHTNESS = 0.4f; // Multiplicador de brillantor de l'ombra (0.0-1.0)
|
||||
static constexpr float SHADOW_OFFSET_X = 2.0f; // Offset espacial X fix (píxels)
|
||||
static constexpr float SHADOW_OFFSET_Y = 2.0f; // Offset espacial Y fix (píxels)
|
||||
|
||||
// Temporització de l'arrencada de l'animació
|
||||
static constexpr float DELAY_INICI_ANIMACIO = 10.0f; // 10s estàtic abans d'animar
|
||||
static constexpr float DURACIO_LERP = 2.0f; // 2s per arribar a amplitud completa
|
||||
|
||||
// Mètodes privats
|
||||
void actualitzar(float delta_time);
|
||||
void actualitzar_animacio_logo(float delta_time); // Actualitza l'animació orbital del logo
|
||||
void dibuixar();
|
||||
void processar_events(const SDL_Event& event);
|
||||
void inicialitzar_titol(); // Carrega i posiciona les lletres del títol
|
||||
};
|
||||
@@ -1,298 +1,413 @@
|
||||
#include "options.hpp"
|
||||
#include "../core/defaults.hpp"
|
||||
#include "../external/fkyaml_node.hpp"
|
||||
#include "project.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "core/defaults.hpp"
|
||||
#include "external/fkyaml_node.hpp"
|
||||
#include "project.h"
|
||||
|
||||
namespace Options {
|
||||
|
||||
// Inicialitzar opcions amb valors per defecte de Defaults::
|
||||
void init() {
|
||||
#ifdef _DEBUG
|
||||
console = true;
|
||||
console = true;
|
||||
#else
|
||||
console = false;
|
||||
console = false;
|
||||
#endif
|
||||
|
||||
// Window
|
||||
window.width = Defaults::Window::WIDTH;
|
||||
window.height = Defaults::Window::HEIGHT;
|
||||
window.fullscreen = false;
|
||||
window.size_increment = Defaults::Window::SIZE_INCREMENT;
|
||||
// Window
|
||||
window.width = Defaults::Window::WIDTH;
|
||||
window.height = Defaults::Window::HEIGHT;
|
||||
window.fullscreen = false;
|
||||
window.zoom_factor = Defaults::Window::BASE_ZOOM;
|
||||
|
||||
// Physics
|
||||
physics.rotation_speed = Defaults::Physics::ROTATION_SPEED;
|
||||
physics.acceleration = Defaults::Physics::ACCELERATION;
|
||||
physics.max_velocity = Defaults::Physics::MAX_VELOCITY;
|
||||
physics.friction = Defaults::Physics::FRICTION;
|
||||
physics.enemy_speed = Defaults::Physics::ENEMY_SPEED;
|
||||
physics.bullet_speed = Defaults::Physics::BULLET_SPEED;
|
||||
// Physics
|
||||
physics.rotation_speed = Defaults::Physics::ROTATION_SPEED;
|
||||
physics.acceleration = Defaults::Physics::ACCELERATION;
|
||||
physics.max_velocity = Defaults::Physics::MAX_VELOCITY;
|
||||
physics.friction = Defaults::Physics::FRICTION;
|
||||
physics.enemy_speed = Defaults::Physics::ENEMY_SPEED;
|
||||
physics.bullet_speed = Defaults::Physics::BULLET_SPEED;
|
||||
|
||||
// Gameplay
|
||||
gameplay.max_enemies = Defaults::Entities::MAX_ORNIS;
|
||||
gameplay.max_bullets = Defaults::Entities::MAX_BALES;
|
||||
// Gameplay
|
||||
gameplay.max_enemies = Defaults::Entities::MAX_ORNIS;
|
||||
gameplay.max_bullets = Defaults::Entities::MAX_BALES;
|
||||
|
||||
// Version
|
||||
version = std::string(Project::VERSION);
|
||||
// Rendering
|
||||
rendering.vsync = Defaults::Rendering::VSYNC_DEFAULT;
|
||||
|
||||
// Audio
|
||||
audio.enabled = Defaults::Audio::ENABLED;
|
||||
audio.volume = Defaults::Audio::VOLUME;
|
||||
audio.music.enabled = Defaults::Music::ENABLED;
|
||||
audio.music.volume = Defaults::Music::VOLUME;
|
||||
audio.sound.enabled = Defaults::Sound::ENABLED;
|
||||
audio.sound.volume = Defaults::Sound::VOLUME;
|
||||
|
||||
// Version
|
||||
version = std::string(Project::VERSION);
|
||||
}
|
||||
|
||||
// Establir la ruta del fitxer de configuració
|
||||
void setConfigFile(const std::string &path) { config_file_path = path; }
|
||||
void setConfigFile(const std::string& path) { config_file_path = path; }
|
||||
|
||||
// Funcions auxiliars per carregar seccions del YAML
|
||||
|
||||
static void loadWindowConfigFromYaml(const fkyaml::node &yaml) {
|
||||
if (yaml.contains("window")) {
|
||||
const auto &win = yaml["window"];
|
||||
static void loadWindowConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("window")) {
|
||||
const auto& win = yaml["window"];
|
||||
|
||||
if (win.contains("width")) {
|
||||
try {
|
||||
auto val = win["width"].get_value<int>();
|
||||
window.width = (val >= Defaults::Window::MIN_WIDTH)
|
||||
? val
|
||||
: Defaults::Window::WIDTH;
|
||||
} catch (...) {
|
||||
window.width = Defaults::Window::WIDTH;
|
||||
}
|
||||
}
|
||||
if (win.contains("width")) {
|
||||
try {
|
||||
auto val = win["width"].get_value<int>();
|
||||
window.width = (val >= Defaults::Window::MIN_WIDTH)
|
||||
? val
|
||||
: Defaults::Window::WIDTH;
|
||||
} catch (...) {
|
||||
window.width = Defaults::Window::WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
if (win.contains("height")) {
|
||||
try {
|
||||
auto val = win["height"].get_value<int>();
|
||||
window.height = (val >= Defaults::Window::MIN_HEIGHT)
|
||||
? val
|
||||
: Defaults::Window::HEIGHT;
|
||||
} catch (...) {
|
||||
window.height = Defaults::Window::HEIGHT;
|
||||
}
|
||||
}
|
||||
if (win.contains("height")) {
|
||||
try {
|
||||
auto val = win["height"].get_value<int>();
|
||||
window.height = (val >= Defaults::Window::MIN_HEIGHT)
|
||||
? val
|
||||
: Defaults::Window::HEIGHT;
|
||||
} catch (...) {
|
||||
window.height = Defaults::Window::HEIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
if (win.contains("fullscreen")) {
|
||||
try {
|
||||
window.fullscreen = win["fullscreen"].get_value<bool>();
|
||||
} catch (...) {
|
||||
window.fullscreen = false;
|
||||
}
|
||||
}
|
||||
if (win.contains("fullscreen")) {
|
||||
try {
|
||||
window.fullscreen = win["fullscreen"].get_value<bool>();
|
||||
} catch (...) {
|
||||
window.fullscreen = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (win.contains("size_increment")) {
|
||||
try {
|
||||
auto val = win["size_increment"].get_value<int>();
|
||||
window.size_increment =
|
||||
(val > 0) ? val : Defaults::Window::SIZE_INCREMENT;
|
||||
} catch (...) {
|
||||
window.size_increment = Defaults::Window::SIZE_INCREMENT;
|
||||
}
|
||||
if (win.contains("zoom_factor")) {
|
||||
try {
|
||||
auto val = win["zoom_factor"].get_value<float>();
|
||||
window.zoom_factor = (val >= Defaults::Window::MIN_ZOOM && val <= 10.0f)
|
||||
? val
|
||||
: Defaults::Window::BASE_ZOOM;
|
||||
} catch (...) {
|
||||
window.zoom_factor = Defaults::Window::BASE_ZOOM;
|
||||
}
|
||||
} else {
|
||||
// Legacy config: infer zoom from width
|
||||
window.zoom_factor = static_cast<float>(window.width) / Defaults::Window::WIDTH;
|
||||
window.zoom_factor = std::max(Defaults::Window::MIN_ZOOM, window.zoom_factor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void loadPhysicsConfigFromYaml(const fkyaml::node &yaml) {
|
||||
if (yaml.contains("physics")) {
|
||||
const auto &phys = yaml["physics"];
|
||||
static void loadPhysicsConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("physics")) {
|
||||
const auto& phys = yaml["physics"];
|
||||
|
||||
if (phys.contains("rotation_speed")) {
|
||||
try {
|
||||
auto val = phys["rotation_speed"].get_value<float>();
|
||||
physics.rotation_speed =
|
||||
(val > 0) ? val : Defaults::Physics::ROTATION_SPEED;
|
||||
} catch (...) {
|
||||
physics.rotation_speed = Defaults::Physics::ROTATION_SPEED;
|
||||
}
|
||||
}
|
||||
if (phys.contains("rotation_speed")) {
|
||||
try {
|
||||
auto val = phys["rotation_speed"].get_value<float>();
|
||||
physics.rotation_speed =
|
||||
(val > 0) ? val : Defaults::Physics::ROTATION_SPEED;
|
||||
} catch (...) {
|
||||
physics.rotation_speed = Defaults::Physics::ROTATION_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
if (phys.contains("acceleration")) {
|
||||
try {
|
||||
auto val = phys["acceleration"].get_value<float>();
|
||||
physics.acceleration =
|
||||
(val > 0) ? val : Defaults::Physics::ACCELERATION;
|
||||
} catch (...) {
|
||||
physics.acceleration = Defaults::Physics::ACCELERATION;
|
||||
}
|
||||
}
|
||||
if (phys.contains("acceleration")) {
|
||||
try {
|
||||
auto val = phys["acceleration"].get_value<float>();
|
||||
physics.acceleration =
|
||||
(val > 0) ? val : Defaults::Physics::ACCELERATION;
|
||||
} catch (...) {
|
||||
physics.acceleration = Defaults::Physics::ACCELERATION;
|
||||
}
|
||||
}
|
||||
|
||||
if (phys.contains("max_velocity")) {
|
||||
try {
|
||||
auto val = phys["max_velocity"].get_value<float>();
|
||||
physics.max_velocity =
|
||||
(val > 0) ? val : Defaults::Physics::MAX_VELOCITY;
|
||||
} catch (...) {
|
||||
physics.max_velocity = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
}
|
||||
if (phys.contains("max_velocity")) {
|
||||
try {
|
||||
auto val = phys["max_velocity"].get_value<float>();
|
||||
physics.max_velocity =
|
||||
(val > 0) ? val : Defaults::Physics::MAX_VELOCITY;
|
||||
} catch (...) {
|
||||
physics.max_velocity = Defaults::Physics::MAX_VELOCITY;
|
||||
}
|
||||
}
|
||||
|
||||
if (phys.contains("friction")) {
|
||||
try {
|
||||
auto val = phys["friction"].get_value<float>();
|
||||
physics.friction = (val > 0) ? val : Defaults::Physics::FRICTION;
|
||||
} catch (...) {
|
||||
physics.friction = Defaults::Physics::FRICTION;
|
||||
}
|
||||
}
|
||||
if (phys.contains("friction")) {
|
||||
try {
|
||||
auto val = phys["friction"].get_value<float>();
|
||||
physics.friction = (val > 0) ? val : Defaults::Physics::FRICTION;
|
||||
} catch (...) {
|
||||
physics.friction = Defaults::Physics::FRICTION;
|
||||
}
|
||||
}
|
||||
|
||||
if (phys.contains("enemy_speed")) {
|
||||
try {
|
||||
auto val = phys["enemy_speed"].get_value<float>();
|
||||
physics.enemy_speed = (val > 0) ? val : Defaults::Physics::ENEMY_SPEED;
|
||||
} catch (...) {
|
||||
physics.enemy_speed = Defaults::Physics::ENEMY_SPEED;
|
||||
}
|
||||
}
|
||||
if (phys.contains("enemy_speed")) {
|
||||
try {
|
||||
auto val = phys["enemy_speed"].get_value<float>();
|
||||
physics.enemy_speed = (val > 0) ? val : Defaults::Physics::ENEMY_SPEED;
|
||||
} catch (...) {
|
||||
physics.enemy_speed = Defaults::Physics::ENEMY_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
if (phys.contains("bullet_speed")) {
|
||||
try {
|
||||
auto val = phys["bullet_speed"].get_value<float>();
|
||||
physics.bullet_speed =
|
||||
(val > 0) ? val : Defaults::Physics::BULLET_SPEED;
|
||||
} catch (...) {
|
||||
physics.bullet_speed = Defaults::Physics::BULLET_SPEED;
|
||||
}
|
||||
if (phys.contains("bullet_speed")) {
|
||||
try {
|
||||
auto val = phys["bullet_speed"].get_value<float>();
|
||||
physics.bullet_speed =
|
||||
(val > 0) ? val : Defaults::Physics::BULLET_SPEED;
|
||||
} catch (...) {
|
||||
physics.bullet_speed = Defaults::Physics::BULLET_SPEED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void loadGameplayConfigFromYaml(const fkyaml::node &yaml) {
|
||||
if (yaml.contains("gameplay")) {
|
||||
const auto &game = yaml["gameplay"];
|
||||
static void loadGameplayConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("gameplay")) {
|
||||
const auto& game = yaml["gameplay"];
|
||||
|
||||
if (game.contains("max_enemies")) {
|
||||
try {
|
||||
auto val = game["max_enemies"].get_value<int>();
|
||||
gameplay.max_enemies =
|
||||
(val > 0 && val <= 50) ? val : Defaults::Entities::MAX_ORNIS;
|
||||
} catch (...) {
|
||||
gameplay.max_enemies = Defaults::Entities::MAX_ORNIS;
|
||||
}
|
||||
}
|
||||
if (game.contains("max_enemies")) {
|
||||
try {
|
||||
auto val = game["max_enemies"].get_value<int>();
|
||||
gameplay.max_enemies =
|
||||
(val > 0 && val <= 50) ? val : Defaults::Entities::MAX_ORNIS;
|
||||
} catch (...) {
|
||||
gameplay.max_enemies = Defaults::Entities::MAX_ORNIS;
|
||||
}
|
||||
}
|
||||
|
||||
if (game.contains("max_bullets")) {
|
||||
try {
|
||||
auto val = game["max_bullets"].get_value<int>();
|
||||
gameplay.max_bullets =
|
||||
(val > 0 && val <= 10) ? val : Defaults::Entities::MAX_BALES;
|
||||
} catch (...) {
|
||||
gameplay.max_bullets = Defaults::Entities::MAX_BALES;
|
||||
}
|
||||
if (game.contains("max_bullets")) {
|
||||
try {
|
||||
auto val = game["max_bullets"].get_value<int>();
|
||||
gameplay.max_bullets =
|
||||
(val > 0 && val <= 10) ? val : Defaults::Entities::MAX_BALES;
|
||||
} catch (...) {
|
||||
gameplay.max_bullets = Defaults::Entities::MAX_BALES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void loadRenderingConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("rendering")) {
|
||||
const auto& rend = yaml["rendering"];
|
||||
|
||||
if (rend.contains("vsync")) {
|
||||
try {
|
||||
int val = rend["vsync"].get_value<int>();
|
||||
// Validar: només 0 o 1
|
||||
rendering.vsync = (val == 0 || val == 1) ? val : Defaults::Rendering::VSYNC_DEFAULT;
|
||||
} catch (...) {
|
||||
rendering.vsync = Defaults::Rendering::VSYNC_DEFAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void loadAudioConfigFromYaml(const fkyaml::node& yaml) {
|
||||
if (yaml.contains("audio")) {
|
||||
const auto& aud = yaml["audio"];
|
||||
|
||||
if (aud.contains("enabled")) {
|
||||
try {
|
||||
audio.enabled = aud["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.enabled = Defaults::Audio::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("volume")) {
|
||||
try {
|
||||
float val = aud["volume"].get_value<float>();
|
||||
audio.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Audio::VOLUME;
|
||||
} catch (...) {
|
||||
audio.volume = Defaults::Audio::VOLUME;
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("music")) {
|
||||
const auto& mus = aud["music"];
|
||||
|
||||
if (mus.contains("enabled")) {
|
||||
try {
|
||||
audio.music.enabled = mus["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.music.enabled = Defaults::Music::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (mus.contains("volume")) {
|
||||
try {
|
||||
float val = mus["volume"].get_value<float>();
|
||||
audio.music.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Music::VOLUME;
|
||||
} catch (...) {
|
||||
audio.music.volume = Defaults::Music::VOLUME;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (aud.contains("sound")) {
|
||||
const auto& snd = aud["sound"];
|
||||
|
||||
if (snd.contains("enabled")) {
|
||||
try {
|
||||
audio.sound.enabled = snd["enabled"].get_value<bool>();
|
||||
} catch (...) {
|
||||
audio.sound.enabled = Defaults::Sound::ENABLED;
|
||||
}
|
||||
}
|
||||
|
||||
if (snd.contains("volume")) {
|
||||
try {
|
||||
float val = snd["volume"].get_value<float>();
|
||||
audio.sound.volume = (val >= 0.0f && val <= 1.0f) ? val : Defaults::Sound::VOLUME;
|
||||
} catch (...) {
|
||||
audio.sound.volume = Defaults::Sound::VOLUME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carregar configuració des del fitxer YAML
|
||||
auto loadFromFile() -> bool {
|
||||
const std::string CONFIG_VERSION = std::string(Project::VERSION);
|
||||
const std::string CONFIG_VERSION = std::string(Project::VERSION);
|
||||
|
||||
std::ifstream file(config_file_path);
|
||||
if (!file.good()) {
|
||||
// El fitxer no existeix → crear-ne un de nou amb valors per defecte
|
||||
if (console) {
|
||||
std::cout << "Fitxer de config no trobat, creant-ne un de nou: "
|
||||
<< config_file_path << '\n';
|
||||
}
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Llegir tot el contingut del fitxer
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
try {
|
||||
// Parsejar YAML
|
||||
auto yaml = fkyaml::node::deserialize(content);
|
||||
|
||||
// Validar versió
|
||||
if (yaml.contains("version")) {
|
||||
version = yaml["version"].get_value<std::string>();
|
||||
std::ifstream file(config_file_path);
|
||||
if (!file.good()) {
|
||||
// El fitxer no existeix → crear-ne un de nou amb valors per defecte
|
||||
if (console) {
|
||||
std::cout << "Fitxer de config no trobat, creant-ne un de nou: "
|
||||
<< config_file_path << '\n';
|
||||
}
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (CONFIG_VERSION != version) {
|
||||
// Versió incompatible → regenerar config
|
||||
if (console) {
|
||||
std::cout << "Versió de config incompatible (esperada: "
|
||||
<< CONFIG_VERSION << ", trobada: " << version
|
||||
<< "), regenerant config\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
// Llegir tot el contingut del fitxer
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
try {
|
||||
// Parsejar YAML
|
||||
auto yaml = fkyaml::node::deserialize(content);
|
||||
|
||||
// Validar versió
|
||||
if (yaml.contains("version")) {
|
||||
version = yaml["version"].get_value<std::string>();
|
||||
}
|
||||
|
||||
if (CONFIG_VERSION != version) {
|
||||
// Versió incompatible → regenerar config
|
||||
if (console) {
|
||||
std::cout << "Versió de config incompatible (esperada: "
|
||||
<< CONFIG_VERSION << ", trobada: " << version
|
||||
<< "), regenerant config\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carregar seccions
|
||||
loadWindowConfigFromYaml(yaml);
|
||||
loadPhysicsConfigFromYaml(yaml);
|
||||
loadGameplayConfigFromYaml(yaml);
|
||||
loadRenderingConfigFromYaml(yaml);
|
||||
loadAudioConfigFromYaml(yaml);
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config carregada correctament des de: " << config_file_path
|
||||
<< '\n';
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (const fkyaml::exception& e) {
|
||||
// Error de parsejat YAML → regenerar config
|
||||
if (console) {
|
||||
std::cerr << "Error parsejant YAML: " << e.what() << '\n';
|
||||
std::cerr << "Creant config nou amb valors per defecte\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carregar seccions
|
||||
loadWindowConfigFromYaml(yaml);
|
||||
loadPhysicsConfigFromYaml(yaml);
|
||||
loadGameplayConfigFromYaml(yaml);
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config carregada correctament des de: " << config_file_path
|
||||
<< '\n';
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (const fkyaml::exception &e) {
|
||||
// Error de parsejat YAML → regenerar config
|
||||
if (console) {
|
||||
std::cerr << "Error parsejant YAML: " << e.what() << '\n';
|
||||
std::cerr << "Creant config nou amb valors per defecte\n";
|
||||
}
|
||||
init();
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Guardar configuració al fitxer YAML
|
||||
auto saveToFile() -> bool {
|
||||
std::ofstream file(config_file_path);
|
||||
if (!file.is_open()) {
|
||||
if (console) {
|
||||
std::cerr << "No s'ha pogut obrir el fitxer de config per escriure: "
|
||||
<< config_file_path << '\n';
|
||||
std::ofstream file(config_file_path);
|
||||
if (!file.is_open()) {
|
||||
if (console) {
|
||||
std::cerr << "No s'ha pogut obrir el fitxer de config per escriure: "
|
||||
<< config_file_path << '\n';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Escriure manualment per controlar format i comentaris
|
||||
file << "# Orni Attack - Fitxer de Configuració\n";
|
||||
file << "# Auto-generat. Les edicions manuals es preserven si són "
|
||||
"vàlides.\n\n";
|
||||
// Escriure manualment per controlar format i comentaris
|
||||
file << "# Orni Attack - Fitxer de Configuració\n";
|
||||
file << "# Auto-generat. Les edicions manuals es preserven si són "
|
||||
"vàlides.\n\n";
|
||||
|
||||
file << "version: \"" << Project::VERSION << "\"\n\n";
|
||||
file << "version: \"" << Project::VERSION << "\"\n\n";
|
||||
|
||||
file << "# FINESTRA\n";
|
||||
file << "window:\n";
|
||||
file << " width: " << window.width << "\n";
|
||||
file << " height: " << window.height << "\n";
|
||||
file << " fullscreen: " << (window.fullscreen ? "true" : "false") << "\n";
|
||||
file << " size_increment: " << window.size_increment << "\n\n";
|
||||
file << "# FINESTRA\n";
|
||||
file << "window:\n";
|
||||
file << " width: " << window.width << " # Calculated from zoom_factor\n";
|
||||
file << " height: " << window.height << " # Calculated from zoom_factor\n";
|
||||
file << " fullscreen: " << (window.fullscreen ? "true" : "false") << "\n";
|
||||
file << " zoom_factor: " << window.zoom_factor << " # 0.5x-max (0.1 increments)\n\n";
|
||||
|
||||
file << "# FÍSICA (tots els valors en px/s, rad/s, etc.)\n";
|
||||
file << "physics:\n";
|
||||
file << " rotation_speed: " << physics.rotation_speed << " # rad/s\n";
|
||||
file << " acceleration: " << physics.acceleration << " # px/s²\n";
|
||||
file << " max_velocity: " << physics.max_velocity << " # px/s\n";
|
||||
file << " friction: " << physics.friction << " # px/s²\n";
|
||||
file << " enemy_speed: " << physics.enemy_speed
|
||||
<< " # unitats/frame\n";
|
||||
file << " bullet_speed: " << physics.bullet_speed
|
||||
<< " # unitats/frame\n\n";
|
||||
file << "# FÍSICA (tots els valors en px/s, rad/s, etc.)\n";
|
||||
file << "physics:\n";
|
||||
file << " rotation_speed: " << physics.rotation_speed << " # rad/s\n";
|
||||
file << " acceleration: " << physics.acceleration << " # px/s²\n";
|
||||
file << " max_velocity: " << physics.max_velocity << " # px/s\n";
|
||||
file << " friction: " << physics.friction << " # px/s²\n";
|
||||
file << " enemy_speed: " << physics.enemy_speed
|
||||
<< " # unitats/frame\n";
|
||||
file << " bullet_speed: " << physics.bullet_speed
|
||||
<< " # unitats/frame\n\n";
|
||||
|
||||
file << "# GAMEPLAY\n";
|
||||
file << "gameplay:\n";
|
||||
file << " max_enemies: " << gameplay.max_enemies << "\n";
|
||||
file << " max_bullets: " << gameplay.max_bullets << "\n";
|
||||
file << "# GAMEPLAY\n";
|
||||
file << "gameplay:\n";
|
||||
file << " max_enemies: " << gameplay.max_enemies << "\n";
|
||||
file << " max_bullets: " << gameplay.max_bullets << "\n\n";
|
||||
|
||||
file.close();
|
||||
file << "# RENDERITZACIÓ\n";
|
||||
file << "rendering:\n";
|
||||
file << " vsync: " << rendering.vsync << " # 0=disabled, 1=enabled\n\n";
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config guardada a: " << config_file_path << '\n';
|
||||
}
|
||||
file << "# AUDIO\n";
|
||||
file << "audio:\n";
|
||||
file << " enabled: " << (audio.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.volume << " # 0.0 to 1.0\n";
|
||||
file << " music:\n";
|
||||
file << " enabled: " << (audio.music.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.music.volume << " # 0.0 to 1.0\n";
|
||||
file << " sound:\n";
|
||||
file << " enabled: " << (audio.sound.enabled ? "true" : "false") << "\n";
|
||||
file << " volume: " << audio.sound.volume << " # 0.0 to 1.0\n";
|
||||
|
||||
return true;
|
||||
file.close();
|
||||
|
||||
if (console) {
|
||||
std::cout << "Config guardada a: " << config_file_path << '\n';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Options
|
||||
} // namespace Options
|
||||
|
||||
@@ -7,42 +7,65 @@ namespace Options {
|
||||
// Estructures de configuració
|
||||
|
||||
struct Window {
|
||||
int width{640};
|
||||
int height{480};
|
||||
bool fullscreen{false};
|
||||
int size_increment{100}; // Increment per F1/F2
|
||||
int width{640};
|
||||
int height{480};
|
||||
bool fullscreen{false};
|
||||
float zoom_factor{1.0f}; // Zoom level (0.5x to max_zoom)
|
||||
};
|
||||
|
||||
struct Physics {
|
||||
float rotation_speed{3.14f}; // rad/s
|
||||
float acceleration{400.0f}; // px/s²
|
||||
float max_velocity{120.0f}; // px/s
|
||||
float friction{20.0f}; // px/s²
|
||||
float enemy_speed{2.0f}; // unitats/frame
|
||||
float bullet_speed{6.0f}; // unitats/frame
|
||||
float rotation_speed{3.14f}; // rad/s
|
||||
float acceleration{400.0f}; // px/s²
|
||||
float max_velocity{120.0f}; // px/s
|
||||
float friction{20.0f}; // px/s²
|
||||
float enemy_speed{2.0f}; // unitats/frame
|
||||
float bullet_speed{6.0f}; // unitats/frame
|
||||
};
|
||||
|
||||
struct Gameplay {
|
||||
int max_enemies{15};
|
||||
int max_bullets{3};
|
||||
int max_enemies{15};
|
||||
int max_bullets{3};
|
||||
};
|
||||
|
||||
struct Rendering {
|
||||
int vsync{1}; // 0=disabled, 1=enabled
|
||||
};
|
||||
|
||||
struct Music {
|
||||
bool enabled{true};
|
||||
float volume{0.8f};
|
||||
};
|
||||
|
||||
struct Sound {
|
||||
bool enabled{true};
|
||||
float volume{1.0f};
|
||||
};
|
||||
|
||||
struct Audio {
|
||||
Music music{};
|
||||
Sound sound{};
|
||||
bool enabled{true};
|
||||
float volume{1.0f};
|
||||
};
|
||||
|
||||
// Variables globals (inline per evitar ODR violations)
|
||||
|
||||
inline std::string version{}; // Versió del config per validació
|
||||
inline bool console{false}; // Eixida de debug
|
||||
inline std::string version{}; // Versió del config per validació
|
||||
inline bool console{false}; // Eixida de debug
|
||||
inline Window window{};
|
||||
inline Physics physics{};
|
||||
inline Gameplay gameplay{};
|
||||
inline Rendering rendering{};
|
||||
inline Audio audio{};
|
||||
|
||||
inline std::string config_file_path{}; // Establert per setConfigFile()
|
||||
inline std::string config_file_path{}; // Establert per setConfigFile()
|
||||
|
||||
// Funcions públiques
|
||||
|
||||
void init(); // Inicialitzar amb valors per defecte
|
||||
void init(); // Inicialitzar amb valors per defecte
|
||||
void setConfigFile(
|
||||
const std::string &path); // Establir ruta del fitxer de config
|
||||
auto loadFromFile() -> bool; // Carregar config YAML
|
||||
auto saveToFile() -> bool; // Guardar config YAML
|
||||
const std::string& path); // Establir ruta del fitxer de config
|
||||
auto loadFromFile() -> bool; // Carregar config YAML
|
||||
auto saveToFile() -> bool; // Guardar config YAML
|
||||
|
||||
} // namespace Options
|
||||
} // namespace Options
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user