4 Commits

23 changed files with 879 additions and 372 deletions

149
CLAUDE.md
View File

@@ -38,18 +38,25 @@ The five current objectives are:
**Iron rule: zero gameplay regressions.** Each phase of the modernization must leave the game playing identically — same feel, same timings, same collisions, same scoring. See [docs/scenes-migration-plan.md](docs/scenes-migration-plan.md) for the phased plan.
The current emulator-thread architecture (Director + game thread + `publishFrame` mutex/cv) is **transitional**. It will be dismantled in Phase 5 and replaced by a single-threaded `SDL_AppIterate` loop in Phase 7.
### Migration Status (2026-04-16)
Phases 07b of the original runtime plan are **done**. Current effort is the **scene-by-scene rewrite of `source/game/modulesequence.cpp`** over a `scenes::` layer in [source/scenes/](source/scenes/):
**Completat.** Totes les fases del pla original (07) i la migració `scenes::` (Steps 010) estan fetes, ModuleGame és una `scenes::Scene` tick-based, el cooperative fiber s'ha eliminat, i el build emscripten/WASM arrenca i es publica a maverick.
- **Done**: `MortScene` (state 100), `BannerScene` (2..5), `MenuScene` (0), `IntroNewLogoScene` (255 when `use_new_logo`), `SlidesScene` (1, 7), `CreditsScene` (8), `SecretaScene` (6). Each registered in `Director::init` via `SceneRegistry`. Each removed from the legacy `ModuleSequence::Go()` switch and deleted from `modulesequence.cpp`.
- **Pending**: `IntroScene` (state 255 when `!use_new_logo` — the old JAILGAMES letter-by-letter), `IntroSpritesScene` (the Sam + momies animation with 3 random variants, hardest of the lot, currently still called from `IntroNewLogoScene::Phase::Delegate` via a temporary `doIntroSprites` exposed as `public` in `ModuleSequence`). Final cleanup of `modulesequence.cpp` comes after those two.
- `SceneRegistry` lookup happens inside `gameFiberEntry()` before falling back to legacy `ModuleSequence::Go()`, with a redirect `num_piramide == 6 && diners < 200 → 7` replicated ahead of the lookup to match the legacy flow.
- For quick tests, `Options::game` exposes `piramide_inicial`, `habitacio_inicial`, `vides`, `diamants_inicial`, `diners_inicial`, `use_new_logo`, `show_title_credits` — all persisted in `config.yaml`.
**Arquitectura actual**:
- Un sol thread (Director). Main loop via SDL3 Callback API (`SDL_MAIN_USE_CALLBACKS`): `SDL_AppInit/Iterate/Event/Quit` a [main.cpp](source/main.cpp).
- `Director::iterate()` posseeix l'estat d'escena (`current_scene_`, `game_state_`) i fa input → tick de l'escena → `JD8_Flip` (sense yield, només converteix `screen``pixel_data`) → overlay → present. Tot en línia recta, zero fibers, zero mutex.
- Totes les escenes (inclòs `ModuleGame`) implementen `scenes::Scene` amb `onEnter/tick(delta_ms)/done/nextState`.
- `ModuleSequence` (el vell dispatcher) eliminat. Despatxa via `game_state_ == 0` (gameplay → `ModuleGame`) o `game_state_ == 1` (cinemàtica → `SceneRegistry::tryCreate(num_piramide)`).
The scenes layer itself lives in [source/scenes/](source/scenes/): `scene.hpp` (interface), `scene_registry.hpp/.cpp`, `timeline.hpp/.cpp`, `sprite_mover.hpp/.cpp`, `frame_animator.hpp/.cpp`, `palette_fade.hpp/.cpp`, `surface_handle.hpp/.cpp`, `scene_utils.hpp/.cpp` (`playMusic`). Scenes are pure tick-based (no fibers, no `while`, no `JG_ShouldUpdate`) — the cooperative fiber still runs underneath them but `JD8_Flip()` inside the mini-while in `gameFiberEntry` is what yields. Once `IntroScene` + `IntroSpritesScene` are migrated, the fiber can be dismantled along with `ModuleGame`.
**Escenes migrades** (totes registrades a `Director::init` via `SceneRegistry`):
- `MortScene` (state 100) · `BannerScene` (2..5) · `MenuScene` (0) · `SlidesScene` (1, 7)
- `CreditsScene` (8) · `SecretaScene` (6) · `IntroNewLogoScene` (255, `use_new_logo=true`)
- `IntroScene` (255, `use_new_logo=false`) · `IntroSpritesScene` (sub-escena de les dues intros)
**Files d'`Options::game` exposats per a tests ràpids** (persistits a `config.yaml`):
`piramide_inicial`, `habitacio_inicial`, `vides`, `diamants_inicial`, `diners_inicial`, `use_new_logo`, `show_title_credits`.
**La capa `scenes::`** ([source/scenes/](source/scenes/)): `scene.hpp` (interfície), `scene_registry.hpp/.cpp`, `timeline`, `sprite_mover`, `frame_animator`, `palette_fade`, `surface_handle`, `scene_utils` (`playMusic`). Pures tick-based, zero while, zero `JG_ShouldUpdate`.
### Modernization Targets
@@ -94,7 +101,7 @@ Flat C-style APIs (no classes), prefixed by subsystem. Being progressively conve
### System Layer (`source/core/system/`)
- **Director** (`director.hpp/cpp`) — **Orchestrator singleton**. Owns main thread. Launches game thread that runs `ModuleGame`/`ModuleSequence::Go()`. Emulator-style architecture: Director runs at ~60 FPS independently, polls SDL events, updates overlay, presents frames. Game thread blocks at `JD8_Flip()` `Director::publishFrame()` until Director consumes the frame. Director is **non-blocking**: if no new frame is available, it re-presents the last known game frame with fresh overlay on top
- **Director** (`director.hpp/cpp`) — **Orchestrator singleton**, únic thread del runtime. Posseeix l'estat d'escena (`current_scene_: unique_ptr<Scene>`, `game_state_`, `last_tick_ms_`) directament com a members. `iterate()` fa: poll events (via `SDL_AppEvent`) → input (Gamepad/KeyRemap/GlobalInputs/Mouse) → `JA_Update` → transició d'escena si `done()``scene->tick(delta_ms)` `JD8_Flip` (converteix `screen``pixel_data`) → overlay → present → `SDL_Delay` al frame target. Dispatcher: `game_state_ == 0``new ModuleGame`, `game_state_ == 1``SceneRegistry::tryCreate(info::ctx.num_piramide)` (amb redirect `num_piramide == 6 && diners < 200 → 7` replicant el vell `ModuleSequence::Go`).
### Presentation Layer (`source/core/rendering/`)
@@ -142,7 +149,7 @@ Follows the pattern from `jaildoctors_dilemma`, persists to YAML:
| F8 | Cycle shader presets |
| F9 | Toggle stretch filter (nearest ↔ linear) |
| F10 | Cycle render info (off → top → bottom → off) |
| F11 | Toggle pause (Director stops resuming the game fiber + `JA_PauseMusic`/`JA_ResumeMusic`) |
| F11 | Toggle pause (Director skips `scene->tick()` + `JA_PauseMusic`/`JA_ResumeMusic`) |
| F12 | Toggle floating options menu |
| ESC | Double-press to quit (with overlay notification) / close menu if open |
| Backspace | Go up one menu level / close menu if at root |
@@ -150,45 +157,48 @@ Follows the pattern from `jaildoctors_dilemma`, persists to YAML:
All key bindings are configurable via `Options::keys_gui` and stored in `config.yaml` (section `controls:` with SDL scancode names). Game movement keys (`Options::keys_game.up/down/left/right`) can be remapped via the CONTROLS submenu — the `KeyRemap` module mirrors custom physical keys to virtual standard scancodes so hardcoded game code keeps working.
### Execution Model (Single-threaded Fibers)
### Execution Model (Single-threaded, Scene-based)
Since Phase 4+5, the old game thread + `publishFrame` mutex/cv has been **removed**. The game code (`ModuleGame`, `ModuleSequence`, all their `Go()` methods with internal `while` loops) runs inside a **cooperative fiber** (see [fiber.hpp](source/core/system/fiber.hpp) / [fiber.cpp](source/core/system/fiber.cpp)). The whole process is single-threaded.
Zero threads, zero fibers, zero mutex. Main loop via SDL3 Callback API (`SDL_MAIN_USE_CALLBACKS`) a [main.cpp](source/main.cpp). Cada frame entra pel `SDL_AppIterate``Director::iterate()`:
```
Main thread (only thread)
─────────────────────────
Director::run() loop {
SDL_PollEvent()
GlobalInputs, Mouse, KeyRemap
SDL_AppIterate → Director::iterate() {
if (quit_requested_) { scene.reset(); return false; }
if (!context_initialized_) initGameContext();
Gamepad/KeyRemap/GlobalInputs/Mouse::update
JA_Update() ← audio pump
if !paused:
GameFiber::resume() ← hands control to game code
↓ (runs until next JD8_Flip)
... game code runs ...
JD8_Flip():
palette → ARGB → pixel_data
GameFiber::yield() ← returns control to Director
copy JD8_GetFramebuffer() → game_frame
if (!paused_) {
if (scene && (scene->done() || JG_Quitting()))
game_state_ = scene->nextState(); scene.reset();
if (!scene) {
if (game_state_ == -1 || JG_Quitting()) return false;
scene = createNextScene(); ← ModuleGame o registry.tryCreate()
scene->onEnter();
}
JI_Update()
scene->tick(now - last_tick_ms_)
JD8_Flip() ← converteix screen indexat → pixel_data
memcpy pixel_data → game_frame
}
memcpy game_frame → presentation_buffer
Overlay::render(presentation_buffer)
Screen::present(presentation_buffer)
SDL_Delay to hit 60fps
SDL_Delay(frame_target - elapsed)
}
SDL_AppEvent → Director::handleEvent() ← events lliurats un a un per SDL
SDL_AppQuit → Director::teardown() ← Options::save + descàrrega ordenada
```
**Fiber backend** ([fiber.cpp](source/core/system/fiber.cpp)):
- **Linux / macOS**: `ucontext_t` + `makecontext`/`swapcontext` (deprecated in POSIX.1-2008 but still functional in glibc and macOS libc; warning silenced with `#pragma`).
- **Windows**: `ConvertThreadToFiber` / `CreateFiber` / `SwitchToFiber` (native Fibers API).
- **Emscripten**: not yet. Phase 7 will add an `emscripten_fiber_*` or Asyncify backend.
**Key points:**
- Single-threaded: zero `std::thread`, zero `std::mutex`, zero `std::condition_variable`.
- `JD8_Flip()` is the natural sync point: it calls `GameFiber::yield()` instead of the old blocking `publishFrame`.
- Pause (F11) works by Director skipping `resume()`: the fiber stays frozen at its last yield, and Director keeps repainting the last frame with fresh overlay.
- Double buffer still exists (`game_frame` + `presentation_buffer`) because Director can present multiple frames per game frame during pause or slow sections. Eliminating it is marginal work and the extra 256 KB copy is cheap at 320×200.
- The state machine alternating `ModuleSequence` (state=1) and `ModuleGame` (state=0) now lives in `gameFiberEntry()` inside an anonymous namespace in [director.cpp](source/core/system/director.cpp), called once as the fiber entry point.
- SDL events still processed only on the main thread (which is now the only thread anyway).
- `Director` posseeix `current_scene_`, `game_state_`, `last_tick_ms_`, `context_initialized_` com a members — abans vivien al stack del fiber.
- `JD8_Flip()` només converteix paleta + `screen` a ARGB (`pixel_data`). Ja **no fa yield** — tot corre lineal.
- `JG_ShouldUpdate()` encara existeix a `jgame.cpp` com a timing-gate per a `ModuleGame::Update()` (10 ms fix), però ja no fa yield. Cap caller fa spin-wait.
- Pausa (F11) simplement salta el bloc de tick; overlay i present continuen, es re-presenta l'últim frame congelat.
- Doble buffer (`game_frame` + `presentation_buffer`) es manté perquè el Director pot presentar múltiples frames per cada tick durant pausa; el cost (256 KB memcpy) és trivial a 320×200.
- SDL3 Callback API compatible amb emscripten: el navegador posseeix el main loop i ens crida via `requestAnimationFrame`. Zero canvis de codi per a portabilitat.
### Rendering Pipeline (inside Screen::present)
@@ -218,6 +228,44 @@ JD8_Flip produces ABGR byte order: `0xFF000000 + R + (G<<8) + (B<<16)`. SDL text
| `~/.config/jailgames/aee/postfx.yaml` | PostFX shader presets (6 defaults: CRT, NTSC, CURVED, SCANLINES, SUBTLE, CRT LIVE) |
| `~/.config/jailgames/aee/crtpi.yaml` | CRT-Pi shader presets (4 defaults: DEFAULT, CURVED, SHARP, MINIMAL) |
### Resource Pack (`source/core/resources/`) — **en construcció**
Sistema d'empaquetat d'assets a l'estil `coffee_crisis_arcade_edition`. Genera un sol fitxer binari opac `resource.pack` que substitueix la carpeta `data/` als releases natius.
**Format AEE1** (fidel a CCAE amb clau pròpia):
```
Header: "AEE1" (4B) + version uint32 + resource_count uint32
Index: per recurs → filename_len uint32 + filename + offset uint64 + size uint64 + checksum uint32
Payload: data_size uint64 + bytes XOR-xifrats amb "AEE_RESOURCES__2026"
```
Checksum: djb2-like amb seed `0x12345678`. Càrrega full-to-RAM (sense mmap).
**Fitxers**:
- [source/core/resources/resource_pack.hpp/cpp](source/core/resources/) — classe `ResourcePack`: `loadPack`, `savePack`, `addFile`, `addDirectory`, `getResource(name) → std::vector<uint8_t>`, `hasResource`
- [source/core/resources/resource_helper.hpp/cpp](source/core/resources/) — namespace `ResourceHelper`: `initializeResourceSystem(pack, enable_fallback)`, `loadFile(relative_path)`, `shutdownResourceSystem`. Prova el pack primer, cau a `file_getresourcefolder()+path` si el fallback està actiu.
- [tools/pack_resources/pack_resources.cpp](tools/pack_resources/pack_resources.cpp) — eina standalone CLI: `pack_resources [input_dir=data] [output=resource.pack]` + `--list pack`.
**Build**:
- `make pack` compila l'eina (target `pack_resources` a `EXCLUDE_FROM_ALL` de [CMakeLists.txt](CMakeLists.txt)) i genera `resource.pack` a la rel. 33 entrades ≈ 4 MB.
- `./build/pack_resources --list resource.pack` inspecciona el pack.
**Estat actual (Fase 1 completada, 2026-04-16)**:
- Classes + eina compilen i funcionen. El pack es genera correctament i el `--list` mostra les 33 entrades (totes les GIFs, OGGs, font `.fnt`+`.gif`, `locale/ca.yaml`, `shaders/*`).
- **Encara no està cablejat al joc**: cap callsite usa `ResourceHelper::loadFile`. Tots segueixen cridant `file_readfile`. El joc funciona exactament igual que abans.
- Nou getter `file_getresourcefolder()` afegit a [jfile.hpp](source/core/jail/jfile.hpp) perquè ResourceHelper puga construir el path del fallback.
**Pendent (Fases 2-6 del pla [.claude/plans/declarative-popping-breeze.md](/home/sergio/.claude/plans/declarative-popping-breeze.md))**:
1. **Fase 2** — Afegir `ResourceHelper::initializeResourceSystem(pack_path, enable_fallback=true)` a [main.cpp](source/main.cpp) just després de `file_setresourcefolder`, i `shutdownResourceSystem()` a `SDL_AppQuit`.
2. **Fase 3** — Migrar callsites `file_readfile``ResourceHelper::loadFile` (tipus canvia `std::vector<char>``std::vector<uint8_t>`):
- [locale.cpp:30](source/core/locale/locale.cpp#L30)
- [text.cpp:65, 129](source/core/rendering/text.cpp) (`.fnt` + bitmap)
- [scene_utils.cpp:12](source/scenes/scene_utils.cpp) (música escenes)
- [modulegame.cpp:52](source/game/modulegame.cpp) (música gameplay)
- [jdraw8.cpp:47, 65](source/core/jail/jdraw8.cpp) (`JD8_LoadSurface`, `JD8_LoadPalette`)
3. **Fase 4** — Eliminar scaffold `.jrf` de [jfile.cpp](source/core/jail/jfile.cpp): `file_setresourcefilename`, `file_setsource`, `SOURCE_FILE`/`SOURCE_FOLDER`, `dictionary_loaded()`, `file_getfilepointer()`, `file_readfile()`. Mantenir només config-folder + `file_setresourcefolder` + `file_getresourcefolder`.
4. **Fase 5** — Als targets release de [Makefile](Makefile) (`_linux_release`/`_windows_release`/`_macos_release`): afegir dependència `pack` i canviar `cp -r data``cp resource.pack`. WASM intacte (segueix usant `--preload-file data@/data`).
5. **Fase 6** — A [main.cpp](source/main.cpp): `enable_fallback = false` només per a `NDEBUG && !__EMSCRIPTEN__` (pack obligatori a Release natiu; Debug i WASM mantenen fallback).
### External Libraries (`source/external/`)
- `gif.h` — Header-only GIF decoder. **Cannot be included from more than one .cpp** (no include guards on functions). Other files use `extern` declarations for `LoadGif()`
@@ -238,12 +286,29 @@ JD8_Flip produces ABGR byte order: `0xFF000000 + R + (G<<8) + (B<<16)`. SDL text
2. ~~**Cheats are broken (`reviu`, `alone`, `obert`)**~~: Fixed. `JI_moveCheats` tradueix `SDL_Scancode` → ASCII via `scancode_to_ascii` abans de ficar-los al buffer ([jinput.cpp:32-37, 55-61](source/core/jail/jinput.cpp#L32-L61)), i `JI_CheatActivated` compara ASCII amb ASCII.
3. **No sound effects in game**: Game code never calls `JA_PlaySound*`/`JA_LoadSound` — only music via `JA_PlayMusic`/`JA_FadeOutMusic`. The SONS and VOL SONS menu items control volume of an empty channel pool. Infrastructure ready for future SFX.
4. ~~**Raw `malloc`/`free` in gameplay structs**~~: Majoritàriament arreglat. `Sprite`/`Entitat` usen `std::vector<Frame>` i `std::vector<Animacio>` ([sprite.hpp](source/game/sprite.hpp)). `jfile.cpp` ja no té el global `scratch[255]` (substituït per `thread_local std::string`). L'API `file_getfilebuffer` (que tornava raw `char*` amb `malloc`) s'ha substituït per `file_readfile` que retorna `std::vector<char>` RAII — elimina els leaks silenciosos que hi havia a `JD8_LoadPalette`, `ModuleGame::Go()` i `scenes::playMusic`. Queda `jail_audio.hpp` barrejant `new`/`malloc`/`SDL_malloc` de forma pairada i correcta (no leak), pendent de polir amb `std::unique_ptr` quan toque.
5. ~~**Blocking loops in cinematics and fades**~~: Fixed. La migració completa de `ModuleSequence::do*()` a la capa `scenes::` (Steps 19) ha eliminat tots els `while(!JG_ShouldUpdate())` i `wait_frame_or_skip()`. Les cinemàtiques ara són tick-based amb acumuladors ms. `JD8_FadeOut`/`JD8_FadeToPal` encara tenen el seu bucle intern de 32 passos (usat per a transicions fora d'escena com al final de `ModuleGame`); el wrapper tick-based `scenes::PaletteFade` el consumeix un pas per tick quan es crida des d'una escena.
6. ~~**`SDL_AddTimer` in audio**~~: Fixed in Phase 3. `jail_audio` is now a header-only `inline` module (single `.cpp` stub only hosts the `stb_vorbis` implementation to avoid multiple definitions). Music uses true streaming via `stb_vorbis_open_memory` + `JA_PumpMusic` with a 0.5s low-water-mark instead of decoding the whole OGG into RAM. Mixing/fade update runs manually via `JA_Update()` called once per frame from `Director::run()`. Ported from the `jaildoctors_dilemma` codebase.
7. ~~**Game thread + `publishFrame` mutex/cv**~~: Fixed in Phase 4+5. Replaced by a cooperative `GameFiber` (ucontext on POSIX, Fibers API on Windows). `JD8_Flip()` calls `GameFiber::yield()`, Director calls `GameFiber::resume()` once per frame. Zero threads, zero mutexes. Emscripten fiber backend still pending for Phase 7.
5. ~~**Blocking loops in cinematics and fades**~~: Fixed. Migració completa de `ModuleSequence::do*()` a la capa `scenes::` (Steps 110) + `ModuleGame` també tick-based (Phase A). Tot `while(!JG_ShouldUpdate())` i `wait_frame_or_skip()` eliminat. Els fades bloquejants `JD8_FadeOut`/`JD8_FadeToPal` també eliminats (Phase B.2): només queda l'API tick-step `JD8_FadeStart*` + `JD8_FadeTickStep`, encapsulada pel wrapper `scenes::PaletteFade`. ModuleGame té fases `FadingIn`/`FadingOut` pròpies.
6. ~~**`SDL_AddTimer` in audio**~~: Fixed in Phase 3. `jail_audio` is now a header-only `inline` module (single `.cpp` stub only hosts the `stb_vorbis` implementation to avoid multiple definitions). Music uses true streaming via `stb_vorbis_open_memory` + `JA_PumpMusic` with a 0.5s low-water-mark instead of decoding the whole OGG into RAM. Mixing/fade update runs manually via `JA_Update()` called once per frame from `Director::iterate()`. Ported from the `jaildoctors_dilemma` codebase.
7. ~~**Game thread + `publishFrame` mutex/cv**~~: Fixed in Phase 4+5 via cooperative `GameFiber`; **eliminated entirely in Phase B.2**. `JD8_Flip()` ja no fa yield — només converteix `screen``pixel_data`. Director posseeix l'estat d'escena (`current_scene_`, `game_state_`) i crida `scene->tick()` directament des d'`iterate()`. Fitxers `source/core/system/fiber.{hpp,cpp}` esborrats. Zero threads, zero mutex, zero fibers.
8. ~~**`ModuleSequence` legacy dispatcher**~~: Eliminated in Step 10. Era el vell switch per `num_piramide`, ara substituït per `SceneRegistry::tryCreate()` i dispatch directe des de `Director::iterate()`. `modulesequence.{hpp,cpp}` esborrats.
### WebAssembly Build
`make wasm` genera el build WASM via Docker (`emscripten/emsdk:latest`) i copia els 3 fitxers (`.js`/`.wasm`/`.data`) a `maverick:/home/sergio/gitea/web_jailgames/static/games/aee/wasm/`, amb un `ssh maverick './deploy.sh'` final. Output local a `dist/wasm/`.
**Diferències respecte build natiu** (a [CMakeLists.txt](CMakeLists.txt) dins `if(EMSCRIPTEN)`):
- SDL3 compilat des de font via `FetchContent` (no hi ha paquet de sistema).
- Shaders SPIR-V omesos (SDL3 GPU no suportat a WebGL2).
- `sdl3gpu_shader.cpp` exclòs dels sources — el fallback `SDL_Renderer` fa tota la presentació.
- [screen.cpp](source/core/rendering/screen.cpp) guarda `#ifndef NO_SHADERS` al voltant de l'include i les crides a `SDL3GPUShader` directes. La resta del codi va via interfície base `ShaderBackend`.
- Link flags: `--preload-file data@/data`, `-fexceptions`, `-sALLOW_MEMORY_GROWTH=1`, `-sMAX_WEBGL_VERSION=2`, `-sINITIAL_MEMORY=67108864`, `-sASSERTIONS=1`, `-sASYNCIFY=1`.
- Defines: `EMSCRIPTEN_BUILD`, `NO_SHADERS`.
**Filesystem**: MEMFS default — no persistent entre recàrregues. `file_setconfigfolder` té fallbacks robustos (`getpwuid``getenv("HOME")``/tmp`) perquè no pete quan emscripten no té `/etc/passwd`. La config es carrega per defecte cada vegada. IDBFS pendent si mai volguéssem persistència a web.
### Pending / Ideas for Later
- **Sound effects**: infraestructura `JA_PlaySound*`/`JA_LoadSound` ja preparada. Cablejar events de gameplay (col·lisió momia, mort, recollir objecte, trencar tomba, cheat activat). Menus SONS/VOL SONS ja controlen el volum del pool.
- **IDBFS persistence a WASM**: montar `/home/web_user/.config` com a IDBFS a l'init i `FS.syncfs` després de cada save. Opcional — ara per ara la config no persistix entre recàrregues de pàgina.
- **Gamepad**: map Y button (North) to `P` key for Pepe character selection at title screen (only input path not covered by current mapping).
- **Menu items for game**: habitacio_inicial, piramide_inicial, vides (already in `Options::game`, not exposed).
- **Multi-language**: add `data/locale/es.yaml` / `en.yaml` and a language selector item in menu. `Locale::load()` already handles arbitrary files.
@@ -251,6 +316,8 @@ JD8_Flip produces ABGR byte order: `0xFF000000 + R + (G<<8) + (B<<16)`. SDL text
- **Notification persistence**: notifications clear on each new one (`showNotification` does `notifications_.clear()`). Could queue instead.
- **FPS counter jitter**: time segment width changes per frame (100 Hz centi updates) causes ~1-2 px horizontal jitter in centered layout. Could lock to max-width or use monospace digits.
- **Notification messages partially hardcoded**: overlay/global_inputs/director now use Locale, but the window title (`Texts::WINDOW_TITLE`) and some game-layer strings remain hardcoded.
- **jail_audio `JA_Sound_t` RAII**: `JA_Music_t` ja està net (vector + string), però `JA_Sound_t` encara usa `Uint8*` via `SDL_LoadWAV` out-param. Petit polish per a completar la coherència RAII.
- **Resource Pack — Fases 2-6**: la classe `ResourcePack`, `ResourceHelper` i l'eina `pack_resources` ja estan fetes (Fase 1). Queda cablejar-ho al joc: init a `main.cpp`, migrar 5 callsites de `file_readfile` a `ResourceHelper::loadFile`, eliminar l'scaffold `.jrf` de `jfile`, integrar `resource.pack` als bundles release, i flip `enable_fallback=false` per a Release natiu. Detall complet a la secció *Resource Pack* i al pla [.claude/plans/declarative-popping-breeze.md](/home/sergio/.claude/plans/declarative-popping-breeze.md).
### Previously Fixed (kept for reference)

View File

@@ -22,6 +22,10 @@ set(APP_SOURCES
# Core - Locale (nova capa)
source/core/locale/locale.cpp
# Core - Resources (pack binari AEE1, estil coffee_crisis)
source/core/resources/resource_pack.cpp
source/core/resources/resource_helper.cpp
# Core - Capa de presentación (nueva)
source/core/rendering/menu.cpp
source/core/rendering/overlay.cpp
@@ -39,7 +43,6 @@ set(APP_SOURCES
# Core - System (nova capa)
source/core/system/director.cpp
source/core/system/fiber.cpp
# Scenes (cinemàtiques i menús reescrits)
source/scenes/timeline.cpp
@@ -81,8 +84,22 @@ set(APP_SOURCES
# Configuración de SDL3
# En macOS bundle mode usamos el xcframework (universal arm64+x86_64).
# En el resto de casos, o en macOS sin bundle, usamos SDL3 del sistema via find_package.
if(APPLE AND MACOS_BUNDLE)
# En emscripten compilamos SDL3 desde source con FetchContent (no hi ha paquet de sistema).
# En el resto de casos, usamos SDL3 del sistema via find_package.
if(EMSCRIPTEN)
include(FetchContent)
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.4.4
GIT_SHALLOW TRUE
)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(SDL3)
message(STATUS "SDL3: compilat des de source per a Emscripten (FetchContent)")
elseif(APPLE AND MACOS_BUNDLE)
set(SDL3_XCFRAMEWORK_SLICE "${CMAKE_SOURCE_DIR}/release/macos/frameworks/SDL3.xcframework/macos-arm64_x86_64")
message(STATUS "SDL3: usando xcframework (${SDL3_XCFRAMEWORK_SLICE})")
else()
@@ -90,8 +107,8 @@ else()
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
endif()
# --- COMPILACIÓ SHADERS SPIR-V (Linux/Windows — macOS usa Metal) ---
if(NOT APPLE)
# --- COMPILACIÓ SHADERS SPIR-V (Linux/Windows — macOS usa Metal, Emscripten no suporta SDL3 GPU) ---
if(NOT APPLE AND NOT EMSCRIPTEN)
find_program(GLSLC_EXE NAMES glslc)
set(SHADERS_DIR "${CMAKE_SOURCE_DIR}/data/shaders")
@@ -138,15 +155,25 @@ if(NOT APPLE)
endforeach()
message(STATUS "glslc no trobat — usant headers SPIR-V precompilats")
endif()
elseif(EMSCRIPTEN)
message(STATUS "Emscripten: shaders SPIR-V omesos (SDL3 GPU no suportat a WebGL2)")
else()
message(STATUS "macOS: shaders SPIR-V omesos (usa Metal)")
endif()
# --- EJECUTABLE ---
# A emscripten excloïm sdl3gpu_shader.cpp — SDL3 GPU no suporta WebGL2, i el
# fallback SDL_Renderer de Screen (amb NO_SHADERS) fa tota la presentació.
if(EMSCRIPTEN)
set(APP_SOURCES_WASM ${APP_SOURCES})
list(REMOVE_ITEM APP_SOURCES_WASM source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp)
add_executable(${PROJECT_NAME} ${APP_SOURCES_WASM})
else()
add_executable(${PROJECT_NAME} ${APP_SOURCES})
endif()
# Shaders han de compilar-se abans que l'executable (Linux/Windows amb glslc)
if(NOT APPLE AND GLSLC_EXE)
if(NOT APPLE AND NOT EMSCRIPTEN AND GLSLC_EXE)
add_dependencies(${PROJECT_NAME} shaders)
endif()
@@ -177,10 +204,44 @@ target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunctio
# --- CONFIGURACIÓN POR PLATAFORMA ---
if(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE mingw32)
elseif(EMSCRIPTEN)
target_compile_definitions(${PROJECT_NAME} PRIVATE EMSCRIPTEN_BUILD NO_SHADERS)
# -fexceptions: SDL3 i fkyaml llancen std::exception; sense això, `throw`
# acaba en `abort()`. També requerit al link per congruència ABI.
target_compile_options(${PROJECT_NAME} PRIVATE -fexceptions)
target_link_options(${PROJECT_NAME} PRIVATE
"SHELL:--preload-file ${CMAKE_SOURCE_DIR}/data@/data"
-fexceptions
-sALLOW_MEMORY_GROWTH=1
-sMAX_WEBGL_VERSION=2
-sINITIAL_MEMORY=67108864
-sASSERTIONS=1
# ASYNCIFY permet que Emscripten gestione yields durant la precarga
# d'assets. El main loop del joc ja usa SDL3 Callback API i no depén
# d'Asyncify — però el preloader del `.data` sí.
-sASYNCIFY=1
)
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
endif()
# Ejecutable en la raíz del proyecto
# Ejecutable en la raíz del proyecto (solo nativos). A Emscripten queda dins build/.
if(NOT EMSCRIPTEN)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()
# --- EINA STANDALONE: pack_resources ---
# Executable auxiliar que empaqueta `data/` a `resource.pack` (format AEE1).
# No es compila per defecte (EXCLUDE_FROM_ALL). Build explícit:
# cmake --build build --target pack_resources
# Després executar: ./build/pack_resources data resource.pack
if(NOT EMSCRIPTEN)
add_executable(pack_resources EXCLUDE_FROM_ALL
tools/pack_resources/pack_resources.cpp
source/core/resources/resource_pack.cpp
)
target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source")
target_compile_options(pack_resources PRIVATE -Wall)
endif()
# --- CLANG-FORMAT TARGETS ---
find_program(CLANG_FORMAT_EXE NAMES clang-format)

View File

@@ -76,6 +76,12 @@ debug:
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
@cmake --build build
# Empaqueta data/ a resource.pack (format AEE1). Build previ de l'eina + execució.
pack:
@cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
@cmake --build build --target pack_resources
@./build/pack_resources data resource.pack
# ==============================================================================
# RELEASE AUTOMÁTICO (detecta SO)
# ==============================================================================
@@ -217,6 +223,32 @@ _macos_release:
$(RMDIR) build/arm
$(RMFILE) "$(DIST_DIR)"/rw.*
# ==============================================================================
# COMPILACIÓN PARA WEBASSEMBLY (requiere Docker amb emscripten/emsdk)
# ==============================================================================
# Genera aee.{html,js,wasm,data} a dist/wasm/. Es pot provar servint amb un
# servidor HTTP local (els navegadors no carreguen `file://` WASM):
# cd dist/wasm && python3 -m http.server 8000
# # després obrir http://localhost:8000/aee.html
wasm:
@echo "Creando release para WebAssembly - Version: $(VERSION)"
docker run --rm \
--user $(shell id -u):$(shell id -g) \
-v $(DIR_ROOT):/src \
-w /src \
emscripten/emsdk:latest \
bash -c "emcmake cmake -S . -B build/wasm -DCMAKE_BUILD_TYPE=Release && cmake --build build/wasm"
@$(MKDIR) "$(DIST_DIR)/wasm"
@cp build/wasm/$(TARGET_NAME).html $(DIST_DIR)/wasm/
@cp build/wasm/$(TARGET_NAME).js $(DIST_DIR)/wasm/
@cp build/wasm/$(TARGET_NAME).wasm $(DIST_DIR)/wasm/
@cp build/wasm/$(TARGET_NAME).data $(DIST_DIR)/wasm/
@echo "Output: $(DIST_DIR)/wasm/$(TARGET_NAME).html"
scp $(DIST_DIR)/wasm/$(TARGET_NAME).js $(DIST_DIR)/wasm/$(TARGET_NAME).wasm $(DIST_DIR)/wasm/$(TARGET_NAME).data \
maverick:/home/sergio/gitea/web_jailgames/static/games/aee/wasm/
ssh maverick 'cd /home/sergio/gitea/web_jailgames && ./deploy.sh'
@echo "Deployed to maverick"
# ==============================================================================
# COMPILACIÓN PARA LINUX (RELEASE)
# ==============================================================================
@@ -247,4 +279,4 @@ _linux_release:
# Elimina la carpeta temporal
$(RMDIR) "$(RELEASE_FOLDER)"
.PHONY: all debug release _windows_release _linux_release _macos_release
.PHONY: all debug pack release wasm _windows_release _linux_release _macos_release

View File

BIN
resource.pack Normal file

Binary file not shown.

View File

@@ -7,12 +7,22 @@
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <string>
#include <vector>
#define STB_VORBIS_HEADER_ONLY
#include "external/stb_vorbis.h"
// Deleter stateless per a buffers reservats amb `SDL_malloc` / `SDL_LoadWAV*`.
// Compatible amb `std::unique_ptr<Uint8[], SDLFreeDeleter>` — zero size
// overhead gràcies a EBO, igual que un unique_ptr amb default_delete.
struct SDLFreeDeleter {
void operator()(Uint8* p) const noexcept {
if (p) SDL_free(p);
}
};
// --- Public Enums ---
enum JA_Channel_state {
JA_CHANNEL_INVALID,
@@ -36,7 +46,9 @@ enum JA_Music_state {
struct JA_Sound_t {
SDL_AudioSpec spec{SDL_AUDIO_S16, 2, 48000};
Uint32 length{0};
Uint8* buffer{nullptr};
// Buffer descomprimit (PCM) propietat del sound. Reservat per SDL_LoadWAV
// via SDL_malloc; el deleter `SDLFreeDeleter` allibera amb SDL_free.
std::unique_ptr<Uint8[], SDLFreeDeleter> buffer;
};
struct JA_Channel_t {
@@ -172,7 +184,7 @@ inline void JA_Update() {
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);
SDL_PutAudioStreamData(channels[i].stream, channels[i].sound->buffer.get(), channels[i].sound->length);
if (channels[i].times > 0) channels[i].times--;
}
} else {
@@ -355,31 +367,26 @@ inline void JA_EnableMusic(const bool 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;
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)) {
auto sound = std::make_unique<JA_Sound_t>();
Uint8* raw = nullptr;
if (!SDL_LoadWAV_IO(SDL_IOFromMem(buffer, size), 1, &sound->spec, &raw, &sound->length)) {
SDL_Log("Failed to load WAV from memory: %s", SDL_GetError());
delete sound;
return nullptr;
}
return sound;
sound->buffer.reset(raw); // adopta el SDL_malloc'd buffer
return sound.release();
}
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)) {
auto sound = std::make_unique<JA_Sound_t>();
Uint8* raw = nullptr;
if (!SDL_LoadWAV(filename, &sound->spec, &raw, &sound->length)) {
SDL_Log("Failed to load WAV file: %s", SDL_GetError());
delete sound;
return nullptr;
}
return sound;
sound->buffer.reset(raw); // adopta el SDL_malloc'd buffer
return sound.release();
}
inline int JA_PlaySound(JA_Sound_t* sound, const int loop = 0, const int group = 0) {
@@ -411,7 +418,7 @@ inline int JA_PlaySoundOnChannel(JA_Sound_t* sound, const int channel, const int
return -1;
}
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer, channels[channel].sound->length);
SDL_PutAudioStreamData(channels[channel].stream, channels[channel].sound->buffer.get(), channels[channel].sound->length);
SDL_SetAudioStreamGain(channels[channel].stream, JA_soundVolume[group]);
SDL_BindAudioStream(sdlAudioDevice, channels[channel].stream);
@@ -423,7 +430,7 @@ inline void JA_DeleteSound(JA_Sound_t* sound) {
for (int i = 0; i < JA_MAX_SIMULTANEOUS_CHANNELS; i++) {
if (channels[i].sound == sound) JA_StopChannel(i);
}
SDL_free(sound->buffer);
// buffer es destrueix automàticament via RAII (SDLFreeDeleter).
delete sound;
}

View File

@@ -3,7 +3,6 @@
#include <fstream>
#include "core/jail/jfile.hpp"
#include "core/system/fiber.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
@@ -151,16 +150,16 @@ void JD8_BlitCKToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int
}
void JD8_Flip() {
// Converteix el framebuffer indexat (paletted) a ARGB (pixel_data).
// El Director crida aquesta funció després del tick de cada escena
// per preparar el frame abans de presentar-lo. Ja no fa yield —
// tot corre en un sol thread sense fibers des de Phase B.2.
for (int x = 0; x < 320; x++) {
for (int y = 0; y < 200; y++) {
Uint32 color = 0xFF000000 + main_palette[screen[x + (y * 320)]].r + (main_palette[screen[x + (y * 320)]].g << 8) + (main_palette[screen[x + (y * 320)]].b << 16);
pixel_data[x + (y * 320)] = color;
}
}
// Cedeix el control al Director. Quan Director::run() ens torne a fer
// resume(), continuarem just ací i el joc continuarà amb la següent
// iteració del seu loop sense bloquejos de mutex/cv.
GameFiber::yield();
}
Uint32* JD8_GetFramebuffer() {
@@ -254,20 +253,9 @@ bool JD8_FadeTickStep() {
return false;
}
void JD8_FadeOut() {
JD8_FadeStartOut();
while (true) {
const bool done = JD8_FadeTickStep();
JD8_Flip();
if (done) break;
}
}
void JD8_FadeToPal(JD8_Palette pal) {
JD8_FadeStartToPal(pal);
while (true) {
const bool done = JD8_FadeTickStep();
JD8_Flip();
if (done) break;
}
}
// Els shims bloquejants `JD8_FadeOut` i `JD8_FadeToPal` han estat
// eliminats a Phase B.2: feien un bucle de 32 iteracions amb `JD8_Flip`
// entre cada una que només funcionava mentre l'entorn tenia fibers i
// `JD8_Flip` cedia el control al Director. Ara tot fade es fa tick a
// tick via `scenes::PaletteFade` (que encapsula `JD8_FadeStartOut` /
// `JD8_FadeStartToPal` + `JD8_FadeTickStep`).

View File

@@ -40,10 +40,9 @@ void JD8_BlitCKScroll(int y, JD8_Surface surface, int sx, int sy, int sh, Uint8
void JD8_BlitCKToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int sw, int sh, JD8_Surface dest, Uint8 colorkey);
// Converteix la pantalla indexada a ARGB i cedeix el control al Director
// (GameFiber::yield). El Director llegirà el framebuffer convertit via
// JD8_GetFramebuffer() i tornarà a cridar Fiber::resume() quan toque el
// pròxim frame.
// Converteix la pantalla indexada a ARGB. El Director crida aquesta
// funció al final de cada tick i després llegeix el framebuffer via
// JD8_GetFramebuffer() per presentar-lo.
void JD8_Flip();
// Accés al framebuffer ARGB de 320x200 actualitzat per l'última crida a
@@ -58,16 +57,13 @@ void JD8_PutPixel(JD8_Surface surface, int x, int y, Uint8 pixel);
void JD8_SetPaletteColor(Uint8 index, Uint8 r, Uint8 g, Uint8 b);
// Fades legacy bloquejants (shim damunt la màquina d'estats de sota).
void JD8_FadeOut();
void JD8_FadeToPal(JD8_Palette pal);
// API de fade no bloquejant (màquina d'estats). `FadeStart*` inicia el
// fade; `FadeTickStep` aplica un pas i retorna `true` quan el fade ha
// acabat. Un pas correspon visualment a una iteració del fade original
// (32 passos en total). El caller és responsable de fer el Flip entre
// passos si el vol veure animat. `FadeIsActive` permet saber si hi ha
// un fade en curs per a enllaçar-lo amb un altre subsistema.
// L'embolcall `scenes::PaletteFade` ho fa més idiomàtic per a escenes.
void JD8_FadeStartOut();
void JD8_FadeStartToPal(JD8_Palette pal);
bool JD8_FadeTickStep();

View File

@@ -105,6 +105,10 @@ void file_setresourcefolder(const char* str) {
resource_folder = str;
}
const char* file_getresourcefolder() {
return resource_folder.c_str();
}
void file_setsource(const int src) {
file_source = src % 2;
if (src == SOURCE_FOLDER && resource_folder.empty()) file_setresourcefolder(DEFAULT_FOLDER);
@@ -161,19 +165,32 @@ std::vector<char> file_readfile(const char* resourcename) {
// Accepta rutes amb subdirectoris (ex: "jailgames/aee") i crea tota la jerarquia.
void file_setconfigfolder(const char* foldername) {
#ifdef _WIN32
config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
const char* base = getenv("APPDATA");
if (!base) base = "C:/";
config_folder = std::string(base) + "/" + foldername;
#elif __APPLE__
struct passwd* pw = getpwuid(getuid());
const char* homedir = pw->pw_dir;
const char* homedir = (pw && pw->pw_dir) ? pw->pw_dir : nullptr;
if (!homedir) homedir = getenv("HOME");
if (!homedir) homedir = "/tmp";
config_folder = std::string(homedir) + "/Library/Application Support/" + foldername;
#elif __linux__
// Nota emscripten: `__linux__` també està definit, però `getpwuid` no
// troba cap /etc/passwd al MEMFS i retorna nullptr. Amb els fallbacks
// HOME → /tmp evitem crashejar al primer arranque dins del navegador.
// La config no persistirà entre recàrregues de la pàgina (MEMFS és
// volàtil); caldria IDBFS si volguéssem persistència a web.
struct passwd* pw = getpwuid(getuid());
const char* homedir = pw->pw_dir;
const char* homedir = (pw && pw->pw_dir) ? pw->pw_dir : nullptr;
if (!homedir) homedir = getenv("HOME");
if (!homedir) homedir = "/tmp";
config_folder = std::string(homedir) + "/.config/" + foldername;
#endif
if (!config_folder.empty()) {
std::filesystem::create_directories(config_folder);
}
}
const char* file_getconfigfolder() {
thread_local std::string folder;

View File

@@ -11,6 +11,7 @@ const char* file_getconfigfolder();
void file_setresourcefilename(const char* str);
void file_setresourcefolder(const char* str);
const char* file_getresourcefolder();
void file_setsource(const int src);
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);

View File

@@ -1,7 +1,5 @@
#include "core/jail/jgame.hpp"
#include "core/system/fiber.hpp"
namespace {
bool quitting = false;
@@ -41,12 +39,9 @@ bool JG_ShouldUpdate() {
cycle_counter++;
return true;
}
// Encara no toca update: cedim el control al Director per a que puga
// processar events, animar l'overlay i mantindre l'àudio viu. Sense
// aquest yield, els spin-waits típics de les cinemàtiques
// (`while (!JG_ShouldUpdate()) { JI_Update(); ... }`) congelarien
// tot el main loop — el fiber no cediria mai.
GameFiber::yield();
// No toca update — retornem false sense més. Des de Phase B.2 ja no
// hi ha fibers: cap caller fa spin-waits (`while (!JG_ShouldUpdate())`)
// i el Director pren el control del main loop frame a frame.
return false;
}

View File

@@ -5,7 +5,9 @@
#include "core/locale/locale.hpp"
#include "core/rendering/overlay.hpp"
#ifndef NO_SHADERS
#include "core/rendering/sdl3gpu/sdl3gpu_shader.hpp"
#endif
#include "game/defines.hpp"
#include "game/options.hpp"
#include "utils/utils.hpp"
@@ -40,10 +42,9 @@ Screen::Screen() {
window_ = SDL_CreateWindow(Locale::get("window.title"), w, h, fullscreen_ ? SDL_WINDOW_FULLSCREEN : 0);
renderer_ = SDL_CreateRenderer(window_, nullptr);
SDL_SetRenderLogicalPresentation(renderer_, GAME_WIDTH, GAME_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, GAME_WIDTH, GAME_HEIGHT);
SDL_SetTextureScaleMode(texture_, SDL_SCALEMODE_NEAREST);
applyFallbackPresentation();
// Inicialitza backend GPU si l'acceleració està activada
initShaders();
@@ -56,10 +57,12 @@ Screen::~Screen() {
Options::window.zoom = zoom_;
Options::window.fullscreen = fullscreen_;
// Destrueix el backend GPU
// Destrueix el backend GPU (només existeix si s'ha compilat amb shaders)
if (shader_backend_) {
#ifndef NO_SHADERS
auto* gpu = dynamic_cast<Rendering::SDL3GPUShader*>(shader_backend_.get());
if (gpu) gpu->destroy();
#endif
shader_backend_.reset();
}
@@ -69,6 +72,13 @@ Screen::~Screen() {
}
void Screen::initShaders() {
#ifdef NO_SHADERS
// Build sense shaders (p.ex. emscripten/WebGL2, on SDL3 GPU no està
// disponible). Es salta tota la inicialització — shader_backend_ es
// queda nul·lptr i tots els `if (shader_backend_)` del render path
// curtcircuiten cap al fallback SDL_Renderer.
return;
#else
if (!Options::video.gpu_acceleration) return;
shader_backend_ = std::make_unique<Rendering::SDL3GPUShader>();
@@ -122,6 +132,7 @@ void Screen::initShaders() {
applyCurrentPostFXPreset();
applyCurrentCrtPiPreset();
#endif
}
void Screen::present(Uint32* pixel_data) {
@@ -192,6 +203,8 @@ void Screen::toggleAspectRatio() {
Options::video.aspect_ratio_4_3 = !Options::video.aspect_ratio_4_3;
if (shader_backend_) {
shader_backend_->setStretch4_3(Options::video.aspect_ratio_4_3);
} else {
applyFallbackPresentation();
}
if (!fullscreen_) {
adjustWindowSize();
@@ -202,6 +215,8 @@ void Screen::toggleIntegerScale() {
Options::video.integer_scale = !Options::video.integer_scale;
if (shader_backend_) {
shader_backend_->setScaleMode(Options::video.integer_scale);
} else {
applyFallbackPresentation();
}
}
@@ -216,6 +231,8 @@ void Screen::toggleStretchFilter() {
Options::video.stretch_filter_linear = !Options::video.stretch_filter_linear;
if (shader_backend_) {
shader_backend_->setStretchFilter(Options::video.stretch_filter_linear);
} else {
applyFallbackPresentation();
}
}
@@ -371,6 +388,27 @@ void Screen::updateRenderInfo() {
0b1001);
}
void Screen::applyFallbackPresentation() {
// Fallback SDL_Renderer (p.ex. emscripten/WebGL2 sense shaders GPU): tria
// el mode de presentació lògica segons 4:3 i integer_scale, i aplica el
// filtre de la textura segons stretch_filter_linear. Sense açò, el path
// fallback mostrava sempre LETTERBOX i ignorava les tres flags.
SDL_ScaleMode scale = Options::video.stretch_filter_linear ? SDL_SCALEMODE_LINEAR : SDL_SCALEMODE_NEAREST;
if (texture_) SDL_SetTextureScaleMode(texture_, scale);
SDL_RendererLogicalPresentation mode;
if (Options::video.aspect_ratio_4_3) {
// La finestra ja té aspect 4:3 (alçada × 1.2); STRETCH estira la
// textura 320×200 fins a omplir-la exactament.
mode = SDL_LOGICAL_PRESENTATION_STRETCH;
} else if (Options::video.integer_scale) {
mode = SDL_LOGICAL_PRESENTATION_INTEGER_SCALE;
} else {
mode = SDL_LOGICAL_PRESENTATION_LETTERBOX;
}
SDL_SetRenderLogicalPresentation(renderer_, GAME_WIDTH, GAME_HEIGHT, mode);
}
void Screen::adjustWindowSize() {
int w = GAME_WIDTH * zoom_;
// Si 4:3 actiu, l'alçada visual és 240 per zoom (200 * 1.2)

View File

@@ -53,6 +53,7 @@ class Screen {
void adjustWindowSize();
void calculateMaxZoom();
void initShaders();
void applyFallbackPresentation(); // Logical presentation + scale mode per al path SDL_Renderer
static Screen* instance_;

View File

@@ -0,0 +1,67 @@
#include "core/resources/resource_helper.hpp"
#include <fstream>
#include <iostream>
#include "core/jail/jfile.hpp"
#include "core/resources/resource_pack.hpp"
namespace ResourceHelper {
namespace {
ResourcePack pack_;
bool pack_loaded_ = false;
bool fallback_enabled_ = true;
auto readFromDisk(const std::string& relative_path) -> std::vector<uint8_t> {
const std::string full = std::string(file_getresourcefolder()) + relative_path;
std::ifstream file(full, std::ios::binary | std::ios::ate);
if (!file) return {};
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(size);
if (!file.read(reinterpret_cast<char*>(data.data()), size)) return {};
return data;
}
} // namespace
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool {
fallback_enabled_ = enable_fallback;
pack_loaded_ = pack_.loadPack(pack_file);
if (pack_loaded_) {
std::cout << "ResourceHelper: pack loaded (" << pack_.getResourceCount()
<< " entries) from " << pack_file << '\n';
} else if (enable_fallback) {
std::cout << "ResourceHelper: no pack at " << pack_file
<< " — using filesystem fallback\n";
} else {
std::cerr << "ResourceHelper: FATAL — no pack at " << pack_file
<< " and fallback disabled\n";
return false;
}
return true;
}
void shutdownResourceSystem() {
pack_.clear();
pack_loaded_ = false;
}
auto loadFile(const std::string& relative_path) -> std::vector<uint8_t> {
if (pack_loaded_ && pack_.hasResource(relative_path)) {
return pack_.getResource(relative_path);
}
if (fallback_enabled_) {
return readFromDisk(relative_path);
}
return {};
}
auto hasPack() -> bool {
return pack_loaded_;
}
} // namespace ResourceHelper

View File

@@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
// API d'alt nivell per a llegir recursos. Prova primer el pack (si està
// carregat), després cau al fitxer solt dins `file_getresourcefolder()`
// si el fallback està activat.
namespace ResourceHelper {
// Inicialitza el sistema. `pack_file` és la ruta absoluta (o relativa al
// CWD) al fitxer de recursos. `enable_fallback` permet llegir de disc
// quan el pack no conté l'entrada (útil per a Debug i WASM).
auto initializeResourceSystem(const std::string& pack_file, bool enable_fallback) -> bool;
// Allibera el pack carregat a memòria.
void shutdownResourceSystem();
// Llegeix un recurs per ruta relativa (p.ex. "logo.gif", "fonts/8bithud.fnt").
// Retorna un vector buit si no es troba.
auto loadFile(const std::string& relative_path) -> std::vector<uint8_t>;
// True si el sistema es va inicialitzar amb un pack vàlid.
[[nodiscard]] auto hasPack() -> bool;
} // namespace ResourceHelper

View File

@@ -0,0 +1,220 @@
#include "core/resources/resource_pack.hpp"
#include <algorithm>
#include <array>
#include <filesystem>
#include <fstream>
#include <iostream>
const std::string ResourcePack::DEFAULT_ENCRYPT_KEY = "AEE_RESOURCES__2026";
namespace {
constexpr const char* MAGIC = "AEE1";
constexpr uint32_t VERSION = 1;
} // namespace
ResourcePack::ResourcePack() = default;
ResourcePack::~ResourcePack() {
clear();
}
auto ResourcePack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
// djb2-like hash, seed 0x12345678 (idèntic a CCAE).
uint32_t checksum = 0x12345678;
for (unsigned char b : data) {
checksum = ((checksum << 5) + checksum) + b;
}
return checksum;
}
void ResourcePack::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] ^= static_cast<uint8_t>(key[i % key.length()]);
}
}
void ResourcePack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
encryptData(data, key); // XOR és simètric
}
auto ResourcePack::loadPack(const std::string& pack_file) -> bool {
std::ifstream file(pack_file, std::ios::binary);
if (!file) {
return false; // No imprimim error: el caller decideix si cal fallback
}
std::array<char, 4> header{};
file.read(header.data(), 4);
if (std::string(header.data(), 4) != MAGIC) {
std::cerr << "ResourcePack: invalid pack file format (bad magic): " << pack_file << '\n';
return false;
}
uint32_t version = 0;
file.read(reinterpret_cast<char*>(&version), sizeof(version));
if (version != VERSION) {
std::cerr << "ResourcePack: unsupported pack version: " << version << '\n';
return false;
}
uint32_t resource_count = 0;
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
resources_.clear();
resources_.reserve(resource_count);
for (uint32_t i = 0; i < resource_count; ++i) {
uint32_t filename_length = 0;
file.read(reinterpret_cast<char*>(&filename_length), sizeof(filename_length));
std::string filename(filename_length, '\0');
file.read(filename.data(), filename_length);
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;
}
uint64_t data_size = 0;
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
data_.resize(data_size);
file.read(reinterpret_cast<char*>(data_.data()), static_cast<std::streamsize>(data_size));
decryptData(data_, DEFAULT_ENCRYPT_KEY);
loaded_ = true;
return true;
}
auto ResourcePack::savePack(const std::string& pack_file) -> bool {
std::ofstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "ResourcePack: could not create pack file: " << pack_file << '\n';
return false;
}
file.write(MAGIC, 4);
uint32_t version = VERSION;
file.write(reinterpret_cast<const char*>(&version), sizeof(version));
auto resource_count = static_cast<uint32_t>(resources_.size());
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
for (const auto& [filename, entry] : resources_) {
auto filename_length = static_cast<uint32_t>(filename.length());
file.write(reinterpret_cast<const char*>(&filename_length), sizeof(filename_length));
file.write(filename.c_str(), filename_length);
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));
}
std::vector<uint8_t> encrypted = data_;
encryptData(encrypted, DEFAULT_ENCRYPT_KEY);
uint64_t data_size = encrypted.size();
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
file.write(reinterpret_cast<const char*>(encrypted.data()), static_cast<std::streamsize>(data_size));
return true;
}
auto ResourcePack::addFile(const std::string& filename, const std::string& filepath) -> bool {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "ResourcePack: could not open file: " << filepath << '\n';
return false;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> file_data(file_size);
if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
std::cerr << "ResourcePack: could not read file: " << filepath << '\n';
return false;
}
ResourceEntry entry;
entry.filename = filename;
entry.offset = data_.size();
entry.size = file_data.size();
entry.checksum = calculateChecksum(file_data);
data_.insert(data_.end(), file_data.begin(), file_data.end());
resources_[filename] = entry;
return true;
}
auto ResourcePack::addDirectory(const std::string& directory) -> bool {
if (!std::filesystem::exists(directory)) {
std::cerr << "ResourcePack: directory does not exist: " << directory << '\n';
return false;
}
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
if (!entry.is_regular_file()) continue;
std::string filepath = entry.path().string();
std::string filename = std::filesystem::relative(entry.path(), directory).string();
std::ranges::replace(filename, '\\', '/');
if (!addFile(filename, filepath)) {
return false;
}
}
return true;
}
auto ResourcePack::getResource(const std::string& filename) -> std::vector<uint8_t> {
auto it = resources_.find(filename);
if (it == resources_.end()) return {};
const ResourceEntry& entry = it->second;
if (entry.offset + entry.size > data_.size()) {
std::cerr << "ResourcePack: invalid resource data: " << filename << '\n';
return {};
}
std::vector<uint8_t> result(data_.begin() + entry.offset,
data_.begin() + entry.offset + entry.size);
uint32_t checksum = calculateChecksum(result);
if (checksum != entry.checksum) {
std::cerr << "ResourcePack: checksum mismatch for: " << filename << '\n';
}
return result;
}
auto ResourcePack::hasResource(const std::string& filename) const -> bool {
return resources_.contains(filename);
}
void ResourcePack::clear() {
resources_.clear();
data_.clear();
loaded_ = false;
}
auto ResourcePack::getResourceCount() const -> size_t {
return resources_.size();
}
auto ResourcePack::getResourceList() const -> std::vector<std::string> {
std::vector<std::string> result;
result.reserve(resources_.size());
for (const auto& [filename, entry] : resources_) {
result.push_back(filename);
}
return result;
}

View File

@@ -0,0 +1,52 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
// Entrada d'un recurs dins el pack (format AEE, equivalent a CCAE).
struct ResourceEntry {
std::string filename;
uint64_t offset{0};
uint64_t size{0};
uint32_t checksum{0};
};
// Pack binari de recursos carregat a memòria. Formato:
// Header: "AEE1" (4 bytes) + version uint32 + resource_count uint32
// Index: per cada recurs -> filename_len uint32 + filename + offset uint64
// + size uint64 + checksum uint32
// Payload: data_size uint64 + bytes xifrats amb XOR (DEFAULT_ENCRYPT_KEY)
class ResourcePack {
public:
ResourcePack();
~ResourcePack();
// I/O del fitxer
auto loadPack(const std::string& pack_file) -> bool;
auto savePack(const std::string& pack_file) -> bool;
// Builders usats per l'eina pack_resources
auto addFile(const std::string& filename, const std::string& filepath) -> bool;
auto addDirectory(const std::string& directory) -> bool;
[[nodiscard]] auto getResource(const std::string& filename) -> std::vector<uint8_t>;
[[nodiscard]] auto hasResource(const std::string& filename) const -> bool;
void clear();
[[nodiscard]] auto getResourceCount() const -> size_t;
[[nodiscard]] auto getResourceList() const -> std::vector<std::string>;
static const std::string DEFAULT_ENCRYPT_KEY;
private:
std::unordered_map<std::string, ResourceEntry> resources_;
std::vector<uint8_t> data_;
bool loaded_{false};
static auto calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t;
static void encryptData(std::vector<uint8_t>& data, const std::string& key);
static void decryptData(std::vector<uint8_t>& data, const std::string& key);
};

View File

@@ -15,7 +15,6 @@
#include "core/rendering/menu.hpp"
#include "core/rendering/overlay.hpp"
#include "core/rendering/screen.hpp"
#include "core/system/fiber.hpp"
#include "game/info.hpp"
#include "game/modulegame.hpp"
#include "game/options.hpp"
@@ -35,14 +34,9 @@ extern void JI_moveCheats(Uint8 new_key);
Director* Director::instance_ = nullptr;
namespace {
Director::~Director() = default;
// Entry del fiber del joc. Dispatcha a una escena segons l'estat actual:
// `gameState == 0` → ModuleGame (gameplay), `gameState == 1` → una
// `scenes::Scene` del registry triada per `info::ctx.num_piramide`. Cada
// escena és tick-based; el JD8_Flip() entre ticks cedeix al Director
// via `GameFiber::yield()`.
void gameFiberEntry() {
void Director::initGameContext() {
info::ctx.num_habitacio = Options::game.habitacio_inicial;
info::ctx.num_piramide = Options::game.piramide_inicial;
info::ctx.diners = Options::game.diners_inicial;
@@ -57,55 +51,31 @@ void gameFiberEntry() {
info::ctx.nou_personatge = true;
fclose(ini);
}
}
int gameState = 1;
while (gameState != -1 && !JG_Quitting()) {
std::unique_ptr<scenes::Scene> scene;
if (gameState == 0) {
// Gameplay. ModuleGame és una scenes::Scene des de Phase A de
// la migració — mateix mini-loop tick+flip que la resta.
scene = std::make_unique<ModuleGame>();
} else {
// gameState == 1: dispatch al registry per num_piramide. El
// vell ModuleSequence::Go() feia aquest redirect al principi:
// si el jugador arriba a la Secreta (6) sense prou diners,
// salta als slides de fracàs (7) abans de buscar l'escena.
std::unique_ptr<scenes::Scene> Director::createNextScene() {
if (game_state_ == 0) {
// Gameplay. ModuleGame és una scenes::Scene des de la Phase A.
return std::make_unique<ModuleGame>();
}
// game_state_ == 1: dispatch al registry per num_piramide. Replica
// del redirect que el vell ModuleSequence::Go() feia: si el jugador
// arriba a la Secreta (6) sense prou diners, salta als slides de
// fracàs (7) abans de buscar l'escena al registry.
if (info::ctx.num_piramide == 6 && info::ctx.diners < 200) {
info::ctx.num_piramide = 7;
}
scene = scenes::SceneRegistry::instance().tryCreate(info::ctx.num_piramide);
return scenes::SceneRegistry::instance().tryCreate(info::ctx.num_piramide);
}
if (!scene) {
// State no registrat — indica un bug del dispatcher o del
// registre d'escenes. Eixim ordenadament en lloc de cremar CPU.
break;
}
scene->onEnter();
Uint32 last = SDL_GetTicks();
while (!scene->done() && !JG_Quitting()) {
JI_Update(); // refresca key_pressed/any_key per a les escenes
const Uint32 now = SDL_GetTicks();
scene->tick(static_cast<int>(now - last));
last = now;
JD8_Flip(); // presenta i cedix al Director
}
gameState = scene->nextState();
}
}
} // namespace
void Director::init() {
instance_ = new Director();
Gamepad::init();
// Registre d'escenes. Cada entrada = un state_key (`num_piramide`)
// amb una factory de `scenes::Scene`. El gameFiberEntry consulta
// aquest registry per a tots els states de seqüència; si una clau
// no apareix ací, el fiber eixirà del loop.
// amb una factory de `scenes::Scene`. iterate() consulta aquest
// registry per a tots els states de seqüència (game_state_ == 1); si
// una clau no apareix ací, Director surt ordenadament.
auto& registry = scenes::SceneRegistry::instance();
registry.registerScene(0, [] { return std::make_unique<scenes::MenuScene>(); });
registry.registerScene(100, [] { return std::make_unique<scenes::MortScene>(); });
@@ -132,12 +102,9 @@ void Director::init() {
}
return std::make_unique<scenes::IntroScene>();
});
GameFiber::init(gameFiberEntry);
}
void Director::destroy() {
GameFiber::destroy();
Gamepad::destroy();
delete instance_;
instance_ = nullptr;
@@ -164,17 +131,17 @@ void Director::setup() {
}
bool Director::iterate() {
if (GameFiber::is_done() || quit_requested_) {
// Si el joc encara no ha acabat (p.ex. eixida per ESC doble-press),
// li donem l'oportunitat de tornar net: marquem quit i reprenem el
// fiber fins que detecte JG_Quitting() i retorne de forma natural.
if (quit_requested_) {
JG_QuitSignal();
while (!GameFiber::is_done()) {
GameFiber::resume();
}
current_scene_.reset(); // destrueix l'escena actual ordenadament
return false;
}
if (!context_initialized_) {
initGameContext();
context_initialized_ = true;
}
constexpr Uint32 FRAME_MS_VSYNC = 16; // ~60 FPS amb VSync
constexpr Uint32 FRAME_MS_NO_VSYNC = 4; // ~250 FPS sense VSync (límit superior)
@@ -205,15 +172,41 @@ bool Director::iterate() {
esc_blocked_ = false;
}
// Cedeix el control al fiber del joc. Quan retorne (per un JD8_Flip()
// dins del joc) tindrem un nou frame a pixel_data. Si estem en pausa,
// no executem el fiber: es queda congelat al seu últim yield i
// continuem presentant l'últim frame conegut.
// Avança l'escena (si no estem pausats). En pausa, es manté l'escena
// congelada i re-presentem l'últim frame amb l'overlay fresc per
// damunt.
if (!paused_) {
GameFiber::resume();
if (GameFiber::is_done()) {
return false;
// Transicions: si l'escena actual ha acabat (o s'ha senyalat
// quit), llegim el seu next state i la destruïm per crear la
// següent a continuació.
if (current_scene_ && (current_scene_->done() || JG_Quitting())) {
game_state_ = current_scene_->nextState();
current_scene_.reset();
}
// Si no hi ha escena activa, construeix la pròxima segons
// game_state_ i info::ctx. Si és impossible (game_state_ == -1,
// quit, o state no registrat), eixim del loop.
if (!current_scene_) {
if (game_state_ == -1 || JG_Quitting()) return false;
current_scene_ = createNextScene();
if (!current_scene_) return false;
current_scene_->onEnter();
last_tick_ms_ = SDL_GetTicks();
}
// Tick de l'escena. JI_Update refresca key_pressed/any_key; el
// delta_ms és el temps real transcorregut des de l'últim tick.
JI_Update();
const Uint32 now = SDL_GetTicks();
const int delta_ms = static_cast<int>(now - last_tick_ms_);
last_tick_ms_ = now;
current_scene_->tick(delta_ms);
// Converteix `screen` indexat → `pixel_data` ARGB amb la paleta
// actual. JD8_Flip ja no fa yield (Phase B.2 eliminà els fibers);
// ara només omple el framebuffer perquè el Director l'aprofite.
JD8_Flip();
std::memcpy(game_frame_, JD8_GetFramebuffer(), sizeof(game_frame_));
has_frame_ = true;
}
@@ -238,12 +231,11 @@ bool Director::iterate() {
}
void Director::teardown() {
// Si el joc encara no ha acabat (p.ex. eixida per SDL_QUIT des del
// sistema), li donem l'oportunitat de tornar net.
// Senyal de quit i descàrrega ordenada de l'escena en curs. Els
// destructors de cada escena són no-bloquejants — ja no fan fades
// bloquejants. La resta de cleanup la gestiona `destroy()`.
JG_QuitSignal();
while (!GameFiber::is_done()) {
GameFiber::resume();
}
current_scene_.reset();
}
void Director::run() {

View File

@@ -4,13 +4,15 @@
#include <atomic>
#include <cstdint>
#include <memory>
// El Director és el thread principal que controla la presentació i els inputs.
// El codi del joc s'executa dins d'un *fiber* cooperatiu (veure fiber.hpp):
// el joc produeix un frame, crida JD8_Flip() que internament fa yield al
// Director, i el Director el presenta abans de tornar-lo a reprendre amb
// GameFiber::resume(). Tot ocorre en un únic thread — sense mutex, sense
// condition_variable, compatible amb el futur port a SDL_AppIterate.
#include "scenes/scene.hpp"
// El Director és l'únic thread del runtime. Cada iterate() fa input →
// tick de l'escena actual → JD8_Flip → overlay → present → sleep al frame
// target. Totes les escenes (`scenes::Scene` i `ModuleGame`) són
// tick-based i no bloquegen — no hi ha fibers, mutex ni condition_variable.
// Compatible amb SDL_AppIterate i amb el futur port a emscripten.
class Director {
public:
static void init();
@@ -40,19 +42,26 @@ class Director {
// Indica si ESC està bloquejada (el joc no l'ha de veure)
auto isEscBlocked() const -> bool { return esc_blocked_ || esc_swallow_until_release_; }
// Pausa: mentre està activa, Director no fa resume() del fiber del joc,
// així que el joc queda congelat al seu últim JD8_Flip.
// Pausa: mentre està activa, iterate() no avança l'escena — es
// continua presentant el darrer frame amb overlay fresc.
void togglePause();
auto isPaused() const -> bool { return paused_; }
private:
Director() = default;
~Director() = default;
~Director();
static Director* instance_;
void pollAllEvents(); // drenatge amb SDL_PollEvent, només per al bucle natiu
// Inicialitza info::ctx a partir de Options::game.* i comprova trick.ini.
// Es crida una sola vegada des d'iterate() a la primera invocació.
void initGameContext();
// Construeix l'escena apropiada segons game_state_ i info::ctx.
// Retorna nullptr si l'state actual no té escena registrada (bug).
std::unique_ptr<scenes::Scene> createNextScene();
// Buffers persistents entre iteracions. Abans eren locals a run(),
// ara són membres perquè iterate() els pot reutilitzar sense tornar-los
// a reservar en cada crida del callback.
@@ -60,6 +69,13 @@ class Director {
Uint32 presentation_buffer_[320 * 200]{};
bool has_frame_{false};
// Estat de l'escena actual. Abans vivia al stack del GameFiber; des
// de la Phase B.2 de la migració viu directament al Director.
std::unique_ptr<scenes::Scene> current_scene_;
int game_state_{1}; // 0 = gameplay (ModuleGame), 1 = via SceneRegistry, -1 = quit
Uint32 last_tick_ms_{0};
bool context_initialized_{false};
std::atomic<bool> quit_requested_{false};
std::atomic<bool> key_pressed_{false};
std::atomic<bool> esc_blocked_{false};

View File

@@ -1,141 +0,0 @@
#include "core/system/fiber.hpp"
#include <cstdlib>
#include <cstring>
#include <iostream>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
// ucontext_t està marcat com a obsolet a POSIX.1-2008 però continua
// funcional a glibc Linux i macOS. Si en el futur migrem a una alternativa
// (boost::context, makecontext personalitzat) només cal tocar aquest fitxer.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <ucontext.h>
#endif
namespace GameFiber {
namespace {
bool initialized_ = false;
bool done_ = false;
EntryFn entry_fn_ = nullptr;
#if defined(_WIN32)
LPVOID main_fiber_ = nullptr;
LPVOID game_fiber_ = nullptr;
void __stdcall trampoline(void* /*param*/) {
if (entry_fn_) entry_fn_();
done_ = true;
SwitchToFiber(main_fiber_);
}
#else
ucontext_t main_ctx_{};
ucontext_t fiber_ctx_{};
void* fiber_stack_ = nullptr;
void trampoline() {
if (entry_fn_) entry_fn_();
done_ = true;
// Retornar al main: uc_link apunta a main_ctx_ en init().
}
#endif
} // namespace
void init(EntryFn entry, std::size_t stack_size) {
if (initialized_) destroy();
entry_fn_ = entry;
done_ = false;
#if defined(_WIN32)
main_fiber_ = ConvertThreadToFiber(nullptr);
if (!main_fiber_) {
// Ja era un fiber (no sol passar en el main thread d'una app SDL).
main_fiber_ = GetCurrentFiber();
}
game_fiber_ = CreateFiber(stack_size, trampoline, nullptr);
if (!game_fiber_) {
std::cerr << "GameFiber::init: CreateFiber failed\n";
return;
}
#else
fiber_stack_ = std::malloc(stack_size);
if (!fiber_stack_) {
std::cerr << "GameFiber::init: malloc failed\n";
return;
}
getcontext(&fiber_ctx_);
fiber_ctx_.uc_stack.ss_sp = fiber_stack_;
fiber_ctx_.uc_stack.ss_size = stack_size;
fiber_ctx_.uc_link = &main_ctx_;
makecontext(&fiber_ctx_, trampoline, 0);
#endif
initialized_ = true;
}
void destroy() {
if (!initialized_) return;
#if defined(_WIN32)
if (game_fiber_) {
DeleteFiber(game_fiber_);
game_fiber_ = nullptr;
}
// No desconvertim el main thread: SDL pot estar-ne pendent i ja no
// tornem a crear fibers en aquesta execució. ConvertFiberToThread()
// només cal si volguerem reutilitzar el main com a thread normal.
#else
if (fiber_stack_) {
std::free(fiber_stack_);
fiber_stack_ = nullptr;
}
#endif
initialized_ = false;
done_ = false;
entry_fn_ = nullptr;
}
void resume() {
if (!initialized_ || done_) return;
#if defined(_WIN32)
SwitchToFiber(game_fiber_);
#else
swapcontext(&main_ctx_, &fiber_ctx_);
#endif
}
void yield() {
if (!initialized_) return;
#if defined(_WIN32)
SwitchToFiber(main_fiber_);
#else
swapcontext(&fiber_ctx_, &main_ctx_);
#endif
}
bool is_done() { return done_; }
bool is_initialized() { return initialized_; }
} // namespace GameFiber
#if !defined(_WIN32)
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#endif

View File

@@ -1,36 +0,0 @@
#pragma once
#include <cstddef>
// Fiber minimalista sobre el suport natiu del SO (ucontext_t en POSIX,
// Fibers API en Windows). Serveix per a implementar un yield/resume
// cooperatiu entre el Director i el codi del joc sense un std::thread
// ni mutex/condition_variable. Substituïx el bloqueig de publishFrame/
// consumeFrame amb un mecanisme de control explícit.
//
// Contracte:
// - GameFiber::init(entry) prepara un fiber que executarà `entry`
// en un stack dedicat. No el comença a executar encara.
// - GameFiber::resume() cedeix el control al fiber. Retorna quan el
// fiber crida GameFiber::yield() o quan la funció entry retorna.
// - GameFiber::yield() es crida des de dins del fiber per a tornar
// el control al main (al punt just després de resume()).
// - GameFiber::is_done() indica si la funció entry ha retornat.
// - GameFiber::destroy() allibera el stack i reinicia l'estat.
//
// Per al port a emscripten (Fase 7) caldrà substituir aquesta capa per
// Asyncify, però el contracte públic pot romandre idèntic.
namespace GameFiber {
using EntryFn = void (*)();
void init(EntryFn entry, std::size_t stack_size = 256 * 1024);
void destroy();
void resume();
void yield();
bool is_done();
bool is_initialized();
} // namespace GameFiber

View File

@@ -39,6 +39,15 @@ SDL_AppResult SDL_AppInit(void** /*appstate*/, int /*argc*/, char* /*argv*/[]) {
Options::setConfigFile(std::string(file_getconfigfolder()) + "config.yaml");
Options::loadFromFile();
#ifdef __EMSCRIPTEN__
// MEMFS no persistix entre recàrregues: força valors sensats per a web.
Options::window.fullscreen = false;
Options::window.zoom = 1;
Options::video.aspect_ratio_4_3 = true;
Options::video.integer_scale = true;
Options::video.stretch_filter_linear = true;
#endif
// Carrega textos (idioma per defecte: valencià)
Locale::load("locale/ca.yaml");

View File

@@ -0,0 +1,98 @@
#include <filesystem>
#include <iostream>
#include <string>
#include "core/resources/resource_pack.hpp"
namespace {
void showHelp() {
std::cout << "AEE - Resource Packer\n";
std::cout << "=====================\n";
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]\n\n";
std::cout << "Options:\n";
std::cout << " --help Show this help message\n";
std::cout << " --list List contents of an existing pack file\n\n";
std::cout << "Arguments:\n";
std::cout << " input_dir Directory to pack (default: data)\n";
std::cout << " output_file Pack file name (default: resource.pack)\n\n";
std::cout << "Examples:\n";
std::cout << " pack_resources # Pack 'data' to 'resource.pack'\n";
std::cout << " pack_resources mydata mypack.pack # Pack 'mydata' to 'mypack.pack'\n";
std::cout << " pack_resources --list my.pack # List contents of 'my.pack'\n";
}
void listPackContents(const std::string& pack_file) {
ResourcePack pack;
if (!pack.loadPack(pack_file)) {
std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
return;
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << pack_file << '\n';
std::cout << "Resources: " << resources.size() << '\n';
for (const auto& r : resources) std::cout << " " << r << '\n';
}
} // namespace
int main(int argc, char* argv[]) {
std::string data_dir = "data";
std::string output_file = "resource.pack";
bool list_mode = false;
bool data_dir_set = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
showHelp();
return 0;
}
if (arg == "--list") {
list_mode = true;
if (i + 1 < argc) output_file = argv[++i];
continue;
}
if (!arg.empty() && arg[0] != '-') {
if (!data_dir_set) {
data_dir = arg;
data_dir_set = true;
} else {
output_file = arg;
}
}
}
if (list_mode) {
listPackContents(output_file);
return 0;
}
std::cout << "AEE - Resource Packer\n=====================\n";
std::cout << "Input directory: " << data_dir << '\n';
std::cout << "Output file: " << output_file << '\n';
if (!std::filesystem::exists(data_dir)) {
std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
return 1;
}
ResourcePack pack;
std::cout << "Scanning and packing resources...\n";
if (!pack.addDirectory(data_dir)) {
std::cerr << "Error: failed to add directory to pack\n";
return 1;
}
std::cout << "Found " << pack.getResourceCount() << " resources\n";
std::cout << "Saving pack file...\n";
if (!pack.savePack(output_file)) {
std::cerr << "Error: failed to save pack file\n";
return 1;
}
auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
std::cout << "Pack file created: " << output_file << " ("
<< (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
return 0;
}