From 469368530baa74e7deb41b66f80e3b653c40d804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 17 Nov 2018 17:33:09 +0100 Subject: [PATCH] Prevent infinite loops --- s3l.h | 29 ++++++++++++++++------------- testSDL.c | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/s3l.h b/s3l.h index d983ec2..0f4234b 100644 --- a/s3l.h +++ b/s3l.h @@ -199,7 +199,10 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 v##Inc = v##Dx >= 0 ? 1 : -1;\ v##Err = 2 * helperDxAbs - helperDyAbs;\ v##ErrAdd = 2 * helperDxAbs;\ - v##ErrSub = 2 * helperDyAbs; + v##ErrSub = 2 * (helperDyAbs != 0 ? helperDyAbs : 1);\ + v##ErrSub = v##ErrSub != 0 ? v##ErrSub : 1; /* don't allow 0, could lead + to an infinite substracting + loop */ #define stepSide(s)\ while (s##Err > 0)\ @@ -216,6 +219,18 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 while (currentY <= endY) { + if (currentY == splitY) + { + if (splitOnLeft) + { + initSide(l,l,r); + } + else + { + initSide(r,r,l); + } + } + p.y = currentY; // draw the line @@ -229,18 +244,6 @@ void S3L_drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2 stepSide(r) stepSide(l) - if (currentY == splitY) - { - if (splitOnLeft) - { - initSide(l,l,r); - } - else - { - initSide(r,r,l); - } - } - ++currentY; } diff --git a/testSDL.c b/testSDL.c index a6fce61..383a508 100644 --- a/testSDL.c +++ b/testSDL.c @@ -42,7 +42,7 @@ const int16_t test_coords[] = 100,100, 99,101, 101,101, // 0, small triangle 190,50, 200,10, 400,80, // 1, arbitrary 40,80, 20,50, 100,30, // 2, arbitrary - 300,300, 290,400, 310,400, // 3, regular + 150,300, 290,400, 450,400, // 3, regular 105,300, 120,300, 201,300, // 4, horizontal line 400,300, 400,320, 400,400 // 5, vertical line };