Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** Dibuja una linea entre dos puntos (Implementacion del algoritmo de Bresenham) ***/
- void NGN_ProDraw::Line(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color) {
- // Parametros iniciales
- int32_t right = (surface_width - 1); // Limites del buffer
- int32_t bottom = (surface_height - 1);
- int32_t x = x1; // Coordenadas de dibujado
- int32_t y = y1;
- int32_t dx = (x2 - x1); // Distancias
- int32_t dy = (y2 - y1);
- int32_t ix = (dx >= 0) ? 1:-1; // Sentido del dibujado
- int32_t iy = (dy >= 0) ? 1:-1;
- int32_t pk = 0; // Precision de la pendiente
- // Valor absoluto de las distancias
- dx = std::abs(dx);
- dy = std::abs(dy);
- // Bloquea el surface
- SDL_LockSurface(surface);
- // Acceso al array de pixeles
- uint32_t* p = (uint32_t*)surface->pixels;
- // Segun la pendiente (mandan la X o mandan las Y)
- if (dx >= dy) {
- // Dibuja el primer pixel
- if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
- p[((y * surface_width) + x)] = color;
- }
- // Calculo de la precision
- pk = ((dy << 1) - dx);
- // Bucle de dibujado
- for (int32_t i = 0; i < dx; i ++) {
- // Incremento de la X
- x += ix;
- // Segun el estado de la precision, recalculala
- if (pk < 0) {
- pk += (dy << 1);
- } else {
- y += iy;
- pk += (dy << 1) - (dx << 1);
- }
- // Dibuja el pixel
- if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
- p[((y * surface_width) + x)] = color;
- }
- }
- } else {
- // Dibuja el primer pixel
- if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
- p[((y * surface_width) + x)] = color;
- }
- // Calculo de la precision
- pk = ((dx << 1) - dy);
- // Bucle de dibujado
- for (int32_t i = 0; i < dy; i ++) {
- // Incremento de la Y
- y += iy;
- // Segun el estado de la precision, recalculala
- if (pk < 0) {
- pk += (dx << 1);
- } else {
- x += ix;
- pk += (dx << 1) - (dy << 1);
- }
- // Dibuja el pixel
- if (!((x < 0) || (y < 0) || (x > right) || (y > bottom))) {
- p[((y * surface_width) + x)] = color;
- }
- }
- }
- // Desbloquea el surface
- SDL_UnlockSurface(surface);
- // Indica el blit
- blit = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement