Afegides guardes en Surface::render() per a no pintar fora del destí (no estava implementat en totes les versions de render)

This commit is contained in:
2025-03-06 12:27:10 +01:00
parent f2bfc970bc
commit 72efa0dc46

View File

@@ -196,14 +196,15 @@ void Surface::drawLine(int x1, int y1, int x2, int y2, Uint8 color)
} }
} }
// 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) void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
{ {
auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData(); auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData();
// Limitar la región para evitar accesos fuera de rango // Limitar la región para evitar accesos fuera de rango en origen
w = std::min(w, surface_data_->width - sx); w = std::min(w, surface_data_->width - sx);
h = std::min(h, surface_data_->height - sy); h = std::min(h, surface_data_->height - sy);
// Limitar la región para evitar accesos fuera de rango en destino
w = std::min(w, surface_data->width - dx); w = std::min(w, surface_data->width - dx);
h = std::min(h, surface_data->height - dy); h = std::min(h, surface_data->height - dy);
@@ -211,16 +212,24 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h)
{ {
for (int ix = 0; ix < w; ++ix) for (int ix = 0; ix < w; ++ix)
{ {
Uint8 color = surface_data_->data[(sx + ix) + (sy + iy) * surface_data_->width]; int src_x = sx + ix;
int src_y = sy + iy;
int dest_x = dx + ix;
int dest_y = dy + iy;
// Verificar que las coordenadas de destino están dentro de los límites
if (dest_x >= 0 && dest_x < surface_data->width && dest_y >= 0 && dest_y < surface_data->height)
{
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
if (color != transparent_color_) if (color != transparent_color_)
{ {
surface_data->data[(dx + ix) + (dy + iy) * surface_data->width] = color; surface_data->data[dest_x + dest_y * surface_data->width] = color;
}
} }
} }
} }
} }
// 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) void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
{ {
auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData(); auto surface_data = Screen::get()->getRendererSurface()->getSurfaceData();
@@ -231,12 +240,16 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
int w = (srcRect) ? srcRect->w : surface_data_->width; int w = (srcRect) ? srcRect->w : surface_data_->width;
int h = (srcRect) ? srcRect->h : surface_data_->height; int h = (srcRect) ? srcRect->h : surface_data_->height;
// Limitar la región para evitar accesos fuera de rango // Limitar la región para evitar accesos fuera de rango en origen
w = std::min(w, surface_data_->width - sx); w = std::min(w, surface_data_->width - sx);
h = std::min(h, surface_data_->height - sy); h = std::min(h, surface_data_->height - sy);
w = std::min(w, surface_data->width - x); w = std::min(w, surface_data->width - x);
h = std::min(h, surface_data->height - y); h = std::min(h, surface_data->height - y);
// Limitar la región para evitar accesos fuera de rango en destino
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 // Renderiza píxel por píxel aplicando el flip si es necesario
for (int iy = 0; iy < h; ++iy) for (int iy = 0; iy < h; ++iy)
{ {
@@ -250,6 +263,9 @@ void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
int dest_x = x + ix; int dest_x = x + ix;
int dest_y = y + iy; int dest_y = y + iy;
// Verificar que las coordenadas de destino están dentro de los límites
if (dest_x >= 0 && dest_x < surface_data->width && dest_y >= 0 && dest_y < surface_data->height)
{
// Copia el píxel si no es transparente // Copia el píxel si no es transparente
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width]; Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
if (color != transparent_color_) if (color != transparent_color_)
@@ -259,6 +275,8 @@ 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 // 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) void Surface::render(SDL_Rect *srcRect, SDL_Rect *dstRect, SDL_RendererFlip flip)