398 lines
10 KiB
C++
398 lines
10 KiB
C++
#include "logo.h"
|
|
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_QUIT, SDL...
|
|
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
|
#include <SDL2/SDL_video.h> // Para SDL_WINDOWEVENT_SIZE_CHANGED
|
|
#include <utility> // Para move
|
|
#include <iostream>
|
|
#include "global_inputs.h" // Para check
|
|
#include "input.h" // Para Input
|
|
#include "jail_audio.h" // Para JA_StopMusic
|
|
#include "param.h" // Para Param, ParamGame, param
|
|
#include "resource.h" // Para Resource
|
|
#include "screen.h" // Para Screen
|
|
#include "section.h" // Para Name, name, Options, options
|
|
#include "sprite.h" // Para Sprite
|
|
#include "texture.h" // Para Texture
|
|
#include "utils.h" // Para Color, Zone
|
|
#include "mouse.h"
|
|
|
|
// Constructor
|
|
Logo::Logo()
|
|
: since_texture_(Resource::get()->getTexture("logo_since_1998.png")),
|
|
since_sprite_(std::make_unique<Sprite>(since_texture_)),
|
|
jail_texture_(Resource::get()->getTexture("logo_jailgames.png"))
|
|
{
|
|
|
|
// Inicializa variables
|
|
counter_ = 0;
|
|
section::name = section::Name::LOGO;
|
|
dest_.x = param.game.game_area.center_x - jail_texture_->getWidth() / 2;
|
|
dest_.y = param.game.game_area.center_y - jail_texture_->getHeight() / 2;
|
|
since_sprite_->setPosition({(param.game.width - since_texture_->getWidth()) / 2, 83 + jail_texture_->getHeight() + 5, since_texture_->getWidth(), since_texture_->getHeight()});
|
|
since_sprite_->setY(dest_.y + jail_texture_->getHeight() + 5);
|
|
since_sprite_->setSpriteClip(0, 0, since_texture_->getWidth(), since_texture_->getHeight());
|
|
since_texture_->setColor(0x00, 0x00, 0x00); // Esto en linux no hace nada ??
|
|
|
|
// Crea los sprites de cada linea
|
|
for (int i = 0; i < jail_texture_->getHeight(); ++i)
|
|
{
|
|
auto temp = std::make_unique<Sprite>(jail_texture_, 0, i, jail_texture_->getWidth(), 1);
|
|
temp->setSpriteClip(0, i, jail_texture_->getWidth(), 1);
|
|
const int posX = (i % 2 == 0) ? param.game.width + (i * 3) : -jail_texture_->getWidth() - (i * 3);
|
|
temp->setX(posX);
|
|
temp->setY(dest_.y + i);
|
|
jail_sprite_.push_back(std::move(temp));
|
|
}
|
|
|
|
// Inicializa el vector de colores
|
|
color_.push_back(Color(0x00, 0x00, 0x00)); // Black
|
|
color_.push_back(Color(0x00, 0x00, 0xd8)); // Blue
|
|
color_.push_back(Color(0xd8, 0x00, 0x00)); // Red
|
|
color_.push_back(Color(0xd8, 0x00, 0xd8)); // Magenta
|
|
color_.push_back(Color(0x00, 0xd8, 0x00)); // Green
|
|
color_.push_back(Color(0x00, 0xd8, 0xd8)); // Cyan
|
|
color_.push_back(Color(0xd8, 0xd8, 0x00)); // Yellow
|
|
color_.push_back(Color(0xFF, 0xFF, 0xFF)); // Bright white
|
|
}
|
|
|
|
// Destructor
|
|
Logo::~Logo()
|
|
{
|
|
jail_texture_->setColor(255, 255, 255);
|
|
since_texture_->setColor(255, 255, 255);
|
|
JA_StopChannel(-1);
|
|
}
|
|
|
|
// Recarga todas las texturas
|
|
void Logo::reloadTextures()
|
|
{
|
|
jail_texture_->reLoad();
|
|
since_texture_->reLoad();
|
|
}
|
|
|
|
// Comprueba el manejador de eventos
|
|
void Logo::checkEvents()
|
|
{
|
|
SDL_Event event;
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
// Evento de salida de la aplicación
|
|
if (event.type == SDL_QUIT)
|
|
{
|
|
section::name = section::Name::QUIT;
|
|
section::options = section::Options::QUIT_FROM_EVENT;
|
|
break;
|
|
}
|
|
|
|
// Comprueba si se ha cambiado el tamaño de la ventana
|
|
else if (event.type == SDL_WINDOWEVENT)
|
|
{
|
|
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
|
|
{
|
|
reloadTextures();
|
|
}
|
|
}
|
|
|
|
// Comprueba el cursor
|
|
Mouse::handleEvent(event);
|
|
}
|
|
}
|
|
|
|
// Comprueba las entradas
|
|
void Logo::checkInput()
|
|
{
|
|
// Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
|
|
if (Input::get()->checkAnyButtonPressed())
|
|
{
|
|
section::name = section::Name::TITLE;
|
|
section::options = section::Options::TITLE_1;
|
|
return;
|
|
}
|
|
|
|
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
|
|
globalInputs::check();
|
|
}
|
|
|
|
// Gestiona el logo de JAILGAMES
|
|
void Logo::updateJAILGAMES()
|
|
{
|
|
if (counter_ == 30)
|
|
{
|
|
JA_PlaySound(Resource::get()->getSound("logo.wav"));
|
|
}
|
|
|
|
if (counter_ > 30)
|
|
{
|
|
for (int i = 0; i < (int)jail_sprite_.size(); ++i)
|
|
{
|
|
if (jail_sprite_[i]->getX() != dest_.x)
|
|
{
|
|
if (i % 2 == 0)
|
|
{
|
|
jail_sprite_[i]->incX(-SPEED);
|
|
if (jail_sprite_[i]->getX() < dest_.x)
|
|
{
|
|
jail_sprite_[i]->setX(dest_.x);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
jail_sprite_[i]->incX(SPEED);
|
|
if (jail_sprite_[i]->getX() > dest_.x)
|
|
{
|
|
jail_sprite_[i]->setX(dest_.x);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Comprueba si ha terminado el logo
|
|
if (counter_ == END_LOGO_COUNTER_MARK + POST_LOGO_DURATION)
|
|
{
|
|
state_ = LogoState::RETROWEEKEND;
|
|
ticks_start_ = SDL_GetTicks();
|
|
}
|
|
}
|
|
|
|
// Gestiona el logo de RETROWEEKEND
|
|
void Logo::updateRETROWEEKEND()
|
|
{
|
|
if (SDL_GetTicks() >= ticks_start_ + 5000)
|
|
{
|
|
section::name = section::Name::INTRO;
|
|
}
|
|
}
|
|
|
|
// Gestiona el color de las texturas
|
|
void Logo::updateTextureColors()
|
|
{
|
|
constexpr int inc = 4;
|
|
|
|
// Manejo de 'sinceTexture'
|
|
for (int i = 0; i <= 7; ++i)
|
|
{
|
|
if (counter_ == SHOW_SINCE_SPRITE_COUNTER_MARK + inc * i)
|
|
{
|
|
since_texture_->setColor(color_[i].r, color_[i].g, color_[i].b);
|
|
}
|
|
}
|
|
|
|
// Manejo de 'jailTexture' y 'sinceTexture' en el fade
|
|
for (int i = 0; i <= 6; ++i)
|
|
{
|
|
if (counter_ == INIT_FADE_COUNTER_MARK + inc * i)
|
|
{
|
|
jail_texture_->setColor(color_[6 - i].r, color_[6 - i].g, color_[6 - i].b);
|
|
since_texture_->setColor(color_[6 - i].r, color_[6 - i].g, color_[6 - i].b);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void Logo::update()
|
|
{
|
|
constexpr int TICKS_SPEED = 15;
|
|
|
|
if (SDL_GetTicks() - ticks_ > TICKS_SPEED)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks_ = SDL_GetTicks();
|
|
|
|
// Actualiza el objeto screen
|
|
Screen::get()->update();
|
|
|
|
// Comprueba las entradas
|
|
checkInput();
|
|
|
|
switch (state_)
|
|
{
|
|
case LogoState::JAILGAMES:
|
|
updateJAILGAMES();
|
|
updateTextureColors();
|
|
break;
|
|
|
|
case LogoState::RETROWEEKEND:
|
|
updateRETROWEEKEND();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
// Gestiona el contador
|
|
counter_++;
|
|
|
|
// Actualiza las variables de globalInputs
|
|
globalInputs::update();
|
|
}
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
void Logo::render()
|
|
{
|
|
Screen::get()->start();
|
|
Screen::get()->clean();
|
|
|
|
switch (state_)
|
|
{
|
|
case LogoState::JAILGAMES:
|
|
renderJAILGAMES();
|
|
break;
|
|
|
|
case LogoState::RETROWEEKEND:
|
|
renderRETROWEEKEND();
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
Screen::get()->render();
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
void Logo::run()
|
|
{
|
|
// Detiene la música
|
|
JA_FadeOutMusic(300);
|
|
|
|
while (section::name == section::Name::LOGO)
|
|
{
|
|
checkInput();
|
|
update();
|
|
checkEvents(); // Tiene que ir antes del render
|
|
render();
|
|
}
|
|
}
|
|
|
|
// Renderiza el logo de JAILGAMES
|
|
void Logo::renderJAILGAMES()
|
|
{
|
|
// Dibuja los sprites
|
|
for (auto &sprite : jail_sprite_)
|
|
{
|
|
sprite->render();
|
|
}
|
|
|
|
if (counter_ >= SHOW_SINCE_SPRITE_COUNTER_MARK)
|
|
{
|
|
since_sprite_->render();
|
|
}
|
|
}
|
|
|
|
// Renderiza el logo de RETROWEEKEND
|
|
void Logo::renderRETROWEEKEND()
|
|
{
|
|
// SDL_RenderCopy(Screen::get()->getRenderer(), mi_textura, nullptr, nullptr);
|
|
|
|
// SDL_RenderCopy(Screen::get()->getRenderer(), Resource::get()->getTexture("logo_retroweekend.png")->getSDLTexture(), nullptr, nullptr);
|
|
// Dibujar y evaporar los píxeles
|
|
renderAndEvaporate(Screen::get()->getRenderer(), pixels_);
|
|
}
|
|
|
|
std::vector<Pixel> Logo::extractPixels(SDL_Texture *texture)
|
|
{
|
|
std::vector<Pixel> pixels;
|
|
|
|
// Obtener el formato y tamaño de la textura
|
|
Uint32 format;
|
|
int access, width, height;
|
|
SDL_QueryTexture(texture, &format, &access, &width, &height);
|
|
|
|
// Imprimir el nombre del formato de la textura
|
|
std::cout << "Texture format: " << SDL_GetPixelFormatName(format) << std::endl;
|
|
|
|
// Crear una superficie temporal para leer los píxeles
|
|
SDL_Surface *temp_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, format);
|
|
if (!temp_surface)
|
|
{
|
|
std::cout << "Error: SDL_CreateRGBSurfaceWithFormat failed - " << SDL_GetError() << std::endl;
|
|
return pixels;
|
|
}
|
|
|
|
SDL_Renderer *renderer = Screen::get()->getRenderer();
|
|
SDL_SetRenderTarget(renderer, nullptr); // Dibujar en pantalla principal
|
|
SDL_RenderClear(renderer); // Limpiar pantalla
|
|
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
|
|
SDL_RenderPresent(renderer); // Mostrar en pantalla
|
|
|
|
std::cout << "Se ha dibujado la textura en pantalla." << std::endl;
|
|
|
|
if (SDL_RenderReadPixels(Screen::get()->getRenderer(), nullptr, format, temp_surface->pixels, temp_surface->pitch) != 0)
|
|
{
|
|
std::cout << "Error: SDL_RenderReadPixels failed - " << SDL_GetError() << std::endl;
|
|
SDL_FreeSurface(temp_surface);
|
|
return pixels;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "SDL_RenderReadPixels se ejecutó correctamente." << std::endl;
|
|
}
|
|
|
|
// Leer los píxeles de la textura
|
|
if (SDL_RenderReadPixels(Screen::get()->getRenderer(), nullptr, format, temp_surface->pixels, temp_surface->pitch) != 0)
|
|
{
|
|
std::cout << "Error: SDL_RenderReadPixels failed - " << SDL_GetError() << std::endl;
|
|
SDL_FreeSurface(temp_surface);
|
|
return pixels;
|
|
}
|
|
|
|
// Extraer los píxeles y almacenarlos en el vector
|
|
SDL_PixelFormat *pixel_format = SDL_AllocFormat(format);
|
|
Uint8 r, g, b, a;
|
|
/*for (int y = 0; y < height; ++y)
|
|
{
|
|
for (int x = 0; x < width; ++x)
|
|
{
|
|
Uint32 pixel_value = ((Uint32 *)temp_surface->pixels)[y * (temp_surface->pitch / 4) + x];
|
|
SDL_GetRGBA(pixel_value, pixel_format, &r, &g, &b, &a);
|
|
if (a > 0)
|
|
{
|
|
pixels.push_back({x, y, r, g, b, a});
|
|
}
|
|
}
|
|
}*/
|
|
|
|
for (int y = 0; y < height; ++y)
|
|
{
|
|
for (int x = 0; x < width; ++x)
|
|
{
|
|
Uint32 pixel_value = ((Uint32 *)temp_surface->pixels)[y * (temp_surface->pitch / 4) + x];
|
|
SDL_GetRGBA(pixel_value, pixel_format, &r, &g, &b, &a);
|
|
|
|
// Imprimir algunos píxeles para depuración
|
|
if (x < 5 && y < 5)
|
|
{
|
|
std::cout << "Pixel (" << x << "," << y << ") - RGBA("
|
|
<< (int)r << "," << (int)g << "," << (int)b << "," << (int)a << ")" << std::endl;
|
|
}
|
|
|
|
// No filtrar por alfa por ahora
|
|
pixels.push_back({x, y, r, g, b, a});
|
|
}
|
|
}
|
|
|
|
std::cout << "Cantidad de píxeles a renderizar: " << pixels.size() << std::endl;
|
|
|
|
// Liberar recursos
|
|
SDL_FreeFormat(pixel_format);
|
|
SDL_FreeSurface(temp_surface);
|
|
|
|
return pixels;
|
|
}
|
|
|
|
void Logo::renderAndEvaporate(SDL_Renderer *renderer, std::vector<Pixel> &pixels)
|
|
{
|
|
for (auto &pixel : pixels)
|
|
{
|
|
// Establecer el color para el píxel actual
|
|
SDL_SetRenderDrawColor(renderer, pixel.r, pixel.g, pixel.b, pixel.a);
|
|
// Dibujar el píxel
|
|
SDL_RenderDrawPoint(renderer, pixel.x, pixel.y);
|
|
// Evaporar el píxel
|
|
// pixel.evaporate();
|
|
}
|
|
}
|