From 33038e85096f304dc6eb3956de182b3bb17214cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Wed, 5 Mar 2025 07:44:12 +0100 Subject: [PATCH] canvi de pc --- source/asset.cpp | 4 ++ source/asset.h | 1 + source/credits.cpp | 6 +-- source/director.cpp | 10 ++--- source/ending.cpp | 10 ++--- source/ending2.cpp | 2 +- source/game.cpp | 2 +- source/game_over.cpp | 2 +- source/global_inputs.cpp | 3 +- source/loading_screen.cpp | 5 ++- source/logo.cpp | 8 ++-- source/notifier.cpp | 14 +++---- source/resource.cpp | 19 +++++---- source/room.cpp | 2 +- source/scoreboard.cpp | 2 +- source/screen.cpp | 33 ++++++++++++---- source/screen.h | 15 +++++--- source/surface.cpp | 81 +++++++++++++++++++++++++++++++-------- source/surface.h | 14 +++---- source/text.cpp | 4 +- source/title.cpp | 18 +++++---- source/title.h | 10 ++--- source/utils.cpp | 72 +--------------------------------- 23 files changed, 173 insertions(+), 164 deletions(-) diff --git a/source/asset.cpp b/source/asset.cpp index 01c26c6..5d3831a 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -152,6 +152,10 @@ std::string Asset::getTypeName(AssetType type) const return "TILEMAP"; break; + case AssetType::PALETTE: + return "PALETTE"; + break; + default: return "ERROR"; break; diff --git a/source/asset.h b/source/asset.h index a57d593..f52501a 100644 --- a/source/asset.h +++ b/source/asset.h @@ -14,6 +14,7 @@ enum class AssetType : int FONT, ROOM, TILEMAP, + PALETTE, MAX_ASSET_TYPE }; diff --git a/source/credits.cpp b/source/credits.cpp index e1d134e..4973409 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -143,7 +143,7 @@ void Credits::fillTexture() // Rellena la textura de texto Screen::get()->setRenderSurfaceData(text_surface_); - Screen::get()->clear(stringToColor("black")); + Screen::get()->clearSurface(stringToColor("black")); auto text = Resource::get()->getText("smb2"); @@ -168,7 +168,7 @@ void Credits::fillTexture() // Rellena la textura que cubre el texto con color transparente Screen::get()->setRenderSurfaceData(text_surface_); - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); // Los primeros 8 pixels crea una malla auto surface = Screen::get()->getRenderSurfaceData(); @@ -252,7 +252,7 @@ void Credits::render() Screen::get()->start(); // Limpia la pantalla - Screen::get()->clear(1); + Screen::get()->clearSurface(1); if (counter_ < 1150) { diff --git a/source/director.cpp b/source/director.cpp index ae766ca..ef49733 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -394,11 +394,11 @@ bool Director::setFileList() Asset::get()->add(prefix + "/data/font/8bithud.txt", AssetType::FONT); // Paletas - Asset::get()->add(prefix + "/data/palette/ruzx-spectrum-8x.gif", AssetType::DATA); - Asset::get()->add(prefix + "/data/palette/ruzx-spectrum-revision-2-8x.gif", AssetType::DATA); - Asset::get()->add(prefix + "/data/palette/zxarne-5-2-8x.gif", AssetType::DATA); - Asset::get()->add(prefix + "/data/palette/zx-spectrum-8x.gif", AssetType::DATA); - Asset::get()->add(prefix + "/data/palette/zx-spectrum-adjusted-8x.gif", AssetType::DATA); + Asset::get()->add(prefix + "/data/palette/zx-spectrum-8x.gif", AssetType::PALETTE); + Asset::get()->add(prefix + "/data/palette/zx-spectrum-adjusted-8x.gif", AssetType::PALETTE); + Asset::get()->add(prefix + "/data/palette/ruzx-spectrum-8x.gif", AssetType::PALETTE); + Asset::get()->add(prefix + "/data/palette/ruzx-spectrum-revision-2-8x.gif", AssetType::PALETTE); + Asset::get()->add(prefix + "/data/palette/zxarne-5-2-8x.gif", AssetType::PALETTE); // Shaders Asset::get()->add(prefix + "/data/shaders/crtpi_192.glsl", AssetType::DATA); diff --git a/source/ending.cpp b/source/ending.cpp index 0b4af9b..316773b 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -84,7 +84,7 @@ void Ending::render() Screen::get()->start(); // Limpia la pantalla - Screen::get()->clear(stringToColor("yellow")); + Screen::get()->clearSurface(stringToColor("yellow")); // Dibuja las imagenes de la escena sprite_pics_.at(current_scene_).image_sprite->render(); @@ -185,7 +185,7 @@ void Ending::iniTexts() Screen::get()->setRenderSurfaceData(st.cover_surface); // Rellena la cover_surface con color transparente - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); // Crea una malla de 8 pixels de alto auto surface = Screen::get()->getRenderSurfaceData(); @@ -252,7 +252,7 @@ void Ending::iniPics() Screen::get()->setRenderSurfaceData(sp.cover_surface); // Rellena la cover_surface con color transparente - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); // Crea una malla en los primeros 8 pixels auto surface = Screen::get()->getRenderSurfaceData(); @@ -473,14 +473,14 @@ void Ending::fillCoverTexture() { // Rellena la textura que cubre el texto con color transparente Screen::get()->setRenderSurfaceData(cover_surface_); - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); // Los primeros 8 pixels crea una malla const Uint8 color = stringToColor("black"); auto surface = Screen::get()->getRenderSurfaceData(); for (int i = 0; i < 256; i += 2) { - + cover_surface_->putPixel(surface, i + 0, options.game.height + 0, color); cover_surface_->putPixel(surface, i + 1, options.game.height + 1, color); cover_surface_->putPixel(surface, i + 0, options.game.height + 2, color); diff --git a/source/ending2.cpp b/source/ending2.cpp index 87e9e6f..cc13fca 100644 --- a/source/ending2.cpp +++ b/source/ending2.cpp @@ -101,7 +101,7 @@ void Ending2::render() Screen::get()->start(); // Limpia la pantalla - Screen::get()->clear(stringToColor("black")); + Screen::get()->clearSurface(stringToColor("black")); // Dibuja los sprites renderSprites(); diff --git a/source/game.cpp b/source/game.cpp index cda5fe0..c1075c7 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -565,7 +565,7 @@ void Game::fillRoomNameTexture() Screen::get()->setRenderSurfaceData(room_name_surface_); // Rellena la textura de color - Screen::get()->clear(stringToColor("white")); + Screen::get()->clearSurface(stringToColor("white")); // Escribe el texto en la textura auto text = Resource::get()->getText("smb2"); diff --git a/source/game_over.cpp b/source/game_over.cpp index dcc1712..e0bc08a 100644 --- a/source/game_over.cpp +++ b/source/game_over.cpp @@ -71,7 +71,7 @@ void GameOver::render() constexpr int Y = 32; Screen::get()->start(); - Screen::get()->clear(1); + Screen::get()->clearSurface(1); auto text = Resource::get()->getText("smb2"); diff --git a/source/global_inputs.cpp b/source/global_inputs.cpp index 77d7ce4..50b580e 100644 --- a/source/global_inputs.cpp +++ b/source/global_inputs.cpp @@ -27,7 +27,8 @@ namespace globalInputs // Cambia la paleta de colores void switchPalette() { - options.video.palette = options.video.palette == Palette::ZXSPECTRUM ? Palette::ZXARNE : Palette::ZXSPECTRUM; + //options.video.palette = options.video.palette == Palette::ZXSPECTRUM ? Palette::ZXARNE : Palette::ZXSPECTRUM; + Screen::get()->nextPalette(); } // Cambia de seccion diff --git a/source/loading_screen.cpp b/source/loading_screen.cpp index 770135f..332840a 100644 --- a/source/loading_screen.cpp +++ b/source/loading_screen.cpp @@ -141,7 +141,7 @@ void LoadingScreen::updateCounter() // Dibuja la pantalla de carga void LoadingScreen::renderLoad() { - loading_first_part_ ? mono_loading_screen_sprite_->render() : color_loading_screen_sprite_->render(); + loading_first_part_ ? mono_loading_screen_sprite_->render(1, stringToColor("black")) : color_loading_screen_sprite_->render(); } // Dibuja el efecto de carga en el borde @@ -149,7 +149,7 @@ void LoadingScreen::renderBorder() { // Pinta el borde de colro azul Uint8 color = stringToColor("blue"); - Screen::get()->clear(color); + Screen::get()->clearSurface(color); // Añade lineas amarillas color = stringToColor("yellow"); @@ -203,6 +203,7 @@ void LoadingScreen::render() // Prepara para empezar a dibujar en la textura de juego Screen::get()->start(); + Screen::get()->clearSurface(stringToColor("white")); // Copia la surface a la surface de Screen screen_surface_->render(0, 0); diff --git a/source/logo.cpp b/source/logo.cpp index d2736c9..1a9c9d8 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -98,7 +98,7 @@ void Logo::updateTextureColors() { /*constexpr int INI = 70; constexpr int INC = 4; - + if (counter_ == INI + INC * 0) { since_1998_surface_->setColor(color_.at(0).r, color_.at(0).g, color_.at(0).b); @@ -221,14 +221,14 @@ void Logo::render() Screen::get()->start(); // Limpia la pantalla - Screen::get()->clear(); + Screen::get()->clearSurface(); // Dibuja los objetos for (const auto &s : jailgames_sprite_) { - s->render(); + s->render(1, stringToColor("white")); } - since_1998_sprite_->render(); + since_1998_sprite_->render(1, stringToColor("white")); // Vuelca el contenido del renderizador en pantalla Screen::get()->render(); diff --git a/source/notifier.cpp b/source/notifier.cpp index 9e11728..9057250 100644 --- a/source/notifier.cpp +++ b/source/notifier.cpp @@ -86,7 +86,7 @@ void Notifier::update() { // const float step = ((float)notifications_[i].counter / notifications_[i].travel_dist); // const int alpha = 255 * step; - //constexpr int ALPHA = 255; + // constexpr int ALPHA = 255; if (options.notifications.getVerticalPosition() == NotificationPosition::TOP) { @@ -96,12 +96,12 @@ void Notifier::update() { notifications_[i].rect.y--; } - //notifications_[i].surface->setAlpha(ALPHA); + // notifications_[i].surface->setAlpha(ALPHA); if (notifications_[i].rect.y == notifications_[i].y) { notifications_[i].state = NotificationStatus::STAY; - //notifications_[i].surface->setAlpha(255); + // notifications_[i].surface->setAlpha(255); notifications_[i].counter = 0; } } @@ -119,7 +119,7 @@ void Notifier::update() // const float step = (notifications_[i].counter / (float)notifications_[i].travel_dist); // const int ALPHA = 255 * (1 - step); - //constexpr int ALPHA = 255; + // constexpr int ALPHA = 255; if (options.notifications.getVerticalPosition() == NotificationPosition::TOP) { @@ -129,7 +129,7 @@ void Notifier::update() { notifications_[i].rect.y++; } - //notifications_[i].surface->setAlpha(ALPHA); + // notifications_[i].surface->setAlpha(ALPHA); if (notifications_[i].rect.y == notifications_[i].y - notifications_[i].travel_dist) { @@ -259,7 +259,7 @@ void Notifier::show(std::vector texts, NotificationText text_is, in else if (shape == NotificationShape::SQUARED) { - Screen::get()->clear(bg_color_); + Screen::get()->clearSurface(bg_color_); } // Dibuja el icono de la notificación @@ -294,7 +294,7 @@ void Notifier::show(std::vector texts, NotificationText text_is, in n.sprite = std::make_shared(n.surface, n.rect); // Deja la notificación invisible - //n.surface->setAlpha(0); + // n.surface->setAlpha(0); // Añade la notificación a la lista notifications_.emplace_back(n); diff --git a/source/resource.cpp b/source/resource.cpp index f79e893..40849bf 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -405,17 +405,16 @@ void Resource::renderProgress() constexpr int BAR_HEIGHT = 10; const int bar_position = options.game.height - BAR_HEIGHT - Y_PADDING; Screen::get()->start(); - Screen::get()->clear(4); + Screen::get()->clearSurface(); + + auto surface = std::make_shared(Screen::get()->getRenderSurfaceData(), 1, 1); + const int wired_bar_width = options.game.width - (X_PADDING * 2); + SDL_Rect rect_wired = {X_PADDING, bar_position, wired_bar_width, X_PADDING}; + surface->fillRect(Screen::get()->getRenderSurfaceData(), &rect_wired, stringToColor("blue")); - //SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 255, 255, 255, 255); -// - //const int wired_bar_width = options.game.width - (X_PADDING * 2); - //SDL_Rect rect_wired = {X_PADDING, bar_position, wired_bar_width, X_PADDING}; - //SDL_RenderDrawRect(Screen::get()->getRenderer(), &rect_wired); -// - //const int full_bar_width = wired_bar_width * count_.getPercentage(); - //SDL_Rect rect_full = {X_PADDING, bar_position, full_bar_width, X_PADDING}; - //SDL_RenderFillRect(Screen::get()->getRenderer(), &rect_full); + const int full_bar_width = wired_bar_width * count_.getPercentage(); + SDL_Rect rect_full = {X_PADDING, bar_position, full_bar_width, X_PADDING}; + surface->fillRect(Screen::get()->getRenderSurfaceData(), &rect_full, stringToColor("white")); Screen::get()->renderWithoutNotifier(); } diff --git a/source/room.cpp b/source/room.cpp index ede431c..24069d8 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -475,7 +475,7 @@ void Room::fillMapTexture() { const Uint8 color = stringToColor(bg_color_); Screen::get()->setRenderSurfaceData(map_surface_); - Screen::get()->clear(color); + Screen::get()->clearSurface(color); // Los tileSetFiles son de 20x20 tiles. El primer tile es el 0. Cuentan hacia la derecha y hacia abajo diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 821c928..72210c5 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -139,7 +139,7 @@ void Scoreboard::fillTexture() Screen::get()->setRenderSurfaceData(surface_); // Limpia la textura - Screen::get()->clear(stringToColor("black")); + Screen::get()->clearSurface(stringToColor("black")); // Anclas constexpr int LINE1 = BLOCK; diff --git a/source/screen.cpp b/source/screen.cpp index ad306e4..dcf19d1 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -13,7 +13,7 @@ #include "notifier.h" // Para Notify #include "options.h" #include "mouse.h" -// #include "surface.h" +#include "surface.h" // [SINGLETON] Screen *Screen::screen_ = nullptr; @@ -39,7 +39,8 @@ Screen *Screen::get() // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) : window_(window), - renderer_(renderer) + renderer_(renderer), + palettes_(Asset::get()->getListByType(AssetType::PALETTE)) { // Ajusta los tamaños adjustGameCanvasRect(); @@ -53,7 +54,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) // Crea la textura donde se dibujan los graficos del juego game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); - //game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); + // game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); if (!game_texture_) { // Registrar el error si está habilitado @@ -65,7 +66,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) // Crea la textura donde se dibuja el borde que rodea el area de juego border_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); - //border_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); + // border_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); if (!border_texture_) { // Registrar el error si está habilitado @@ -77,11 +78,11 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) // Crea la surface donde se dibujan los graficos del juego game_surface_ = std::make_shared(nullptr, options.game.width, options.game.height); - game_surface_->loadPalette(Asset::get()->get("zx-spectrum-8x.gif")); + game_surface_->loadPalette(palettes_.front()); // Crea la surface donde se dibujan los graficos del juego border_surface_ = std::make_shared(nullptr, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); - border_surface_->loadPalette(Asset::get()->get("zx-spectrum-8x.gif")); + border_surface_->loadPalette(palettes_.front()); // Establece el modo de video setVideoMode(options.video.mode); @@ -106,7 +107,7 @@ void Screen::clear(Color color) } // Limpia la pantalla -void Screen::clear(Uint8 index) +void Screen::clearSurface(Uint8 index) { game_surface_->clear(index); } @@ -125,7 +126,7 @@ void Screen::render() game_surface_->copyToTexture(renderer_, game_texture_); // Renderiza sobre gameCanvas los overlays - //renderNotifications(); + // renderNotifications(); // Si está el borde activo, vuelca gameCanvas sobre borderCanvas if (options.video.border.enabled) @@ -140,6 +141,9 @@ void Screen::render() // Vuelca el contenido del renderizador en pantalla void Screen::renderWithoutNotifier() { + // Copia la surface a game_texture_ + game_surface_->copyToTexture(renderer_, game_texture_); + // Si está el borde activo, vuelca gameCanvas sobre borderCanvas if (options.video.border.enabled) { @@ -373,4 +377,17 @@ void Screen::resetShaders() void Screen::setRenderSurfaceData(std::shared_ptr surface) { (surface) ? game_surface_->redirectSurfaceDataTo(surface) : game_surface_->restoreOriginalSurfaceData(); +} + +// Cambia la paleta +void Screen::nextPalette() +{ + ++current_palette_; + if (current_palette_ == static_cast(palettes_.size())) + { + current_palette_ = 0; + } + + game_surface_->loadPalette(palettes_.at(current_palette_)); + border_surface_->loadPalette(palettes_.at(current_palette_)); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 176027c..4b6ff1d 100644 --- a/source/screen.h +++ b/source/screen.h @@ -34,10 +34,12 @@ private: std::shared_ptr border_surface_; // Objeto para trabajar con surfaces // Variables - int window_width_; // Ancho de la pantalla o ventana - int window_height_; // Alto de la pantalla o ventana - SDL_Rect game_rect_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana - Uint8 border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla + int window_width_; // Ancho de la pantalla o ventana + int window_height_; // Alto de la pantalla o ventana + SDL_Rect game_rect_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana + Uint8 border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla + std::vector palettes_; // Listado de los ficheros de paletta disponibles + Uint8 current_palette_ = 0; // Indice para el vector de paletas // Dibuja las notificaciones void renderNotifications(); @@ -78,7 +80,7 @@ public: // Limpia la pantalla void clear(Color color = {0x00, 0x00, 0x00}); - void clear(Uint8 index); + void clearSurface(Uint8 index = 1); // Prepara para empezar a dibujar en la textura de juego void start(); @@ -139,4 +141,7 @@ public: // Prepara para empezar a dibujar en la textura del borde void startDrawOnBorder() { setRenderSurfaceData(border_surface_); } + + // Cambia la paleta + void nextPalette(); }; \ No newline at end of file diff --git a/source/surface.cpp b/source/surface.cpp index 11e439b..2a24b83 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -10,25 +10,45 @@ #include "asset.h" // for Asset #include "gif.h" // for LoadGif, LoadPalette -Surface::Surface(std::shared_ptr surface_dest, int w, int h) - : surface_data_dest_(surface_dest), - surface_data_(std::make_shared(w, h)), - original_surface_data_(surface_data_), - transparent_color_(0) {} - -Surface::Surface(std::shared_ptr surface_dest, const std::string &file_path) - : surface_data_dest_(surface_dest), - transparent_color_(0) +Surface::Surface(std::shared_ptr surface_data_dest, int w, int h) { - // Carga la SurfaceData - SurfaceData loadedData = loadSurface(file_path); - surface_data_ = std::make_shared(std::move(loadedData)); + // Inicializar surface_data_ con un nuevo SurfaceData + surface_data_ = std::make_shared(w, h); - // Inicializa el puntero original + // Guardar la copia original de surface_data_ original_surface_data_ = surface_data_; + + // Si se proporciona un surface_data_dest, enlazamos surface_data_dest_ + if (surface_data_dest) + { + surface_data_dest_ = surface_data_dest; + } + else + { + // Si no se pasa otro puntero, surface_data_dest_ apunta al propio surface_data_ + surface_data_dest_ = surface_data_; + } } -Surface::~Surface() {} +Surface::Surface(std::shared_ptr surface_data_dest, const std::string &file_path) +{ + // Cargar surface_data_ desde el archivo + surface_data_ = std::make_shared(loadSurface(file_path)); + + // Guardar la copia original de surface_data_ + original_surface_data_ = surface_data_; + + // Si se proporciona un surface_data_dest, enlazamos surface_data_dest_ + if (surface_data_dest) + { + surface_data_dest_ = surface_data_dest; + } + else + { + // Si no se pasa otro puntero, surface_data_dest_ apunta al propio surface_data_ + surface_data_dest_ = surface_data_; + } +} // Carga una superficie desde un archivo SurfaceData Surface::loadSurface(const std::string &file_path) @@ -335,8 +355,6 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar // Limitar la región para evitar accesos fuera de rango w = std::min(w, surface_data_->width - sx); h = std::min(h, surface_data_->height - sy); - w = std::min(w, surface_data_dest_->width - x); - h = std::min(h, surface_data_dest_->height - y); // Renderiza píxel por píxel aplicando el flip si es necesario for (int iy = 0; iy < h; ++iy) @@ -351,6 +369,12 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar int dest_x = x + ix; int dest_y = y + iy; + // Verifica que las coordenadas de destino estén dentro de los límites + if (dest_x < 0 || dest_y < 0 || dest_x >= surface_data_dest_->width || dest_y >= surface_data_dest_->height) + { + continue; // Saltar píxeles fuera del rango del destino + } + // Copia el píxel si no es transparente Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width]; if (color != transparent_color_) @@ -430,3 +454,28 @@ bool Surface::fadePalette() // Devolver si el índice 15 coincide con el índice 0 return palette_[15] == palette_[0]; } + +// Método para redirigir surface_data_ al surface_data_ de otro objeto +void Surface::redirectSurfaceDataTo(const std::shared_ptr& newSurfaceData) +{ + // Guardar el surface_data_ original para poder restaurarlo + original_surface_data_ = surface_data_; + + // Redirigir surface_data_ al nuevo surface_data_ + surface_data_ = newSurfaceData; +} + +void Surface::redirectSurfaceDataTo(const std::shared_ptr &otherSurface) +{ + // Guardar el surface_data_ original para poder restaurarlo + original_surface_data_ = surface_data_; + + // Redirigir surface_data_ al nuevo surface_data_ + surface_data_ = otherSurface->getSurfaceData(); +} + +// Método para restaurar surface_data_ al valor original +void Surface::restoreOriginalSurfaceData() +{ + surface_data_ = original_surface_data_; +} diff --git a/source/surface.h b/source/surface.h index 3f364d9..d218628 100644 --- a/source/surface.h +++ b/source/surface.h @@ -68,11 +68,11 @@ private: public: // Constructor - Surface(std::shared_ptr surface_data_dest, int w, int h); + Surface(std::shared_ptr surface_data_dest = nullptr, int w = 0, int h = 0); Surface(std::shared_ptr surface_data_dest, const std::string &file_path); // Destructor - ~Surface(); + ~Surface() = default; // Carga una SurfaceData desde un archivo SurfaceData loadSurface(const std::string &file_path); @@ -124,10 +124,10 @@ public: void setPalette(const std::array &palette) { palette_ = palette; } void setSurface(std::shared_ptr surface) { surface_data_ = surface; } - // Permite que una Surface apunte al SurfaceData de otra Surface - void redirectSurfaceDataTo(const std::shared_ptr &newSurfaceData) { surface_data_ = newSurfaceData; } - void redirectSurfaceDataTo(const std::shared_ptr &otherSurface) { surface_data_ = otherSurface->getSurfaceData(); } + // Método para redirigir surface_data_ al surface_data_ de otro objeto + void redirectSurfaceDataTo(const std::shared_ptr &newSurfaceData); + void redirectSurfaceDataTo(const std::shared_ptr &otherSurface); - // Método para restaurar - void restoreOriginalSurfaceData() { surface_data_ = original_surface_data_; } + // Método para restaurar surface_data_ al valor original + void restoreOriginalSurfaceData(); }; diff --git a/source/text.cpp b/source/text.cpp index 94b511c..a744f5b 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -148,7 +148,7 @@ std::shared_ptr Text::writeToSurface(const std::string &text, int zoom, auto height = box_height_ * zoom; auto surface = std::make_shared(Screen::get()->getRenderSurfaceData(), width, height); Screen::get()->setRenderSurfaceData(surface); - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); write(0, 0, text, kerning); Screen::get()->setRenderSurfaceData(nullptr); @@ -162,7 +162,7 @@ std::shared_ptr Text::writeDXToSurface(Uint8 flags, const std::string & auto height = box_height_ + shadow_distance; auto surface = std::make_shared(Screen::get()->getRenderSurfaceData(), width, height); Screen::get()->setRenderSurfaceData(surface); - Screen::get()->clear(stringToColor("transparent")); + Screen::get()->clearSurface(stringToColor("transparent")); writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght); Screen::get()->setRenderSurfaceData(nullptr); diff --git a/source/title.cpp b/source/title.cpp index 4682954..54ad490 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -23,8 +23,8 @@ // Constructor Title::Title() - : surface_(Resource::get()->getSurface("title_logo.gif")), - sprite_(std::make_shared(surface_, 0, 0, surface_->getWidth(), surface_->getHeight())), + : title_logo_surface_(Resource::get()->getSurface("title_logo.gif")), + title_logo_sprite_(std::make_shared(title_logo_surface_, 0, 0, title_logo_surface_->getWidth(), title_logo_surface_->getHeight())), bg_surface_(std::make_shared(Screen::get()->getRenderSurfaceData(), options.game.width, options.game.height)) { // Carga la surface con los gráficos de la pantalla de carga @@ -33,6 +33,8 @@ Title::Title() pLoadPal(Asset::get()->get("loading_screen_color.gif").c_str()); pSetSource(loading_screen_); + //title_logo_surface_->setSurfaceDataDest(bg_surface_->getSurfaceData()); + // Inicializa variables state_ = options.section.subsection == Subsection::TITLE_WITH_LOADING_SCREEN ? TitleState::SHOW_LOADING_SCREEN : TitleState::SHOW_MENU; options.section.section = Section::TITLE; @@ -246,7 +248,7 @@ void Title::render() { // Prepara para empezar a dibujar en la textura de juego Screen::get()->start(); - Screen::get()->clear(stringToColor("black")); + Screen::get()->clearSurface(stringToColor("black")); if (state_ == TitleState::SHOW_MENU) { @@ -270,7 +272,7 @@ void Title::render() pFlip(Screen::get()->getRenderer()); // Dibuja el logo del título - sprite_->render(); + title_logo_sprite_->render(); } // Vuelca el contenido del renderizador en pantalla @@ -314,11 +316,10 @@ void Title::fillSurface() Screen::get()->setRenderSurfaceData(bg_surface_); // Rellena la textura de color - bg_surface_->setColor(255, 0xFF000000); - bg_surface_->clear(255); + Screen::get()->clearSurface(4); // Pinta el gráfico del titulo a partir del sprite - sprite_->render(); + title_logo_sprite_->render(); // Escribe el texto en la textura auto text = Resource::get()->getText("smb2"); @@ -328,6 +329,7 @@ void Title::fillSurface() text->writeDX(TEXT_CENTER | TEXT_COLOR, PLAY_AREA_CENTER_X, 13 * TEXT_SIZE, "2.ACHIEVEMENTS", 1, COLOR); text->writeDX(TEXT_CENTER | TEXT_COLOR, PLAY_AREA_CENTER_X, 15 * TEXT_SIZE, "3.REDEFINE KEYS", 1, COLOR); text->writeDX(TEXT_CENTER | TEXT_COLOR, PLAY_AREA_CENTER_X, 20 * TEXT_SIZE, "ESC.EXIT GAME", 1, COLOR); + text->writeColored(PLAY_AREA_CENTER_X, 30 * TEXT_SIZE, "ESC.EXIT GAME", COLOR); // Devuelve el puntero del renderizador a su sitio Screen::get()->setRenderSurfaceData(nullptr); @@ -352,7 +354,7 @@ void Title::createCheevosTexture() // Rellena la textura con color sólido const Uint8 CHEEVOS_BG_COLOR = stringToColor("black"); - Screen::get()->clear(CHEEVOS_BG_COLOR); + Screen::get()->clearSurface(CHEEVOS_BG_COLOR); // Escribe la lista de logros en la textura const std::string CHEEVOS_OWNER = "ACHIEVEMENTS"; diff --git a/source/title.h b/source/title.h index e53b440..619fd0e 100644 --- a/source/title.h +++ b/source/title.h @@ -28,11 +28,11 @@ private: }; // Objetos y punteros - std::shared_ptr surface_; // Textura con los graficos - std::shared_ptr sprite_; // SSprite para manejar la surface - std::shared_ptr bg_surface_; // Textura para dibujar el fondo de la pantalla - std::shared_ptr cheevos_surface_; // Textura con la lista de logros - std::shared_ptr cheevos_sprite_; // SSprite para manejar la surface con la lista de logros + std::shared_ptr title_logo_surface_; // Textura con los graficos + std::shared_ptr title_logo_sprite_; // SSprite para manejar la surface + std::shared_ptr bg_surface_; // Textura para dibujar el fondo de la pantalla + std::shared_ptr cheevos_surface_; // Textura con la lista de logros + std::shared_ptr cheevos_sprite_; // SSprite para manejar la surface con la lista de logros // Variables int counter_ = 0; // Contador diff --git a/source/utils.cpp b/source/utils.cpp index fdd143d..287a7f2 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -358,76 +358,6 @@ bool checkCollision(const SDL_Point &p, const LineDiagonal &l) return true; } -// Convierte una cadena a un color -Color stringToColor(Palette pal, const std::string &str) -{ - // Mapas de colores para cada paleta - static const std::unordered_map zxSpectrumColors = { - {"black", {0x00, 0x00, 0x00}}, - {"bright_black", {0x00, 0x00, 0x00}}, - {"blue", {0x00, 0x00, 0xD8}}, - {"bright_blue", {0x00, 0x00, 0xFF}}, - {"red", {0xD8, 0x00, 0x00}}, - {"bright_red", {0xFF, 0x00, 0x00}}, - {"magenta", {0xD8, 0x00, 0xD8}}, - {"bright_magenta", {0xFF, 0x00, 0xFF}}, - {"green", {0x00, 0xD8, 0x00}}, - {"bright_green", {0x00, 0xFF, 0x00}}, - {"cyan", {0x00, 0xD8, 0xD8}}, - {"bright_cyan", {0x00, 0xFF, 0xFF}}, - {"yellow", {0xD8, 0xD8, 0x00}}, - {"bright_yellow", {0xFF, 0xFF, 0x00}}, - {"white", {0xD8, 0xD8, 0xD8}}, - {"bright_white", {0xFF, 0xFF, 0xFF}}}; - - static const std::unordered_map zxArneColors = { - {"black", {0x00, 0x00, 0x00}}, - {"bright_black", {0x3C, 0x35, 0x1F}}, - {"blue", {0x31, 0x33, 0x90}}, - {"bright_blue", {0x15, 0x59, 0xDB}}, - {"red", {0xA7, 0x32, 0x11}}, - {"bright_red", {0xD8, 0x55, 0x25}}, - {"magenta", {0xA1, 0x55, 0x89}}, - {"bright_magenta", {0xCD, 0x7A, 0x50}}, - {"green", {0x62, 0x9A, 0x31}}, - {"bright_green", {0x9C, 0xD3, 0x3C}}, - {"cyan", {0x28, 0xA4, 0xCB}}, - {"bright_cyan", {0x65, 0xDC, 0xD6}}, - {"yellow", {0xE8, 0xBC, 0x50}}, - {"bright_yellow", {0xF1, 0xE7, 0x82}}, - {"white", {0xBF, 0xBF, 0xBD}}, - {"bright_white", {0xF2, 0xF1, 0xED}}}; - - // Selecciona el mapa de colores adecuado según la paleta - const std::unordered_map *paletteMap = nullptr; - - if (pal == Palette::ZXSPECTRUM) - { - paletteMap = &zxSpectrumColors; - } - else if (pal == Palette::ZXARNE) - { - paletteMap = &zxArneColors; - } - else - { - // Paleta desconocida, devolvemos negro por defecto - return {0x00, 0x00, 0x00}; - } - - // Busca el color en el mapa - auto it = paletteMap->find(str); - if (it != paletteMap->end()) - { - return it->second; - } - else - { - // Si no se encuentra el color, devolvemos negro por defecto - return {0x00, 0x00, 0x00}; - } -} - // Convierte una cadena a un indice de la paleta Uint8 stringToColor(const std::string &str) { @@ -460,7 +390,7 @@ Uint8 stringToColor(const std::string &str) else { // Si no se encuentra el color, devolvemos negro por defecto - return {0}; + return 0; } }