150 lines
5.4 KiB
C++
150 lines
5.4 KiB
C++
#include "game/scenes/title_scene.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "core/input/global_inputs.hpp"
|
|
#include "core/locale/locale.hpp"
|
|
#include "core/rendering/notifier.hpp"
|
|
#include "core/rendering/render_info.hpp"
|
|
#include "core/rendering/screen.hpp"
|
|
#include "core/rendering/surface.hpp"
|
|
#include "core/rendering/text.hpp"
|
|
#include "core/resources/resource_cache.hpp"
|
|
#include "core/system/event_buffer.hpp"
|
|
#include "core/ui/ui_config.hpp"
|
|
#include "game/options.hpp"
|
|
#include "game/scenes/placeholder_input.hpp"
|
|
#include "utils/defines.hpp"
|
|
|
|
namespace {
|
|
|
|
// TODO: migrar a un logger unificat quan es decideixi (veure INC-03 a
|
|
// docs/audit-core-rendering.md).
|
|
const char* const SCENE_NAME = "TITLE";
|
|
|
|
// Textos hardcodejats fins que tinguem artwork. Quan apareguen com
|
|
// a sprites, aquesta secció desapareix sencera.
|
|
const char* const TITLE_LINE_1 = "Aventures en Egipte";
|
|
const char* const TITLE_LINE_2 = "A R C A D E";
|
|
|
|
constexpr int TITLE_Y_PERCENT = 25;
|
|
constexpr int PROMPT_Y_PERCENT = 60;
|
|
constexpr int COPYRIGHT_Y_PERCENT = 90;
|
|
constexpr int LOGO_GAP_PX = 2; // Separació vertical entre el minilogo i el text de copyright.
|
|
|
|
// Separació vertical entre la primera i segona línia del títol. Una
|
|
// alçada de línia és l'opció més llegible al canvas de 240 px sense
|
|
// entrar a configuració extra; si demà cal afinar-ho, mou-se a
|
|
// ui.yaml.
|
|
[[nodiscard]] auto titleLineGap(const Text& font) -> int {
|
|
return font.getLineHeight();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TitleScene::TitleScene(std::shared_ptr<Text> display_font, std::shared_ptr<Text> condensed_font, std::shared_ptr<Surface> mini_logo, SceneFactory next_factory)
|
|
: display_font_(std::move(display_font)),
|
|
condensed_font_(std::move(condensed_font)),
|
|
mini_logo_(std::move(mini_logo)),
|
|
next_factory_(std::move(next_factory)),
|
|
timer_ms_(UiConfig::get().titleDurationMs()) {
|
|
std::cout << "[scene] " << SCENE_NAME << " start\n";
|
|
}
|
|
|
|
TitleScene::~TitleScene() = default;
|
|
|
|
void TitleScene::update(int delta_ms) {
|
|
GlobalInputs::update(delta_ms);
|
|
if (GlobalInputs::wantsQuit()) {
|
|
wants_quit_ = true;
|
|
return;
|
|
}
|
|
|
|
timer_ms_ -= delta_ms;
|
|
blink_elapsed_ms_ += delta_ms;
|
|
|
|
const bool SKIPPED = std::ranges::any_of(EventBuffer::events, [](const SDL_Event& ev) {
|
|
return ev.type == SDL_EVENT_KEY_DOWN && !Placeholder::isReservedScancode(ev.key.scancode);
|
|
});
|
|
|
|
if ((timer_ms_ <= 0 || SKIPPED) && next_factory_) {
|
|
next_scene_ = next_factory_();
|
|
}
|
|
|
|
Notifier::get().update(delta_ms);
|
|
RenderInfo::get().update(delta_ms, Options::buildRenderInfoFrame(UiConfig::get().renderInfoText(), UiConfig::get().renderInfoShadow()));
|
|
}
|
|
|
|
void TitleScene::render() {
|
|
const auto& ui_config = UiConfig::get();
|
|
const auto& palette = Resource::Cache::get().getPalette("ui");
|
|
|
|
Screen::get().fillRect(0, 0, GameCanvas::WIDTH, GameCanvas::HEIGHT, palette[ui_config.titleBgColor()]);
|
|
|
|
const Uint8 TEXT_COLOR = ui_config.titleTextColor();
|
|
|
|
if (display_font_) {
|
|
const int TITLE_Y = (GameCanvas::HEIGHT * TITLE_Y_PERCENT) / 100;
|
|
display_font_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
|
|
GameCanvas::CENTER_X,
|
|
TITLE_Y,
|
|
TITLE_LINE_1,
|
|
display_font_->getDefaultKerning(),
|
|
TEXT_COLOR);
|
|
display_font_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
|
|
GameCanvas::CENTER_X,
|
|
TITLE_Y + titleLineGap(*display_font_),
|
|
TITLE_LINE_2,
|
|
display_font_->getDefaultKerning(),
|
|
TEXT_COLOR);
|
|
}
|
|
|
|
if (condensed_font_) {
|
|
// Cicle de parpadeig: visible_ms ON + hidden_ms OFF, repetint-se.
|
|
// Si la suma val 0 (configuració degenerada), cau a "sempre visible".
|
|
const int VISIBLE_MS = std::max(0, ui_config.titleBlinkVisibleMs());
|
|
const int HIDDEN_MS = std::max(0, ui_config.titleBlinkHiddenMs());
|
|
const int CYCLE_MS = VISIBLE_MS + HIDDEN_MS;
|
|
const bool VISIBLE = (CYCLE_MS == 0) || ((blink_elapsed_ms_ % CYCLE_MS) < VISIBLE_MS);
|
|
|
|
if (VISIBLE) {
|
|
const int PROMPT_Y = (GameCanvas::HEIGHT * PROMPT_Y_PERCENT) / 100;
|
|
condensed_font_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
|
|
GameCanvas::CENTER_X,
|
|
PROMPT_Y,
|
|
Locale::get().text("title.press_start"),
|
|
condensed_font_->getDefaultKerning() - 1,
|
|
TEXT_COLOR);
|
|
}
|
|
}
|
|
|
|
// Footer: minilogo centrat horitzontalment damunt del © de
|
|
// copyright. El text va al 90% Y, el logo just damunt amb un petit
|
|
// gap (LOGO_GAP_PX). Tots dos blocs són hardcodejats fins que
|
|
// tinguem artwork final.
|
|
if (condensed_font_) {
|
|
const int COPY_Y = (GameCanvas::HEIGHT * COPYRIGHT_Y_PERCENT) / 100;
|
|
if (mini_logo_) {
|
|
const int LOGO_X = GameCanvas::CENTER_X - (mini_logo_->getWidth() / 2);
|
|
const int LOGO_Y = COPY_Y - mini_logo_->getHeight() - LOGO_GAP_PX;
|
|
mini_logo_->render(LOGO_X, LOGO_Y);
|
|
}
|
|
condensed_font_->writeDX(Text::CENTER_FLAG | Text::COLOR_FLAG,
|
|
GameCanvas::CENTER_X,
|
|
COPY_Y,
|
|
"© 2026 JailDesigner",
|
|
condensed_font_->getDefaultKerning() - 1,
|
|
ui_config.titleCopyrightColor());
|
|
}
|
|
|
|
Notifier::get().render();
|
|
RenderInfo::get().render();
|
|
Screen::get().present();
|
|
}
|