afegida escena TITOL
This commit is contained in:
149
source/game/escenes/escena_titol.cpp
Normal file
149
source/game/escenes/escena_titol.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
// escena_titol.cpp - Implementació de l'escena de títol
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#include "escena_titol.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "../../core/audio/audio.hpp"
|
||||
#include "../../core/system/gestor_escenes.hpp"
|
||||
#include "../../core/system/global_events.hpp"
|
||||
#include "build/project.h"
|
||||
|
||||
EscenaTitol::EscenaTitol(SDLManager& sdl)
|
||||
: sdl_(sdl),
|
||||
text_(sdl.obte_renderer()),
|
||||
estat_actual_(EstatTitol::INIT),
|
||||
temps_acumulat_(0.0f) {
|
||||
std::cout << "Escena Titol: Inicialitzant...\n";
|
||||
}
|
||||
|
||||
void EscenaTitol::executar() {
|
||||
SDL_Event event;
|
||||
Uint64 last_time = SDL_GetTicks();
|
||||
|
||||
while (GestorEscenes::actual == GestorEscenes::Escena::TITOL) {
|
||||
// Calcular delta_time real
|
||||
Uint64 current_time = SDL_GetTicks();
|
||||
float delta_time = (current_time - last_time) / 1000.0f;
|
||||
last_time = current_time;
|
||||
|
||||
// Limitar delta_time per evitar grans salts
|
||||
if (delta_time > 0.05f) {
|
||||
delta_time = 0.05f;
|
||||
}
|
||||
|
||||
// Actualitzar comptador de FPS
|
||||
sdl_.updateFPS(delta_time);
|
||||
|
||||
// Processar events SDL
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// Manejo de finestra
|
||||
if (sdl_.handleWindowEvent(event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Events globals (F1/F2/F3/F4/ESC/QUIT)
|
||||
if (GlobalEvents::handle(event, sdl_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Processar events de l'escena
|
||||
processar_events(event);
|
||||
}
|
||||
|
||||
// Actualitzar lògica
|
||||
actualitzar(delta_time);
|
||||
|
||||
// Actualitzar sistema d'audio
|
||||
Audio::update();
|
||||
|
||||
// Actualitzar colors oscil·lats
|
||||
sdl_.updateColors(delta_time);
|
||||
|
||||
// Netejar pantalla
|
||||
sdl_.neteja(0, 0, 0);
|
||||
|
||||
// Dibuixar
|
||||
dibuixar();
|
||||
|
||||
// Presentar renderer (swap buffers)
|
||||
sdl_.presenta();
|
||||
}
|
||||
|
||||
std::cout << "Escena Titol: Finalitzant...\n";
|
||||
}
|
||||
|
||||
void EscenaTitol::actualitzar(float delta_time) {
|
||||
switch (estat_actual_) {
|
||||
case EstatTitol::INIT:
|
||||
temps_acumulat_ += delta_time;
|
||||
if (temps_acumulat_ >= DURACIO_INIT) {
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
}
|
||||
break;
|
||||
case EstatTitol::MAIN:
|
||||
// No hi ha lògica d'actualització en l'estat MAIN
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaTitol::dibuixar() {
|
||||
// En l'estat INIT, no dibuixar res (pantalla negra)
|
||||
if (estat_actual_ == EstatTitol::INIT) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Estat MAIN: Dibuixar text de títol i copyright
|
||||
if (estat_actual_ == EstatTitol::MAIN) {
|
||||
// Text principal centrat (vertical i horitzontalment)
|
||||
const std::string main_text = "PRESS BUTTON TO PLAY";
|
||||
const float escala_main = 1.0f;
|
||||
const float spacing = 2.0f;
|
||||
|
||||
float text_width = text_.get_text_width(main_text, escala_main, spacing);
|
||||
float text_height = text_.get_text_height(escala_main);
|
||||
|
||||
float x_center = (Defaults::Game::WIDTH - text_width) / 2.0f;
|
||||
float y_center = (Defaults::Game::HEIGHT - text_height) / 2.0f;
|
||||
|
||||
text_.render(main_text, Punt{x_center, y_center}, escala_main, spacing);
|
||||
|
||||
// Copyright a la part inferior (centrat horitzontalment)
|
||||
// Convert to uppercase since VectorText only supports A-Z
|
||||
std::string copyright = Project::COPYRIGHT;
|
||||
for (char& c : copyright) {
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
c = c - 32; // Convert to uppercase
|
||||
}
|
||||
}
|
||||
const float escala_copy = 0.6f;
|
||||
|
||||
float copy_width = text_.get_text_width(copyright, escala_copy, spacing);
|
||||
float copy_height = text_.get_text_height(escala_copy);
|
||||
|
||||
float x_copy = (Defaults::Game::WIDTH - copy_width) / 2.0f;
|
||||
float y_copy = Defaults::Game::HEIGHT - copy_height - 20.0f; // 20px des del fons
|
||||
|
||||
text_.render(copyright, Punt{x_copy, y_copy}, escala_copy, spacing);
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaTitol::processar_events(const SDL_Event& event) {
|
||||
// Qualsevol tecla o clic de ratolí
|
||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
|
||||
switch (estat_actual_) {
|
||||
case EstatTitol::INIT:
|
||||
// Saltar a MAIN
|
||||
estat_actual_ = EstatTitol::MAIN;
|
||||
break;
|
||||
case EstatTitol::MAIN:
|
||||
// Anar al joc
|
||||
GestorEscenes::actual = GestorEscenes::Escena::JOC;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user