forked from jaildesigner-jailgames/jaildoctors_dilemma
Transició a surface: game.cpp fet
This commit is contained in:
@@ -107,21 +107,16 @@ void Surface::clear(Uint8 color)
|
||||
std::fill(surface_data_->data, surface_data_->data + total_pixels, color);
|
||||
}
|
||||
|
||||
// Pone un pixel en la superficie de destino
|
||||
void Surface::putPixel(int x, int y, Uint8 color)
|
||||
// Pone un pixel en la SurfaceData
|
||||
void Surface::putPixel(std::shared_ptr<SurfaceData> surface_data, int x, int y, Uint8 color)
|
||||
{
|
||||
if (color == transparent_color_)
|
||||
{
|
||||
return; // Color transparente, no dibujar
|
||||
}
|
||||
|
||||
if (x < 0 || y < 0 || x >= surface_data_dest_->width || y >= surface_data_dest_->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_dest_->width;
|
||||
surface_data_dest_->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
|
||||
@@ -130,6 +125,29 @@ 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)
|
||||
{
|
||||
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));
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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)
|
||||
{
|
||||
@@ -158,7 +176,7 @@ 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 *clip, SDL_RendererFlip flip)
|
||||
void Surface::render(int x, int y, SDL_Rect *srcRect, SDL_RendererFlip flip)
|
||||
{
|
||||
if (!surface_data_ || !surface_data_dest_)
|
||||
{
|
||||
@@ -166,10 +184,10 @@ void Surface::render(int x, int y, SDL_Rect *clip, SDL_RendererFlip flip)
|
||||
}
|
||||
|
||||
// Determina la región de origen (clip) a renderizar
|
||||
int sx = (clip) ? clip->x : 0;
|
||||
int sy = (clip) ? clip->y : 0;
|
||||
int w = (clip) ? clip->w : surface_data_->width;
|
||||
int h = (clip) ? clip->h : surface_data_->height;
|
||||
int sx = (srcRect) ? srcRect->x : 0;
|
||||
int sy = (srcRect) ? srcRect->y : 0;
|
||||
int w = (srcRect) ? srcRect->w : surface_data_->width;
|
||||
int h = (srcRect) ? srcRect->h : surface_data_->height;
|
||||
|
||||
// Limitar la región para evitar accesos fuera de rango
|
||||
w = std::min(w, surface_data_->width - sx);
|
||||
@@ -200,6 +218,65 @@ void Surface::render(int x, int y, SDL_Rect *clip, 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_)
|
||||
{
|
||||
throw std::runtime_error("Surface source or destination is null.");
|
||||
}
|
||||
|
||||
// Si srcRect es nullptr, tomar toda la superficie fuente
|
||||
int sx = (srcRect) ? srcRect->x : 0;
|
||||
int sy = (srcRect) ? srcRect->y : 0;
|
||||
int sw = (srcRect) ? srcRect->w : surface_data_->width;
|
||||
int sh = (srcRect) ? srcRect->h : surface_data_->height;
|
||||
|
||||
// Si dstRect es nullptr, asignar las mismas dimensiones que srcRect
|
||||
int dx = (dstRect) ? dstRect->x : 0;
|
||||
int dy = (dstRect) ? dstRect->y : 0;
|
||||
int dw = (dstRect) ? dstRect->w : sw;
|
||||
int dh = (dstRect) ? dstRect->h : sh;
|
||||
|
||||
// Asegurarse de que srcRect y dstRect tienen las mismas dimensiones
|
||||
if (sw != dw || sh != dh)
|
||||
{
|
||||
dw = sw; // Respetar las dimensiones de srcRect
|
||||
dh = sh;
|
||||
}
|
||||
|
||||
// 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, surface_data_dest_->width - dx);
|
||||
dh = std::min(dh, surface_data_dest_->height - dy);
|
||||
|
||||
int final_width = std::min(sw, dw);
|
||||
int final_height = std::min(sh, dh);
|
||||
|
||||
// Renderiza píxel por píxel aplicando el flip si es necesario
|
||||
for (int iy = 0; iy < final_height; ++iy)
|
||||
{
|
||||
for (int ix = 0; ix < final_width; ++ix)
|
||||
{
|
||||
// Coordenadas de origen
|
||||
int src_x = (flip == SDL_FLIP_HORIZONTAL) ? (sx + final_width - 1 - ix) : (sx + ix);
|
||||
int src_y = (flip == SDL_FLIP_VERTICAL) ? (sy + final_height - 1 - iy) : (sy + iy);
|
||||
|
||||
// Coordenadas de destino
|
||||
int dest_x = dx + ix;
|
||||
int dest_y = dy + iy;
|
||||
|
||||
// Copiar el píxel si no es transparente
|
||||
Uint8 color = surface_data_->data[src_x + src_y * surface_data_->width];
|
||||
if (color != transparent_color_)
|
||||
{
|
||||
surface_data_dest_->data[dest_x + dest_y * surface_data_dest_->width] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 = 0, Uint8 target_color = 0, SDL_Rect *clip = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user