forked from jaildesigner-jailgames/jaildoctors_dilemma
Redistribuits els .cpp en carpetes
Actualitzat cmake Modificats els include de SDL2 a SDL3
This commit is contained in:
226
source/sections/logo.cpp
Normal file
226
source/sections/logo.cpp
Normal file
@@ -0,0 +1,226 @@
|
||||
#include "logo.h"
|
||||
|
||||
#include <SDL3/SDL_events.h> // Para SDL_PollEvent, SDL_Event
|
||||
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks
|
||||
|
||||
#include "defines.h" // Para GAME_SPEED
|
||||
#include "global_events.h" // Para check
|
||||
#include "global_inputs.h" // Para check
|
||||
#include "options.h" // Para Options, SectionState, options, Section
|
||||
#include "resource.h" // Para Resource
|
||||
#include "s_sprite.h" // Para SSprite
|
||||
#include "screen.h" // Para Screen
|
||||
#include "surface.h" // Para Surface
|
||||
#include "utils.h" // Para PaletteColor
|
||||
|
||||
// Constructor
|
||||
Logo::Logo()
|
||||
: jailgames_surface_(Resource::get()->getSurface("jailgames.gif")),
|
||||
since_1998_surface_(Resource::get()->getSurface("since_1998.gif")),
|
||||
since_1998_sprite_(std::make_shared<SSprite>(since_1998_surface_, (256 - since_1998_surface_->getWidth()) / 2, 83 + jailgames_surface_->getHeight() + 5, since_1998_surface_->getWidth(), since_1998_surface_->getHeight())) {
|
||||
since_1998_sprite_->setClip(0, 0, since_1998_surface_->getWidth(), since_1998_surface_->getHeight());
|
||||
since_1998_color_ = static_cast<Uint8>(PaletteColor::BLACK);
|
||||
jailgames_color_ = static_cast<Uint8>(PaletteColor::BRIGHT_WHITE);
|
||||
|
||||
// Crea los sprites de cada linea
|
||||
for (int i = 0; i < jailgames_surface_->getHeight(); ++i) {
|
||||
jailgames_sprite_.push_back(std::make_shared<SSprite>(jailgames_surface_, 0, i, jailgames_surface_->getWidth(), 1));
|
||||
jailgames_sprite_.back()->setClip(0, i, jailgames_surface_->getWidth(), 1);
|
||||
jailgames_sprite_.at(i)->setX((i % 2 == 0) ? (256 + (i * 3)) : (-181 - (i * 3)));
|
||||
jailgames_sprite_.at(i)->setY(83 + i);
|
||||
}
|
||||
|
||||
// Inicializa variables
|
||||
options.section.section = Section::LOGO;
|
||||
|
||||
// Inicializa el vector de colores
|
||||
const std::vector<Uint8> COLORS = {
|
||||
static_cast<Uint8>(PaletteColor::BLACK),
|
||||
static_cast<Uint8>(PaletteColor::BLUE),
|
||||
static_cast<Uint8>(PaletteColor::RED),
|
||||
static_cast<Uint8>(PaletteColor::MAGENTA),
|
||||
static_cast<Uint8>(PaletteColor::GREEN),
|
||||
static_cast<Uint8>(PaletteColor::CYAN),
|
||||
static_cast<Uint8>(PaletteColor::YELLOW),
|
||||
static_cast<Uint8>(PaletteColor::BRIGHT_WHITE)};
|
||||
for (const auto& color : COLORS) {
|
||||
color_.push_back(color);
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
}
|
||||
|
||||
// Comprueba el manejador de eventos
|
||||
void Logo::checkEvents() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
globalEvents::check(event);
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas
|
||||
void Logo::checkInput() {
|
||||
globalInputs::check();
|
||||
}
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
void Logo::updateJAILGAMES() {
|
||||
if (counter_ > 30) {
|
||||
for (int i = 1; i < (int)jailgames_sprite_.size(); ++i) {
|
||||
constexpr int SPEED = 8;
|
||||
constexpr int DEST = 37;
|
||||
if (jailgames_sprite_.at(i)->getX() != 37) {
|
||||
if (i % 2 == 0) {
|
||||
jailgames_sprite_.at(i)->incX(-SPEED);
|
||||
if (jailgames_sprite_.at(i)->getX() < DEST) {
|
||||
jailgames_sprite_.at(i)->setX(DEST);
|
||||
}
|
||||
} else {
|
||||
jailgames_sprite_.at(i)->incX(SPEED);
|
||||
if (jailgames_sprite_.at(i)->getX() > DEST) {
|
||||
jailgames_sprite_.at(i)->setX(DEST);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
void Logo::updateTextureColors() {
|
||||
constexpr int INI = 70;
|
||||
constexpr int INC = 4;
|
||||
|
||||
if (counter_ == INI + INC * 0) {
|
||||
since_1998_color_ = color_.at(0);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 1) {
|
||||
since_1998_color_ = color_.at(1);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 2) {
|
||||
since_1998_color_ = color_.at(2);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 3) {
|
||||
since_1998_color_ = color_.at(3);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 4) {
|
||||
since_1998_color_ = color_.at(4);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 5) {
|
||||
since_1998_color_ = color_.at(5);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 6) {
|
||||
since_1998_color_ = color_.at(6);
|
||||
}
|
||||
|
||||
else if (counter_ == INI + INC * 7) {
|
||||
since_1998_color_ = color_.at(7);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 0) {
|
||||
jailgames_color_ = color_.at(6);
|
||||
since_1998_color_ = color_.at(6);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 1) {
|
||||
jailgames_color_ = color_.at(5);
|
||||
since_1998_color_ = color_.at(5);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 2) {
|
||||
jailgames_color_ = color_.at(4);
|
||||
since_1998_color_ = color_.at(4);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 3) {
|
||||
jailgames_color_ = color_.at(3);
|
||||
since_1998_color_ = color_.at(3);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 4) {
|
||||
jailgames_color_ = color_.at(2);
|
||||
since_1998_color_ = color_.at(2);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 5) {
|
||||
jailgames_color_ = color_.at(1);
|
||||
since_1998_color_ = color_.at(1);
|
||||
}
|
||||
|
||||
else if (counter_ == INIT_FADE_ + INC * 6) {
|
||||
jailgames_color_ = color_.at(0);
|
||||
since_1998_color_ = color_.at(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables
|
||||
void Logo::update() {
|
||||
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||
if (SDL_GetTicks() - ticks_ > GAME_SPEED) {
|
||||
// Actualiza el contador de ticks
|
||||
ticks_ = SDL_GetTicks();
|
||||
|
||||
// Comprueba las entradas
|
||||
checkInput();
|
||||
|
||||
// Incrementa el contador
|
||||
counter_++;
|
||||
|
||||
// Gestiona el logo de JAILGAME
|
||||
updateJAILGAMES();
|
||||
|
||||
// Gestiona el color de las texturas
|
||||
updateTextureColors();
|
||||
|
||||
// Actualiza el objeto Screen
|
||||
Screen::get()->update();
|
||||
|
||||
// Comprueba si ha terminado el logo
|
||||
if (counter_ == END_LOGO_ + POST_LOGO_) {
|
||||
endSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja en pantalla
|
||||
void Logo::render() {
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
Screen::get()->start();
|
||||
Screen::get()->clearSurface(static_cast<Uint8>(PaletteColor::BLACK));
|
||||
|
||||
// Dibuja los objetos
|
||||
for (const auto& s : jailgames_sprite_) {
|
||||
s->render(1, jailgames_color_);
|
||||
}
|
||||
since_1998_sprite_->render(1, since_1998_color_);
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
Screen::get()->render();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
void Logo::run() {
|
||||
while (options.section.section == Section::LOGO) {
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
// Termina la sección
|
||||
void Logo::endSection() {
|
||||
if (options.section.subsection == Subsection::LOGO_TO_TITLE) {
|
||||
options.section.section = Section::TITLE;
|
||||
}
|
||||
|
||||
else if (options.section.subsection == Subsection::LOGO_TO_INTRO) {
|
||||
options.section.section = Section::LOADING_SCREEN;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user