fase 4+5: fibers cooperatius substitueixen el game thread, sense mutex ni cv
This commit is contained in:
@@ -8,12 +8,14 @@
|
||||
#include "core/input/key_remap.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/jail/jail_audio.hpp"
|
||||
#include "core/jail/jdraw8.hpp"
|
||||
#include "core/jail/jgame.hpp"
|
||||
#include "core/jail/jinput.hpp"
|
||||
#include "core/locale/locale.hpp"
|
||||
#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/modulesequence.hpp"
|
||||
@@ -24,12 +26,57 @@ extern void JI_moveCheats(Uint8 new_key);
|
||||
|
||||
Director* Director::instance_ = nullptr;
|
||||
|
||||
namespace {
|
||||
|
||||
// Entry del fiber del joc. Executa la màquina d'estats que alterna entre
|
||||
// ModuleSequence (state=1) i ModuleGame (state=0) fins que el joc demana
|
||||
// eixir. Quan el joc crida JD8_Flip() des de dins d'aquest fiber, el
|
||||
// control torna automàticament al Director.
|
||||
void gameFiberEntry() {
|
||||
info::ctx.num_habitacio = Options::game.habitacio_inicial;
|
||||
info::ctx.num_piramide = Options::game.piramide_inicial;
|
||||
info::ctx.diners = 0;
|
||||
info::ctx.diamants = 0;
|
||||
info::ctx.vida = Options::game.vides;
|
||||
info::ctx.momies = 0;
|
||||
info::ctx.nou_personatge = false;
|
||||
info::ctx.pepe_activat = false;
|
||||
|
||||
FILE* ini = fopen("trick.ini", "rb");
|
||||
if (ini != nullptr) {
|
||||
info::ctx.nou_personatge = true;
|
||||
fclose(ini);
|
||||
}
|
||||
|
||||
int gameState = 1;
|
||||
while (gameState != -1 && !JG_Quitting()) {
|
||||
switch (gameState) {
|
||||
case 0: {
|
||||
auto* moduleGame = new ModuleGame();
|
||||
gameState = moduleGame->Go();
|
||||
delete moduleGame;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
auto* moduleSequence = new ModuleSequence();
|
||||
gameState = moduleSequence->Go();
|
||||
delete moduleSequence;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Director::init() {
|
||||
instance_ = new Director();
|
||||
Gamepad::init();
|
||||
GameFiber::init(gameFiberEntry);
|
||||
}
|
||||
|
||||
void Director::destroy() {
|
||||
GameFiber::destroy();
|
||||
Gamepad::destroy();
|
||||
delete instance_;
|
||||
instance_ = nullptr;
|
||||
@@ -49,11 +96,11 @@ void Director::togglePause() {
|
||||
}
|
||||
|
||||
void Director::run() {
|
||||
// Llança el game thread
|
||||
game_thread_ = std::thread(&Director::gameThreadFunc, this);
|
||||
|
||||
// Doble buffer: game_frame és el frame net del joc, presentation_buffer
|
||||
// és el frame + overlay (es regenera cada iteració des de game_frame)
|
||||
// és el frame + overlay (es regenera cada iteració des de game_frame).
|
||||
// El doble buffer encara té sentit perquè el Director pot presentar
|
||||
// més frames que els que genera el joc (p.ex. durant pauses o mentre
|
||||
// el joc està en un "wait" intern que triga milisegons).
|
||||
Uint32 game_frame[320 * 200]{};
|
||||
Uint32 presentation_buffer[320 * 200]{};
|
||||
bool has_frame = false;
|
||||
@@ -61,8 +108,7 @@ void Director::run() {
|
||||
constexpr Uint32 FRAME_MS_VSYNC = 16; // ~60 FPS amb VSync
|
||||
constexpr Uint32 FRAME_MS_NO_VSYNC = 4; // ~250 FPS sense VSync (límit superior)
|
||||
|
||||
// Bucle principal del director (no-bloquejant)
|
||||
while (!game_thread_done_ && !quit_requested_) {
|
||||
while (!GameFiber::is_done() && !quit_requested_) {
|
||||
Uint32 frame_start = SDL_GetTicks();
|
||||
|
||||
handleEvents();
|
||||
@@ -77,8 +123,7 @@ void Director::run() {
|
||||
JA_Update();
|
||||
|
||||
// Dispara els crèdits cinematogràfics la primera vegada que el joc
|
||||
// arriba al menú del títol (info::ctx.num_piramide == 0). Lectura no
|
||||
// atòmica d'un int global: race benigna, tard d'1 frame en el pitjor cas.
|
||||
// arriba al menú del títol (info::ctx.num_piramide == 0).
|
||||
static bool credits_triggered = false;
|
||||
if (!credits_triggered && info::ctx.num_piramide == 0) {
|
||||
if (Options::game.show_title_credits) {
|
||||
@@ -92,21 +137,15 @@ void Director::run() {
|
||||
esc_blocked_ = false;
|
||||
}
|
||||
|
||||
// Consumeix un frame nou si n'hi ha un disponible (no bloqueja).
|
||||
// Si estem en pausa, no consumim: el game thread es queda bloquejat a publishFrame.
|
||||
bool new_frame = 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.
|
||||
if (!paused_) {
|
||||
std::lock_guard lock(mutex_);
|
||||
if (frame_ready_ && latest_frame_ != nullptr) {
|
||||
memcpy(game_frame, latest_frame_, sizeof(game_frame));
|
||||
frame_ready_ = false;
|
||||
frame_consumed_ = true;
|
||||
has_frame = true;
|
||||
new_frame = true;
|
||||
}
|
||||
}
|
||||
if (new_frame) {
|
||||
frame_consumed_cv_.notify_one(); // desbloqueja el joc
|
||||
GameFiber::resume();
|
||||
if (GameFiber::is_done()) break;
|
||||
memcpy(game_frame, JD8_GetFramebuffer(), sizeof(game_frame));
|
||||
has_frame = true;
|
||||
}
|
||||
|
||||
// Presenta sempre: parteix del frame net del joc, afegeix overlay i envia
|
||||
@@ -123,17 +162,12 @@ void Director::run() {
|
||||
}
|
||||
}
|
||||
|
||||
// Assegura que el game thread ix (despertar-lo per si està esperant)
|
||||
quit_requested_ = true;
|
||||
// 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.
|
||||
JG_QuitSignal();
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
frame_consumed_ = true;
|
||||
}
|
||||
frame_consumed_cv_.notify_all();
|
||||
|
||||
if (game_thread_.joinable()) {
|
||||
game_thread_.join();
|
||||
while (!GameFiber::is_done()) {
|
||||
GameFiber::resume();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,10 +252,9 @@ void Director::handleEvents() {
|
||||
esc_blocked_ = false;
|
||||
key_pressed_ = true;
|
||||
JG_QuitSignal();
|
||||
// Si estem en pausa, la desactivem (sense reprendre la música,
|
||||
// estem eixint): el game thread està bloquejat a publishFrame
|
||||
// i necessita que Director consumeixca frames per despertar-lo
|
||||
// i poder veure la senyal de quit.
|
||||
// Si estem en pausa, la desactivem: el fiber del joc està
|
||||
// congelat i necessita ser reprès per veure la senyal de
|
||||
// quit i poder tornar de forma natural.
|
||||
paused_ = false;
|
||||
}
|
||||
continue; // no processa més aquest event
|
||||
@@ -254,70 +287,11 @@ void Director::handleEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
void Director::publishFrame(Uint32* pixels) {
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
latest_frame_ = pixels;
|
||||
frame_ready_ = true;
|
||||
frame_consumed_ = false;
|
||||
}
|
||||
frame_produced_cv_.notify_one();
|
||||
|
||||
// Espera que el director consumeixca el frame
|
||||
{
|
||||
std::unique_lock lock(mutex_);
|
||||
frame_consumed_cv_.wait(lock, [this] {
|
||||
return frame_consumed_ || quit_requested_;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void Director::requestQuit() {
|
||||
quit_requested_ = true;
|
||||
JG_QuitSignal();
|
||||
frame_consumed_cv_.notify_all();
|
||||
frame_produced_cv_.notify_all();
|
||||
}
|
||||
|
||||
auto Director::consumeKeyPressed() -> bool {
|
||||
return key_pressed_.exchange(false);
|
||||
}
|
||||
|
||||
void Director::gameThreadFunc() {
|
||||
info::ctx.num_habitacio = Options::game.habitacio_inicial;
|
||||
info::ctx.num_piramide = Options::game.piramide_inicial;
|
||||
info::ctx.diners = 0;
|
||||
info::ctx.diamants = 0;
|
||||
info::ctx.vida = Options::game.vides;
|
||||
info::ctx.momies = 0;
|
||||
info::ctx.nou_personatge = false;
|
||||
info::ctx.pepe_activat = false;
|
||||
|
||||
FILE* ini = fopen("trick.ini", "rb");
|
||||
if (ini != nullptr) {
|
||||
info::ctx.nou_personatge = true;
|
||||
fclose(ini);
|
||||
}
|
||||
|
||||
int gameState = 1;
|
||||
while (gameState != -1 && !quit_requested_) {
|
||||
switch (gameState) {
|
||||
case 0: {
|
||||
auto* moduleGame = new ModuleGame();
|
||||
gameState = moduleGame->Go();
|
||||
delete moduleGame;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
auto* moduleSequence = new ModuleSequence();
|
||||
gameState = moduleSequence->Go();
|
||||
delete moduleSequence;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
game_thread_done_ = true;
|
||||
// Despertar el director per si esperava un frame
|
||||
frame_produced_cv_.notify_all();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user