Transició a surface: ending.cpp fet

This commit is contained in:
2025-03-03 20:48:31 +01:00
parent d7e0178602
commit b320030547
11 changed files with 128 additions and 134 deletions

View File

@@ -14,9 +14,9 @@
#include "options.h" // for Options, options, OptionsVideo, Sect... #include "options.h" // for Options, options, OptionsVideo, Sect...
#include "resource.h" // for Resource #include "resource.h" // for Resource
#include "screen.h" // for Screen #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 "text.h" // for Text, TEXT_STROKE
#include "surface.h" // for Texture #include "surface.h" // for Surface
#include "utils.h" // for Color, stringToColor, Palette #include "utils.h" // for Color, stringToColor, Palette
// Constructor // Constructor
@@ -43,8 +43,8 @@ Ending::Ending()
Screen::get()->setBorderColor(stringToColor(options.video.palette, "black")); Screen::get()->setBorderColor(stringToColor(options.video.palette, "black"));
// Crea la textura para cubrir el texto // Crea la textura para cubrir el texto
cover_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height + 8); cover_surface_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height + 8);
SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(cover_surface_, SDL_BLENDMODE_BLEND);
// Rellena la textura para la cortinilla // Rellena la textura para la cortinilla
fillCoverTexture(); fillCoverTexture();
@@ -53,7 +53,7 @@ Ending::Ending()
// Destructor // Destructor
Ending::~Ending() Ending::~Ending()
{ {
SDL_DestroyTexture(cover_texture_); SDL_DestroyTexture(cover_surface_);
} }
// Actualiza el objeto // Actualiza el objeto
@@ -174,61 +174,55 @@ void Ending::iniTexts()
const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2; const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2;
const int HEIGHT = text->getCharacterSize() + 2 + 2; const int HEIGHT = text->getCharacterSize() + 2 + 2;
Color color = stringToColor(options.video.palette, "black"); Uint8 color = stringToColor("black");
EndingTexture st; EndingTexture st;
// Crea la textura // Crea la textura
st.image_texture = std::make_shared<Texture>(Screen::get()->getRenderer()); st.image_surface = std::make_shared<Surface>(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT);
st.image_texture->createBlank(WIDTH, HEIGHT); Screen::get()->setRenderSurfaceData(st.image_surface);
st.image_texture->setAsRenderTarget(Screen::get()->getRenderer());
st.image_texture->setBlendMode(SDL_BLENDMODE_BLEND);
text->writeDX(TEXT_STROKE, 2, 2, txt.caption, 1, color, 2, color); text->writeDX(TEXT_STROKE, 2, 2, txt.caption, 1, color, 2, color);
// Crea el sprite // Crea el sprite
st.image_sprite = std::make_shared<Sprite>(st.image_texture, 0, 0, st.image_texture->getWidth(), st.image_texture->getHeight()); st.image_sprite = std::make_shared<SSprite>(st.image_surface, 0, 0, st.image_surface->getWidth(), st.image_surface->getHeight());
st.image_sprite->setPosition((options.game.width - st.image_texture->getWidth()) / 2, txt.pos); st.image_sprite->setPosition((options.game.width - st.image_surface->getWidth()) / 2, txt.pos);
// Crea la coverTexture // Crea la cover_surface
st.cover_texture = std::make_shared<Texture>(Screen::get()->getRenderer()); st.cover_surface = std::make_shared<Surface>(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT + 8);
st.cover_texture->createBlank(WIDTH, HEIGHT + 8); Screen::get()->setRenderSurfaceData(st.cover_surface);
st.cover_texture->setAsRenderTarget(Screen::get()->getRenderer());
st.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Rellena la coverTexture con color transparente // Rellena la cover_surface con color transparente
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); Screen::get()->clean(stringToColor("transparent"));
SDL_RenderClear(Screen::get()->getRenderer());
// Crea una malla de 8 pixels de alto // Crea una malla de 8 pixels de alto
color = stringToColor(options.video.palette, "black"); auto surface = Screen::get()->getRenderSurfaceData();
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF); color = stringToColor("black");
for (int i = 0; i < WIDTH; i += 2) for (int i = 0; i < WIDTH; i += 2)
{ {
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0); st.cover_surface->putPixel(surface, i, 0, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2); st.cover_surface->putPixel(surface, i, 2, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4); st.cover_surface->putPixel(surface, i, 4, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6); st.cover_surface->putPixel(surface, i, 6, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5); st.cover_surface->putPixel(surface, i + 1, 5, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7); st.cover_surface->putPixel(surface, i + 1, 7, color);
} }
// El resto se rellena de color sólido // El resto se rellena de color sólido
SDL_Rect rect = {0, 8, WIDTH, HEIGHT}; SDL_Rect rect = {0, 8, WIDTH, HEIGHT};
color = stringToColor(options.video.palette, "black"); st.cover_surface->fillRect(surface, &rect, color);
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF);
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
// Crea el sprite // Crea el sprite
st.cover_sprite = std::make_shared<Sprite>(st.cover_texture, 0, 0, st.cover_texture->getWidth(), st.cover_texture->getHeight() - 8); st.cover_sprite = std::make_shared<SSprite>(st.cover_surface, 0, 0, st.cover_surface->getWidth(), st.cover_surface->getHeight() - 8);
st.cover_sprite->setPosition((options.game.width - st.cover_texture->getWidth()) / 2, txt.pos); st.cover_sprite->setPosition((options.game.width - st.cover_surface->getWidth()) / 2, txt.pos);
st.cover_sprite->setClip(0, 8, st.cover_texture->getWidth(), st.cover_texture->getHeight()); st.cover_sprite->setClip(0, 8, st.cover_surface->getWidth(), st.cover_surface->getHeight());
// Inicializa variables // Inicializa variables
st.cover_clip_desp = 8; st.cover_clip_desp = 8;
st.cover_clip_height = HEIGHT; st.cover_clip_height = HEIGHT;
sprite_texts_.push_back(st); sprite_texts_.push_back(st);
Screen::get()->setRenderSurfaceData(nullptr);
} }
} }
@@ -238,22 +232,11 @@ void Ending::iniPics()
// Vector con las rutas y la posición // Vector con las rutas y la posición
std::vector<TextAndPosition> pics; std::vector<TextAndPosition> pics;
if (options.video.palette == Palette::ZXSPECTRUM) pics.push_back({"ending1.gif", 48});
{ pics.push_back({"ending2.gif", 26});
pics.push_back({"ending1.gif", 48}); pics.push_back({"ending3.gif", 29});
pics.push_back({"ending2.gif", 26}); pics.push_back({"ending4.gif", 63});
pics.push_back({"ending3.gif", 29}); pics.push_back({"ending5.gif", 53});
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});
}
// Crea los sprites // Crea los sprites
sprite_pics_.clear(); sprite_pics_.clear();
@@ -263,54 +246,50 @@ void Ending::iniPics()
EndingTexture sp; EndingTexture sp;
// Crea la texture // Crea la texture
sp.image_texture = Resource::get()->getSurface(pic.caption); sp.image_surface = Resource::get()->getSurface(pic.caption);
const int WIDTH = sp.image_texture->getWidth(); const int WIDTH = sp.image_surface->getWidth();
const int HEIGHT = sp.image_texture->getHeight(); const int HEIGHT = sp.image_surface->getHeight();
// Crea el sprite // Crea el sprite
sp.image_sprite = std::make_shared<Sprite>(sp.image_texture, 0, 0, WIDTH, HEIGHT); sp.image_sprite = std::make_shared<SSprite>(sp.image_surface, 0, 0, WIDTH, HEIGHT);
sp.image_sprite->setPosition((options.game.width - WIDTH) / 2, pic.pos); sp.image_sprite->setPosition((options.game.width - WIDTH) / 2, pic.pos);
// Crea la coverTexture // Crea la cover_surface
sp.cover_texture = std::make_shared<Texture>(Screen::get()->getRenderer()); sp.cover_surface = std::make_shared<Surface>(Screen::get()->getRenderSurfaceData(), WIDTH, HEIGHT + 8);
sp.cover_texture->createBlank(WIDTH, HEIGHT + 8); Screen::get()->setRenderSurfaceData(sp.cover_surface);
sp.cover_texture->setAsRenderTarget(Screen::get()->getRenderer());
sp.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND);
// Rellena la coverTexture con color transparente // Rellena la cover_surface con color transparente
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); Screen::get()->clean(stringToColor("transparent"));
SDL_RenderClear(Screen::get()->getRenderer());
// Crea una malla en los primeros 8 pixels // Crea una malla en los primeros 8 pixels
Color c = stringToColor(options.video.palette, "black"); auto surface = Screen::get()->getRenderSurfaceData();
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF); auto color = stringToColor("black");
for (int i = 0; i < WIDTH; i += 2) for (int i = 0; i < WIDTH; i += 2)
{ {
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0); sp.cover_surface->putPixel(surface, i, 0, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2); sp.cover_surface->putPixel(surface, i, 2, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 4); sp.cover_surface->putPixel(surface, i, 4, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 6); sp.cover_surface->putPixel(surface, i, 6, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 5); sp.cover_surface->putPixel(surface, i + 1, 5, color);
SDL_RenderDrawPoint(Screen::get()->getRenderer(), i + 1, 7); sp.cover_surface->putPixel(surface, i + 1, 7, color);
} }
// El resto se rellena de color sólido // El resto se rellena de color sólido
SDL_Rect rect = {0, 8, WIDTH, HEIGHT}; SDL_Rect rect = {0, 8, WIDTH, HEIGHT};
c = stringToColor(options.video.palette, "black"); sp.cover_surface->fillRect(surface, &rect, color);
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF);
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
// Crea el sprite // Crea el sprite
sp.cover_sprite = std::make_shared<Sprite>(sp.cover_texture, 0, 0, sp.cover_texture->getWidth(), sp.cover_texture->getHeight() - 8); sp.cover_sprite = std::make_shared<SSprite>(sp.cover_surface, 0, 0, sp.cover_surface->getWidth(), sp.cover_surface->getHeight() - 8);
sp.cover_sprite->setPosition((options.game.width - sp.cover_texture->getWidth()) / 2, pic.pos); sp.cover_sprite->setPosition((options.game.width - sp.cover_surface->getWidth()) / 2, pic.pos);
sp.cover_sprite->setClip(0, 8, sp.cover_texture->getWidth(), sp.cover_texture->getHeight()); sp.cover_sprite->setClip(0, 8, sp.cover_surface->getWidth(), sp.cover_surface->getHeight());
// Inicializa variables // Inicializa variables
sp.cover_clip_desp = 8; sp.cover_clip_desp = 8;
sp.cover_clip_height = HEIGHT; sp.cover_clip_height = HEIGHT;
sprite_pics_.push_back(sp); sprite_pics_.push_back(sp);
Screen::get()->setRenderSurfaceData(nullptr);
} }
} }
@@ -500,7 +479,7 @@ void Ending::checkChangeScene()
void Ending::fillCoverTexture() void Ending::fillCoverTexture()
{ {
// Rellena la textura que cubre el texto con color transparente // 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_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
SDL_RenderClear(Screen::get()->getRenderer()); SDL_RenderClear(Screen::get()->getRenderer());
@@ -534,7 +513,7 @@ void Ending::renderCoverTexture()
const int OFFSET = std::min(cover_counter_, 100); const int OFFSET = std::min(cover_counter_, 100);
SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, OFFSET * 2}; SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, OFFSET * 2};
SDL_Rect dstRect = {0, 0, 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);
} }
} }

View File

@@ -5,8 +5,8 @@
#include <memory> // for shared_ptr #include <memory> // for shared_ptr
#include <string> // for string #include <string> // for string
#include <vector> // for vector #include <vector> // for vector
class Sprite; // lines 12-12 class SSprite; // lines 12-12
class Texture; // lines 14-14 class Surface; // lines 14-14
class Ending class Ending
{ {
@@ -14,10 +14,10 @@ private:
// Estructuras // Estructuras
struct EndingTexture // Estructura con dos texturas y sprites, uno para mostrar y el otro hace de cortinilla struct EndingTexture // Estructura con dos texturas y sprites, uno para mostrar y el otro hace de cortinilla
{ {
std::shared_ptr<Texture> image_texture; // Textura a mostrar std::shared_ptr<Surface> image_surface; // Surface a mostrar
std::shared_ptr<Sprite> image_sprite; // Sprite para mostrar la textura std::shared_ptr<SSprite> image_sprite; // SSprite para mostrar la textura
std::shared_ptr<Texture> cover_texture; // Textura que cubre a la otra textura std::shared_ptr<Surface> cover_surface; // Surface que cubre a la otra textura
std::shared_ptr<Sprite> cover_sprite; // Sprite para mostrar la textura que cubre a la otra textura std::shared_ptr<SSprite> 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_desp; // Desplazamiento del spriteClip de la textura de cobertura
int cover_clip_height; // Altura 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 // Objetos y punteros
SDL_Texture *cover_texture_; // Textura para cubrir el texto SDL_Texture *cover_surface_; // Surface para cubrir el texto
// Variables // Variables
int counter_; // Contador int counter_; // Contador

View File

@@ -16,8 +16,8 @@
// Constructor // Constructor
GameOver::GameOver() GameOver::GameOver()
: player_sprite_(std::make_shared<AnimatedSprite>(Resource::get()->getSurface("player_game_over.gif"), Resource::get()->getAnimations("player_game_over.ani"))), : player_sprite_(std::make_shared<SAnimatedSprite>(Resource::get()->getSurface("player_game_over.gif"), Resource::get()->getAnimations("player_game_over.ani"))),
tv_sprite_(std::make_shared<AnimatedSprite>(Resource::get()->getSurface("tv.gif"), Resource::get()->getAnimations("tv.ani"))), tv_sprite_(std::make_shared<SAnimatedSprite>(Resource::get()->getSurface("tv.gif"), Resource::get()->getAnimations("tv.ani"))),
pre_counter_(0), pre_counter_(0),
counter_(0), counter_(0),
ticks_(0) ticks_(0)
@@ -34,7 +34,7 @@ GameOver::GameOver()
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"}; const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
for (const auto &color : COLORS) for (const auto &color : COLORS)
{ {
colors_.push_back(stringToColor(options.video.palette, color)); colors_.push_back(stringToColor(color));
} }
color_ = colors_.back(); color_ = colors_.back();
} }
@@ -71,7 +71,7 @@ void GameOver::render()
constexpr int Y = 32; constexpr int Y = 32;
Screen::get()->start(); Screen::get()->start();
Screen::get()->clean(); Screen::get()->clean(1);
auto text = Resource::get()->getText("smb2"); auto text = Resource::get()->getText("smb2");
@@ -146,11 +146,8 @@ void GameOver::updateColor()
// Dibuja los sprites // Dibuja los sprites
void GameOver::renderSprites() void GameOver::renderSprites()
{ {
player_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b); player_sprite_->render(1, color_);
player_sprite_->render(); tv_sprite_->render(1, color_);
tv_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b);
tv_sprite_->render();
} }
// Actualiza los contadores // Actualiza los contadores

View File

@@ -4,7 +4,7 @@
#include <memory> // for shared_ptr #include <memory> // for shared_ptr
#include <vector> // for vector #include <vector> // for vector
#include "utils.h" // for Color #include "utils.h" // for Color
class AnimatedSprite; // lines 8-8 class SAnimatedSprite; // lines 8-8
class GameOver class GameOver
{ {
@@ -15,15 +15,15 @@ private:
static constexpr int COUNTER_FADE_LENGHT_ = 20; // Contador: duración del fade static constexpr int COUNTER_FADE_LENGHT_ = 20; // Contador: duración del fade
// Objetos y punteros // Objetos y punteros
std::shared_ptr<AnimatedSprite> player_sprite_; // Sprite con el jugador std::shared_ptr<SAnimatedSprite> player_sprite_; // Sprite con el jugador
std::shared_ptr<AnimatedSprite> tv_sprite_; // Sprite con el televisor std::shared_ptr<SAnimatedSprite> tv_sprite_; // Sprite con el televisor
// Variables // Variables
int pre_counter_ = 0; // Contador previo int pre_counter_ = 0; // Contador previo
int counter_ = 0; // Contador int counter_ = 0; // Contador
Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa
std::vector<Color> colors_; // Vector con los colores para el fade std::vector<Uint8> colors_; // Vector con los colores para el fade
Color color_; // Color usado para el texto y los sprites Uint8 color_; // Color usado para el texto y los sprites
// Actualiza el objeto // Actualiza el objeto
void update(); void update();

View File

@@ -62,6 +62,12 @@ void SMovingSprite::render()
surface_->render(pos_.x, pos_.y, &clip_, flip_); 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 // Establece la posición y_ el tamaño del objeto
void SMovingSprite::setPos(SDL_Rect rect) void SMovingSprite::setPos(SDL_Rect rect)
{ {

View File

@@ -44,6 +44,7 @@ public:
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
void render() override; void render() override;
void render(Uint8 source_color, Uint8 target_color) override;
// Obtiene la variable // Obtiene la variable
float getPosX() const { return x_; } float getPosX() const { return x_; }

View File

@@ -23,6 +23,11 @@ void SSprite::render()
surface_->render(pos_.x, pos_.y, &clip_); 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 // Establece la posición del objeto
void SSprite::setPosition(int x, int y) void SSprite::setPosition(int x, int y)
{ {

View File

@@ -24,6 +24,7 @@ public:
// Muestra el sprite por pantalla // Muestra el sprite por pantalla
virtual void render(); virtual void render();
virtual void render(Uint8 source_color, Uint8 target_color);
// Reinicia las variables a cero // Reinicia las variables a cero
virtual void clear(); virtual void clear();

View File

@@ -86,7 +86,7 @@ public:
void render(SDL_Rect *srcRect = nullptr, SDL_Rect *dstRect = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); 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 // 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 // Establece un color en la paleta
void setColor(int index, Uint32 color); void setColor(int index, Uint32 color);

View File

@@ -8,9 +8,9 @@
#include <iostream> // Para cerr #include <iostream> // Para cerr
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "s_sprite.h" // Para SSprite #include "s_sprite.h" // Para SSprite
#include "surface.h" // Para Surface #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 // Llena una estructuta TextFile desde un fichero
std::shared_ptr<TextFile> loadTextFile(const std::string &file_path) std::shared_ptr<TextFile> 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<Surface> Text::writeToSurface(const std::string &text, int zoom, int kerning) std::shared_ptr<Surface> Text::writeToSurface(const std::string &text, int zoom, int kerning)
{ {
auto renderer = Screen::get()->getRenderer(); auto renderer = Screen::get()->getRenderer();
auto surface = std::make_shared<Surface>(renderer);
auto width = lenght(text, kerning) * zoom; auto width = lenght(text, kerning) * zoom;
auto height = box_height_ * zoom; auto height = box_height_ * zoom;
auto temp = SDL_GetRenderTarget(renderer); auto temp = SDL_GetRenderTarget(renderer);
//surface->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); auto surface = std::make_shared<Surface>(Screen::get()->getRenderSurfaceData(), width, height);
//surface->setBlendMode(SDL_BLENDMODE_BLEND); Screen::get()->setRenderSurfaceData(surface);
//surface->setAsRenderTarget(renderer); Screen::get()->clean(stringToColor("transparent"));
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); write(0, 0, text, kerning);
SDL_RenderClear(renderer); Screen::get()->setRenderSurfaceData(nullptr);
//zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning);
SDL_SetRenderTarget(renderer, temp);
return surface; return surface;
} }
// Escribe el texto con extras en una surface // Escribe el texto con extras en una surface
std::shared_ptr<Surface> Text::writeDXToSurface(Uint8 flags, const std::string &text, int kerning, Color textColor, Uint8 shadow_distance, Color shadow_color, int lenght) std::shared_ptr<Surface> 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 renderer = Screen::get()->getRenderer();
auto surface = std::make_shared<Surface>(renderer);
auto width = Text::lenght(text, kerning) + shadow_distance; auto width = Text::lenght(text, kerning) + shadow_distance;
auto height = box_height_ + shadow_distance; auto height = box_height_ + shadow_distance;
auto temp = SDL_GetRenderTarget(renderer); auto temp = SDL_GetRenderTarget(renderer);
//surface->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); auto surface = std::make_shared<Surface>(Screen::get()->getRenderSurfaceData(), width, height);
//surface->setBlendMode(SDL_BLENDMODE_BLEND); Screen::get()->setRenderSurfaceData(surface);
//surface->setAsRenderTarget(renderer); Screen::get()->clean(stringToColor("transparent"));
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght); writeDX(flags, 0, 0, text, kerning, textColor, shadow_distance, shadow_color, lenght);
SDL_SetRenderTarget(renderer, temp); Screen::get()->setRenderSurfaceData(nullptr);
return surface; return surface;
} }
// Escribe el texto con colores // 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); int shift = 0;
write(x, y, text, kerning, lenght);
//sprite_->getSurface()->setColor(255, 255, 255); if (lenght == -1)
{
lenght = text.length();
}
sprite_->setY(y);
for (int i = 0; i < lenght; ++i)
{
auto index = static_cast<int>(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<int>(text[i])].w + kerning;
}
} }
// Escribe el texto con sombra // 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); writeColored(x + shadow_distance, y + shadow_distance, text, color, kerning, lenght);
write(x + shadow_distance, y + shadow_distance, text, kerning, lenght);
//sprite_->getSurface()->setColor(255, 255, 255);
write(x, y, text, 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 // 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 centered = ((flags & TEXT_CENTER) == TEXT_CENTER);
const auto shadowed = ((flags & TEXT_SHADOW) == TEXT_SHADOW); const auto shadowed = ((flags & TEXT_SHADOW) == TEXT_SHADOW);

View File

@@ -1,10 +1,10 @@
#pragma once #pragma once
#include <SDL2/SDL_stdinc.h> // Para Uint8 #include <SDL2/SDL.h> // Para Uint8
#include <memory> // Para unique_ptr, shared_ptr #include <memory> // Para unique_ptr, shared_ptr
#include <string> // Para string #include <string> // Para string
#include "s_sprite.h" // Para SSprite #include "s_sprite.h" // Para SSprite
#include "utils.h" // Para Color #include "utils.h" // Para Uint8
class Surface; // lines 9-9 class Surface; // lines 9-9
constexpr int TEXT_COLOR = 1; constexpr int TEXT_COLOR = 1;
@@ -55,19 +55,19 @@ public:
std::shared_ptr<Surface> writeToSurface(const std::string &text, int zoom = 1, int kerning = 1); std::shared_ptr<Surface> writeToSurface(const std::string &text, int zoom = 1, int kerning = 1);
// Escribe el texto con extras en una textura // Escribe el texto con extras en una textura
std::shared_ptr<Surface> 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<Surface> 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 // 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 // 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 // Escribe el texto centrado en un punto x
void writeCentered(int x, int y, const std::string &text, int kerning = 1, int lenght = -1); void writeCentered(int x, int y, const std::string &text, int kerning = 1, int lenght = -1);
// Escribe texto con extras // 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 // Obtiene la longitud en pixels de una cadena
int lenght(const std::string &text, int kerning = 1) const; int lenght(const std::string &text, int kerning = 1) const;