fase 1: jail i game a c++ idiomàtic (raii, info::ctx, cheats arreglats)
This commit is contained in:
@@ -1,39 +1,63 @@
|
||||
#include "core/jail/jinput.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#include "core/system/director.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
// keystates és actualitzat per SDL internament. Des del joc només fem lectures.
|
||||
const bool* keystates = nullptr;
|
||||
Uint8 cheat[5];
|
||||
bool key_pressed = false;
|
||||
int waitTime = 0;
|
||||
|
||||
void JI_DisableKeyboard(Uint32 time) {
|
||||
waitTime = time;
|
||||
// Buffer dels últims 5 caràcters tecle. Emmagatzemem caràcters ASCII
|
||||
// lowercase (traduïts des de SDL_Scancode) per a poder comparar directament
|
||||
// amb les cadenes dels cheats ("reviu", "alone", "obert").
|
||||
Uint8 cheat[5] = {0, 0, 0, 0, 0};
|
||||
|
||||
bool key_pressed = false;
|
||||
|
||||
// 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
|
||||
// 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 scancode_to_ascii(Uint8 scancode) {
|
||||
if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) {
|
||||
return static_cast<Uint8>('a' + (scancode - SDL_SCANCODE_A));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool input_blocked = false;
|
||||
} // namespace
|
||||
|
||||
void JI_DisableKeyboard(Uint32 time) {
|
||||
wait_ms = static_cast<float>(time);
|
||||
}
|
||||
|
||||
void JI_SetInputBlocked(bool blocked) {
|
||||
input_blocked = blocked;
|
||||
}
|
||||
|
||||
static Uint8 virtual_keystates[JI_VSRC_COUNT][SDL_SCANCODE_COUNT] = {{0}};
|
||||
|
||||
void JI_SetVirtualKey(int scancode, int source, bool pressed) {
|
||||
if (scancode < 0 || scancode >= SDL_SCANCODE_COUNT) return;
|
||||
if (source < 0 || source >= JI_VSRC_COUNT) return;
|
||||
virtual_keystates[source][scancode] = pressed ? 1 : 0;
|
||||
}
|
||||
|
||||
void JI_moveCheats(Uint8 new_key) {
|
||||
void JI_moveCheats(Uint8 scancode) {
|
||||
cheat[0] = cheat[1];
|
||||
cheat[1] = cheat[2];
|
||||
cheat[2] = cheat[3];
|
||||
cheat[3] = cheat[4];
|
||||
cheat[4] = new_key;
|
||||
cheat[4] = scancode_to_ascii(scancode);
|
||||
}
|
||||
|
||||
void JI_Update() {
|
||||
@@ -43,14 +67,22 @@ void JI_Update() {
|
||||
keystates = SDL_GetKeyboardState(NULL);
|
||||
}
|
||||
|
||||
if (waitTime > 0) waitTime--;
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
if (last_update_tick == 0) last_update_tick = now;
|
||||
const float delta_ms = static_cast<float>(now - last_update_tick);
|
||||
last_update_tick = now;
|
||||
|
||||
if (wait_ms > 0.0f) {
|
||||
wait_ms -= delta_ms;
|
||||
if (wait_ms < 0.0f) wait_ms = 0.0f;
|
||||
}
|
||||
|
||||
// Consumim el flag de "alguna tecla no-GUI polsada" del director
|
||||
key_pressed = Director::get()->consumeKeyPressed();
|
||||
}
|
||||
|
||||
bool JI_KeyPressed(int key) {
|
||||
if (waitTime > 0 || keystates == nullptr) return false;
|
||||
if (wait_ms > 0.0f || keystates == nullptr) return false;
|
||||
// Input bloquejat (p.ex. menú flotant obert)
|
||||
if (input_blocked) return false;
|
||||
// ESC bloquejada pel Director (primera pulsació mostra notificació)
|
||||
@@ -64,13 +96,17 @@ bool JI_KeyPressed(int key) {
|
||||
}
|
||||
|
||||
bool JI_CheatActivated(const char* cheat_code) {
|
||||
bool found = true;
|
||||
for (size_t i = 0; i < strlen(cheat_code); i++) {
|
||||
if (cheat[i] != cheat_code[i]) found = false;
|
||||
const size_t len = std::strlen(cheat_code);
|
||||
if (len > sizeof(cheat)) return false;
|
||||
// Compara contra els últims `len` caràcters del buffer. El buffer té
|
||||
// mida fixa 5 i acumula sempre el darrer tecle a la posició 4.
|
||||
const size_t offset = sizeof(cheat) - len;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (cheat[offset + i] != static_cast<Uint8>(cheat_code[i])) return false;
|
||||
}
|
||||
return found;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JI_AnyKey() {
|
||||
return waitTime > 0 ? false : key_pressed;
|
||||
return wait_ms > 0.0f ? false : key_pressed;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user