refactor: JI_* a Ji:: i JG_* a Jg::

This commit is contained in:
2026-05-16 14:43:16 +02:00
parent 9d30dd538c
commit bbcc10da81
27 changed files with 174 additions and 163 deletions
+12 -12
View File
@@ -2,7 +2,7 @@
namespace {
bool quitting = false;
bool is_quitting = false;
Uint32 update_ticks = 0;
Uint32 update_time = 0;
Uint32 cycle_counter = 0;
@@ -10,29 +10,29 @@ namespace {
} // namespace
void JG_Init() {
void Jg::init() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
update_time = SDL_GetTicks();
last_delta_time = update_time;
}
void JG_Finalize() {
void Jg::finalize() {
SDL_Quit();
}
void JG_QuitSignal() {
quitting = true;
void Jg::quitSignal() {
is_quitting = true;
}
auto JG_Quitting() -> bool {
return quitting;
auto Jg::quitting() -> bool {
return is_quitting;
}
void JG_SetUpdateTicks(Uint32 milliseconds) {
void Jg::setUpdateTicks(Uint32 milliseconds) {
update_ticks = milliseconds;
}
auto JG_ShouldUpdate() -> bool {
auto Jg::shouldUpdate() -> bool {
const Uint32 now = SDL_GetTicks();
if (now - update_time > update_ticks) {
update_time = now;
@@ -40,16 +40,16 @@ auto JG_ShouldUpdate() -> bool {
return true;
}
// 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())`)
// 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;
}
auto JG_GetCycleCounter() -> Uint32 {
auto Jg::getCycleCounter() -> Uint32 {
return cycle_counter;
}
auto JG_GetDeltaMs() -> Uint32 {
auto Jg::getDeltaMs() -> Uint32 {
const Uint32 now = SDL_GetTicks();
const Uint32 delta = now - last_delta_time;
last_delta_time = now;
+24 -20
View File
@@ -1,20 +1,24 @@
#pragma once
#include <SDL3/SDL.h>
void JG_Init();
void JG_Finalize();
void JG_QuitSignal();
auto JG_Quitting() -> bool;
void JG_SetUpdateTicks(Uint32 milliseconds);
auto JG_ShouldUpdate() -> bool;
auto JG_GetCycleCounter() -> Uint32;
// Temps transcorregut (en ms) des de l'última crida a JG_GetDeltaMs.
// Helper per a la migració progressiva a time-based (Fase 4+).
auto JG_GetDeltaMs() -> Uint32;
#pragma once
#include <SDL3/SDL.h>
namespace Jg {
void init();
void finalize();
void quitSignal();
auto quitting() -> bool;
void setUpdateTicks(Uint32 milliseconds);
auto shouldUpdate() -> bool;
auto getCycleCounter() -> Uint32;
// Temps transcorregut (en ms) des de l'última crida a Jg::getDeltaMs.
// Helper per a la migració progressiva a time-based (Fase 4+).
auto getDeltaMs() -> Uint32;
} // namespace Jg
+14 -13
View File
@@ -17,18 +17,18 @@ namespace {
bool key_pressed = false;
// Temps restant en mil·lisegons durant el qual JI_KeyPressed/JI_AnyKey
// Temps restant en mil·lisegons durant el qual Ji::keyPressed/Ji::anyKey
// retornen false. Utilitzat per a evitar que pulsacions fortuïtes
// saltin cinemàtiques al començament.
float wait_ms = 0.0F;
// Per a calcular el delta entre crides a JI_Update sense que els callers
// Per a calcular el delta entre crides a Ji::update sense que els callers
// hagen de passar-lo explícitament. Es reinicia a la primera crida.
Uint64 last_update_tick = 0;
bool input_blocked = false;
Uint8 virtual_keystates[JI_VSRC_COUNT][SDL_SCANCODE_COUNT] = {{0}};
Uint8 virtual_keystates[static_cast<size_t>(Ji::VirtualSource::COUNT)][SDL_SCANCODE_COUNT] = {{0}};
auto scancode_to_ascii(Uint8 scancode) -> Uint8 {
if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) {
@@ -39,25 +39,26 @@ namespace {
} // namespace
void JI_DisableKeyboard(Uint32 time) {
void Ji::disableKeyboard(Uint32 time) {
wait_ms = static_cast<float>(time);
}
void JI_SetInputBlocked(bool blocked) {
void Ji::setInputBlocked(bool blocked) {
input_blocked = blocked;
}
void JI_SetVirtualKey(int scancode, int source, bool pressed) {
void Ji::setVirtualKey(int scancode, VirtualSource source, bool pressed) {
if (scancode < 0 || scancode >= SDL_SCANCODE_COUNT) {
return;
}
if (source < 0 || source >= JI_VSRC_COUNT) {
const auto src_idx = static_cast<size_t>(source);
if (src_idx >= static_cast<size_t>(VirtualSource::COUNT)) {
return;
}
virtual_keystates[source][scancode] = pressed ? 1 : 0;
virtual_keystates[src_idx][scancode] = pressed ? 1 : 0;
}
void JI_moveCheats(Uint8 scancode) {
void Ji::moveCheats(Uint8 scancode) {
cheat[0] = cheat[1];
cheat[1] = cheat[2];
cheat[2] = cheat[3];
@@ -65,7 +66,7 @@ void JI_moveCheats(Uint8 scancode) {
cheat[4] = scancode_to_ascii(scancode);
}
void JI_Update() {
void Ji::update() {
// El director ha processat tots els events. Ací només refresquem
// el snapshot del teclat i consumim el flag de tecla polsada.
if (keystates == nullptr) {
@@ -88,7 +89,7 @@ void JI_Update() {
key_pressed = Director::get()->consumeKeyPressed();
}
auto JI_KeyPressed(int key) -> bool {
auto Ji::keyPressed(int key) -> bool {
if (wait_ms > 0.0F || keystates == nullptr) {
return false;
}
@@ -109,7 +110,7 @@ auto JI_KeyPressed(int key) -> bool {
return std::ranges::any_of(virtual_keystates, [key](const auto& vk) { return vk[key] != 0; });
}
auto JI_CheatActivated(const char* cheat_code) -> bool {
auto Ji::cheatActivated(const char* cheat_code) -> bool {
const size_t len = std::strlen(cheat_code);
if (len > sizeof(cheat)) {
return false;
@@ -125,6 +126,6 @@ auto JI_CheatActivated(const char* cheat_code) -> bool {
return true;
}
auto JI_AnyKey() -> bool {
auto Ji::anyKey() -> bool {
return wait_ms > 0.0F ? false : key_pressed;
}
+36 -27
View File
@@ -1,27 +1,36 @@
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
void JI_DisableKeyboard(Uint32 time);
// Bloqueja tot l'input cap al joc (JI_KeyPressed retorna false per a tot)
void JI_SetInputBlocked(bool blocked);
// Estableix l'estat d'una tecla virtual. Múltiples fonts (gamepad, remap)
// s'agrupen per OR. JI_KeyPressed retorna true si el teclat real O qualsevol
// font virtual està premuda.
enum JI_VirtualSource : std::uint8_t {
JI_VSRC_GAMEPAD = 0,
JI_VSRC_REMAP = 1,
JI_VSRC_COUNT = 2
};
void JI_SetVirtualKey(int scancode, int source, bool pressed);
void JI_Update();
auto JI_KeyPressed(int key) -> bool;
auto JI_CheatActivated(const char* cheat_code) -> bool;
auto JI_AnyKey() -> bool;
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
namespace Ji {
void disableKeyboard(Uint32 time);
// Bloqueja tot l'input cap al joc (Ji::keyPressed retorna false per a tot)
void setInputBlocked(bool blocked);
// Estableix l'estat d'una tecla virtual. Múltiples fonts (gamepad, remap)
// s'agrupen per OR. Ji::keyPressed retorna true si el teclat real O qualsevol
// font virtual està premuda.
enum class VirtualSource : std::uint8_t {
GAMEPAD = 0,
REMAP = 1,
COUNT = 2
};
void setVirtualKey(int scancode, VirtualSource source, bool pressed);
void update();
// Avança el buffer rotatori de cheats afegint `scancode` per detectar
// seqüències com "reviu", "alone", "obert". Usat pel Director quan rep
// un KEY_DOWN; el joc no l'ha de cridar directament.
void moveCheats(Uint8 scancode);
auto keyPressed(int key) -> bool;
auto cheatActivated(const char* cheat_code) -> bool;
auto anyKey() -> bool;
} // namespace Ji