Transició a surface: vaig per title.cpp

This commit is contained in:
2025-03-02 21:56:19 +01:00
parent db3a0d7263
commit 8f1d1df5d6
27 changed files with 416 additions and 490 deletions

View File

@@ -1,40 +1,36 @@
#include "loading_screen.h"
#include <SDL2/SDL_error.h> // for SDL_GetError
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <stdlib.h> // for rand
#include <iostream> // for char_traits, basic_ostream, operator<<
#include "defines.h" // for GAME_SPEED
#include "global_events.h" // for check
#include "global_inputs.h" // for check
#include "jail_audio.h" // for JA_PlayMusic, JA_SetVolume, JA_StopMusic
#include "options.h" // for Options, options, OptionsVideo, Section...
#include "resource.h" // for Resource
#include "screen.h" // for Screen
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
#include "utils.h" // for Color, stringToColor, Palette
#include <SDL2/SDL_error.h> // for SDL_GetError
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <stdlib.h> // for rand
#include <iostream> // for char_traits, basic_ostream, operator<<
#include "defines.h" // for GAME_SPEED
#include "global_events.h" // for check
#include "global_inputs.h" // for check
#include "jail_audio.h" // for JA_PlayMusic, JA_SetVolume, JA_StopMusic
#include "options.h" // for Options, options, OptionsVideo, Section...
#include "resource.h" // for Resource
#include "screen.h" // for Screen
#include "s_sprite.h" // for SSprite
#include "surface.h" // for Texture
#include "utils.h" // for Color, stringToColor, Palette
// Constructor
LoadingScreen::LoadingScreen()
: mono_loading_screen_surface_(Resource::get()->getSurface("loading_screen_bn.gif")),
color_loading_screen_surface_(Resource::get()->getSurface("loading_screen_color.gif")),
mono_loading_screen_sprite_(std::make_shared<SSprite>(mono_loading_screen_surface_, 0, 0, mono_loading_screen_surface_->getWidth(), mono_loading_screen_surface_->getHeight())),
color_loading_screen_sprite_(std::make_shared<SSprite>(color_loading_screen_surface_, 0, 0, color_loading_screen_surface_->getWidth(), color_loading_screen_surface_->getHeight())),
screen_surface_(std::make_shared<Surface>(Screen::get()->getSurface(), options.game.width, options.game.height))
{
// Reserva memoria para los punteros
if (options.video.palette == Palette::ZXSPECTRUM)
{
mono_loading_screen_texture_ = Resource::get()->getTexture("loading_screen_bn.png");
color_loading_screen_texture_ = Resource::get()->getTexture("loading_screen_color.png");
}
else if (options.video.palette == Palette::ZXARNE)
{
mono_loading_screen_texture_ = Resource::get()->getTexture("loading_screen_bn_zxarne.png");
color_loading_screen_texture_ = Resource::get()->getTexture("loading_screen_color_zxarne.png");
}
mono_loading_screen_sprite_ = std::make_shared<Sprite>(mono_loading_screen_texture_, 0, 0, mono_loading_screen_texture_->getWidth(), mono_loading_screen_texture_->getHeight());
color_loading_screen_sprite_ = std::make_shared<Sprite>(color_loading_screen_texture_, 0, 0, color_loading_screen_texture_->getWidth(), color_loading_screen_texture_->getHeight());
// Cambia el destino de las surfaces
mono_loading_screen_surface_->setSurfaceDest(screen_surface_->getSurface());
color_loading_screen_surface_->setSurfaceDest(screen_surface_->getSurface());
texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height);
clearTexture();
// Configura la superficie donde se van a pintar los sprites
screen_surface_->setColor(0, 0xFF000000);
screen_surface_->clear(0);
// Inicializa variables
options.section.section = Section::LOADING_SCREEN;
@@ -65,7 +61,6 @@ LoadingScreen::LoadingScreen()
LoadingScreen::~LoadingScreen()
{
JA_StopMusic();
SDL_DestroyTexture(texture_);
}
// Comprueba el manejador de eventos
@@ -152,32 +147,32 @@ void LoadingScreen::renderLoad()
// Dibuja el efecto de carga en el borde
void LoadingScreen::renderBorder()
{
// Pinta el borde de colro azul
Color color = stringToColor(options.video.palette, "blue");
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
SDL_RenderClear(Screen::get()->getRenderer());
// Pinta el borde de colro azul
Color color = stringToColor(options.video.palette, "blue");
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
SDL_RenderClear(Screen::get()->getRenderer());
// Añade lineas amarillas
color = stringToColor(options.video.palette, "yellow");
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
const int WIDTH = options.game.width + (options.video.border.width * 2);
const int HEIGHT = options.game.height + (options.video.border.height * 2);
bool drawEnabled = rand() % 2 == 0 ? true : false;
// Añade lineas amarillas
color = stringToColor(options.video.palette, "yellow");
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
const int WIDTH = options.game.width + (options.video.border.width * 2);
const int HEIGHT = options.game.height + (options.video.border.height * 2);
bool drawEnabled = rand() % 2 == 0 ? true : false;
int row = 0;
while (row < HEIGHT)
{
const int ROW_HEIGHT = (rand() % 4) + 3;
if (drawEnabled)
{
for (int i = row; i < row + ROW_HEIGHT; ++i)
{
SDL_RenderDrawLine(Screen::get()->getRenderer(), 0, i, WIDTH, i);
}
}
row += ROW_HEIGHT;
drawEnabled = !drawEnabled;
}
int row = 0;
while (row < HEIGHT)
{
const int ROW_HEIGHT = (rand() % 4) + 3;
if (drawEnabled)
{
for (int i = row; i < row + ROW_HEIGHT; ++i)
{
SDL_RenderDrawLine(Screen::get()->getRenderer(), 0, i, WIDTH, i);
}
}
row += ROW_HEIGHT;
drawEnabled = !drawEnabled;
}
}
// Actualiza las variables
@@ -190,7 +185,7 @@ void LoadingScreen::update()
checkInput();
updateCounter();
updateLoad();
fillTexture();
renderLoad();
Screen::get()->update();
}
}
@@ -210,8 +205,8 @@ void LoadingScreen::render()
// Prepara para empezar a dibujar en la textura de juego
Screen::get()->start();
// Copila la textura a la pantalla
SDL_RenderCopy(Screen::get()->getRenderer(), texture_, nullptr, nullptr);
// Copia la surface a la surface de Screen
screen_surface_->render(0, 0);
// Vuelca el contenido del renderizador en pantalla
Screen::get()->render();
@@ -237,33 +232,4 @@ void LoadingScreen::run()
}
JA_SetVolume(128);
}
// Dibuja sobre la textura
void LoadingScreen::fillTexture()
{
// Empieza a dibujar en la textura
auto temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
SDL_SetRenderTarget(Screen::get()->getRenderer(), texture_);
// Dibuja la pantalla de carga
renderLoad();
// Deja el renderizador como estaba
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
}
// Limpia la textura
void LoadingScreen::clearTexture()
{
// Empieza a dibujar en la textura
auto temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
SDL_SetRenderTarget(Screen::get()->getRenderer(), texture_);
// Limpia
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(Screen::get()->getRenderer());
// Deja el renderizador como estaba
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
}