From b3200305474c4f9da03aeabd9d3ceadfa83f9f9a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 3 Mar 2025 20:48:31 +0100 Subject: [PATCH] =?UTF-8?q?Transici=C3=B3=20a=20surface:=20ending.cpp=20fe?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/ending.cpp | 135 ++++++++++++++++--------------------- source/ending.h | 14 ++-- source/game_over.cpp | 15 ++--- source/game_over.h | 10 +-- source/s_moving_sprite.cpp | 6 ++ source/s_moving_sprite.h | 1 + source/s_sprite.cpp | 5 ++ source/s_sprite.h | 1 + source/surface.h | 2 +- source/text.cpp | 61 +++++++++-------- source/text.h | 12 ++-- 11 files changed, 128 insertions(+), 134 deletions(-) diff --git a/source/ending.cpp b/source/ending.cpp index 09c89fc..ad4afc7 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -14,9 +14,9 @@ #include "options.h" // for Options, options, OptionsVideo, Sect... #include "resource.h" // for Resource #include "screen.h" // for Screen -#include "s_sprite.h" // for Sprite +#include "s_sprite.h" // for SSprite #include "text.h" // for Text, TEXT_STROKE -#include "surface.h" // for Texture +#include "surface.h" // for Surface #include "utils.h" // for Color, stringToColor, Palette // Constructor @@ -43,8 +43,8 @@ Ending::Ending() Screen::get()->setBorderColor(stringToColor(options.video.palette, "black")); // Crea la textura para cubrir el texto - cover_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height + 8); - SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND); + cover_surface_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height + 8); + SDL_SetTextureBlendMode(cover_surface_, SDL_BLENDMODE_BLEND); // Rellena la textura para la cortinilla fillCoverTexture(); @@ -53,7 +53,7 @@ Ending::Ending() // Destructor Ending::~Ending() { - SDL_DestroyTexture(cover_texture_); + SDL_DestroyTexture(cover_surface_); } // Actualiza el objeto @@ -174,61 +174,55 @@ void Ending::iniTexts() const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2; const int HEIGHT = text->getCharacterSize() + 2 + 2; - Color color = stringToColor(options.video.palette, "black"); + Uint8 color = stringToColor("black"); EndingTexture st; // Crea la textura - st.image_texture = std::make_shared(Screen::get()->getRenderer()); - st.image_texture->createBlank(WIDTH, HEIGHT); - st.image_texture->setAsRenderTarget(Screen::get()->getRenderer()); - st.image_texture->setBlendMode(SDL_BLENDMODE_BLEND); + st.image_surface = std::make_shared(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT); + Screen::get()->setRenderSurfaceData(st.image_surface); text->writeDX(TEXT_STROKE, 2, 2, txt.caption, 1, color, 2, color); // Crea el sprite - st.image_sprite = std::make_shared(st.image_texture, 0, 0, st.image_texture->getWidth(), st.image_texture->getHeight()); - st.image_sprite->setPosition((options.game.width - st.image_texture->getWidth()) / 2, txt.pos); + st.image_sprite = std::make_shared(st.image_surface, 0, 0, st.image_surface->getWidth(), st.image_surface->getHeight()); + st.image_sprite->setPosition((options.game.width - st.image_surface->getWidth()) / 2, txt.pos); - // Crea la coverTexture - st.cover_texture = std::make_shared(Screen::get()->getRenderer()); - st.cover_texture->createBlank(WIDTH, HEIGHT + 8); - st.cover_texture->setAsRenderTarget(Screen::get()->getRenderer()); - st.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND); + // Crea la cover_surface + st.cover_surface = std::make_shared(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT + 8); + Screen::get()->setRenderSurfaceData(st.cover_surface); - // Rellena la coverTexture con color transparente - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); - SDL_RenderClear(Screen::get()->getRenderer()); + // Rellena la cover_surface con color transparente + Screen::get()->clean(stringToColor("transparent")); // Crea una malla de 8 pixels de alto - color = stringToColor(options.video.palette, "black"); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF); + auto surface = Screen::get()->getRenderSurfaceData(); + color = stringToColor("black"); for (int i = 0; i < WIDTH; i += 2) { - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6); + st.cover_surface->putPixel(surface, i, 0, color); + st.cover_surface->putPixel(surface, i, 2, color); + st.cover_surface->putPixel(surface, i, 4, color); + st.cover_surface->putPixel(surface, i, 6, color); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7); + st.cover_surface->putPixel(surface, i + 1, 5, color); + st.cover_surface->putPixel(surface, i + 1, 7, color); } // El resto se rellena de color sólido SDL_Rect rect = {0, 8, WIDTH, HEIGHT}; - color = stringToColor(options.video.palette, "black"); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF); - SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); + st.cover_surface->fillRect(surface, &rect, color); // Crea el sprite - st.cover_sprite = std::make_shared(st.cover_texture, 0, 0, st.cover_texture->getWidth(), st.cover_texture->getHeight() - 8); - st.cover_sprite->setPosition((options.game.width - st.cover_texture->getWidth()) / 2, txt.pos); - st.cover_sprite->setClip(0, 8, st.cover_texture->getWidth(), st.cover_texture->getHeight()); + st.cover_sprite = std::make_shared(st.cover_surface, 0, 0, st.cover_surface->getWidth(), st.cover_surface->getHeight() - 8); + st.cover_sprite->setPosition((options.game.width - st.cover_surface->getWidth()) / 2, txt.pos); + st.cover_sprite->setClip(0, 8, st.cover_surface->getWidth(), st.cover_surface->getHeight()); // Inicializa variables st.cover_clip_desp = 8; st.cover_clip_height = HEIGHT; sprite_texts_.push_back(st); + Screen::get()->setRenderSurfaceData(nullptr); } } @@ -238,22 +232,11 @@ void Ending::iniPics() // Vector con las rutas y la posición std::vector pics; - if (options.video.palette == Palette::ZXSPECTRUM) - { - pics.push_back({"ending1.gif", 48}); - pics.push_back({"ending2.gif", 26}); - pics.push_back({"ending3.gif", 29}); - pics.push_back({"ending4.gif", 63}); - pics.push_back({"ending5.gif", 53}); - } - else - { - pics.push_back({"ending1_zxarne.gif", 48}); - pics.push_back({"ending2_zxarne.gif", 26}); - pics.push_back({"ending3_zxarne.gif", 29}); - pics.push_back({"ending4_zxarne.gif", 63}); - pics.push_back({"ending5_zxarne.gif", 53}); - } + pics.push_back({"ending1.gif", 48}); + pics.push_back({"ending2.gif", 26}); + pics.push_back({"ending3.gif", 29}); + pics.push_back({"ending4.gif", 63}); + pics.push_back({"ending5.gif", 53}); // Crea los sprites sprite_pics_.clear(); @@ -263,54 +246,50 @@ void Ending::iniPics() EndingTexture sp; // Crea la texture - sp.image_texture = Resource::get()->getSurface(pic.caption); - const int WIDTH = sp.image_texture->getWidth(); - const int HEIGHT = sp.image_texture->getHeight(); + sp.image_surface = Resource::get()->getSurface(pic.caption); + const int WIDTH = sp.image_surface->getWidth(); + const int HEIGHT = sp.image_surface->getHeight(); // Crea el sprite - sp.image_sprite = std::make_shared(sp.image_texture, 0, 0, WIDTH, HEIGHT); + sp.image_sprite = std::make_shared(sp.image_surface, 0, 0, WIDTH, HEIGHT); sp.image_sprite->setPosition((options.game.width - WIDTH) / 2, pic.pos); - // Crea la coverTexture - sp.cover_texture = std::make_shared(Screen::get()->getRenderer()); - sp.cover_texture->createBlank(WIDTH, HEIGHT + 8); - sp.cover_texture->setAsRenderTarget(Screen::get()->getRenderer()); - sp.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND); + // Crea la cover_surface + sp.cover_surface = std::make_shared(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT + 8); + Screen::get()->setRenderSurfaceData(sp.cover_surface); - // Rellena la coverTexture con color transparente - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); - SDL_RenderClear(Screen::get()->getRenderer()); + // Rellena la cover_surface con color transparente + Screen::get()->clean(stringToColor("transparent")); // Crea una malla en los primeros 8 pixels - Color c = stringToColor(options.video.palette, "black"); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF); + auto surface = Screen::get()->getRenderSurfaceData(); + auto color = stringToColor("black"); for (int i = 0; i < WIDTH; i += 2) { - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6); + sp.cover_surface->putPixel(surface, i, 0, color); + sp.cover_surface->putPixel(surface, i, 2, color); + sp.cover_surface->putPixel(surface, i, 4, color); + sp.cover_surface->putPixel(surface, i, 6, color); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5); - SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7); + sp.cover_surface->putPixel(surface, i + 1, 5, color); + sp.cover_surface->putPixel(surface, i + 1, 7, color); } // El resto se rellena de color sólido SDL_Rect rect = {0, 8, WIDTH, HEIGHT}; - c = stringToColor(options.video.palette, "black"); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF); - SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); + sp.cover_surface->fillRect(surface, &rect, color); // Crea el sprite - sp.cover_sprite = std::make_shared(sp.cover_texture, 0, 0, sp.cover_texture->getWidth(), sp.cover_texture->getHeight() - 8); - sp.cover_sprite->setPosition((options.game.width - sp.cover_texture->getWidth()) / 2, pic.pos); - sp.cover_sprite->setClip(0, 8, sp.cover_texture->getWidth(), sp.cover_texture->getHeight()); + sp.cover_sprite = std::make_shared(sp.cover_surface, 0, 0, sp.cover_surface->getWidth(), sp.cover_surface->getHeight() - 8); + sp.cover_sprite->setPosition((options.game.width - sp.cover_surface->getWidth()) / 2, pic.pos); + sp.cover_sprite->setClip(0, 8, sp.cover_surface->getWidth(), sp.cover_surface->getHeight()); // Inicializa variables sp.cover_clip_desp = 8; sp.cover_clip_height = HEIGHT; sprite_pics_.push_back(sp); + Screen::get()->setRenderSurfaceData(nullptr); } } @@ -500,7 +479,7 @@ void Ending::checkChangeScene() void Ending::fillCoverTexture() { // Rellena la textura que cubre el texto con color transparente - SDL_SetRenderTarget(Screen::get()->getRenderer(), cover_texture_); + SDL_SetRenderTarget(Screen::get()->getRenderer(), cover_surface_); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); SDL_RenderClear(Screen::get()->getRenderer()); @@ -534,7 +513,7 @@ void Ending::renderCoverTexture() const int OFFSET = std::min(cover_counter_, 100); SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, OFFSET * 2}; SDL_Rect dstRect = {0, 0, 256, OFFSET * 2}; - SDL_RenderCopy(Screen::get()->getRenderer(), cover_texture_, &srcRect, &dstRect); + SDL_RenderCopy(Screen::get()->getRenderer(), cover_surface_, &srcRect, &dstRect); } } diff --git a/source/ending.h b/source/ending.h index c1c0124..b99233d 100644 --- a/source/ending.h +++ b/source/ending.h @@ -5,8 +5,8 @@ #include // for shared_ptr #include // for string #include // for vector -class Sprite; // lines 12-12 -class Texture; // lines 14-14 +class SSprite; // lines 12-12 +class Surface; // lines 14-14 class Ending { @@ -14,10 +14,10 @@ private: // Estructuras struct EndingTexture // Estructura con dos texturas y sprites, uno para mostrar y el otro hace de cortinilla { - std::shared_ptr image_texture; // Textura a mostrar - std::shared_ptr image_sprite; // Sprite para mostrar la textura - std::shared_ptr cover_texture; // Textura que cubre a la otra textura - std::shared_ptr cover_sprite; // Sprite para mostrar la textura que cubre a la otra textura + std::shared_ptr image_surface; // Surface a mostrar + std::shared_ptr image_sprite; // SSprite para mostrar la textura + std::shared_ptr cover_surface; // Surface que cubre a la otra textura + std::shared_ptr cover_sprite; // SSprite para mostrar la textura que cubre a la otra textura int cover_clip_desp; // Desplazamiento del spriteClip de la textura de cobertura int cover_clip_height; // Altura del spriteClip de la textura de cobertura }; @@ -42,7 +42,7 @@ private: }; // Objetos y punteros - SDL_Texture *cover_texture_; // Textura para cubrir el texto + SDL_Texture *cover_surface_; // Surface para cubrir el texto // Variables int counter_; // Contador diff --git a/source/game_over.cpp b/source/game_over.cpp index 2336861..9173921 100644 --- a/source/game_over.cpp +++ b/source/game_over.cpp @@ -16,8 +16,8 @@ // Constructor GameOver::GameOver() - : player_sprite_(std::make_shared(Resource::get()->getSurface("player_game_over.gif"), Resource::get()->getAnimations("player_game_over.ani"))), - tv_sprite_(std::make_shared(Resource::get()->getSurface("tv.gif"), Resource::get()->getAnimations("tv.ani"))), + : player_sprite_(std::make_shared(Resource::get()->getSurface("player_game_over.gif"), Resource::get()->getAnimations("player_game_over.ani"))), + tv_sprite_(std::make_shared(Resource::get()->getSurface("tv.gif"), Resource::get()->getAnimations("tv.ani"))), pre_counter_(0), counter_(0), ticks_(0) @@ -34,7 +34,7 @@ GameOver::GameOver() const std::vector COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"}; for (const auto &color : COLORS) { - colors_.push_back(stringToColor(options.video.palette, color)); + colors_.push_back(stringToColor(color)); } color_ = colors_.back(); } @@ -71,7 +71,7 @@ void GameOver::render() constexpr int Y = 32; Screen::get()->start(); - Screen::get()->clean(); + Screen::get()->clean(1); auto text = Resource::get()->getText("smb2"); @@ -146,11 +146,8 @@ void GameOver::updateColor() // Dibuja los sprites void GameOver::renderSprites() { - player_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b); - player_sprite_->render(); - - tv_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b); - tv_sprite_->render(); + player_sprite_->render(1, color_); + tv_sprite_->render(1, color_); } // Actualiza los contadores diff --git a/source/game_over.h b/source/game_over.h index 1a26d57..6652dce 100644 --- a/source/game_over.h +++ b/source/game_over.h @@ -4,7 +4,7 @@ #include // for shared_ptr #include // for vector #include "utils.h" // for Color -class AnimatedSprite; // lines 8-8 +class SAnimatedSprite; // lines 8-8 class GameOver { @@ -15,15 +15,15 @@ private: static constexpr int COUNTER_FADE_LENGHT_ = 20; // Contador: duración del fade // Objetos y punteros - std::shared_ptr player_sprite_; // Sprite con el jugador - std::shared_ptr tv_sprite_; // Sprite con el televisor + std::shared_ptr player_sprite_; // Sprite con el jugador + std::shared_ptr tv_sprite_; // Sprite con el televisor // Variables int pre_counter_ = 0; // Contador previo int counter_ = 0; // Contador Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa - std::vector colors_; // Vector con los colores para el fade - Color color_; // Color usado para el texto y los sprites + std::vector colors_; // Vector con los colores para el fade + Uint8 color_; // Color usado para el texto y los sprites // Actualiza el objeto void update(); diff --git a/source/s_moving_sprite.cpp b/source/s_moving_sprite.cpp index 6b0a3fa..fb99538 100644 --- a/source/s_moving_sprite.cpp +++ b/source/s_moving_sprite.cpp @@ -62,6 +62,12 @@ void SMovingSprite::render() surface_->render(pos_.x, pos_.y, &clip_, flip_); } +// Muestra el sprite por pantalla +void SMovingSprite::render(Uint8 source_color, Uint8 target_color) +{ + surface_->renderWithColorReplace(pos_.x, pos_.y, source_color, target_color, &clip_, flip_); +} + // Establece la posición y_ el tamaño del objeto void SMovingSprite::setPos(SDL_Rect rect) { diff --git a/source/s_moving_sprite.h b/source/s_moving_sprite.h index 58a179f..2711acd 100644 --- a/source/s_moving_sprite.h +++ b/source/s_moving_sprite.h @@ -44,6 +44,7 @@ public: // Muestra el sprite por pantalla void render() override; + void render(Uint8 source_color, Uint8 target_color) override; // Obtiene la variable float getPosX() const { return x_; } diff --git a/source/s_sprite.cpp b/source/s_sprite.cpp index ecfec3b..a211c38 100644 --- a/source/s_sprite.cpp +++ b/source/s_sprite.cpp @@ -23,6 +23,11 @@ void SSprite::render() surface_->render(pos_.x, pos_.y, &clip_); } +void SSprite::render(Uint8 source_color, Uint8 target_color) +{ + surface_->renderWithColorReplace(pos_.x, pos_.y, source_color, target_color, &clip_); +} + // Establece la posición del objeto void SSprite::setPosition(int x, int y) { diff --git a/source/s_sprite.h b/source/s_sprite.h index 15c6a35..8fc83f2 100644 --- a/source/s_sprite.h +++ b/source/s_sprite.h @@ -24,6 +24,7 @@ public: // Muestra el sprite por pantalla virtual void render(); + virtual void render(Uint8 source_color, Uint8 target_color); // Reinicia las variables a cero virtual void clear(); diff --git a/source/surface.h b/source/surface.h index 61b5cdb..7e5ca8d 100644 --- a/source/surface.h +++ b/source/surface.h @@ -86,7 +86,7 @@ public: void render(SDL_Rect *srcRect = nullptr, SDL_Rect *dstRect = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); // Copia una región de la SurfaceData de origen a la SurfaceData de destino reemplazando un color por otro - void renderWithColorReplace(int x, int y, Uint8 source_color = 0, Uint8 target_color = 0, SDL_Rect *clip = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); + void renderWithColorReplace(int x, int y, Uint8 source_color = 0, Uint8 target_color = 0, SDL_Rect *srcRect = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); // Establece un color en la paleta void setColor(int index, Uint32 color); diff --git a/source/text.cpp b/source/text.cpp index 6335f4b..1e8c55a 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -8,9 +8,9 @@ #include // Para cerr #include // Para runtime_error #include "screen.h" // Para Screen -#include "s_sprite.h" // Para SSprite +#include "s_sprite.h" // Para SSprite #include "surface.h" // Para Surface -#include "utils.h" // Para Color, getFileName, printWithDots +#include "utils.h" // Para Uint8, getFileName, printWithDots // Llena una estructuta TextFile desde un fichero std::shared_ptr loadTextFile(const std::string &file_path) @@ -145,54 +145,59 @@ void Text::write(int x, int y, const std::string &text, int kerning, int lenght) std::shared_ptr Text::writeToSurface(const std::string &text, int zoom, int kerning) { auto renderer = Screen::get()->getRenderer(); - auto surface = std::make_shared(renderer); auto width = lenght(text, kerning) * zoom; auto height = box_height_ * zoom; auto temp = SDL_GetRenderTarget(renderer); - //surface->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); - //surface->setBlendMode(SDL_BLENDMODE_BLEND); - //surface->setAsRenderTarget(renderer); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - SDL_RenderClear(renderer); - //zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning); - SDL_SetRenderTarget(renderer, temp); - + auto surface = std::make_shared(Screen::get()->getRenderSurfaceData(), width, height); + Screen::get()->setRenderSurfaceData(surface); + Screen::get()->clean(stringToColor("transparent")); + write(0, 0, text, kerning); + Screen::get()->setRenderSurfaceData(nullptr); + return surface; } // Escribe el texto con extras en una surface -std::shared_ptr Text::writeDXToSurface(Uint8 flags, const std::string &text, int kerning, Color textColor, Uint8 shadow_distance, Color shadow_color, int lenght) +std::shared_ptr Text::writeDXToSurface(Uint8 flags, const std::string &text, int kerning, Uint8 textColor, Uint8 shadow_distance, Uint8 shadow_color, int lenght) { auto renderer = Screen::get()->getRenderer(); - auto surface = std::make_shared(renderer); auto width = Text::lenght(text, kerning) + shadow_distance; auto height = box_height_ + shadow_distance; auto temp = SDL_GetRenderTarget(renderer); - //surface->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); - //surface->setBlendMode(SDL_BLENDMODE_BLEND); - //surface->setAsRenderTarget(renderer); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - SDL_RenderClear(renderer); + auto surface = std::make_shared(Screen::get()->getRenderSurfaceData(), width, height); + Screen::get()->setRenderSurfaceData(surface); + Screen::get()->clean(stringToColor("transparent")); writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght); - SDL_SetRenderTarget(renderer, temp); + Screen::get()->setRenderSurfaceData(nullptr); return surface; } // Escribe el texto con colores -void Text::writeColored(int x, int y, const std::string &text, Color color, int kerning, int lenght) +void Text::writeColored(int x, int y, const std::string &text, Uint8 color, int kerning, int lenght) { - //sprite_->getSurface()->setColor(color.r, color.g, color.b); - write(x, y, text, kerning, lenght); - //sprite_->getSurface()->setColor(255, 255, 255); + int shift = 0; + + if (lenght == -1) + { + lenght = text.length(); + } + + sprite_->setY(y); + for (int i = 0; i < lenght; ++i) + { + auto index = static_cast(text[i]); + sprite_->setClip(offset_[index].x, offset_[index].y, box_width_, box_height_); + sprite_->setX(x + shift); + sprite_->render(1, color); + shift += offset_[static_cast(text[i])].w + kerning; + } } // Escribe el texto con sombra -void Text::writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance, int kerning, int lenght) +void Text::writeShadowed(int x, int y, const std::string &text, Uint8 color, Uint8 shadow_distance, int kerning, int lenght) { - //sprite_->getSurface()->setColor(color.r, color.g, color.b); - write(x + shadow_distance, y + shadow_distance, text, kerning, lenght); - //sprite_->getSurface()->setColor(255, 255, 255); + writeColored(x + shadow_distance, y + shadow_distance, text, color, kerning, lenght); write(x, y, text, kerning, lenght); } @@ -204,7 +209,7 @@ void Text::writeCentered(int x, int y, const std::string &text, int kerning, int } // Escribe texto con extras -void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning, Color textColor, Uint8 shadow_distance, Color shadow_color, int lenght) +void Text::writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning, Uint8 textColor, Uint8 shadow_distance, Uint8 shadow_color, int lenght) { const auto centered = ((flags & TEXT_CENTER) == TEXT_CENTER); const auto shadowed = ((flags & TEXT_SHADOW) == TEXT_SHADOW); diff --git a/source/text.h b/source/text.h index 9576f80..1584dd9 100644 --- a/source/text.h +++ b/source/text.h @@ -1,10 +1,10 @@ #pragma once -#include // Para Uint8 +#include // Para Uint8 #include // Para unique_ptr, shared_ptr #include // Para string #include "s_sprite.h" // Para SSprite -#include "utils.h" // Para Color +#include "utils.h" // Para Uint8 class Surface; // lines 9-9 constexpr int TEXT_COLOR = 1; @@ -55,19 +55,19 @@ public: std::shared_ptr writeToSurface(const std::string &text, int zoom = 1, int kerning = 1); // Escribe el texto con extras en una textura - std::shared_ptr writeDXToSurface(Uint8 flags, const std::string &text, int kerning = 1, Color textColor = Color(), Uint8 shadow_distance = 1, Color shadow_color = Color(), int lenght = -1); + std::shared_ptr writeDXToSurface(Uint8 flags, const std::string &text, int kerning = 1, Uint8 textColor = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1); // Escribe el texto con colores - void writeColored(int x, int y, const std::string &text, Color color, int kerning = 1, int lenght = -1); + void writeColored(int x, int y, const std::string &text, Uint8 color, int kerning = 1, int lenght = -1); // Escribe el texto con sombra - void writeShadowed(int x, int y, const std::string &text, Color color, Uint8 shadow_distance = 1, int kerning = 1, int lenght = -1); + void writeShadowed(int x, int y, const std::string &text, Uint8 color, Uint8 shadow_distance = 1, int kerning = 1, int lenght = -1); // Escribe el texto centrado en un punto x void writeCentered(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); // Escribe texto con extras - void writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning = 1, Color textColor = Color(), Uint8 shadow_distance = 1, Color shadow_color = Color(), int lenght = -1); + void writeDX(Uint8 flags, int x, int y, const std::string &text, int kerning = 1, Uint8 textColor = Uint8(), Uint8 shadow_distance = 1, Uint8 shadow_color = Uint8(), int lenght = -1); // Obtiene la longitud en pixels de una cadena int lenght(const std::string &text, int kerning = 1) const;