forked from jaildesigner-jailgames/jaildoctors_dilemma
Imposible canviar la rendererSurface
This commit is contained in:
@@ -11,30 +11,17 @@
|
||||
#include "gif.h" // for LoadGif, LoadPalette
|
||||
|
||||
// Constructor
|
||||
Surface::Surface(std::shared_ptr<std::shared_ptr<SurfaceData>> surface_dest, int w, int h)
|
||||
: surface_data_dest_(surface_dest),
|
||||
Surface::Surface(std::shared_ptr<Surface> surface_dest, int w, int h)
|
||||
: surface_dest_(surface_dest),
|
||||
surface_data_(std::make_shared<SurfaceData>(w, h)),
|
||||
original_surface_data_(surface_data_),
|
||||
transparent_color_(0)
|
||||
{
|
||||
if (!surface_data_dest_)
|
||||
{
|
||||
surface_data_dest_ = std::make_shared<std::shared_ptr<SurfaceData>>(surface_data_);
|
||||
}
|
||||
}
|
||||
transparent_color_(0){}
|
||||
|
||||
Surface::Surface(std::shared_ptr<std::shared_ptr<SurfaceData>> surface_dest, const std::string &file_path)
|
||||
: surface_data_dest_(surface_dest),
|
||||
Surface::Surface(std::shared_ptr<Surface> surface_dest, const std::string &file_path)
|
||||
: surface_dest_(surface_dest),
|
||||
transparent_color_(0)
|
||||
{
|
||||
SurfaceData loadedData = loadSurface(file_path);
|
||||
surface_data_ = std::make_shared<SurfaceData>(std::move(loadedData));
|
||||
original_surface_data_ = surface_data_;
|
||||
|
||||
if (!surface_data_dest_)
|
||||
{
|
||||
surface_data_dest_ = std::make_shared<std::shared_ptr<SurfaceData>>(surface_data_);
|
||||
}
|
||||
}
|
||||
|
||||
// Carga una superficie desde un archivo
|
||||
@@ -117,48 +104,45 @@ void Surface::clear(Uint8 color)
|
||||
}
|
||||
|
||||
// Pone un pixel en la SurfaceData
|
||||
void Surface::putPixel(std::shared_ptr<SurfaceData> surface_data, int x, int y, Uint8 color)
|
||||
void Surface::putPixel(int x, int y, Uint8 color)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= surface_data->width || y >= surface_data->height)
|
||||
if (x < 0 || y < 0 || x >= surface_data_->width || y >= surface_data_->height)
|
||||
{
|
||||
return; // Coordenadas fuera de rango
|
||||
}
|
||||
|
||||
const int index = x + y * surface_data->width;
|
||||
surface_data->data[index] = color;
|
||||
const int index = x + y * surface_data_->width;
|
||||
surface_data_->data[index] = color;
|
||||
}
|
||||
|
||||
// Obtiene el color de un pixel de la superficie de origen
|
||||
// Obtiene el color de un pixel de la surface_data
|
||||
Uint8 Surface::getPixel(int x, int y)
|
||||
{
|
||||
return surface_data_->data[x + y * surface_data_->width];
|
||||
}
|
||||
|
||||
// Dibuja un rectangulo
|
||||
void Surface::fillRect(std::shared_ptr<SurfaceData> surface_data, SDL_Rect *rect, Uint8 color)
|
||||
void Surface::fillRect(SDL_Rect *rect, Uint8 color)
|
||||
{
|
||||
if (!rect)
|
||||
return; // Verificar si el rectángulo es válido
|
||||
|
||||
// Limitar los valores del rectángulo al tamaño de la superficie
|
||||
int x_start = std::max(0, rect->x);
|
||||
int y_start = std::max(0, rect->y);
|
||||
int x_end = std::min(rect->x + rect->w, static_cast<int>(surface_data->width));
|
||||
int y_end = std::min(rect->y + rect->h, static_cast<int>(surface_data->height));
|
||||
int x_end = std::min(rect->x + rect->w, static_cast<int>(surface_data_->width));
|
||||
int y_end = std::min(rect->y + rect->h, static_cast<int>(surface_data_->height));
|
||||
|
||||
// Recorrer cada píxel dentro del rectángulo directamente
|
||||
for (int y = y_start; y < y_end; ++y)
|
||||
{
|
||||
for (int x = x_start; x < x_end; ++x)
|
||||
{
|
||||
const int index = x + y * surface_data->width;
|
||||
surface_data->data[index] = color;
|
||||
const int index = x + y * surface_data_->width;
|
||||
surface_data_->data[index] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja una linea
|
||||
void Surface::drawLine(std::shared_ptr<SurfaceData> surface_data, int x1, int y1, int x2, int y2, Uint8 color)
|
||||
void Surface::drawLine(int x1, int y1, int x2, int y2, Uint8 color)
|
||||
{
|
||||
// Calcula las diferencias
|
||||
int dx = std::abs(x2 - x1);
|
||||
@@ -173,9 +157,9 @@ void Surface::drawLine(std::shared_ptr<SurfaceData> surface_data, int x1, int y1
|
||||
while (true)
|
||||
{
|
||||
// Asegúrate de no dibujar fuera de los límites de la superficie
|
||||
if (x1 >= 0 && x1 < surface_data->width && y1 >= 0 && y1 < surface_data->height)
|
||||
if (x1 >= 0 && x1 < surface_data_->width && y1 >= 0 && y1 < surface_data_->height)
|
||||
{
|
||||
surface_data->data[x1 + y1 * surface_data->width] = color;
|
||||
surface_data_->data[x1 + y1 * surface_data_->width] = color;
|
||||
}
|
||||
|
||||
// Si alcanzamos el punto final, salimos
|
||||
@@ -199,17 +183,13 @@ void Surface::drawLine(std::shared_ptr<SurfaceData> surface_data, int x1, int y1
|
||||
// Copia una región de la superficie de origen a la de destino
|
||||
void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
|
||||
{
|
||||
if (!surface_data_ || !surface_data_dest_ || !*surface_data_dest_) {
|
||||
throw std::runtime_error("Surface source or destination is null.");
|
||||
}
|
||||
|
||||
auto &dest = **surface_data_dest_;
|
||||
auto surface_data = surface_dest_->getSurfaceData();
|
||||
|
||||
// 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, dest.width - dx);
|
||||
h = std::min(h, dest.height - dy);
|
||||
w = std::min(w, surface_data->width - dx);
|
||||
h = std::min(h, surface_data->height - dy);
|
||||
|
||||
for (int iy = 0; iy < h; ++iy)
|
||||
{
|
||||
@@ -218,7 +198,7 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
|
||||
Uint8 color = surface_data_->data[(sx + ix) + (sy + iy) * surface_data_->width];
|
||||
if (color != transparent_color_)
|
||||
{
|
||||
dest.data[(dx + ix) + (dy + iy) * dest.width] = color;
|
||||
surface_data->data[(dx + ix) + (dy + iy) * surface_data->width] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,12 +207,8 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
|
||||
// Copia una región de la superficie de origen a la de destino
|
||||
void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
{
|
||||
if (!surface_data_ || !surface_data_dest_ || !*surface_data_dest_) {
|
||||
throw std::runtime_error("Surface source or destination is null.");
|
||||
}
|
||||
|
||||
auto &dest = **surface_data_dest_;
|
||||
|
||||
auto surface_data = surface_dest_->getSurfaceData();
|
||||
|
||||
// Determina la región de origen (clip) a renderizar
|
||||
int sx = (srcRect) ? srcRect->x : 0;
|
||||
int sy = (srcRect) ? srcRect->y : 0;
|
||||
@@ -242,8 +218,8 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
// 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, dest.width - x);
|
||||
h = std::min(h, dest.height - y);
|
||||
w = std::min(w, surface_data->width - x);
|
||||
h = std::min(h, surface_data->height - y);
|
||||
|
||||
// Renderiza píxel por píxel aplicando el flip si es necesario
|
||||
for (int iy = 0; iy < h; ++iy)
|
||||
@@ -262,7 +238,7 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
|
||||
if (color != transparent_color_)
|
||||
{
|
||||
dest.data[dest_x + dest_y * dest.width] = color;
|
||||
surface_data->data[dest_x + dest_y * surface_data->width] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,11 +247,7 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
// Copia una región de la superficie de origen a la de destino
|
||||
void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip)
|
||||
{
|
||||
if (!surface_data_ || !surface_data_dest_ || !*surface_data_dest_) {
|
||||
throw std::runtime_error("Surface source or destination is null.");
|
||||
}
|
||||
|
||||
auto &dest = **surface_data_dest_;
|
||||
auto surface_data = surface_dest_->getSurfaceData();
|
||||
|
||||
// Si srcRect es nullptr, tomar toda la superficie fuente
|
||||
int sx = (srcRect) ? srcRect->x : 0;
|
||||
@@ -299,8 +271,8 @@ void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip
|
||||
// Limitar la región para evitar accesos fuera de rango en src y dst
|
||||
sw = std::min(sw, surface_data_->width - sx);
|
||||
sh = std::min(sh, surface_data_->height - sy);
|
||||
dw = std::min(dw, dest.width - dx);
|
||||
dh = std::min(dh, dest.height - dy);
|
||||
dw = std::min(dw, surface_data->width - dx);
|
||||
dh = std::min(dh, surface_data->height - dy);
|
||||
|
||||
int final_width = std::min(sw, dw);
|
||||
int final_height = std::min(sh, dh);
|
||||
@@ -322,7 +294,7 @@ void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip
|
||||
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
|
||||
if (color != transparent_color_)
|
||||
{
|
||||
dest.data[dest_x + dest_y * dest.width] = color;
|
||||
surface_data->data[dest_x + dest_y * surface_data->width] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -331,11 +303,7 @@ void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip
|
||||
// Copia una región de la SurfaceData de origen a la SurfaceData de destino reemplazando un color por otro
|
||||
void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 target_color, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
{
|
||||
if (!surface_data_ || !surface_data_dest_ || !*surface_data_dest_) {
|
||||
throw std::runtime_error("Surface source or destination is null.");
|
||||
}
|
||||
|
||||
auto &dest = **surface_data_dest_;
|
||||
auto surface_data = surface_dest_->getSurfaceData();
|
||||
|
||||
// Determina la región de origen (clip) a renderizar
|
||||
int sx = (srcRect) ? srcRect->x : 0;
|
||||
@@ -361,7 +329,7 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar
|
||||
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 >= dest.width || dest_y >= dest.height)
|
||||
if (dest_x < 0 || dest_y < 0 || dest_x >= surface_data->width || dest_y >= surface_data->height)
|
||||
{
|
||||
continue; // Saltar píxeles fuera del rango del destino
|
||||
}
|
||||
@@ -370,7 +338,7 @@ void Surface::renderWithColorReplace(int x, int y, Uint8 source_color, Uint8 tar
|
||||
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
|
||||
if (color != transparent_color_)
|
||||
{
|
||||
dest.data[dest_x + dest_y * dest.width] =
|
||||
surface_data->data[dest_x + dest_y * surface_data->width] =
|
||||
(color == source_color) ? target_color : color;
|
||||
}
|
||||
}
|
||||
@@ -444,27 +412,4 @@ bool Surface::fadePalette()
|
||||
|
||||
// Devolver si el índice 15 coincide con el índice 0
|
||||
return palette_[15] == palette_[0];
|
||||
}
|
||||
|
||||
// Permite que una Surface apunte al SurfaceData de otra Surface
|
||||
void Surface::redirectSurfaceDataTo(const std::shared_ptr<SurfaceData> &newSurfaceData)
|
||||
{
|
||||
if (surface_data_dest_)
|
||||
{
|
||||
*surface_data_dest_ = newSurfaceData;
|
||||
}
|
||||
}
|
||||
|
||||
void Surface::redirectSurfaceDataTo(const std::shared_ptr<Surface> &otherSurface)
|
||||
{
|
||||
redirectSurfaceDataTo(otherSurface->getSurfaceData());
|
||||
}
|
||||
|
||||
// Método para restaurar
|
||||
void Surface::restoreOriginalSurfaceData()
|
||||
{
|
||||
if (surface_data_dest_)
|
||||
{
|
||||
*surface_data_dest_ = original_surface_data_;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user