millorat el renderitzat amb el borde activat

This commit is contained in:
2025-03-21 08:27:24 +01:00
parent b8cde79107
commit 2ee2d5c45d
4 changed files with 50 additions and 20 deletions

View File

@@ -179,7 +179,7 @@ void Surface::setColor(int index, Uint32 color)
void Surface::clear(Uint8 color)
{
const size_t total_pixels = surface_data_->width * surface_data_->height;
Uint8* data_ptr = surface_data_->data.get(); // Explicitly get a typed pointer
Uint8* data_ptr = surface_data_->data.get();
std::fill(data_ptr, data_ptr + total_pixels, color);
}
@@ -523,6 +523,50 @@ void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture)
}
}
// Vuelca la superficie a una textura
void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcRect, SDL_Rect *destRect)
{
if (!renderer || !texture || !surface_data_)
{
throw std::runtime_error("Renderer or texture is null.");
}
if (surface_data_->width <= 0 || surface_data_->height <= 0 || !surface_data_->data.get())
{
throw std::runtime_error("Invalid surface dimensions or data.");
}
Uint32 *pixels = nullptr;
int pitch = 0;
if (SDL_LockTexture(texture, destRect, reinterpret_cast<void **>(&pixels), &pitch) != 0)
{
throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError()));
}
int row_stride = pitch / sizeof(Uint32);
for (int y = 0; y < surface_data_->height; ++y)
{
for (int x = 0; x < surface_data_->width; ++x)
{
int texture_index = y * row_stride + x;
int surface_index = y * surface_data_->width + x;
pixels[texture_index] = palette_[surface_data_->data.get()[surface_index]];
}
}
SDL_UnlockTexture(texture);
// Renderiza la textura con los rectángulos especificados
if (SDL_RenderCopy(renderer, texture, srcRect, destRect) != 0)
{
throw std::runtime_error("Failed to copy texture to renderer: " + std::string(SDL_GetError()));
}
}
// Realiza un efecto de fundido en la paleta principal
bool Surface::fadePalette()
{