diff --git a/source/core/rendering/surface.cpp b/source/core/rendering/surface.cpp index de32ec2..ae85e1c 100644 --- a/source/core/rendering/surface.cpp +++ b/source/core/rendering/surface.cpp @@ -211,37 +211,38 @@ void Surface::drawRectBorder(const SDL_FRect* rect, Uint8 color) { // NOLINT(re } } -// Dibuja una linea +// Dibuja una linea (Bresenham en enteros) void Surface::drawLine(float x1, float y1, float x2, float y2, Uint8 color) { // NOLINT(readability-convert-member-functions-to-static) - // Calcula las diferencias - float dx = std::abs(x2 - x1); - float dy = std::abs(y2 - y1); + int ix1 = static_cast(std::lround(x1)); + int iy1 = static_cast(std::lround(y1)); + const int IX2 = static_cast(std::lround(x2)); + const int IY2 = static_cast(std::lround(y2)); - // Determina la dirección del incremento - float sx = (x1 < x2) ? 1 : -1; - float sy = (y1 < y2) ? 1 : -1; + const int DX = std::abs(IX2 - ix1); + const int DY = std::abs(IY2 - iy1); + const int SX = (ix1 < IX2) ? 1 : -1; + const int SY = (iy1 < IY2) ? 1 : -1; - float err = dx - dy; + const int SURF_W = static_cast(surface_data_->width); + const int SURF_H = static_cast(surface_data_->height); + Uint8* data_ptr = surface_data_->data.get(); + int err = DX - DY; 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) { - surface_data_->data.get()[static_cast(x1 + (y1 * surface_data_->width))] = color; + if (ix1 >= 0 && ix1 < SURF_W && iy1 >= 0 && iy1 < SURF_H) { + data_ptr[ix1 + (iy1 * SURF_W)] = color; } - - // Si alcanzamos el punto final, salimos - if (x1 == x2 && y1 == y2) { + if (ix1 == IX2 && iy1 == IY2) { break; } - int e2 = 2 * err; - if (e2 > -dy) { - err -= dy; - x1 += sx; + if (e2 > -DY) { + err -= DY; + ix1 += SX; } - if (e2 < dx) { - err += dx; - y1 += sy; + if (e2 < DX) { + err += DX; + iy1 += SY; } } }